Skip to main content

Doing dirty (but extremely useful) things with equals.

Project description

dirty-equals

Doing dirty (but extremely useful) things with equals.

CI Coverage pypi versions license


Documentation: https://dirty-equals.helpmanual.io (Currently a work in progress!)

Source Code: https://github.com/samuelcolvin/dirty-equals


dirty-equals is a python library that (mis)uses the __eq__ method to make python code (generally unit tests) more declarative and therefore easier to read and write.

dirty-equals can be used in whatever context you like, but it comes into its own when writing unit tests for applications where you're commonly checking the response to API calls and the contents of a database.

Usage

Here's a trivial example of what dirty-equals can do:

from dirty_equals import IsPositive

assert 1 == IsPositive
assert -2 == IsPositive  # this will fail!

That doesn't look very useful yet!, but consider the following unit test code using dirty-equals:

from dirty_equals import IsJson, IsNow, IsPositiveInt, IsStr

...

# user_data is a dict returned from a database or API which we want to test
assert user_data == {
    # we want to check that id is a positive int
    'id': IsPositiveInt,
    # we know avatar_file should be a string, but we need a regex as we don't know whole value
    'avatar_file': IsStr(regex=r'/[a-z0-9\-]{10}/example\.png'),
    # settings_json is JSON, but it's more robust to compare the value it encodes, not strings
    'settings_json': IsJson({'theme': 'dark', 'language': 'en'}),
    # created_ts is datetime, we don't know the exact value, but we know it should be close to now
    'created_ts': IsNow(delta=3),
}

Without dirty-equals, you'd have to compare individual fields and/or modify some fields before comparison - the test would not be declarative or as clear.

dirty-equals can do so much more than that, for example:

  • PartialDict let's you compare a subset of a dictionary
  • IsStrictDict let's you confirm order in a dictionary
  • IsList and IsTuple lets you compare partial lists and tuples, with or without order constraints
  • nesting any of these types inside any others
  • IsInstance lets you simply confirm the type of an object
  • You can even use boolean operators | and & to combine multiple conditions
  • and much more...

Installation

Simply:

pip install dirty-equals

dirty-equals requires Python 3.7+.

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

dirty-equals-0.0.3.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dirty_equals-0.0.3-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file dirty-equals-0.0.3.tar.gz.

File metadata

  • Download URL: dirty-equals-0.0.3.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.9.10 Linux/5.11.0-1028-azure

File hashes

Hashes for dirty-equals-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0152eacad66f854f60d4c55d4e0c9f9df36a4614c649d3f5aaaa461b07a09aab
MD5 1d0e956a36662e759bbf50a316552c03
BLAKE2b-256 6b80860b6db66b2d6cbe2219bb3d6e52c4fd8de9c53509e15dd90b4dbb2fb7e8

See more details on using hashes here.

File details

Details for the file dirty_equals-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: dirty_equals-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.9.10 Linux/5.11.0-1028-azure

File hashes

Hashes for dirty_equals-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ad18a745180ef78f369462fc399e0fee26791d8a04fec15900f58075ee56d89b
MD5 028ebeeb76148f00790aee2729351188
BLAKE2b-256 d2be88cb4baa915b61ff19bef86e8cc53ac783612cb016e254f98f040c4cf99b

See more details on using hashes here.

Supported by

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