def test_non_marshalable_links(): # pylint: disable=line-too-long """Test that ValueError is raised if one of links of the embedded representation is not marshallable. 1. Create json marshaler that raises exception when marshal_link method is called. 2. Create an embedded representation marshaler. 3. Try to call marshal_links method. 4. Check that ValueError is raised. 5. Check the error message. """ # pylint: enable=line-too-long class _LinkErrorMarshaler(JSONMarshaler): def marshal_link(self, link): raise Exception() LinksRepresentation = namedtuple("LinksRepresentation", "links") links = [Link(relations=["first"], target="/first")] marshaler = RepresentationMarshaler( marshaler=_LinkErrorMarshaler(), embedded_representation=LinksRepresentation(links=links), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_links() expected_message = "Failed to marshal links of the embedded representation" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_missing_links(): """Test that ValueError is raised if an embedded representation does not have links attribute. 1. Create an embedded representation marshaler for an object without links attribute. 2. Try to call marshal_links method. 3. Check that ValueError is raised. 4. Check the error message. """ marshaler = RepresentationMarshaler(marshaler=JSONMarshaler(), embedded_representation=object()) with pytest.raises(ValueError) as error_info: marshaler.marshal_links() expected_message = "Failed to get links of the embedded representation" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_marshal(): """Test that data of an embedded representation is properly marshaled. 1. Create an embedded representation. 2. Create an embedded representation marshaler for the representation. 3. Replace marshaler methods so that they return predefined data. 4. Marshal the representation. 5. Check the marshaled data. """ marshaler = RepresentationMarshaler( marshaler=JSONMarshaler(), embedded_representation=EmbeddedRepresentation(relations=["self"]), ) marshaler.marshal_relations = lambda: "marshal_relations" marshaler.marshal_classes = lambda: "marshal_classes" marshaler.marshal_properties = lambda: "marshal_properties" marshaler.marshal_entities = lambda: "marshal_entities" marshaler.marshal_links = lambda: "marshal_links" marshaler.marshal_actions = lambda: "marshal_actions" marshaler.marshal_title = lambda: "marshal_title" actual_data = marshaler.marshal() expected_data = { "rel": "marshal_relations", "class": "marshal_classes", "properties": "marshal_properties", "entities": "marshal_entities", "links": "marshal_links", "actions": "marshal_actions", "title": "marshal_title", } assert actual_data == expected_data, "Embedded represenation is not properly marshaled"
def test_non_iterable_links(): # pylint: disable=line-too-long """Test that ValueError is raised if an embedded representation has a non-iterable object as its links. 1. Create an embedded representation marshaler for an object with non-iterable links. 2. Try to call marshal_links method. 3. Check that ValueError is raised. 4. Check the error message. """ # pylint: enable=line-too-long LinksRepresentation = namedtuple("LinksRepresentation", "links") marshaler = RepresentationMarshaler( marshaler=JSONMarshaler(), embedded_representation=LinksRepresentation(links=None), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_links() expected_message = "Failed to iterate over links of the embedded representation" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_links(links): """Test that links are properly marshaled. 1. Create an embedded representation marshaler. 2. Replace marshal_link of the marshaler so that it returns fake data. 3. Marshal links. 4. Check the marshaled links. """ json_marshaler = JSONMarshaler() json_marshaler.marshal_link = links.index LinksRepresentation = namedtuple("LinksRepresentation", "links") marshaler = RepresentationMarshaler( marshaler=json_marshaler, embedded_representation=LinksRepresentation(links=links), ) actual_data = marshaler.marshal_links() assert actual_data == list(range(len(links))), "Wrong links"