示例#1
0
    def test_it_matches_with_attributes_correctly(self, attributes, matches):
        other = ValueObject(one="one", two="two")

        if matches:
            assert other == AnyObject.with_attrs(attributes)
        else:
            assert other != AnyObject.with_attrs(attributes)
示例#2
0
    def __init__(self, attributes):
        attributes["identities"] = [
            AnyObject(UserIdentity, identity)
            for identity in attributes["identities"]
        ]

        super().__init__(User, attributes)
示例#3
0
    def test_it_mocks_attributes(self):
        matcher = AnyObject.with_attrs({"a": "A"})

        assert matcher.a == "A"

        with pytest.raises(AttributeError):
            matcher.b  # pylint: disable=pointless-statement
示例#4
0
    def test_setting_magic_methods_as_attributes_does_not_set_attributes(
            self, magic_method):
        # There's no sensible reason to do it, but we should still be able to
        # function normally if you do.

        weird_matcher = AnyObject.with_attrs({magic_method: "test"})

        result = getattr(weird_matcher, magic_method)

        assert callable(result)
示例#5
0
    def assert_groups_match_commands(db_session, commands):
        groups = list(
            db_session.query(Group).filter(
                Group.authority == AUTHORITY).order_by(Group.name))

        expected_groups = sorted(
            [
                AnyObject.of_type(Group).with_attrs(command.body.attributes)
                for command in commands
            ],
            key=attrgetter("name"),
        )

        assert groups == expected_groups
示例#6
0
    def assert_membership_matches_commands(db_session, commands):
        # Sort by `group_id` as these tests always use the same `user_id`
        memberships = list(
            db_session.query(GroupMembership).order_by(
                GroupMembership.group_id))

        expected_memberships = sorted(
            [
                AnyObject.of_type(GroupMembership).with_attrs(
                    {
                        "user_id": command.body.member.id,
                        "group_id": command.body.group.id,
                    }) for command in commands
            ],
            key=attrgetter("group_id"),
        )

        assert memberships == expected_memberships
示例#7
0
    def instance_of(type_):
        """Specify that this item must be an instance of the provided type.

        :return: An instance of AnyObject configured with the given type.
        """
        return AnyObject.of_type(type_)
示例#8
0
    def test_type_and_attributes_at_once(self):
        matcher = AnyObject.of_type(ValueObject).with_attrs({"one": "one"})

        assert ValueObject("one", "two") == matcher
        assert NotValueObject("one", "two") != matcher
        assert ValueObject("bad", "two") != matcher
示例#9
0
 def test_it_raise_ValueError_if_attributes_does_not_support_items(
         self, bad_input):
     with pytest.raises(ValueError):
         AnyObject.with_attrs(bad_input)
示例#10
0
 def test_it_matches_types_correctly(self, type_, instance, matches):
     if matches:
         assert instance == AnyObject(type_=type_)
     else:
         assert instance != AnyObject(type_=type_)
示例#11
0
    def test_stringification(self, type_, attributes, string):
        matcher = AnyObject(type_=type_, attributes=attributes)

        assert str(matcher) == string