Skip to content

testutils: use a rows parser for test comparisons#12138

Merged
systay merged 6 commits intovitessio:mainfrom
planetscale:vmg/test-rows-equals
Feb 14, 2023
Merged

testutils: use a rows parser for test comparisons#12138
systay merged 6 commits intovitessio:mainfrom
planetscale:vmg/test-rows-equals

Conversation

@vmg
Copy link
Collaborator

@vmg vmg commented Jan 24, 2023

Description

A pretty common pattern in Vitess end2end tests is comparing the results of a query against a hardcoded string that represents a %v-formatted syntax of the rows we're expecting. It usually looks something like this:

assert.Equals(t, "[[INT64(1) INT64(8)] [INT64(1) INT64(9)]]", fmt.Sprintf("%v", rows))

This is all well and good! There are several helpers that print the rows directly without having to call Sprintf and that compare them directly. I like the literal strings to define the expectations for row results.

However, shit gets funky when testing queries whose result from Vitess/MySQL doesn't have consistent ordering. How can we handle such comparisons when the sql.Rows returned by Vitess can have any ordering but our expectation string is obviously fixed?

This is how it's done at the moment:

actual := fmt.Sprintf("%v", qr.Rows)
assert.Equal(mcmp.t, utils.SortString(expected), utils.SortString(actual))

This... doesn't seem generally safe? Is SortString doing something very smart here? If you look at its implementation, you can see that, huh, it's doing the opposite of something very smart. It re-arranges all the individual bytes of the string alphabetically and returns that.

So, for a rows string like "[[INT64(1) INT64(8)] [INT64(1) INT64(9)]]", it generates " (((())))114444666689IIIINNNNTTTT[[[]]]" and compares that against the other formatted rows. The many ways in which this is not a generally safe way to compare row results are left to the reader. I bet you can come up with hundreds!

So, onto the actual content of the PR: this PR introduces a generally safe way to compare unordered sets of rows! 🎉

It works by implementing a simple Lexer + Parser that can take a "row string" like the ones we use in our tests and converts it into an actual sql.Row (i.e. a []sql.Value slice). Then, it performs an ordering-agnostic comparison of the sql.Value entries against the values returned by Vitess/MySQL. It works very well in practice! We've tested it internally at PlanetScale.

I'm hoping this will catch some actual mismatches in our end2end tests down the road.

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Documentation was added or is not required

Deployment Notes

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Jan 24, 2023

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • If this is a change that users need to know about, please apply the release notes (needs details) label so that merging is blocked unless the summary release notes document is included.
  • If a test is added or modified, there should be a documentation on top of the test to explain what the expected behavior is what the test does.

If a new flag is being introduced:

  • Is it really necessary to add this flag?
  • Flag names should be clear and intuitive (as far as possible)
  • Help text should be descriptive.
  • Flag names should use dashes (-) as word separators rather than underscores (_).

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow should be required, the maintainer team should be notified.

Bug fixes

  • There should be at least one unit or end-to-end test.
  • The Pull Request description should include a link to an issue that describes the bug.

Non-trivial changes

  • There should be some code comments as to why things are implemented the way they are.

New/Existing features

  • Should be documented, either by modifying the existing documentation or creating new documentation.
  • New features should have a link to a feature request issue or an RFC that documents the use cases, corner cases and test cases.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • vtctl command output order should be stable and awk-able.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from VTop, if used there.

@vitess-bot vitess-bot bot added NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says labels Jan 24, 2023
Copy link
Collaborator

@systay systay left a comment

Choose a reason for hiding this comment

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

This is really great. Thank you!

@systay systay removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says labels Jan 24, 2023
@systay systay changed the title tesutils: use a rows parser for test comparisons testutils: use a rows parser for test comparisons Jan 24, 2023
@systay systay requested a review from shlomi-noach as a code owner January 26, 2023 15:12
@dbussink dbussink force-pushed the vmg/test-rows-equals branch from 040d29c to af5903f Compare February 7, 2023 08:44
vmg and others added 6 commits February 14, 2023 11:32
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Vicent Marti <vmg@strn.cat>
@vmg vmg force-pushed the vmg/test-rows-equals branch from af5903f to 6464bf4 Compare February 14, 2023 10:54
@systay systay merged commit 0ade71d into vitessio:main Feb 14, 2023
@systay systay deleted the vmg/test-rows-equals branch February 14, 2023 15:31
@vmg
Copy link
Collaborator Author

vmg commented Feb 14, 2023

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants