def test_missing_title(): """Test that ValueError is raised if an entity does not have title attribute. 1. Create an entity marshaler for an object without title attribute. 2. Try to call marshal_title method. 3. Check that ValueError is raised. 4. Check the error message. """ marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=object()) with pytest.raises(ValueError) as error_info: marshaler.marshal_title() assert error_info.value.args[ 0] == "Failed to get entity's title", "Wrong error"
def test_marshal(): """Test that entity data is properly marshaled. 1. Create an entity. 2. Create an entity marshaler for the entity. 3. Replace marshaler methods so that they return predefined data. 4. Marshal the entity. 5. Check the marshaled data. """ marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=Entity()) 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 = { "class": "marshal_classes", "properties": "marshal_properties", "entities": "marshal_entities", "links": "marshal_links", "actions": "marshal_actions", "title": "marshal_title", } assert actual_data == expected_data, "Entity is not properly marshaled"
def test_title(title, expected_title): """Test that title is properly marshaled. 1. Create an entity marshaler for an object with specific title. 2. Marshal title. 3. Check the marshaled title. """ TitleEntity = namedtuple("TitleEntity", "title") marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=TitleEntity(title=title)) actual_title = marshaler.marshal_title() assert actual_title == expected_title, "Wrong title"