ingenerator / behat-tableassert

Assertions for all sorts of tabular data in behat
BSD 3-Clause "New" or "Revised" License
5 stars 5 forks source link

Add ignoreRowSequence option #5

Closed TravisCarden closed 7 years ago

TravisCarden commented 7 years ago

I'm considering behat-tableassert for a project, but I need the ability to ignore the sort order of rows. What's the chance of getting an ignoreRowSequence option?

acoulton commented 7 years ago

If you wanted to look at implementing that, it would probably be useful. It's not something we'll have time to do in the short-to-medium term.

I think it might be quite complex to compare rows in literally any order - allowing for the possibility that some might be missing, extra, or have different values in one or more columns - and especially if you still wanted to produce a meaningful diff. If there wasn't an exact match, I guess you'd need some sort of fuzzy confidence-based matching to establish which rows in the result were meant to be equivalent to the rows in the expectation. I'd be a little worried that the complexity of the comparison would risk false-positives...

A simpler alternative might be to pick one fairly-reliable column to sort by before making the comparison. You could add an option for that, or implement something that takes a TableNode, sorts it by one or more columns and then returns a new TableNode with predictable ordering?

TravisCarden commented 7 years ago

Ooh! I like the idea of specifying a row to sort by. So what would the ideal interface look like? This?

$this->doAssert(
  'Failed asserting that two tables were equivalent: ',
  ['sortByColumn' => 'id'],
  $expected,
  $actual,
  $message
);
acoulton commented 7 years ago

That looks good to me.

Possibly for most flexibility 'sortByColumn' => ['id'] to support sorting on multiple columns. Although that may be overcomplicating and could easily be added later if required.

TravisCarden commented 7 years ago

A little further use has convinced me that there's too much variance between the assumptions of my application and those of your library, so I'm going to look for an alternative. Thanks for your time, and good luck on your own endeavors!

acoulton commented 7 years ago

Hi @TravisCarden no worries - I'd be interested to hear what you end up doing : always useful to have other perspectives. Cheers.

TravisCarden commented 7 years ago

You bet, @acoulton. I've come up with this for myself: https://github.com/TravisCarden/my_devel/commit/83998431b17f405ae6f5379b4abbe0231581f1fc.

TravisCarden commented 7 years ago

Update: I created a Packagist package for it: traviscarden/behat-table-comparison. Example output:

Example Output

acoulton commented 7 years ago

@TravisCarden thanks for that - it looks good. I suspect it won't quite handle some of the ways we're using / asserting tables in our projects but I'll keep an eye on it.