]> git.openstreetmap.org Git - rails.git/blob - test/models/relation_tag_test.rb
erblint: remove leading blank lines
[rails.git] / test / models / relation_tag_test.rb
1 require "test_helper"
2
3 class RelationTagTest < ActiveSupport::TestCase
4   def test_length_key_valid
5     tag = create(:relation_tag)
6     (0..255).each do |i|
7       tag.k = "k" * i
8       assert tag.valid?
9     end
10   end
11
12   def test_length_value_valid
13     tag = create(:relation_tag)
14     (0..255).each do |i|
15       tag.v = "v" * i
16       assert tag.valid?
17     end
18   end
19
20   def test_length_key_invalid
21     tag = create(:relation_tag)
22     tag.k = "k" * 256
23     assert_not tag.valid?, "Key should be too long"
24     assert tag.errors[:k].any?
25   end
26
27   def test_length_value_invalid
28     tag = create(:relation_tag)
29     tag.v = "v" * 256
30     assert_not tag.valid?, "Value should be too long"
31     assert tag.errors[:v].any?
32   end
33
34   def test_empty_tag_invalid
35     tag = RelationTag.new
36     assert_not tag.valid?, "Empty relation tag should be invalid"
37     assert tag.errors[:relation].any?
38   end
39
40   def test_uniquness
41     existing = create(:relation_tag)
42     tag = RelationTag.new
43     tag.relation_id = existing.relation_id
44     tag.k = existing.k
45     tag.v = existing.v
46     assert tag.new_record?
47     assert_not tag.valid?
48     assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
49     assert tag.new_record?
50   end
51
52   ##
53   # test that tags can be updated and saved uniquely, i.e: tag.save!
54   # only affects the single tag that the activerecord object
55   # represents. this amounts to testing that the primary key is
56   # unique.
57   #
58   # Commenting this out - I attempted to fix it, but composite primary keys
59   # wasn't playing nice with the column already called :id. Seemed to be
60   # impossible to have validations on the :id column. If someone knows better
61   # please fix, otherwise this test is shelved.
62   #
63   # def test_update
64   #   v = "probably unique string here 3142592654"
65   #   assert_equal 0, RelationTag.count(:conditions => ['v=?', v])
66
67   #   # make sure we select a tag on a relation which has more than one tag
68   #   id = current_relations(:multi_tag_relation).relation_id
69   #   tag = RelationTag.find(:first, :conditions => ["id = ?", id])
70   #   tag.v = v
71   #   tag.save!
72
73   #   assert_equal 1, RelationTag.count(:conditions => ['v=?', v])
74   # end
75 end