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)
def __init__(self, attributes): attributes["identities"] = [ AnyObject(UserIdentity, identity) for identity in attributes["identities"] ] super().__init__(User, attributes)
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
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)
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
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
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_)
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
def test_it_raise_ValueError_if_attributes_does_not_support_items( self, bad_input): with pytest.raises(ValueError): AnyObject.with_attrs(bad_input)
def test_it_matches_types_correctly(self, type_, instance, matches): if matches: assert instance == AnyObject(type_=type_) else: assert instance != AnyObject(type_=type_)
def test_stringification(self, type_, attributes, string): matcher = AnyObject(type_=type_, attributes=attributes) assert str(matcher) == string