-require 'test_helper'
+require "test_helper"
class OldRelationTagTest < ActiveSupport::TestCase
- api_fixtures
-
- def test_tag_count
- assert_equal 12, OldRelationTag.count
- end
-
def test_length_key_valid
- key = "k"
- (0..255).each do |i|
- tag = OldRelationTag.new
- tag.relation_id = relation_tags(:t1).relation_id
- tag.version = 1
- tag.k = key*i
- tag.v = "v"
- assert tag.valid?
+ tag = create(:old_relation_tag)
+ [0, 255].each do |i|
+ tag.k = "k" * i
+ assert_predicate tag, :valid?
end
end
def test_length_value_valid
- val = "v"
- (0..255).each do |i|
- tag = OldRelationTag.new
- tag.relation_id = relation_tags(:t1).relation_id
- tag.version = 1
- tag.k = "k"
- tag.v = val*i
- assert tag.valid?
+ tag = create(:old_relation_tag)
+ [0, 255].each do |i|
+ tag.v = "v" * i
+ assert_predicate tag, :valid?
end
end
def test_length_key_invalid
- ["k"*256].each do |i|
- tag = OldRelationTag.new
- tag.relation_id = relation_tags(:t1).relation_id
- tag.version = 1
- tag.k = i
- tag.v = "v"
- assert !tag.valid?, "Key should be too long"
- assert tag.errors[:k].any?
- end
+ tag = create(:old_relation_tag)
+ tag.k = "k" * 256
+ assert_not tag.valid?, "Key should be too long"
+ assert_predicate tag.errors[:k], :any?
end
def test_length_value_invalid
- ["k"*256].each do |i|
- tag = OldRelationTag.new
- tag.relation_id = relation_tags(:t1).relation_id
- tag.version = 1
- tag.k = "k"
- tag.v = i
- assert !tag.valid?, "Value should be too long"
- assert tag.errors[:v].any?
- end
+ tag = create(:old_relation_tag)
+ tag.v = "v" * 256
+ assert_not tag.valid?, "Value should be too long"
+ assert_predicate tag.errors[:v], :any?
end
- def test_empty_tag_invalid
- tag = OldRelationTag.new
- assert !tag.valid?, "Empty tag should be invalid"
- assert tag.errors[:old_relation].any?
+ def test_orphaned_tag_invalid
+ tag = create(:old_relation_tag)
+ tag.old_relation = nil
+ assert_not tag.valid?, "Orphaned tag should be invalid"
+ assert_predicate tag.errors[:old_relation], :any?
end
def test_uniqueness
+ existing = create(:old_relation_tag)
tag = OldRelationTag.new
- tag.relation_id = relation_tags(:t1).relation_id
- tag.version = relation_tags(:t1).version
- tag.k = relation_tags(:t1).k
- tag.v = relation_tags(:t1).v
- assert tag.new_record?
- assert !tag.valid?
- assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
- assert tag.new_record?
+ tag.relation_id = existing.relation_id
+ tag.version = existing.version
+ tag.k = existing.k
+ tag.v = existing.v
+ assert_predicate tag, :new_record?
+ assert_not tag.valid?
+ assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
+ assert_predicate tag, :new_record?
end
end