openstreetmap-website/test/models/user_preference_test.rb
Andy Allan 408c883d24 Skip intermediate lengths when testing tag validity
It's really the upper and lower boundaries that we want to check,
and it's reasonable to assume all the intermediate values will work
fine if the boundary values are tested.

This also saves about 75% of the time taken and almost 85% of the
assertions when running all the model tests.
2021-06-16 09:53:57 +01:00

43 lines
1.2 KiB
Ruby

require "test_helper"
class UserPreferenceTest < ActiveSupport::TestCase
# Checks that you cannot add a new preference, that is a duplicate
def test_add_duplicate_preference
up = create(:user_preference)
new_up = UserPreference.new
new_up.user = up.user
new_up.k = up.k
new_up.v = "some other value"
assert_not_equal new_up.v, up.v
assert_raise(ActiveRecord::RecordNotUnique) { new_up.save }
end
def test_check_valid_length
key = "k"
val = "v"
[1, 255].each do |i|
up = UserPreference.new
up.user = create(:user)
up.k = key * i
up.v = val * i
assert up.valid?
assert up.save!
resp = UserPreference.find(up.id)
assert_equal key * i, resp.k, "User preference with #{i} #{key} chars (i.e. #{key.length * i} bytes) fails"
assert_equal val * i, resp.v, "User preference with #{i} #{val} chars (i.e. #{val.length * i} bytes) fails"
end
end
def test_check_invalid_length
key = "k"
val = "v"
[0, 256].each do |i|
up = UserPreference.new
up.user = create(:user)
up.k = key * i
up.v = val * i
assert_not up.valid?
assert_raise(ActiveRecord::RecordInvalid) { up.save! }
end
end
end