def test_missing_classes(): """Test that ValueError is raised if an embedded representation does not have classes attribute. 1. Create an embedded representation marshaler for an object without classes attribute. 2. Try to call marshal_classes 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_classes() assert error_info.value.args[ 0] == "Failed to get classes of the embedded representation", ( "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_classes(): # pylint: disable=line-too-long """Test that ValueError is raised if an embedded representation has a non-iterable object as its classes. 1. Create an embedded representation marshaler for an object with non-iterable classes. 2. Try to call marshal_classes method. 3. Check that ValueError is raised. 4. Check the error message. """ # pylint: enable=line-too-long ClassesRepresentation = namedtuple("ClassesRepresentation", "classes") marshaler = RepresentationMarshaler( marshaler=JSONMarshaler(), embedded_representation=ClassesRepresentation(classes=None), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_classes() expected_message = "Failed to iterate over classes of the embedded representation" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_classes(classes, expected_classes): """Test that classes are properly marshaled. 1. Create an embedded representation marshaler for an object with specific classes. 2. Marshal classes. 3. Check the marshaled classes. """ ClassesRepresentation = namedtuple("ClassesRepresentation", "classes") marshaler = RepresentationMarshaler( marshaler=JSONMarshaler(), embedded_representation=ClassesRepresentation(classes=classes), ) actual_classes = marshaler.marshal_classes() assert actual_classes == expected_classes, "Wrong classes"