Similar Dict
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.
Metadata
Release files for similar-dict 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| similar_dict-0.2.0.tar.gz | 2.6 kB | Without provenance | Details for similar_dict-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| similar_dict-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for similar_dict-0.2.0-py3-none-any.whl |
Total release size: 5.6 kB
Release files / similar_dict-0.2.0.tar.gz
| Download URL | similar_dict-0.2.0.tar.gz |
|---|---|
| Size | 2.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
95313c71c9bc6a6db1b63b6f40e7584b4e5da5b30cfcc926dc070c865e5c5418
|
|
BLAKE2b-256 checksum How to use checksums |
12c5d196015ce3967b8a5f0ac18483799fc62eeb75094209aa3d9afd5c2eb0f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.8.17 Darwin/22.6.0
|
Release files / similar_dict-0.2.0-py3-none-any.whl
| Download URL | similar_dict-0.2.0-py3-none-any.whl |
|---|---|
| Size | 3.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c894284eddc698be98c67cd8376477e2e70ca70df374e471ea152bf021a5da9
|
|
BLAKE2b-256 checksum How to use checksums |
a63948280ad1ba012e4617af776d233bba1555061c5395823b7a5f2684b16cf8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.8.17 Darwin/22.6.0
|
Security for similar-dict 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.