Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

similar_dict-0.2.0.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

similar_dict-0.2.0-py3-none-any.whl (3.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page