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