From b9c5df4dad88cc9e49046f86856caf1d9ac041f3 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 1 Feb 2024 18:37:28 +0000 Subject: [PATCH 1/1] Use with_settings for temporary changes to settings --- .../diary_entries_controller_test.rb | 11 +------- test/controllers/messages_controller_test.rb | 13 +-------- test/models/relation_test.rb | 27 +++++++++---------- test/test_helper.rb | 9 +++---- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index b47446878..b83e9b7a0 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -677,7 +677,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest get diary_rss_path assert_select "rss>channel>item", :count => 2 - with_diary_feed_delay(6) do + with_settings(:diary_feed_delay => 6) do get diary_rss_path assert_select "rss>channel>item", :count => 1 end @@ -1003,13 +1003,4 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}" end end - - def with_diary_feed_delay(value) - diary_feed_delay = Settings.diary_feed_delay - Settings.diary_feed_delay = value - - yield - - Settings.diary_feed_delay = diary_feed_delay - end end diff --git a/test/controllers/messages_controller_test.rb b/test/controllers/messages_controller_test.rb index ca17401fd..fcfc8da5e 100644 --- a/test/controllers/messages_controller_test.rb +++ b/test/controllers/messages_controller_test.rb @@ -214,7 +214,7 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest # Check that sending a message fails when the message limit is hit assert_no_difference "ActionMailer::Base.deliveries.size" do assert_no_difference "Message.count" do - with_message_limit(0) do + with_settings(:max_messages_per_hour => 0) do perform_enqueued_jobs do post messages_path(:display_name => recipient_user.display_name, :message => { :title => "Test Message", :body => "Test message body" }) @@ -472,15 +472,4 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest assert_response :not_found assert_template "no_such_message" end - - private - - def with_message_limit(value) - max_messages_per_hour = Settings.max_messages_per_hour - Settings.max_messages_per_hour = value - - yield - - Settings.max_messages_per_hour = max_messages_per_hour - end end diff --git a/test/models/relation_test.rb b/test/models/relation_test.rb index 193126df1..575813ad5 100644 --- a/test/models/relation_test.rb +++ b/test/models/relation_test.rb @@ -235,22 +235,19 @@ class RelationTest < ActiveSupport::TestCase # the maximum number of members in a relation. def test_max_members_per_relation_limit # Speed up unit test by using a small relation member limit - default_limit = Settings.max_number_of_relation_members - Settings.max_number_of_relation_members = 20 - - user = create(:user) - changeset = create(:changeset, :user => user) - relation = create(:relation, :changeset => changeset) - node = create(:node, :longitude => 116, :latitude => 39) - # Create relation which exceeds the relation member limit by one - 0.upto(Settings.max_number_of_relation_members) do |i| - create(:relation_member, :relation => relation, :member_type => "Node", :member_id => node.id, :sequence_id => i) - end + with_settings(:max_number_of_relation_members => 20) do + user = create(:user) + changeset = create(:changeset, :user => user) + relation = create(:relation, :changeset => changeset) + node = create(:node, :longitude => 116, :latitude => 39) + # Create relation which exceeds the relation member limit by one + 0.upto(Settings.max_number_of_relation_members) do |i| + create(:relation_member, :relation => relation, :member_type => "Node", :member_id => node.id, :sequence_id => i) + end - assert_raise OSM::APITooManyRelationMembersError do - relation.create_with_history user + assert_raise OSM::APITooManyRelationMembersError do + relation.create_with_history user + end end - - Settings.max_number_of_relation_members = default_limit end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5bf8a5239..5fe1ada47 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -384,14 +384,11 @@ module ActiveSupport Settings.merge!(saved_settings) end - def with_user_account_deletion_delay(value) + def with_user_account_deletion_delay(value, &block) freeze_time - default_value = Settings.user_account_deletion_delay - Settings.user_account_deletion_delay = value - yield - - Settings.user_account_deletion_delay = default_value + with_settings(:user_account_deletion_delay => value, &block) + ensure unfreeze_time end end -- 2.39.5