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.7.1.post0.tar.gz (48.9 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.7.1.post0-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file dirty_equals-0.7.1.post0.tar.gz.

File metadata

  • Download URL: dirty_equals-0.7.1.post0.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for dirty_equals-0.7.1.post0.tar.gz
Algorithm Hash digest
SHA256 78ff80578a46163831ecb3255cf30d03d1dc2fbca8e67f820105691a1bc556dc
MD5 9a870749ede1ae06a891dabe7756c26b
BLAKE2b-256 11f8b4bfe281b13091d47c0204fe0f3e033df1275281c2ee0ac0dbda9de65588

See more details on using hashes here.

File details

Details for the file dirty_equals-0.7.1.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for dirty_equals-0.7.1.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fb9217ea7cd04c0e95ace3bc717e2ee5532b8990518533483e53b5a43903c88
MD5 64b5b44bc4994412f438a65fbf777e0f
BLAKE2b-256 910abf890d9b65ee240ff1a9f68fed10cfdd94409d2504d36e076a63e125f8e3

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