def _get_common_deserialized_components( cls, data: Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]) \ -> Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Deserializes the common child components of the data dictionary :param data: The data to deserialize :return: The deserialized dictionary """ deserialized = { "media_type": MediaType[data["media_type"]], "id": Id.deserialize(data["id"]), "title": Title.deserialize(data["title"]), "relations": list(map(lambda x: Relation.deserialize(x), data["relations"])), "releasing_status": ReleasingStatus[data["releasing_status"]], "cover_url": data["cover_url"] } for date in ["releasing_start", "releasing_end"]: date_data = data[date] if date_data is not None: deserialized[date] = Date.deserialize(date_data) else: deserialized[date] = None return deserialized
def _get_common_deserialized_components( cls, data: Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]) \ -> Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Deserializes the common child components of the data dictionary :param data: The data to deserialize :return: The deserialized dictionary """ deserialized = { "media_id": Id.deserialize(data["media_id"]), "media_type": MediaType[data["media_type"]], "username": data["username"], "score": Score.deserialize(data["score"]), "consuming_status": ConsumingStatus[data["consuming_status"]] } for date in ["consuming_start", "consuming_end"]: if data[date] is not None: deserialized[date] = Date.deserialize(data[date]) else: deserialized[date] = None return deserialized
def test_string_representation(self): """ Tests that the string representation is correct :return: None """ _id = Id({IdType.MYANIMELIST: 1, IdType.ANILIST: 2}) representation = str(_id) serialised = json.loads(representation) self.assertEqual(_id, Id.deserialize(serialised))
def _deserialize(cls, data: Dict[ str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]): """ Deserializes a dictionary into an object of this type :param data: The data to deserialize :return: The deserialized object :raises TypeError: If a type error occurred :raises ValueError: If the data could not be deserialized """ source = Id.deserialize(data["source"]) source_type = MediaType[data["source_type"]] dest = Id.deserialize(data["dest"]) dest_type = MediaType[data["dest_type"]] relation_type = RelationType[data["type"]] generated = cls(source, source_type, dest, dest_type, relation_type) # type: Relation return generated
def test_invalid_deserialization(self): """ Tests that invalid serialized data raises ValueErrors when deserialized :return: None """ for data in [{ "A": 1 }, {}, { "ANILIST": "1" }, { "Anilist": 1 }, { "ANILIST": None }, []]: try: Id.deserialize(data) self.fail() except (TypeError, ValueError): pass
def test_deserialization(self): """ Tests deserializing an ID object :return: None """ self.assertEqual(Id.deserialize({ "MYANIMELIST": 1, "ANILIST": 2 }), Id({ IdType.MYANIMELIST: 1, IdType.ANILIST: 2 }))