]> git.openstreetmap.org Git - rails.git/blob - test/models/changeset_tag_test.rb
Replace changeset_tags fixtures with a factory.
[rails.git] / test / models / changeset_tag_test.rb
1 require "test_helper"
2
3 class ChangesetTagTest < ActiveSupport::TestCase
4   api_fixtures
5
6   def test_length_key_valid
7     key = "k"
8     (0..255).each do |i|
9       tag = ChangesetTag.new
10       tag.changeset_id = 1
11       tag.k = key * i
12       tag.v = "v"
13       assert tag.valid?
14     end
15   end
16
17   def test_length_value_valid
18     val = "v"
19     (0..255).each do |i|
20       tag = ChangesetTag.new
21       tag.changeset_id = 1
22       tag.k = "k"
23       tag.v = val * i
24       assert tag.valid?
25     end
26   end
27
28   def test_length_key_invalid
29     ["k" * 256].each do |k|
30       tag = ChangesetTag.new
31       tag.changeset_id = 1
32       tag.k = k
33       tag.v = "v"
34       assert !tag.valid?, "Key #{k} should be too long"
35       assert tag.errors[:k].any?
36     end
37   end
38
39   def test_length_value_invalid
40     ["v" * 256].each do |v|
41       tag = ChangesetTag.new
42       tag.changeset_id = 1
43       tag.k = "k"
44       tag.v = v
45       assert !tag.valid?, "Value #{v} should be too long"
46       assert tag.errors[:v].any?
47     end
48   end
49
50   def test_empty_tag_invalid
51     tag = ChangesetTag.new
52     assert !tag.valid?, "Empty tag should be invalid"
53     assert tag.errors[:changeset].any?
54   end
55
56   def test_uniqueness
57     existing = create(:changeset_tag)
58     tag = ChangesetTag.new
59     tag.changeset_id = existing.changeset_id
60     tag.k = existing.k
61     tag.v = existing.v
62     assert tag.new_record?
63     assert !tag.valid?
64     assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
65     assert tag.new_record?
66   end
67 end