Esempio n. 1
0
    def test_fix_match(self, env, result):
        result.fix.match(
            actual={'foo': 1, 'bar': 2, 'baz': 2},
            expected={'foo': 1, 'bar': 2, 'bat': 3},
            description='basic fix match',
            actual_description='description for actual',
            expected_description='description for expected',
            include_tags=['foo', 'bar'],
            exclude_tags=['baz', 'bat']
        )

        result.fix.match(
            actual={'foo': 1},
            expected={'foo': 5},
            description='simple failing match',
        )

        result.fix.match(
            actual={'foo': 1, 'bar': 'hello'},
            expected={
                'foo': cmp.Equal(1),
                'bar': re.compile('he*')
            },
            description='match with regex & custom func'
        )

        result.fix.match(
            actual={'foo': 'hello'},
            expected={
                'foo': re.compile('aaa*')
            },
            description='failing pattern match'
        )

        result.fix.match(
            actual={'foo': 1},
            expected={
                'foo': cmp.Equal(5)
            },
            description='failing comparator func'
        )

        result.fix.match(
            actual={'foo': 1},
            expected={
                'foo': error_func
            },
            description='error func'
        )

        typed_fixmsg_actual = test_utils.FixMessage(
            (('foo', 1), ('bar', 2.0)), typed_values=True)
        typed_fixmsg_ex = test_utils.FixMessage(
            (('foo', 1), ('bar', 2)), typed_values=True)
        result.fix.match(
            actual=typed_fixmsg_actual,
            expected=typed_fixmsg_ex,
            description='typed fixmsgs have different value types')
Esempio n. 2
0
    def test_dict_match(self, env, result):
        result.dict.match(actual={
            'foo': 1,
            'bar': 2,
            'baz': 2
        },
                          expected={
                              'foo': 1,
                              'bar': 2,
                              'bat': 3
                          },
                          description='basic dict match',
                          actual_description='description for actual',
                          expected_description='description for expected',
                          include_keys=['foo', 'bar'],
                          exclude_keys=['baz', 'bat'])

        result.dict.match(
            actual={'foo': 1},
            expected={'foo': 5},
            description='simple failing match',
        )

        result.dict.match(actual={
            'foo': 1,
            'bar': 'hello'
        },
                          expected={
                              'foo': cmp.Equal(1),
                              'bar': re.compile('he*')
                          },
                          description='match with regex & custom func')

        result.dict.match(actual={'foo': 'hello'},
                          expected={'foo': re.compile('aaa*')},
                          description='failing pattern match')

        result.dict.match(actual={'foo': 1},
                          expected={'foo': cmp.Equal(5)},
                          description='failing comparator func')

        result.dict.match(actual={'foo': 1},
                          expected={'foo': error_func},
                          description='error func')

        result.dict.match(actual={
            'foo': 1,
            'bar': 2
        },
                          expected={
                              'foo': 1.0,
                              'bar': 2.0
                          },
                          description='type checking fails',
                          value_cmp_func=cmp.COMPARE_FUNCTIONS['check_types'])
Esempio n. 3
0
    def test_fix_match(self, env, result):
        result.fix.match(
            actual={'foo': 1, 'bar': 2, 'baz': 2},
            expected={'foo': 1, 'bar': 2, 'bat': 3},
            description='basic fix match',
            actual_description='description for actual',
            expected_description='description for expected',
            include_tags=['foo', 'bar'],
            exclude_tags=['baz', 'bat']
        )

        result.fix.match(
            actual={'foo': 1},
            expected={'foo': 5},
            description='simple failing match',
        )

        result.fix.match(
            actual={'foo': 1, 'bar': 'hello'},
            expected={
                'foo': cmp.Equal(1),
                'bar': re.compile('he*')
            },
            description='match with regex & custom func'
        )

        result.fix.match(
            actual={'foo': 'hello'},
            expected={
                'foo': re.compile('aaa*')
            },
            description='failing pattern match'
        )

        result.fix.match(
            actual={'foo': 1},
            expected={
                'foo': cmp.Equal(5)
            },
            description='failing comparator func'
        )

        result.fix.match(
            actual={'foo': 1},
            expected={
                'foo': error_func
            },
            description='error func'
        )
