def test_sub_entities(sub_entities): """Test that sub-entities are properly marshaled. 1. Create json marshaler with overridden marshal_embedded_link and marshal_embedded_representation. 2. Create an entity marshaler. 3. Marshal sub-entities. 4. Check the marshaled sub-entities. """ class _SubEntitiesMarshaler(JSONMarshaler): def marshal_embedded_link(self, embedded_link): if not hasattr(embedded_link, "target"): pytest.fail( "Try to mershal embedded link instead of embedded representation" ) return sub_entities.index(embedded_link) def marshal_embedded_representation(self, embedded_representation): if hasattr(embedded_representation, "target"): pytest.fail( "Try to marshal embedded representation instead of embedded link" ) return sub_entities.index(embedded_representation) SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities") marshaler = EntityMarshaler( marshaler=_SubEntitiesMarshaler(), entity=SubEntitiesEntity(entities=sub_entities), ) actual_data = marshaler.marshal_entities() assert actual_data == list(range(len(sub_entities))), "Wrong entities"
def test_non_marshalable_sub_entities(sub_entity): """Test that ValueError is raised if one of sub-entities of the entity is not marshallable. 1. Create json marshaler that raises exception when either marshal_embedded_link or marshal_embedded_representation is called. 2. Create an entity marshaler. 3. Try to call marshal_entities method. 4. Check that ValueError is raised. 5. Check the error message. """ class _SubEntityErrorMarshaler(JSONMarshaler): def marshal_embedded_link(self, embedded_link): raise Exception() def marshal_embedded_representation(self, embedded_representation): raise Exception() SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities") marshaler = EntityMarshaler( marshaler=_SubEntityErrorMarshaler(), entity=SubEntitiesEntity(entities=[sub_entity]), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_entities() assert error_info.value.args[ 0] == "Failed to marshal sub-entities of the entity", "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_classes(classes, expected_classes): """Test that classes are properly marshaled. 1. Create an entity marshaler for an object with specific classes. 2. Marshal classes. 3. Check the marshaled classes. """ ClassesEntity = namedtuple("ClassesEntity", "classes") marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=ClassesEntity(classes=classes)) actual_classes = marshaler.marshal_classes() assert actual_classes == expected_classes, "Wrong classes"
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"
def test_missing_properties(): """Test that ValueError is raised if an entity does not have properties attribute. 1. Create an entity marshaler for an object without properties attribute. 2. Try to call marshal_properties 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_properties() assert error_info.value.args[ 0] == "Failed to get entity's properties", "Wrong error"
def test_non_iterable_actions(): """Test that ValueError is raised if an entity provides a non-iterable object as its actions. 1. Create an entity marshaler for an object with non-iterable actions. 2. Try to call marshal_actions method. 3. Check that ValueError is raised. 4. Check the error message. """ ActionsEntity = namedtuple("ActionsEntity", "actions") marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=ActionsEntity(actions=None)) with pytest.raises(ValueError) as error_info: marshaler.marshal_actions() assert error_info.value.args[ 0] == "Failed to iterate over entity's actions", "Wrong error"
def test_properties(properties): """Test that properties are properly marshaled. 1. Create an entity marshaler for an object with specific properties. 2. Marshal properties. 3. Check the marshaled properties. """ PropertiesEntity = namedtuple("PropertiesEntity", "properties") marshaler = EntityMarshaler( marshaler=JSONMarshaler(), entity=PropertiesEntity(properties=properties), ) actual_properties = json.dumps(marshaler.marshal_properties(), sort_keys=True) expected_properties = json.dumps(properties, sort_keys=True) assert actual_properties == expected_properties, "Wrong properties"
def create_entity_marshaler(self, entity): """Factory method to create a marshaler for an entity. :param entity: Siren entity to marshal. :returns: :class:`EntityMarshaler <lila.serialization.json.entity.EntityMarshaler>`. """ return EntityMarshaler(entity=entity, marshaler=self)
def test_actions(actions): """Test that actions are properly marshaled. 1. Create an entity marshaler. 2. Replace marshal_action of the marshaler so that it returns fake data. 3. Marshal actions. 4. Check the marshaled actions. """ json_marshaler = JSONMarshaler() json_marshaler.marshal_action = actions.index ActionsEntity = namedtuple("ActionsEntity", "actions") marshaler = EntityMarshaler(marshaler=json_marshaler, entity=ActionsEntity(actions=actions)) actual_data = marshaler.marshal_actions() assert actual_data == list(range(len(actions))), "Wrong actions"
def test_links(links): """Test that links are properly marshaled. 1. Create an entity 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 LinksEntity = namedtuple("LinksEntity", "links") marshaler = EntityMarshaler(marshaler=json_marshaler, entity=LinksEntity(links=links)) actual_data = marshaler.marshal_links() assert actual_data == list(range(len(links))), "Wrong links"
def test_non_iterable_sub_entities(): """Test that ValueError is raised if an entity has a non-iterable object as its sub-entities. 1. Create an entity marshaler for an object with non-iterable sub-entities. 2. Try to call marshal_entities method. 3. Check that ValueError is raised. 4. Check the error message. """ SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities") marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=SubEntitiesEntity(entities=None)) with pytest.raises(ValueError) as error_info: marshaler.marshal_entities() assert error_info.value.args[ 0] == "Failed to iterate over sub-entities of the entity", ( "Wrong error")
def test_invalid_properties(): """Test that ValueError is raised if entity's properties are not valid json object. 1. Create an entity marshaler for an object with invalid json object as its properties. 2. Try to call marshal_properties method. 3. Check that ValueError is raised. 4. Check the error message. """ PropertiesEntity = namedtuple("PropertiesEntity", "properties") marshaler = EntityMarshaler( marshaler=JSONMarshaler(), entity=PropertiesEntity(properties=object()), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_properties() assert error_info.value.args[ 0] == "Failed to marshal entity's properties", "Wrong error"
def test_non_marshalable_links(): """Test that ValueError is raised if one of links of the entity is not marshallable. 1. Create json marshaler that raises exception when marshal_link method is called. 2. Create an entity marshaler. 3. Try to call marshal_links method. 4. Check that ValueError is raised. 5. Check the error message. """ class _LinkErrorMarshaler(JSONMarshaler): def marshal_link(self, link): raise Exception() LinksEntity = namedtuple("LinksEntity", "links") links = [Link(relations=["first"], target="/first")] marshaler = EntityMarshaler(marshaler=_LinkErrorMarshaler(), entity=LinksEntity(links=links)) with pytest.raises(ValueError) as error_info: marshaler.marshal_links() assert error_info.value.args[ 0] == "Failed to marshal entity's links", "Wrong error"
def test_non_marshalable_actions(): """Test that ValueError is raised if one of actions of the entity is not marshallable. 1. Create an entity marshaler for an object with an action that can't be marshaled. 2. Try to call marshal_actions method. 3. Check that ValueError is raised. 4. Check the error message. """ class _ActionErrorMarshaler(JSONMarshaler): def marshal_action(self, action): raise Exception() ActionsEntity = namedtuple("ActionsEntity", "actions") actions = [Action(name="action", target="/action")] marshaler = EntityMarshaler( marshaler=_ActionErrorMarshaler(), entity=ActionsEntity(actions=actions), ) with pytest.raises(ValueError) as error_info: marshaler.marshal_actions() assert error_info.value.args[ 0] == "Failed to marshal entity's actions", "Wrong error"