def test_json_deserialization_no_subclassing(self): # simple case json_object = {"name": "name1", "g_id": 1} result = BGGenome.from_json(data=json_object) self.assertEqual(result.name, "name1") # g_id is not mandatory for genome deserialization itself, but is required by the supervising class self.assertEqual( BGGenome.from_json(data={ "name": "name1" }).name, "name1") # BGGenome scheme info shall be ignored at this level, as it is supplied by the supervising class self.assertEqual( BGGenome.from_json(data={ "name": "name1", BGGenome_JSON_SCHEMA_JSON_KEY: "lalal" }).name, "name1") # error case when "name" is not present with self.assertRaises(ValueError): BGGenome.from_json(data={})
def test_json_deserialization_subclassing(self): # being provided an explicit JSONSchema, it shall be utilized for json deserialization class BGGenomeJSONSchemaNameOptional(BGGenome.BGGenomeJSONSchema): @post_load def make_object(self, data): if "name" not in data: data["name"] = "default_name" return super(BGGenomeJSONSchemaNameOptional, self).make_object(data=data) self.assertEqual( BGGenome.from_json( data={}, json_schema_class=BGGenomeJSONSchemaNameOptional).name, "default_name")