Esempio n. 4
0
    def test_fix_match(self, env, result):
        result.fix.match(actual={
            'foo': 1,
            'bar': 2,
            'baz': 2
        },
                         expected={
                             'foo': 1,
                             'bar': 2,
                             'bat': 3
                         },
                         description='basic fix match',
                         actual_description='description for actual',
                         expected_description='description for expected',
                         include_tags=['foo', 'bar'],
                         exclude_tags=['baz', 'bat'])

        result.fix.match(actual={
            'foo': 1,
            'bar': 'hello'
        },
                         expected={
                             'foo': cmp.Equal(1),
                             'bar': re.compile('he*')
                         },
                         description='match with regex & custom func')
Esempio n. 5
0
    def test_fix_match(self, env, result):
        result.fix.match(
            actual={
                "foo": 1,
                "bar": 2,
                "baz": 2
            },
            expected={
                "foo": 1,
                "bar": 2,
                "bat": 3
            },
            description="basic fix match",
            actual_description="description for actual",
            expected_description="description for expected",
            include_tags=["foo", "bar"],
            exclude_tags=["baz", "bat"],
        )

        result.fix.match(
            actual={
                "foo": 1,
                "bar": "hello"
            },
            expected={
                "foo": cmp.Equal(1),
                "bar": re.compile("he*")
            },
            description="match with regex & custom func",
        )

        result.fix.match(
            actual={
                "foo": 1,
                "bar": 1.54
            },
            expected={
                "foo": "1",
                "bar": "1.54"
            },
            description="default untyped fixmatch will stringify",
        )

        typed_fixmsg = test_utils.FixMessage((("foo", 1), ("bar", 1.54)),
                                             typed_values=True)
        result.fix.match(
            actual=typed_fixmsg,
            expected=typed_fixmsg.copy(),
            description="typed fixmatch will compare types",
        )

        untyped_fixmsg = test_utils.FixMessage((("foo", "1"), ("bar", "1.54")),
                                               typed_values=False)
        result.fix.match(
            actual=typed_fixmsg,
            expected=untyped_fixmsg,
            description="mixed fixmatch will compare string values",
        )
Esempio n. 6
0
    def test_dict_match(self, env, result):

        result.dict.match(
            actual={"foo": 1, "bar": 2, "baz": 2},
            expected={"foo": 1, "bar": 2, "bat": 3},
            description="basic dict match",
            actual_description="description for actual",
            expected_description="description for expected",
            include_keys=["foo", "bar"],
            exclude_keys=["baz", "bat"],
        )

        result.dict.match(
            actual={"foo": 1, "bar": "hello"},
            expected={"foo": cmp.Equal(1), "bar": re.compile("he*")},
            description="match with regex & custom func",
        )

        result.dict.match(
            actual={"foo": 1, "bar": "2"},
            expected={"foo": 1, "bar": "2"},
            description="dict match checking types",
            value_cmp_func=cmp.COMPARE_FUNCTIONS["check_types"],
        )

        class AlwaysComparesTrue(object):
            """Object that compares equal to any other object."""

            def __eq__(self, _):
                return True

        result.dict.match(
            actual={"foo": 1, "bar": 2},
            expected={
                "foo": AlwaysComparesTrue(),
                "bar": AlwaysComparesTrue(),
            },
            description="comparison of different types",
        )

        class HelloObj(object):
            """Object that returns 'hello' as its str() representation."""

            def __str__(self):
                return "hello"

        result.dict.match(
            actual={"foo": 1, "bar": "hello"},
            expected={"foo": "1", "bar": HelloObj()},
            description="match with stringify method",
            value_cmp_func=cmp.COMPARE_FUNCTIONS["stringify"],
        )
