]> git.openstreetmap.org Git - rails.git/blob - test/models/user_preference_test.rb
add lock, information and water classes for nominatim terms
[rails.git] / test / models / user_preference_test.rb
1 require "test_helper"
2
3 class UserPreferenceTest < ActiveSupport::TestCase
4   # Checks that you cannot add a new preference, that is a duplicate
5   def test_add_duplicate_preference
6     up = create(:user_preference)
7     new_up = build(:user_preference)
8     new_up.user = up.user
9     new_up.k = up.k
10     new_up.v = "some other value"
11     assert_not_equal new_up.v, up.v
12     assert_raise(ActiveRecord::RecordNotUnique) { new_up.save }
13   end
14
15   def test_check_valid_length
16     key = "k"
17     val = "v"
18     [1, 255].each do |i|
19       up = build(:user_preference)
20       up.user = create(:user)
21       up.k = key * i
22       up.v = val * i
23       assert_predicate up, :valid?
24       assert up.save!
25       resp = UserPreference.find(up.id)
26       assert_equal key * i, resp.k, "User preference with #{i} #{key} chars (i.e. #{key.length * i} bytes) fails"
27       assert_equal val * i, resp.v, "User preference with #{i} #{val} chars (i.e. #{val.length * i} bytes) fails"
28     end
29   end
30
31   def test_check_invalid_length
32     key = "k"
33     val = "v"
34     [0, 256].each do |i|
35       up = build(:user_preference)
36       up.user = create(:user)
37       up.k = key * i
38       up.v = val * i
39       assert_not_predicate up, :valid?
40       assert_raise(ActiveRecord::RecordInvalid) { up.save! }
41     end
42   end
43 end