]> git.openstreetmap.org Git - rails.git/blob - test/models/relation_tag_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / models / relation_tag_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class RelationTagTest < ActiveSupport::TestCase
6   def test_length_key_valid
7     tag = create(:relation_tag)
8     [0, 255].each do |i|
9       tag.k = "k" * i
10       assert_predicate tag, :valid?
11     end
12   end
13
14   def test_length_value_valid
15     tag = create(:relation_tag)
16     [0, 255].each do |i|
17       tag.v = "v" * i
18       assert_predicate tag, :valid?
19     end
20   end
21
22   def test_length_key_invalid
23     tag = create(:relation_tag)
24     tag.k = "k" * 256
25     assert_not_predicate tag, :valid?, "Key should be too long"
26     assert_predicate tag.errors[:k], :any?
27   end
28
29   def test_length_value_invalid
30     tag = create(:relation_tag)
31     tag.v = "v" * 256
32     assert_not_predicate tag, :valid?, "Value should be too long"
33     assert_predicate tag.errors[:v], :any?
34   end
35
36   def test_orphaned_tag_invalid
37     tag = create(:relation_tag)
38     tag.relation = nil
39     assert_not_predicate tag, :valid?, "Orphaned tag should be invalid"
40     assert_predicate tag.errors[:relation], :any?
41   end
42
43   def test_uniqueness
44     existing = create(:relation_tag)
45     tag = build(:relation_tag, :relation => existing.relation, :k => existing.k, :v => existing.v)
46     assert_predicate tag, :new_record?
47     assert_not_predicate tag, :valid?
48     assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
49     assert_predicate 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