def test_non_parsable_actions(): """Test that ValueError is raised if one of actions can't be parsed. 1. Create json parser that raises exception when parse_action is called. 2. Create an embedded representation parser with the parser. 3. Try to call parse_actions method. 4. Check that ValueError is raised. 5. Check the error message. """ class _ActionErrorParser(JSONParser): def parse_action(self, data): raise Exception() parser = EmbeddedRepresentationParser(data={"actions": [{}]}, parser=_ActionErrorParser()) with pytest.raises(ValueError) as error_info: parser.parse_actions() assert error_info.value.args[ 0] == "Failed to parse actions of the embedded representation", ( "Wrong error")
def test_actions(actions_data): """Test that actions are properly parsed. 1. Create json parser. 2. Replace parse_action method of the parser so that it returns fake data. 3. Create an embedded representation parser with the parser. 4. Parse fields. 5. Check the parsed fields. """ json_parser = JSONParser() json_parser.parse_action = actions_data.index parser = EmbeddedRepresentationParser(data={"actions": actions_data}, parser=json_parser) actual_actions = parser.parse_actions() assert actual_actions == tuple(range(len(actions_data))), "Wrong actions"
def test_parse(component_validator): """Test that data of embedded representation is properly parsed. 1. Create an embedded representation. 2. Create an embedded representation parser. 3. Replace parser methods so that they return predefined data. 4. Parse the embedded representation. 5. Check the data of the embedded representation. """ representation = EmbeddedRepresentation( relations=["parsed relation"], classes=("parsed class 1", "parsed class 2"), properties={ "property 1": 1, "property 2": [1, 2] }, entities=( EmbeddedLink(target="/embedded/link/target", relations=["relation"]), EmbeddedRepresentation(relations=["relation"]), ), links=[Link(target="/link/target", relations=["relation"])], actions=[Action(target="/action/target", name="action")], title="parsed title", ) parser = EmbeddedRepresentationParser(data={}, parser=JSONParser()) parser.parse_relations = lambda: representation.relations parser.parse_classes = lambda: representation.classes parser.parse_properties = lambda: representation.properties parser.parse_entities = lambda: representation.entities parser.parse_links = lambda: representation.links parser.parse_actions = lambda: representation.actions parser.parse_title = lambda: representation.title actual_representation = parser.parse() component_validator.validate_embedded_representation( actual_representation, representation)