✨ 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 with str | 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:
def test_dict(insert_assert):
data = get_data()
# insert_assert(data)
assert data == {
"foo": 1,
"bar": [
{"name": "Pydantic", "tags": ["validation", "json"]},
{"name": "FastAPI", "description": "Web API framework in Python"},
{"name": "SQLModel"},
],
"baz": 3,
}
def test_dict(insert_assert):
data = get_data()
# insert_assert(data)
assert data == {
"foo": 1,
"bar": [
{"name": "Pydantic", "tags": ["validation", "json"]},
{"name": "FastAPI", "description": "Web API framework in Python"},
{"name": "SQLModel"},
],
"baz": 3,
}
When updating it to run insert_assert again, you can pass as the second argument the old data:
def test_dict(insert_assert):
data = get_data()
insert_assert(data, {
"foo": 1,
"bar": [
{"name": "Pydantic", "tags": ["validation", "json"]},
{"name": "FastAPI", "description": "Web API framework in Python"},
{"name": "SQLModel"},
],
"baz": 3,
})
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:
✨ 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.