]> git.openstreetmap.org Git - rails.git/blobdiff - test/models/relation_tag_test.rb
Use assert_not_predicate in tests that have assert_predicate
[rails.git] / test / models / relation_tag_test.rb
index 0b5bedb05e88a77414c6f1adb2b45028d225583b..2e5e1503cd1ec1fef078b7e034e8e289da0179b2 100644 (file)
@@ -1,81 +1,60 @@
-require 'test_helper'
+require "test_helper"
 
 class RelationTagTest < ActiveSupport::TestCase
-  api_fixtures
-  
-  def test_relation_tag_count
-    assert_equal 9, RelationTag.count
-  end
-  
   def test_length_key_valid
-    key = "k"
-    (0..255).each do |i|
-      tag = RelationTag.new
-      tag.relation_id = 1
-      tag.k = key*i
-      tag.v = "v"
-      assert tag.valid?
+    tag = create(: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 = RelationTag.new
-      tag.relation_id = 1
-      tag.k = "k"
-      tag.v = val*i
-      assert tag.valid?
+    tag = create(: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 = RelationTag.new
-      tag.relation_id = 1
-      tag.k = i
-      tag.v = "v"
-      assert !tag.valid?, "Key #{i} should be too long"
-      assert tag.errors[:k].any?
-    end
+    tag = create(:relation_tag)
+    tag.k = "k" * 256
+    assert_not_predicate tag, :valid?, "Key should be too long"
+    assert_predicate tag.errors[:k], :any?
   end
-  
+
   def test_length_value_invalid
-    ["v"*256].each do |i|
-      tag = RelationTag.new
-      tag.relation_id = 1
-      tag.k = "k"
-      tag.v = i
-      assert !tag.valid?, "Value #{i} should be too long"
-      assert tag.errors[:v].any?
-    end
+    tag = create(:relation_tag)
+    tag.v = "v" * 256
+    assert_not_predicate tag, :valid?, "Value should be too long"
+    assert_predicate tag.errors[:v], :any?
   end
-  
-  def test_empty_tag_invalid
-    tag = RelationTag.new
-    assert !tag.valid?, "Empty relation tag should be invalid"
-    assert tag.errors[:relation].any?
+
+  def test_orphaned_tag_invalid
+    tag = create(:relation_tag)
+    tag.relation = nil
+    assert_not_predicate tag, :valid?, "Orphaned tag should be invalid"
+    assert_predicate tag.errors[:relation], :any?
   end
-  
-  def test_uniquness
-    tag = RelationTag.new
-    tag.relation_id = current_relation_tags(:t1).relation_id
-    tag.k = current_relation_tags(:t1).k
-    tag.v = current_relation_tags(:t1).v
-    assert tag.new_record?
-    assert !tag.valid?
-    assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
-    assert tag.new_record?
+
+  def test_uniqueness
+    existing = create(:relation_tag)
+    tag = build(:relation_tag, :relation => existing.relation, :k => existing.k, :v => existing.v)
+    assert_predicate tag, :new_record?
+    assert_not_predicate tag, :valid?
+    assert_raise(ActiveRecord::RecordInvalid) { tag.save! }
+    assert_predicate tag, :new_record?
   end
 
   ##
   # test that tags can be updated and saved uniquely, i.e: tag.save!
-  # only affects the single tag that the activerecord object 
+  # only affects the single tag that the activerecord object
   # represents. this amounts to testing that the primary key is
   # unique.
   #
   # Commenting this out - I attempted to fix it, but composite primary keys
-  # wasn't playing nice with the column already called :id. Seemed to be 
+  # wasn't playing nice with the column already called :id. Seemed to be
   # impossible to have validations on the :id column. If someone knows better
   # please fix, otherwise this test is shelved.
   #