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

Webmock configuration gone since 2.0.0 #398

Open
strayer opened this issue May 26, 2021 · 6 comments
Open

Webmock configuration gone since 2.0.0 #398

strayer opened this issue May 26, 2021 · 6 comments

Comments

@strayer
Copy link

strayer commented May 26, 2021

  • Rails version: 6.0.3.6
  • Algolia Rails integration version: 2.0.0
  • Algolia Client Version: 2.0.4
  • Language Version: 2.7.3p183

Description

The README mentions the algolia webmock sample configuration in algolia/webmock. This is used by us heavily in the tests, but seems to be gone without a trace in the newest algolia client version.

@dirceu-jr
Copy link

I used the sample configuration provided here with algolia/webmock too. It's not working in 2.0.0.

@magni-
Copy link

magni- commented Jun 17, 2021

Looks like there was a PR to add this back which was closed? algolia/algoliasearch-client-ruby#420

@bvogel
Copy link
Contributor

bvogel commented Jun 28, 2021

Webmock has been replaced by a MockRequester

@ojsdude
Copy link

ojsdude commented Aug 10, 2021

MockRequester is only on algoliasearch-client-ruby... This is algoliasearch-rails

@allanohorn
Copy link

allanohorn commented Nov 25, 2021

I wanted to upgrade today but needed to maintain my testing infrastructure so came up with a hacky solution.

  1. Copied the MockRequester in the comment ^ and slightly modified it
    # Need to add "status: published" otherwise we get stuck in an infinite loop due to algolia/search_index "wait_task"
    Algolia::Http::Response.new(
      status: 204,
      body: '{"hits": [], "status": "published"}',
      headers: {}
    )
  1. I put this at the top of my test_helper file.
MOCK_REQUESTOR = nil
if Rails.env.test?
  MOCK_REQUESTOR = MockRequester.new  # Copied the mock requestor for 
  mock_client = Algolia::Search::Client.new(
    Algolia::Search::Config.new(AlgoliaSearch.configuration),
    http_requester: MOCK_REQUESTOR
  )
  # AlgoliaSearch isn't set up to override the clicent but we can do it anyways
  AlgoliaSearch.instance_variable_set(:@client, mock_client)
end
  1. I then have some helper functions that I make available to all my tests. This is more related to my testing setup but figured it might be helpful.
module ActiveSupport
  class TestCase
    def initialize(*args)
      super
      @mock_requester = MOCK_REQUESTOR
    end
  
    def reset_algolia_requests
      @mock_requester.requests = []
    end


    def algolia_requests(model = nil)
      requests = @mock_requester.requests
      # Ignore get requests, don't care about those in context of out testing (just looking for indexing calls)
      requests = requests.select { |r| %i[post put].include? r[:method] }
      return requests.select { |r| r[:path].include? model.index_name } if model
      requests
    end

    # The include objects argument is really just here so that we can "pretty print" the output without a huge blob more easily
    def algolia_reindex_request_summary(model = nil, include_objects: false)
      algolia_requests(model).map do |r|
        body = ::JSON.parse(r[:body])['requests']
        {
          path: r[:path][3..], # Get rid of the "/1/" atthe front
          index_name: r[:path][3..].split('/')[1], # Isolates only the index name
          ids: (body.map { |b| b['objectID'] }).sort,
          objects: body.map { |b| b['body'] }
          body_count: body.count,
        }
      end
    end
  end

@bvogel
Copy link
Contributor

bvogel commented Nov 25, 2021

MockRequester is only on algoliasearch-client-ruby... This is algoliasearch-rails

this gem requires algolia which is algoliasearch-client-ruby, so it's avaliable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants