Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of opensearch in tests #7374

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/commands/document/sync_all_to_search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion app/commands/document/sync_to_search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
1 change: 0 additions & 1 deletion app/commands/solution/sync_all_to_search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion app/commands/solution/sync_to_search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
44 changes: 20 additions & 24 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -331,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
Expand Down
Loading