✨ Add support for sorting data in insert_assert
based on previous data (e.g. from a previous run) to minimize the diff
#148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✨ Add support for sorting data in
insert_assert
based on previous data (e.g. from a previous run) to minimize the diff.Motivation
Of of the main use cases for me, and where
insert_assert
shines the most (for me) is updating the assert for a big OpenAPI output from FastAPI (in FastAPI tests and SQLModel tests).Nevertheless, as the previous tests used Pydantic 1.x, the output generated by Pydantic v2 has some slight changes.
But, Pydantic v2 outputs some keys in JSON Schema in different order than v1... which is fine, because dicts are not ordered, equality is the same, tests would still pass, etc. ...but the resulting diff from the previous data and the new inserted data is quite big, just for these differences (e.g.
title
now comes before the rest). And that makes it more difficult to see the actual changes (e.g. values withstr | None
now have a schema of "any between string and null").For the FastAPI tests, during the migration to Pydantic v2, I manually updated all those differences one by one to check the actual content change.
Now I'm updating SQLModel and having a local version of this helps a lot, the git diff shows only what actually changed, and I can verify and update anything necessary much more quickly.
Problem Example
Imagine you have a test that looks like:
But now
get_data()
was updated and returns:If you just run
insert_assert
as before:You would normally get this:
This has a larger diff, although the differences are not that big:
Solution
Now let's start with the same original example:
When updating it to run
insert_assert
again, you can pass as the second argument the old data:And now when you run it, it will have the same new data, but with the keys in the new dicts sorted based on the order of the older data, minimizing the git diff:
Notice, for example, how
"foo"
was kept at the top of the dict, so there's no diff for"foo"
now (which didn't change).And the dict for
FastAPI
doesn't have diff changes.