| 1 |
## Boolean Logic |
| 2 |
|
| 3 |
*dirty-equals* types can be combined based on either `&` |
| 4 |
(and, all checks must be `True` for the combined check to be `True`) or `|` |
| 5 |
(or, any check can be `True` for the combined check to be `True`). |
| 6 |
|
| 7 |
Types can also be inverted using the `~` operator, this is equivalent to using `!=` instead of `==`. |
| 8 |
|
| 9 |
Example: |
| 10 |
```py title="Boolean Combination of Types" |
| 11 |
from dirty_equals import Contains, HasLen |
| 12 |
|
| 13 |
assert ['a', 'b', 'c'] == HasLen(3) & Contains('a') # (1)! |
| 14 |
assert ['a', 'b', 'c'] == HasLen(3) | Contains('z') # (2)! |
| 15 |
|
| 16 |
assert ['a', 'b', 'c'] != Contains('z') |
| 17 |
assert ['a', 'b', 'c'] == ~Contains('z') |
| 18 |
``` |
| 19 |
|
| 20 |
1. The object on the left has to both have length 3 **and** contain `"a"` |
| 21 |
2. The object on the left has to either have length 3 **or** contain `"z"` |
| 22 |
|
| 23 |
## Initialised vs. Class comparison |
| 24 |
|
| 25 |
!!! warning |
| 26 |
|
| 27 |
This does not work with PyPy. |
| 28 |
|
| 29 |
*dirty-equals* allows comparison with types regardless of whether they've been initialised. |
| 30 |
|
| 31 |
This saves users adding `()` in lots of places. |
| 32 |
|
| 33 |
Example: |
| 34 |
|
| 35 |
```py title="Initialised vs. Uninitialised" |
| 36 |
from dirty_equals import IsInt |
| 37 |
|
| 38 |
# these two cases are the same |
| 39 |
assert 1 == IsInt |
| 40 |
assert 1 == IsInt() |
| 41 |
``` |
| 42 |
|
| 43 |
!!! Note |
| 44 |
Types that require at least on argument when being initialised (like [`IsApprox`][dirty_equals.IsApprox]) |
| 45 |
cannot be used like this, comparisons will just return `False`. |
| 46 |
|
| 47 |
## `__repr__` and pytest compatibility |
| 48 |
|
| 49 |
dirty-equals types have reasonable `__repr__` methods, which describe types and generally are a close match |
| 50 |
of how they would be created: |
| 51 |
|
| 52 |
```py title="__repr__" |
| 53 |
from dirty_equals import IsApprox, IsInt |
| 54 |
|
| 55 |
assert repr(IsInt) == 'IsInt' |
| 56 |
assert repr(IsInt()) == 'IsInt()' |
| 57 |
assert repr(IsApprox(42)) == 'IsApprox(approx=42)' |
| 58 |
``` |
| 59 |
|
| 60 |
However, the repr method of types changes when an equals (`==`) operation on them returns a `True`, in this case |
| 61 |
the `__repr__` method will return `repr(other)`. |
| 62 |
|
| 63 |
```py title="repr() after comparison" |
| 64 |
from dirty_equals import IsInt |
| 65 |
|
| 66 |
v = IsInt() |
| 67 |
assert 42 == v |
| 68 |
assert repr(v) == '42' |
| 69 |
``` |
| 70 |
|
| 71 |
This black magic is designed to make the output of pytest when asserts on large objects fail as simple as |
| 72 |
possible to read. |
| 73 |
|
| 74 |
Consider the following unit test: |
| 75 |
|
| 76 |
```py title="pytest error example" |
| 77 |
from datetime import datetime |
| 78 |
|
| 79 |
from dirty_equals import IsNow, IsPositiveInt |
| 80 |
|
| 81 |
|
| 82 |
def test_partial_dict(): |
| 83 |
api_response_data = { |
| 84 |
'id': 1, # (1)! |
| 85 |
'first_name': 'John', |
| 86 |
'last_name': 'Doe', |
| 87 |
'created_at': datetime.now().isoformat(), |
| 88 |
'phone': '+44 123456789', |
| 89 |
} |
| 90 |
|
| 91 |
assert api_response_data == { |
| 92 |
'id': IsPositiveInt(), |
| 93 |
'first_name': 'John', |
| 94 |
'last_name': 'Doe', |
| 95 |
'created_at': IsNow(iso_string=True), |
| 96 |
# phone number is missing, so the test will fail |
| 97 |
} |
| 98 |
``` |
| 99 |
|
| 100 |
1. For simplicity we've hardcoded `id` here, but in a test it could be any positive int, |
| 101 |
hence why we need `IsPositiveInt()` |
| 102 |
|
| 103 |
Here's an except from the output of `pytest -vv` show the error details: |
| 104 |
|
| 105 |
```txt title="pytest output" |
| 106 |
E Common items: |
| 107 |
E {'created_at': '2022-02-25T15:41:38.493512', |
| 108 |
E 'first_name': 'John', |
| 109 |
E 'id': 1, |
| 110 |
E 'last_name': 'Doe'} |
| 111 |
E Left contains 1 more item: |
| 112 |
E {'phone': '+44 123456789'} |
| 113 |
E Full diff: |
| 114 |
E { |
| 115 |
E 'created_at': '2022-02-25T15:41:38.493512', |
| 116 |
E 'first_name': 'John', |
| 117 |
E 'id': 1, |
| 118 |
E 'last_name': 'Doe', |
| 119 |
E + 'phone': '+44 123456789', |
| 120 |
E } |
| 121 |
``` |
| 122 |
|
| 123 |
It's easy to see that the `phone` key is missing, `id` and `created_at` are represented by the exact |
| 124 |
values they were compared to, so don't show as different in the "Full diff" section. |
| 125 |
|
| 126 |
!!! Warning |
| 127 |
This black magic only works when using initialised types, if `IsPositiveInt` was used instead `IsPositiveInt()` |
| 128 |
in the above example, the output would not be as clean. |