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

Minitest assertions and indexing controls. #396

Merged
merged 7 commits into from
Aug 6, 2016
Merged

Minitest assertions and indexing controls. #396

merged 7 commits into from
Aug 6, 2016

Conversation

robacarp
Copy link

@robacarp robacarp commented Jun 1, 2016

This adds support for index update assertions, and methods to control search index cleaning in a Minitest environment.

Critiques and suggestions welcome. One necessary improvement is better integration with the Chewy ORM interoperability layer. Currently the SearchIndexReceiver#indexed? and #deleted? methods expect an object to respond to #id. I'm happy to continue the work and enhance the integration if it's welcome, but I'd like some feedback before investing more time into it. We are currently using these testing helpers as is and they're functional for our needs.

The ideas and implementation in the SearchIndexReceiver mock class is largely ported directly from the rspec/update_index implementation of update_index and friends. I'm sure it would be possible to make both test suites leverage the functionality if desired, but my rspec is rusty at best.

Additions to a project test_helper.rb:

require 'chewy/minitest/helpers'

Chewy.strategy(:bypass)

And to an example test file:

require 'test_helper'

class UsersIndexTest < ActiveSupport::TestCase
  include Chewy::Minitest::Helpers

  # Optionally, enable indexing for all tests in this file.
  index_everything!

  test 'users index contains expected fields' do
    expected_fields = [:id, :email, :name, :username, :first_name, :last_name]
    meta_fields = [:_score, :_explanation]

    # Enables communicating with the test Elastic server for this block
    run_indexing do
      user = create :user
    end

    # Query Elastic for the record just added
    record = UsersIndex.filter { _id == user.entity.id }.first
    given_fields = record.attributes.keys.map &:to_sym
    assert_equal (expected_fields + meta_fields).sort, given_fields.sort
  end

  test 'creating a user causes the users index to be updated' do
    user = build :user

    # validates some change happened in UsersIndex
    updates = assert_indexes UsersIndex::Users do
      user.save
    end

    # validates that the updates contained an index request for the object
    assert updates.indexed? user

    # also possible, though perhaps not necessary in this example:
    # validates that an object is not deleted from the index
    refute updates.deleted? user
  end

end

Related to #392

@@ -1230,6 +1231,12 @@ rake chewy:update[-users,projects] # updates every index in application except s

Just add `require 'chewy/rspec'` to your spec_helper.rb and you will get additional features: See [update_index.rb](lib/chewy/rspec/update_index.rb) for more details.

### Minitest integration

Add `require 'chewy/minitest/helpers'` to your test_helper.rb, and then for tests which you'd like indexing enabled, `include Chewy::Minitest::Helpers`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 'chewy/minitest' ?

@pyromaniac
Copy link
Contributor

Does it makes sense to add some specs for this?

@robacarp
Copy link
Author

robacarp commented Jun 3, 2016

@pyromaniac yes, and I'll add them 👍

@robacarp
Copy link
Author

@pyromaniac I'd appreciate another look when you have a chance. I had a bit of a brain warp wiring up rspec to test the minitest helper, and I'm pretty out of date on my rspec style, but I think it's at least mostly tested.

@robacarp
Copy link
Author

I'm having a hard time replicating the test failures Travis is reporting on my workstation. I had not been running Ruby 2.0.0-p598, but even after installing it I don't see those failures. They appear to be indicating that the version of active support it's loading with bundle exec rake doesn't support the class_methods part of ActiveSupport::Concern, but I'm not sure.

@pyromaniac
Copy link
Contributor

It seems to be about AS < 4.2, it is not about ruby

@pyromaniac
Copy link
Contributor

Everything seems to be ok, just fix the specs and I'll merge it

@pyromaniac
Copy link
Contributor

Hey, is it ready?

@robacarp
Copy link
Author

robacarp commented Aug 6, 2016

Yep. Thanks!

On Aug 6, 2016, at 02:55, Arkadiy Zabazhanov notifications@github.com wrote:

Hey, is it ready?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@pyromaniac pyromaniac merged commit 49c6002 into toptal:master Aug 6, 2016
@pyromaniac
Copy link
Contributor

Great job! Thanks!

@robacarp robacarp deleted the minitest_helper branch August 9, 2016 17:04
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

Successfully merging this pull request may close these issues.

2 participants