Compare dictionary values by a type-or-value template
Project description
This is a small test helper that allows you to compare dicts (and sequences of them) to an expected template. This template can be real values or types.
If they are real values, normal comparison is used. If it’s a type, then isinstance() is used to make sure it’s of the correct type, but no value comparison is made.
This allows your test to focus on the things that are important and ignore things that do not affect the application, but merely its presentation.
Example
Consider the following pydantic objects:
class Author(BaseModel): given_name: str surname: str class Book(BaseModel): title: str published_at: datetime author: Author
A json schema for these is built from a dict that pydantic generates via the model_json_schema method and will look like this:
expected_book_schema = { "$defs": { "Author": { "properties": { "given_name": {"title": "Given Name", "type": "string"}, "surname": {"title": "Surname", "type": "string"}, }, "required": ["given_name", "surname"], "title": "Author", "type": "object", } }, "properties": { "title": {"title": "Title", "type": "string"}, "published_at": { "format": "date-time", "title": "Published At", "type": "string", }, "author": {"$ref": "#/$defs/Author"}, }, "required": ["title", "published_at", "author"], "title": "Book", "type": "object", }
Now suppose the you don’t really care about the formatting/cases/wording of the title as it doesn’t affect the program. You care about the other stuff in your test: that the keys are correct and that they serialise to the correct type.
So we change our expected dictionary for a similarity match:
expected_book_schema = { "$defs": { "Author": { "properties": { "given_name": {"title": str, "type": "string"}, "surname": {"title": str, "type": "string"}, }, "required": ["given_name", "surname"], "title": str, "type": "object", } }, "properties": { "title": {"title": str, "type": "string"}, "published_at": { "format": "date-time", "title": str, "type": "string", }, "author": {"$ref": "#/$defs/Author"}, }, "required": ["title", "published_at", "author"], "title": str, "type": "object", }
HINT: all titles have been replaced with a str type.
And so let’s look at our test:
def test_book_schema(): assert similar_dict(Book.model_json_schema(), expected_book_schema)
This test will succeed, despite that it’s not an exact match for the Schema definition.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file similar_dict-0.2.0.tar.gz
.
File metadata
- Download URL: similar_dict-0.2.0.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.17 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95313c71c9bc6a6db1b63b6f40e7584b4e5da5b30cfcc926dc070c865e5c5418 |
|
MD5 | bc682a439170c174e8e5273a7eefacef |
|
BLAKE2b-256 | 12c5d196015ce3967b8a5f0ac18483799fc62eeb75094209aa3d9afd5c2eb0f1 |
File details
Details for the file similar_dict-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: similar_dict-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.17 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c894284eddc698be98c67cd8376477e2e70ca70df374e471ea152bf021a5da9 |
|
MD5 | db36e8c86b1b705185175642183350e4 |
|
BLAKE2b-256 | a63948280ad1ba012e4617af776d233bba1555061c5395823b7a5f2684b16cf8 |