|
| 1 | +import pytest |
| 2 | + |
| 3 | + |
| 4 | +@pytest.mark.parametrize( |
| 5 | + "test_string, expected_result", |
| 6 | + [ |
| 7 | + # type matches |
| 8 | + ("dogs are great!", True), # full containment - beginning |
| 9 | + ("go, dogs, go!", True), # full containment - middle |
| 10 | + ("I like dogs", True), # full containment - end |
| 11 | + ("dogs", True), # equality |
| 12 | + ("", False), # reverse containment |
| 13 | + ("dog", False), # reverse containment |
| 14 | + ("good dog!", False), # partial overlap |
| 15 | + ("cats", False), # no overlap |
| 16 | + # type mismatches |
| 17 | + (1231, False), |
| 18 | + (11.21, False), |
| 19 | + ([], False), |
| 20 | + ({}, False), |
| 21 | + (True, False), |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_string_containing( |
| 25 | + test_string, expected_result, StringContaining # noqa: N803 |
| 26 | +): |
| 27 | + |
| 28 | + assert (test_string == StringContaining("dogs")) is expected_result |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize( |
| 32 | + "test_dict, expected_result", |
| 33 | + [ |
| 34 | + # type matches |
| 35 | + ({"dogs": "yes", "cats": "maybe", "spiders": "nope"}, True), # full containment |
| 36 | + ({"dogs": "yes", "cats": "maybe"}, True), # equality |
| 37 | + ({}, False), # reverse containment |
| 38 | + ({"dogs": "yes"}, False), # reverse containment |
| 39 | + ({"dogs": "yes", "birds": "only outside"}, False), # partial overlap |
| 40 | + ({"coyotes": "from afar"}, False), # no overlap |
| 41 | + # type mismatches |
| 42 | + ('{"dogs": "yes", "cats": "maybe"}', False), |
| 43 | + (1231, False), |
| 44 | + (11.21, False), |
| 45 | + ([], False), |
| 46 | + (True, False), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_dictionary_containing( |
| 50 | + test_dict, expected_result, DictionaryContaining # noqa: N803 |
| 51 | +): |
| 52 | + |
| 53 | + assert ( |
| 54 | + test_dict == DictionaryContaining({"dogs": "yes", "cats": "maybe"}) |
| 55 | + ) is expected_result |
| 56 | + |
| 57 | + |
| 58 | +class Animal(object): # noqa: B903 |
| 59 | + def __init__(self, name=None, age=None, description=None): |
| 60 | + self.name = name |
| 61 | + self.age = age |
| 62 | + self.description = description |
| 63 | + |
| 64 | + |
| 65 | +class Dog(Animal): |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +class Cat(Animal): |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "test_obj, type_and_attrs_result, type_only_result, attrs_only_result", |
| 75 | + [ |
| 76 | + # type matches |
| 77 | + (Dog("Maisey", 7, "silly"), True, True, True), # full attr containment |
| 78 | + (Dog("Maisey", 7), True, True, True), # type and attr equality |
| 79 | + (Dog(), False, True, False), # reverse attr containment |
| 80 | + (Dog("Maisey"), False, True, False), # reverse attr containment |
| 81 | + (Dog("Charlie", 7, "goofy"), False, True, False), # partial attr overlap |
| 82 | + (Dog("Bodhi", 6, "floppy"), False, True, False), # no attr overlap |
| 83 | + # type mismatches |
| 84 | + (Cat("Maisey", 7), False, False, True), # attr equality |
| 85 | + (Cat("Piper", 1, "doglike"), False, False, False), |
| 86 | + ("Good girl, Maisey", False, False, False), |
| 87 | + ({"name": "Maisey", "age": 7}, False, False, False), |
| 88 | + (1231, False, False, False), |
| 89 | + (11.21, False, False, False), |
| 90 | + ([], False, False, False), |
| 91 | + (True, False, False, False), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_object_described_by( |
| 95 | + test_obj, |
| 96 | + type_and_attrs_result, |
| 97 | + type_only_result, |
| 98 | + attrs_only_result, |
| 99 | + ObjectDescribedBy, # noqa: N803 |
| 100 | +): |
| 101 | + |
| 102 | + assert ( |
| 103 | + test_obj == ObjectDescribedBy(type=Dog, attrs={"name": "Maisey", "age": 7}) |
| 104 | + ) is type_and_attrs_result |
| 105 | + |
| 106 | + assert (test_obj == ObjectDescribedBy(type=Dog)) is type_only_result |
| 107 | + |
| 108 | + assert ( |
| 109 | + test_obj == ObjectDescribedBy(attrs={"name": "Maisey", "age": 7}) |
| 110 | + ) is attrs_only_result |
0 commit comments