]> git.openstreetmap.org Git - rails.git/commitdiff
Refactor create element consistency check
authorAnton Khorev <tony29@yandex.ru>
Sat, 30 Mar 2024 07:09:55 +0000 (10:09 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sat, 30 Mar 2024 07:09:55 +0000 (10:09 +0300)
app/models/concerns/consistency_validations.rb
test/models/node_test.rb
test/models/relation_test.rb
test/models/way_test.rb

index 101fd43103955a024eb57c676ff69a3f356bffc8..97b6743ebc80752b1a5f9e9ef0f7b8f2592dc7fc 100644 (file)
@@ -23,13 +23,7 @@ module ConsistencyValidations
 
   # This is similar to above, just some validations don't apply
   def check_create_consistency(new, user)
-    if new.changeset.nil?
-      raise OSM::APIChangesetMissingError
-    elsif new.changeset.user_id != user.id
-      raise OSM::APIUserChangesetMismatchError
-    elsif !new.changeset.open?
-      raise OSM::APIChangesetAlreadyClosedError, new.changeset
-    end
+    check_changeset_consistency(new.changeset, user)
   end
 
   ##
index ee0a77649ea995f94f947364133df8c2d2ce11c5..dd241155481212ac23d4ff92e032e1c86a335894 100644 (file)
@@ -362,4 +362,30 @@ class NodeTest < ActiveSupport::TestCase
     assert_equal relation_member2.relation.id, cr.second.id
     assert_equal relation_member3.relation.id, cr.third.id
   end
+
+  test "raises missing changeset exception when creating" do
+    user = create(:user)
+    node = Node.new
+    assert_raises OSM::APIChangesetMissingError do
+      node.create_with_history(user)
+    end
+  end
+
+  test "raises user-changeset mismatch exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset)
+    node = Node.new(:changeset => changeset)
+    assert_raises OSM::APIUserChangesetMismatchError do
+      node.create_with_history(user)
+    end
+  end
+
+  test "raises already closed changeset exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset, :closed, :user => user)
+    node = Node.new(:changeset => changeset)
+    assert_raises OSM::APIChangesetAlreadyClosedError do
+      node.create_with_history(user)
+    end
+  end
 end
index 575813ad532131ea90e45fe032c2087d4b862258..b11bfa9042ebbb9a049d49339e3719bf1ef625c7 100644 (file)
@@ -250,4 +250,30 @@ class RelationTest < ActiveSupport::TestCase
       end
     end
   end
+
+  test "raises missing changeset exception when creating" do
+    user = create(:user)
+    relation = Relation.new
+    assert_raises OSM::APIChangesetMissingError do
+      relation.create_with_history(user)
+    end
+  end
+
+  test "raises user-changeset mismatch exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset)
+    relation = Relation.new(:changeset => changeset)
+    assert_raises OSM::APIUserChangesetMismatchError do
+      relation.create_with_history(user)
+    end
+  end
+
+  test "raises already closed changeset exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset, :closed, :user => user)
+    relation = Relation.new(:changeset => changeset)
+    assert_raises OSM::APIChangesetAlreadyClosedError do
+      relation.create_with_history(user)
+    end
+  end
 end
index 8674b37904d0110f49ef2b9210761672aaeaa8b3..558984e357f9a1d2694c5b76135b3e7759dc04d5 100644 (file)
@@ -217,4 +217,30 @@ class WayTest < ActiveSupport::TestCase
     assert_equal 1, cr.size
     assert_equal relation.id, cr.first.id
   end
+
+  test "raises missing changeset exception when creating" do
+    user = create(:user)
+    way = Way.new
+    assert_raises OSM::APIChangesetMissingError do
+      way.create_with_history(user)
+    end
+  end
+
+  test "raises user-changeset mismatch exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset)
+    way = Way.new(:changeset => changeset)
+    assert_raises OSM::APIUserChangesetMismatchError do
+      way.create_with_history(user)
+    end
+  end
+
+  test "raises already closed changeset exception when creating" do
+    user = create(:user)
+    changeset = create(:changeset, :closed, :user => user)
+    way = Way.new(:changeset => changeset)
+    assert_raises OSM::APIChangesetAlreadyClosedError do
+      way.create_with_history(user)
+    end
+  end
 end