| 1 |
<p align="center"> |
| 2 |
<img src="./img/logo-text.svg" alt="dirty-equals"> |
| 3 |
</p> |
| 4 |
<p align="center"> |
| 5 |
<em>Doing dirty (but extremely useful) things with equals.</em> |
| 6 |
</p> |
| 7 |
<p align="center"> |
| 8 |
<a href="https://github.com/samuelcolvin/dirty-equals/actions?query=event%3Apush+branch%3Amain+workflow%3ACI"> |
| 9 |
<img src="https://github.com/samuelcolvin/dirty-equals/workflows/CI/badge.svg?event=push" alt="CI"> |
| 10 |
</a> |
| 11 |
<a href="https://codecov.io/gh/samuelcolvin/dirty-equals"> |
| 12 |
<img src="https://codecov.io/gh/samuelcolvin/dirty-equals/branch/main/graph/badge.svg" alt="Coverage"> |
| 13 |
</a> |
| 14 |
<a href="https://pypi.python.org/pypi/dirty-equals"> |
| 15 |
<img src="https://img.shields.io/pypi/v/dirty-equals.svg" alt="pypi"> |
| 16 |
</a> |
| 17 |
<a href="https://github.com/samuelcolvin/dirty-equals"> |
| 18 |
<img src="https://img.shields.io/pypi/pyversions/dirty-equals.svg" alt="versions"> |
| 19 |
</a> |
| 20 |
<a href="https://github.com/samuelcolvin/dirty-equals/blob/main/LICENSE"> |
| 21 |
<img src="https://img.shields.io/github/license/samuelcolvin/dirty-equals.svg" alt="license"> |
| 22 |
</a> |
| 23 |
</p> |
| 24 |
|
| 25 |
--- |
| 26 |
|
| 27 |
{{ version }} |
| 28 |
|
| 29 |
**dirty-equals** is a python library that (mis)uses the `__eq__` method to make python code (generally unit tests) |
| 30 |
more declarative and therefore easier to read and write. |
| 31 |
|
| 32 |
*dirty-equals* can be used in whatever context you like, but it comes into its own when writing unit tests for |
| 33 |
applications where you're commonly checking the response to API calls and the contents of a database. |
| 34 |
|
| 35 |
## Usage |
| 36 |
|
| 37 |
Here's a trivial example of what *dirty-equals* can do: |
| 38 |
|
| 39 |
```{.py title="Trivial Usage" test="skip"} |
| 40 |
from dirty_equals import IsPositive |
| 41 |
|
| 42 |
assert 1 == IsPositive # (1)! |
| 43 |
assert -2 == IsPositive # this will fail! (2) |
| 44 |
``` |
| 45 |
|
| 46 |
1. This `assert` will pass since `1` is indeed positive, so the result of `1 == IsPositive` is `True`. |
| 47 |
2. This will fail (raise a `AssertionError`) since `-2` is not positive, |
| 48 |
so the result of `-2 == IsPositive` is `False`. |
| 49 |
|
| 50 |
**Not that interesting yet!**, but consider the following unit test code using **dirty-equals**: |
| 51 |
|
| 52 |
```py title="More Powerful Usage" lint="skip" |
| 53 |
from dirty_equals import IsJson, IsNow, IsPositiveInt, IsStr |
| 54 |
|
| 55 |
|
| 56 |
def test_user_endpoint(client: 'HttpClient', db_conn: 'Database'): |
| 57 |
client.post('/users/create/', data=...) |
| 58 |
|
| 59 |
user_data = db_conn.fetchrow('select * from users') |
| 60 |
assert user_data == { |
| 61 |
'id': IsPositiveInt, # (1)! |
| 62 |
'username': 'samuelcolvin', # (2)! |
| 63 |
'avatar_file': IsStr(regex=r'/[a-z0-9\-]{10}/example\.png'), # (3)! |
| 64 |
'settings_json': IsJson({'theme': 'dark', 'language': 'en'}), # (4)! |
| 65 |
'created_ts': IsNow(delta=3), # (5)! |
| 66 |
} |
| 67 |
``` |
| 68 |
|
| 69 |
1. We don't actually care what the `id` is, just that it's present, it's an `int` and it's positive. |
| 70 |
2. We can use a normal key and value here since we know exactly what value `username` should have before we test it. |
| 71 |
3. `avatar_file` is a string, but we don't know all of the string before the `assert`, |
| 72 |
just the format (regex) it should match. |
| 73 |
4. `settings_json` is a `JSON` string, but it's simpler and more robust to confirm it represents a particular python |
| 74 |
object rather than compare strings. |
| 75 |
5. `created_at` is a `datetime`, although we don't know (or care) about its exact value; |
| 76 |
since the user was just created we know it must be close to now. `delta` is optional, it defaults to 2 seconds. |
| 77 |
|
| 78 |
Without **dirty-equals**, you'd have to compare individual fields and/or modify some fields before comparison |
| 79 |
- the test would not be declarative or as clear. |
| 80 |
|
| 81 |
**dirty-equals** can do so much more than that, for example: |
| 82 |
|
| 83 |
* [`IsPartialDict`][dirty_equals.IsPartialDict] lets you compare a subset of a dictionary |
| 84 |
* [`IsStrictDict`][dirty_equals.IsStrictDict] lets you confirm order in a dictionary |
| 85 |
* [`IsList`][dirty_equals.IsList] and [`IsTuple`][dirty_equals.IsTuple] lets you compare partial lists and tuples, |
| 86 |
with or without order constraints |
| 87 |
* nesting any of these types inside any others |
| 88 |
* [`IsInstance`][dirty_equals.IsInstance] lets you simply confirm the type of an object |
| 89 |
* You can even use [boolean operators](./usage.md#boolean-logic) `|` and `&` to combine multiple conditions |
| 90 |
* and much more... |
| 91 |
|
| 92 |
## Installation |
| 93 |
|
| 94 |
Simply: |
| 95 |
|
| 96 |
```bash |
| 97 |
pip install dirty-equals |
| 98 |
``` |
| 99 |
|
| 100 |
**dirty-equals** requires **Python 3.9+**. |