]> git.openstreetmap.org Git - rails.git/commitdiff
Simplify changeset tag tests by using factories
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 23 Feb 2022 11:44:56 +0000 (11:44 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 30 Mar 2022 16:03:04 +0000 (17:03 +0100)
This saves lots of boilerplate in creating a valid test object

test/models/changeset_tag_test.rb

index 6846f90b455e73256c24b1908e386df564d18f1a..4ac372416616c7410bae782fff1432196291ffb9 100644 (file)
@@ -2,51 +2,33 @@ require "test_helper"
 
 class ChangesetTagTest < ActiveSupport::TestCase
   def test_length_key_valid
-    changeset = create(:changeset)
-
-    key = "k"
+    tag = create(:changeset_tag)
     [0, 255].each do |i|
-      tag = ChangesetTag.new
-      tag.changeset_id = changeset.id
-      tag.k = key * i
-      tag.v = "v"
+      tag.k = "k" * i
       assert_predicate tag, :valid?
     end
   end
 
   def test_length_value_valid
-    changeset = create(:changeset)
-
-    val = "v"
+    tag = create(:changeset_tag)
     [0, 255].each do |i|
-      tag = ChangesetTag.new
-      tag.changeset_id = changeset.id
-      tag.k = "k"
-      tag.v = val * i
+      tag.v = "v" * i
       assert_predicate tag, :valid?
     end
   end
 
   def test_length_key_invalid
-    ["k" * 256].each do |k|
-      tag = ChangesetTag.new
-      tag.changeset_id = 1
-      tag.k = k
-      tag.v = "v"
-      assert_not tag.valid?, "Key #{k} should be too long"
-      assert_predicate tag.errors[:k], :any?
-    end
+    tag = create(:changeset_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
-    ["v" * 256].each do |v|
-      tag = ChangesetTag.new
-      tag.changeset_id = 1
-      tag.k = "k"
-      tag.v = v
-      assert_not tag.valid?, "Value #{v} should be too long"
-      assert_predicate tag.errors[:v], :any?
-    end
+    tag = create(:changeset_tag)
+    tag.v = "v" * 256
+    assert_not tag.valid?, "Value should be too long"
+    assert_predicate tag.errors[:v], :any?
   end
 
   def test_orphaned_tag_invalid