Esempio n. 7
0
    def test_fix_match(self, env, result):
        result.fix.match(actual={
            'foo': 1,
            'bar': 2,
            'baz': 2
        },
                         expected={
                             'foo': 1,
                             'bar': 2,
                             'bat': 3
                         },
                         description='basic fix match',
                         actual_description='description for actual',
                         expected_description='description for expected',
                         include_tags=['foo', 'bar'],
                         exclude_tags=['baz', 'bat'])

        result.fix.match(actual={
            'foo': 1,
            'bar': 'hello'
        },
                         expected={
                             'foo': cmp.Equal(1),
                             'bar': re.compile('he*')
                         },
                         description='match with regex & custom func')

        result.fix.match(actual={
            'foo': 1,
            'bar': 1.54
        },
                         expected={
                             'foo': '1',
                             'bar': '1.54'
                         },
                         description='default untyped fixmatch will stringify')

        typed_fixmsg = test_utils.FixMessage((('foo', 1), ('bar', 1.54)),
                                             typed_values=True)
        result.fix.match(actual=typed_fixmsg,
                         expected=typed_fixmsg.copy(),
                         description='typed fixmatch will compare types')

        untyped_fixmsg = test_utils.FixMessage((('foo', '1'), ('bar', '1.54')),
                                               typed_values=False)
        result.fix.match(
            actual=typed_fixmsg,
            expected=untyped_fixmsg,
            description='mixed fixmatch will compare string values')
Esempio n. 8
0
    def test_fix_match(self, env, result):
        result.fix.match(
            actual={
                "foo": 1,
                "bar": 2,
                "baz": 2
            },
            expected={
                "foo": 1,
                "bar": 2,
                "bat": 3
            },
            description="basic fix match",
            actual_description="description for actual",
            expected_description="description for expected",
            include_tags=["foo", "bar"],
            exclude_tags=["baz", "bat"],
        )

        result.fix.match(
            actual={"foo": 1},
            expected={"foo": 5},
            description="simple failing match",
        )

        result.fix.match(
            actual={
                "foo": 1,
                "bar": "hello"
            },
            expected={
                "foo": cmp.Equal(1),
                "bar": re.compile("he*")
            },
            description="match with regex & custom func",
        )

        result.fix.match(
            actual={"foo": "hello"},
            expected={"foo": re.compile("aaa*")},
            description="failing pattern match",
        )

        result.fix.match(
            actual={"foo": 1},
            expected={"foo": cmp.Equal(5)},
            description="failing comparator func",
        )

        result.fix.match(
            actual={"foo": 1},
            expected={"foo": error_func},
            description="error func",
        )

        typed_fixmsg_actual = test_utils.FixMessage((("foo", 1), ("bar", 2.0)),
                                                    typed_values=True)
        typed_fixmsg_ex = test_utils.FixMessage((("foo", 1), ("bar", 2)),
                                                typed_values=True)
        result.fix.match(
            actual=typed_fixmsg_actual,
            expected=typed_fixmsg_ex,
            description="typed fixmsgs have different value types",
        )
Esempio n. 9
0
    def test_dict_match(self, env, result):
        result.dict.match(
            actual={
                "foo": 1,
                "bar": 2,
                "baz": 2
            },
            expected={
                "foo": 1,
                "bar": 2,
                "bat": 3
            },
            description="basic dict match",
            actual_description="description for actual",
            expected_description="description for expected",
            include_keys=["foo", "bar"],
            exclude_keys=["baz", "bat"],
        )

        result.dict.match(
            actual={"foo": 1},
            expected={"foo": 5},
            description="simple failing match",
        )

        result.dict.match(
            actual={
                "foo": 1,
                "bar": "hello"
            },
            expected={
                "foo": cmp.Equal(1),
                "bar": re.compile("he*")
            },
            description="match with regex & custom func",
        )

        result.dict.match(
            actual={"foo": "hello"},
            expected={"foo": re.compile("aaa*")},
            description="failing pattern match",
        )

        result.dict.match(
            actual={"foo": 1},
            expected={"foo": cmp.Equal(5)},
            description="failing comparator func",
        )

        result.dict.match(
            actual={"foo": 1},
            expected={"foo": error_func},
            description="error func",
        )

        result.dict.match(
            actual={
                "foo": 1,
                "bar": 2
            },
            expected={
                "foo": 1.0,
                "bar": 2.0
            },
            description="type checking fails",
            value_cmp_func=cmp.COMPARE_FUNCTIONS["check_types"],
        )
