From 8eaafcecf1f2e3825e893cdd6b3a905218b8d777 Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 23 Jan 2025 13:43:27 +0000 Subject: [PATCH 1/3] Get rid of opensearch in tests --- test/test_helper.rb | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 366ba1682c..3c950a7e8e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -129,16 +129,16 @@ def ==(other) end # Setup our indexes once (we'll keep them clear in teardowns) -opensearch = Exercism.opensearch_client -[ - Document::OPENSEARCH_INDEX, - Solution::OPENSEARCH_INDEX, - Exercise::Representation::OPENSEARCH_INDEX -].map do |index| - opensearch.indices.delete(index:) if opensearch.indices.exists(index:) - opensearch.indices.create(index:) -end -Exercism::TOUCHED_OPENSEARCH_INDEXES = [] # rubocop:disable Style/MutableConstant +# opensearch = Exercism.opensearch_client +# [ +# Document::OPENSEARCH_INDEX, +# Solution::OPENSEARCH_INDEX, +# Exercise::Representation::OPENSEARCH_INDEX +# ].map do |index| +# opensearch.indices.delete(index:) if opensearch.indices.exists(index:) +# opensearch.indices.create(index:) +# end +# Exercism::TOUCHED_OPENSEARCH_INDEXES = [] class ActionMailer::TestCase def assert_email(email, to, subject, fixture, bulk: false) # rubocop:disable Lint/UnusedMethodArgument @@ -170,6 +170,7 @@ class ActiveSupport::TestCase setup do reset_redis! reset_rack_attack! + stub_opensearch! # We do it like this (rather than stub/unstub) so that we # can have this method globally without disabling mocha's @@ -181,7 +182,7 @@ class ActiveSupport::TestCase end teardown do - reset_opensearch! + unstub_opensearch! Bullet.perform_out_of_channel_notifications if Bullet.notification? Bullet.end_request @@ -306,18 +307,13 @@ def download_s3_file(bucket, key) ###################### # OpenSearch Helpers # ###################### - def reset_opensearch! - return unless Exercism::TOUCHED_OPENSEARCH_INDEXES.present? + def stub_opensearch! + opensearch_client = Exercism.opensearch_client + Exercism.define_method(:opensearch_client) { opensearch_client } + end - OpenSearch::Client.unstub(:new) + def unstub_opensearch! Exercism.unstub(:opensearch_client) - opensearch = Exercism.opensearch_client - - Exercism::TOUCHED_OPENSEARCH_INDEXES.map do |index| - opensearch.indices.delete(index:) if opensearch.indices.exists(index:) - opensearch.indices.create(index:) - end - Exercism::TOUCHED_OPENSEARCH_INDEXES.clear end def get_opensearch_doc(index, id) From b4d4ef4a46cd8c3a57f97484fe347b843a649d47 Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 23 Jan 2025 13:44:23 +0000 Subject: [PATCH 2/3] Fix idiotic coding --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 3c950a7e8e..deab8971cc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -308,7 +308,7 @@ def download_s3_file(bucket, key) # OpenSearch Helpers # ###################### def stub_opensearch! - opensearch_client = Exercism.opensearch_client + opensearch_client = mock Exercism.define_method(:opensearch_client) { opensearch_client } end From 27cd1ae359b1132afa1fd8b160c8b5ed3e15ab1a Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 23 Jan 2025 15:05:20 +0000 Subject: [PATCH 3/3] Remove Exercism::TOUCHED_OPENSEARCH_INDEXES.each --- app/commands/document/sync_all_to_search_index.rb | 1 - app/commands/document/sync_to_search_index.rb | 1 - .../exercise/representation/sync_to_search_index.rb | 1 - app/commands/solution/sync_all_to_search_index.rb | 1 - app/commands/solution/sync_to_search_index.rb | 1 - test/test_helper.rb | 8 ++++---- 6 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/commands/document/sync_all_to_search_index.rb b/app/commands/document/sync_all_to_search_index.rb index 0c6bd84bda..c39584f04c 100644 --- a/app/commands/document/sync_all_to_search_index.rb +++ b/app/commands/document/sync_all_to_search_index.rb @@ -19,7 +19,6 @@ def call end Exercism.opensearch_client.bulk(body:) - Exercism::TOUCHED_OPENSEARCH_INDEXES << Document::OPENSEARCH_INDEX if Rails.env.test? end end diff --git a/app/commands/document/sync_to_search_index.rb b/app/commands/document/sync_to_search_index.rb index 737e54671b..5c50434a76 100644 --- a/app/commands/document/sync_to_search_index.rb +++ b/app/commands/document/sync_to_search_index.rb @@ -11,6 +11,5 @@ def call id: doc.id, body: Document::CreateSearchIndexDocument.(doc) ) - Exercism::TOUCHED_OPENSEARCH_INDEXES << Document::OPENSEARCH_INDEX if Rails.env.test? end end diff --git a/app/commands/exercise/representation/sync_to_search_index.rb b/app/commands/exercise/representation/sync_to_search_index.rb index 61953a43ef..f3cbc371db 100644 --- a/app/commands/exercise/representation/sync_to_search_index.rb +++ b/app/commands/exercise/representation/sync_to_search_index.rb @@ -27,7 +27,6 @@ def create_document! id: representation.id, body: Exercise::Representation::CreateSearchIndexDocument.(representation) ) - Exercism::TOUCHED_OPENSEARCH_INDEXES << Exercise::Representation::OPENSEARCH_INDEX if Rails.env.test? end def delete_document! diff --git a/app/commands/solution/sync_all_to_search_index.rb b/app/commands/solution/sync_all_to_search_index.rb index 5419ede72d..8568cb8766 100644 --- a/app/commands/solution/sync_all_to_search_index.rb +++ b/app/commands/solution/sync_all_to_search_index.rb @@ -25,7 +25,6 @@ def call end Exercism.opensearch_client.bulk(body:) - Exercism::TOUCHED_OPENSEARCH_INDEXES << Solution::OPENSEARCH_INDEX if Rails.env.test? end end diff --git a/app/commands/solution/sync_to_search_index.rb b/app/commands/solution/sync_to_search_index.rb index 76c7ce2306..aa4ae8ff1f 100644 --- a/app/commands/solution/sync_to_search_index.rb +++ b/app/commands/solution/sync_to_search_index.rb @@ -20,7 +20,6 @@ def create_document! id: solution.id, body: Solution::CreateSearchIndexDocument.(solution) ) - Exercism::TOUCHED_OPENSEARCH_INDEXES << Solution::OPENSEARCH_INDEX if Rails.env.test? end def delete_document! diff --git a/test/test_helper.rb b/test/test_helper.rb index deab8971cc..191a162aef 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -308,7 +308,7 @@ def download_s3_file(bucket, key) # OpenSearch Helpers # ###################### def stub_opensearch! - opensearch_client = mock + opensearch_client = Exercism.opensearch_client Exercism.define_method(:opensearch_client) { opensearch_client } end @@ -327,9 +327,9 @@ def wait_for_opensearch_to_be_synced perform_enqueued_jobs # Force an index refresh to ensure there are no concurrent actions in the background - Exercism::TOUCHED_OPENSEARCH_INDEXES.each do |index| - Exercism.opensearch_client.indices.refresh(index:) - end + # Exercism::TOUCHED_OPENSEARCH_INDEXES.each do |index| + # Exercism.opensearch_client.indices.refresh(index:) + # end end def perform_enqueued_jobs_until_empty