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

Allow matching out-of-order arrays with .in_any_order #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rusterholz
Copy link

My team needed a way to reliably, consistently test the JSON returned by our API, despite the facts that:

  • the controller that builds the JSON might retrieve models in any order (not deterministic), and
  • the client that will consume the JSON does not care about the order of models in the array.

For example, our API might respond with {"objects":[{"id":1,"some_attribute":"foo"},{"id":2,"some_attribute":"bar"}]} and that would be totally valid, but it is just as likely to respond with {"objects":[{"id":2,"some_attribute":"bar"},{"id":1,"some_attribute":"foo"}]} instead, and that should also be valid.

To allow the be_json_eql matcher to match out-of-order arrays, I've added the chain method .in_any_order. It works by sorting each array in lexical order according to the JSON string representation of the element.

You can also pass false to .in_any_order if you want to, which then behaves as if you had not chained .in_any_order at all (in case someone needs to determine indifference dynamically at runtime).

Examples are available in the modified be_json_eql_spec.rb file. I also added some tests to ensure that this feature should work correctly when combined with the other features of this matcher.

@aliismayilov
Copy link

Our team also need this functionality. 👍 to be merged

@rusterholz
Copy link
Author

@aliismayilov It looks like the project managers haven't touched this repository in a few months... so, if you like, you can just use my fork for now!

@aliismayilov
Copy link

thanks @rusterholz !!

module Indifference
extend self

def toggle_indifference( toggle )
Copy link
Contributor

Choose a reason for hiding this comment

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

Please omit spaces around arguments in the method signature.

@rusterholz rusterholz changed the title Allow matching out-of-order arrays with .order_indifferent Allow matching out-of-order arrays with .in_any_order Mar 8, 2015
@rusterholz
Copy link
Author

Bump!

I incorporated your feedback from above in my latest commit.

@opsb
Copy link

opsb commented Oct 8, 2015

This would be very useful

@madis
Copy link

madis commented Dec 18, 2018

Is there an alternative for this functionality or plans to merge it?

@askreet
Copy link

askreet commented Aug 3, 2020

Stumbled on this today, what a fine vintage this PR is. Would be useful to us as well.

@jfo84
Copy link

jfo84 commented Aug 4, 2020

Also stumbling across this just now. This would be very useful functionality.

@jji-fieldwire
Copy link

+1 from me too

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.

8 participants