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: dirty-equals.helpmanual.io

Source Code: 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:

  • IsPartialDict lets you compare a subset of a dictionary
  • IsStrictDict lets 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.6.0.tar.gz (47.2 kB view details)

Uploaded Source

Built Distribution

dirty_equals-0.6.0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file dirty_equals-0.6.0.tar.gz.

File metadata

  • Download URL: dirty_equals-0.6.0.tar.gz
  • Upload date:
  • Size: 47.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for dirty_equals-0.6.0.tar.gz
Algorithm Hash digest
SHA256 4c4e4b9b52670ad8b880c46734e5ffc52e023250ae817398b78b30e329c3955d
MD5 20cc7fce7b4a90f452524a9a8ad5d031
BLAKE2b-256 e7c6ae9831943fc9bb249555d3ca3ec427f31eb66a0e9814d9a47a105c84a334

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: dirty_equals-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for dirty_equals-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c29af40193a862ce66f932236c2a4be97489bbf7caf8a90e4a606e7c47c41b3
MD5 af48c63b5f716b9b1d2245b90413dbac
BLAKE2b-256 8605fb4988c044b1d421d2ec28b952dcf3741f4dbf1c60d68d42159c40c60e7c

See more details on using hashes here.

Provenance

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