Esempio n. 1
0
    def test_or_operator(self) -> None:
        assert 5 == IsInstance(float) | 5
        assert 5 != EqualTo(6) | IsInstance(float)

        assert str(EqualTo(5)
                   | IsInstance(int)) == '(EqualTo(5) | IsInstance(int))'

        failing_or_operator = EqualTo(5) | IsInstance(float)
        assert not 6 == failing_or_operator
        assert str(
            failing_or_operator
        ) == '(EqualTo(5)[FAILED for 6] | IsInstance(float)[FAILED for 6])[FAILED for 6]'

        one_or = EqualTo(5) | IsInstance(int)
        another_or = IsOdd() | IsTruthy()
        combo_or = one_or | another_or
        assert 5 == combo_or
        assert 6 == combo_or
        assert str(combo_or
                   ) == '(EqualTo(5) | IsInstance(int) | IsOdd() | IsTruthy())'

        or_or_normal = one_or | IsTruthy()
        assert 6 == or_or_normal
        assert str(
            or_or_normal) == '(EqualTo(5) | IsInstance(int) | IsTruthy())'
Esempio n. 2
0
    def test_and_operator(self) -> None:
        assert 5 == IsInstance(int) & 5
        assert 5 != EqualTo(5) & IsInstance(float)

        assert str(EqualTo(5)
                   & IsInstance(int)) == '(EqualTo(5) & IsInstance(int))'

        failing_and_operator = EqualTo(5) & IsInstance(float)
        assert not 5 == failing_and_operator
        assert str(
            failing_and_operator
        ) == '(EqualTo(5) & IsInstance(float)[FAILED for 5])[FAILED for 5]'

        one_and = EqualTo(5) & IsInstance(int)
        another_and = IsOdd() & IsTruthy()
        combo_and = one_and & another_and
        assert 5 == combo_and
        assert 6 != combo_and
        assert str(combo_and
                   ) == '(EqualTo(5) & IsInstance(int) & IsOdd() & IsTruthy())'

        and_and_normal = one_and & IsTruthy()
        assert 5 == and_and_normal
        assert str(
            and_and_normal) == '(EqualTo(5) & IsInstance(int) & IsTruthy())'
Esempio n. 3
0
def test_assert_that_matches():
    assert that(5).matches(And(EqualTo(5), IsInstance(int)))

    with pytest.raises(AssertionError) as exc_info:
        assert that(5).matches(EqualTo(4))
    assert str(exc_info.value).split(
        '\n')[0] == 'assert that(5).matches(EqualTo(4)[FAILED for 5])'
Esempio n. 4
0
def test_dict_contains_all_of():
    test_input = {'a': [1, 2, 3], 'b': 4, 'c': 'extra'}

    def dict_to_match() -> Dict[Any, Any]:
        return {
            'a': And(HasLength(3), All(IsInstance(int))),
            'b': 4,
            'd': NotPresent
        }

    assert not test_input == dict_to_match()
    assert test_input == DictContainsAllOf(dict_to_match())

    assert not test_input == DictContainsAllOf({'a': [1]})
    assert not test_input == DictContainsAllOf({
        'a': [1, 2, 3],
        'b': 4,
        'c': 'extra',
        'e': 'not there'
    })

    assert (
        str(
            DictContainsAllOf({
                'a': And(HasLength(3), All(IsInstance(int))),
                'b': 4,
                'd': NotPresent
            })) ==
        "DictContainsAllOf({'a': And(HasLength(3), All(IsInstance(int))), 'b': 4, 'd': NotPresent})"
    )