Esempio n. 10
0

def test_custom_callable():
    custom_callable = cmp.Custom(lambda value: value % 2 == 0,
                                 description="Value is even.")

    assert custom_callable(4) == True
    assert str(custom_callable) == "Value is even."


@pytest.mark.parametrize(
    "composed_callable,value,expected,description",
    (
        (cmp.LessEqual(5) & cmp.Greater(2), 4, True, "(VAL <= 5 and VAL > 2)"),
        (
            cmp.In([1, 2, 3]) | cmp.Equal(None),
            None,
            True,
            "(VAL in [1, 2, 3] or VAL == None)",
        ),
        (
            cmp.And(
                cmp.Or(cmp.Equal("foo"), cmp.In([1, 2, 3]), cmp.Less(10)),
                cmp.Or(cmp.Greater(5), cmp.IsFalse()),
            ),
            8,
            True,
            "((VAL == foo or VAL in [1, 2, 3] or "
            "VAL < 10) and (VAL > 5 or bool(VAL) is False))",
        ),
    ),
Esempio n. 11
0
    assert callable_obj(value) == expected
    assert str(callable_obj) == description


def test_custom_callable():
    custom_callable = cmp.Custom(lambda value: value % 2 == 0,
                                 description='Value is even.')

    assert custom_callable(4) == True
    assert str(custom_callable) == 'Value is even.'


@pytest.mark.parametrize(
    'composed_callable,value,expected,description',
    ((cmp.LessEqual(5) & cmp.Greater(2), 4, True, '(VAL <= 5 and VAL > 2)'),
     (cmp.In([1, 2, 3]) | cmp.Equal(None), None, True,
      '(VAL in [1, 2, 3] or VAL == None)'), (
          cmp.And(cmp.Or(
              cmp.Equal('foo'),
              cmp.In([1, 2, 3]),
              cmp.Less(10),
          ), cmp.Or(cmp.Greater(5), cmp.IsFalse())),
          8,
          True,
          "((VAL == foo or VAL in [1, 2, 3] or "
          "VAL < 10) and (VAL > 5 or bool(VAL) is False))",
      )))
def test_comparator_composition(composed_callable, value, expected,
                                description):
    assert composed_callable(value) == expected
    assert str(composed_callable) == description
Esempio n. 12
0
    def test_dict_match(self, env, result):

        result.dict.match(actual={
            'foo': 1,
            'bar': 2,
            'baz': 2
        },
                          expected={
                              'foo': 1,
                              'bar': 2,
                              'bat': 3
                          },
                          description='basic dict match',
                          actual_description='description for actual',
                          expected_description='description for expected',
                          include_keys=['foo', 'bar'],
                          exclude_keys=['baz', 'bat'])

        result.dict.match(actual={
            'foo': 1,
            'bar': 'hello'
        },
                          expected={
                              'foo': cmp.Equal(1),
                              'bar': re.compile('he*')
                          },
                          description='match with regex & custom func')

        result.dict.match(actual={
            'foo': 1,
            'bar': '2'
        },
                          expected={
                              'foo': 1,
                              'bar': '2'
                          },
                          description='dict match checking types',
                          value_cmp_func=cmp.COMPARE_FUNCTIONS['check_types'])

        class AlwaysComparesTrue(object):
            """Object that compares equal to any other object."""
            def __eq__(self, _):
                return True

        result.dict.match(actual={
            'foo': 1,
            'bar': 2
        },
                          expected={
                              'foo': AlwaysComparesTrue(),
                              'bar': AlwaysComparesTrue()
                          },
                          description='comparison of different types')

        class HelloObj(object):
            """Object that returns 'hello' as its str() representation."""
            def __str__(self):
                return 'hello'

        result.dict.match(actual={
            'foo': 1,
            'bar': 'hello'
        },
                          expected={
                              'foo': '1',
                              'bar': HelloObj()
                          },
                          description='match with stringify method',
                          value_cmp_func=cmp.COMPARE_FUNCTIONS['stringify'])