From f50ca826aad559c77f948ccca61cbdc0bbddf3d1 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 30 Mar 2024 10:09:55 +0300 Subject: [PATCH 1/1] Refactor create element consistency check --- .../concerns/consistency_validations.rb | 8 +----- test/models/node_test.rb | 26 +++++++++++++++++++ test/models/relation_test.rb | 26 +++++++++++++++++++ test/models/way_test.rb | 26 +++++++++++++++++++ 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/consistency_validations.rb b/app/models/concerns/consistency_validations.rb index 101fd4310..97b6743eb 100644 --- a/app/models/concerns/consistency_validations.rb +++ b/app/models/concerns/consistency_validations.rb @@ -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 ## diff --git a/test/models/node_test.rb b/test/models/node_test.rb index ee0a77649..dd2411554 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb @@ -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 diff --git a/test/models/relation_test.rb b/test/models/relation_test.rb index 575813ad5..b11bfa904 100644 --- a/test/models/relation_test.rb +++ b/test/models/relation_test.rb @@ -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 diff --git a/test/models/way_test.rb b/test/models/way_test.rb index 8674b3790..558984e35 100644 --- a/test/models/way_test.rb +++ b/test/models/way_test.rb @@ -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 -- 2.39.5