]> git.openstreetmap.org Git - rails.git/commitdiff
Use factory_bot to build new model objects
authorAndy Allan <git@gravitystorm.co.uk>
Thu, 6 Jan 2022 10:46:38 +0000 (10:46 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Thu, 6 Jan 2022 10:46:38 +0000 (10:46 +0000)
test/models/issue_test.rb
test/models/message_test.rb
test/models/node_test.rb
test/models/relation_test.rb
test/models/user_preference_test.rb

index 073dc26447d921fb81214a828557d5af9b36745f..02e2580b81daa08ad5dc081248d134f7d43e621d 100644 (file)
@@ -17,23 +17,23 @@ class IssueTest < ActiveSupport::TestCase
     diary_entry = create(:diary_entry)
     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
 
-    issue = Issue.new(:reportable => user, :assigned_role => "administrator")
+    issue = build(:issue, :reportable => user, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, user
 
-    issue = Issue.new(:reportable => note, :assigned_role => "administrator")
+    issue = build(:issue, :reportable => note, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, note.author
 
-    issue = Issue.new(:reportable => anonymous_note, :assigned_role => "administrator")
+    issue = build(:issue, :reportable => anonymous_note, :assigned_role => "administrator")
     issue.save!
     assert_nil issue.reported_user
 
-    issue = Issue.new(:reportable => diary_entry, :assigned_role => "administrator")
+    issue = build(:issue, :reportable => diary_entry, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, diary_entry.user
 
-    issue = Issue.new(:reportable => diary_comment, :assigned_role => "administrator")
+    issue = build(:issue, :reportable => diary_comment, :assigned_role => "administrator")
     issue.save!
     assert_equal issue.reported_user, diary_comment.user
   end
index d5391c6f8e2e6ad136154e5cbe02ca77482f67da..3a25c1a4b53162e6bf0acc1220cbaeacbe0dfe8b 100644 (file)
@@ -4,7 +4,7 @@ class MessageTest < ActiveSupport::TestCase
   EURO = "\xe2\x82\xac".freeze # euro symbol
 
   def test_check_empty_message_fails
-    message = Message.new
+    message = build(:message, :title => nil, :body => nil, :sent_on => nil)
     assert_not message.valid?
     assert message.errors[:title].any?
     assert message.errors[:body].any?
index 214ff595d9549ccfd56d8faf66faa5a58f85aea5..2e61082343d401fe30ed38bfc313d5615da88154 100644 (file)
@@ -71,13 +71,11 @@ class NodeTest < ActiveSupport::TestCase
   # Check that you can create a node and store it
   def test_create
     changeset = create(:changeset)
-    node_template = Node.new(
-      :lat => 12.3456,
-      :lon => 65.4321,
-      :changeset_id => changeset.id,
-      :visible => 1,
-      :version => 1
-    )
+    node_template = build(:node, :lat => 12.3456,
+                                 :lon => 65.4321,
+                                 :changeset_id => changeset.id,
+                                 :visible => true,
+                                 :version => 1)
     assert node_template.create_with_history(changeset.user)
 
     node = Node.find(node_template.id)
index e3ad13dc71ee4975cce25cbfb9beb7c1fb64ac58..2aaaaed8aedd22ddb512e11e52956eaea51630cd 100644 (file)
@@ -193,10 +193,9 @@ class RelationTest < ActiveSupport::TestCase
     assert_nil changeset.max_lon
     assert_nil changeset.max_lat
     assert_nil changeset.min_lat
-    new_relation = Relation.new
-    new_relation.id = super_relation.id
-    new_relation.version = super_relation.version
-    new_relation.changeset = changeset
+    new_relation = build(:relation, :id => super_relation.id,
+                                    :version => super_relation.version,
+                                    :changeset => changeset)
     new_relation.add_member node_member.member_type, node_member.member_id, node_member.member_role
     # one member(relation type) was removed, so any_relation flag is expected to be true.
     super_relation.update_from(new_relation, user)
@@ -221,10 +220,9 @@ class RelationTest < ActiveSupport::TestCase
     assert_nil changeset.max_lat
     assert_nil changeset.min_lat
 
-    new_relation = Relation.new
-    new_relation.id = orig_relation.id
-    new_relation.version = orig_relation.version
-    new_relation.changeset_id = changeset.id
+    new_relation = build(:relation, :id => orig_relation.id,
+                                    :version => orig_relation.version,
+                                    :changeset_id => changeset.id)
     orig_relation.delete_with_history!(new_relation, user)
     changeset.reload
     assert_equal 39, changeset.min_lon
index d6f1dcce5698acaed266f2c3da979e2dc0179c44..2f5e16b18f47703b1b827b0bf0a33e296dd62623 100644 (file)
@@ -4,7 +4,7 @@ class UserPreferenceTest < ActiveSupport::TestCase
   # Checks that you cannot add a new preference, that is a duplicate
   def test_add_duplicate_preference
     up = create(:user_preference)
-    new_up = UserPreference.new
+    new_up = build(:user_preference)
     new_up.user = up.user
     new_up.k = up.k
     new_up.v = "some other value"
@@ -16,7 +16,7 @@ class UserPreferenceTest < ActiveSupport::TestCase
     key = "k"
     val = "v"
     [1, 255].each do |i|
-      up = UserPreference.new
+      up = build(:user_preference)
       up.user = create(:user)
       up.k = key * i
       up.v = val * i
@@ -32,7 +32,7 @@ class UserPreferenceTest < ActiveSupport::TestCase
     key = "k"
     val = "v"
     [0, 256].each do |i|
-      up = UserPreference.new
+      up = build(:user_preference)
       up.user = create(:user)
       up.k = key * i
       up.v = val * i