Esempio n. 5
0
def test_last():
    assert 'abcdef' == Last(3)('def')
    assert [1, 2, 3, 'a', 'b', 'c'] == Last(3)(All(IsInstance(str)))
    assert [1, 2, 3, 'a', 'b', 'c'] != Last(4)(All(IsInstance(str)))
    assert [1, 2, 3] == Last()(3)

    with pytest.raises(AssertionError):
        assert [1, 2, 3] == Last(2)

    with pytest.raises(TypeError) as te_info:
        assert [1, 2, 3] == Last(2)([0, 1])([2])  # type: ignore[operator]
    assert str(te_info.value) == "'_Last' object is not callable"

    with pytest.raises(AssertionError) as exc_info:
        assert [1, 2, 3] == Last(2)([1, 2])
    assert str(exc_info.value).split(
        '\n')[0] == 'assert [1, 2, 3] == Last(2)([1, 2])[FAILED for [1, 2, 3]]'
Esempio n. 6
0
def test_and():
    assert AllOf is And

    assert [1] == [And(IsInstance(int), GreaterThan(0))]
    assert {
        'a': [1]
    } == {
        'a': And(IsInstance(list), HasLength(GreaterThan(0)))
    }
    assert not {
        'a': []
    } == {
        'a': And(IsInstance(list), HasLength(GreaterThan(0)))
    }

    assert str(And(IsInstance(list), HasLength(
        GreaterThan(0)))) == 'And(IsInstance(list), HasLength(GreaterThan(0)))'
Esempio n. 7
0
    def test_matcher_in_dataclass():
        @dataclass
        class Data:
            i: int
            ldsb: List[Dict[str, bool]]

        assert Data(1, [{
            'foo': True
        }]) == Data(IsInstance(int).as_(int), [{
            'foo': IsTruthy().as_(bool)
        }])

        assert (
            str(Data(
                IsInstance(int).as_(int), [{
                    'foo': IsTruthy().as_(bool)
                }])) ==
            "test_matcher_in_dataclass.<locals>.Data(i=IsInstance(int), ldsb=[{'foo': IsTruthy()}])"
        )
Esempio n. 8
0
def test_is_instance():
    assert OfType is IsInstance

    assert [1] == [IsInstance(int, str)]
    assert 'str' == IsInstance(int, str)
    assert {'a': 'abc'} == {'a': IsInstance(str)}
    assert not ['abc'] == [IsInstance(int)]

    assert str([IsInstance(int)]) == '[IsInstance(int)]'
    assert str(IsInstance(str, int)) == 'IsInstance(str, int)'
Esempio n. 9
0
def test_matcher_in_mock_call_params():
    m = MagicMock()

    m(5)
    m.assert_called_once_with(Anything())
    m.assert_called_once_with(And(IsInstance(int), GreaterThan(3)))

    with pytest.raises(AssertionError) as exc_info:
        m.assert_called_once_with(GreaterThan(5))
    if sys.version_info >= (3, 8):
        assert (str(exc_info.value) == "expected call not found.\n"
                "Expected: mock(GreaterThan(5)[FAILED for 5])\n"
                "Actual: mock(5)")
    else:
        assert (str(exc_info.value) ==
                "Expected call: mock(GreaterThan(5)[FAILED for 5])\n"
                "Actual call: mock(5)")

    m.do_stuff_to([{'a': 1}, {'a': 2}])
    m.do_stuff_to.assert_called_once_with(InAnyOrder([{'a': 2}, {'a': 1}]))
Esempio n. 10
0
def test_are_not():
    assert [1, 2, 3] == AreNot(IsInstance(str))
    assert not [1, 2, ''] == AreNot(IsInstance(str))

    assert str(AreNot(IsInstance(str))) == 'AreNot(IsInstance(str))'
Esempio n. 11
0
def test_all():
    assert {'a': [1, 2, 3]} == {'a': All(IsInstance(int), GreaterThan(0))}
    assert not [-1, 1, 2, 3] == All(GreaterThan(0))

    assert str(All(GreaterThan(0))) == 'All(GreaterThan(0))'
Esempio n. 12
0
 def dict_to_match() -> Dict[Any, Any]:
     return {
         'a': And(HasLength(3), All(IsInstance(int))),
         'b': 4,
         'd': NotPresent
     }