def test_to_dict(self): '''method to test BaseModel.to_dict()''' # testing key equality base_dict = BaseModel().to_dict() my_dict = self.Base_Model.to_dict() base_keys = base_dict.keys() my_keys = my_dict.keys() self.assertEqual(base_keys, my_keys) # testing __class__ key self.assertTrue('__class__' in my_dict) # test that __dict__ & .to_dict() are diff self.assertIsNot(self.Base_Model.__dict__, self.Base_Model.to_dict())
class Test_06_BaseModel_To_Dict(unittest.TestCase): '''Test BaseModel To_Dict Method''' def setUp(self): '''Set Up''' self.dct1 = BaseModel().to_dict() self.dct2 = BaseModel().to_dict() def test_01_is_dict_type(self): '''Test to_dict simple''' self.assertIsInstance( self.dct1, dict, "Failed to_dict does not prodice dictionary" " type") def test_02_required_keys(self): '''Test for proper output format''' key_list = self.dct1.keys() self.assertIn('id', key_list, "Error 'id' not in to_dict() output") self.assertIn('created_at', key_list, "Error 'created_by' not in to_dict() output") self.assertIn('updated_at', key_list, "Error 'updated_at' not in to_dict() output") self.assertIn('__class__', key_list, "Error '__class__' not in to_dict() output") def test_03_value_type(self): '''Test for proper value format''' value_list = self.dct1.values() for e in value_list: self.assertIsInstance(e, str, "Error to_dict has non-str value") def test_04_classname_value(self): '''Test if class name is properly stored''' self.assertEqual('BaseModel', self.dct1['__class__'], "Error incorrect key for BaseModel") def test_05_different_to_dict(self): '''Test for different outputs''' self.assertNotEqual(self.dct1, self.dct2, "Error to_dict does not produce different output")
my_model = BaseModel() my_model.name = "Holberton" my_model.my_number = 89 print(my_model.id) print(my_model) print(type(my_model.created_at)) print("--") my_model_json = my_model.to_dict() print(my_model_json) print("JSON of my_model:") for key in my_model_json.keys(): print("\t{}: ({}) - {}".format(key, type(my_model_json[key]), my_model_json[key])) print("--") my_new_model = BaseModel(**my_model_json) print(my_new_model.id) print(my_new_model) print(type(my_new_model.created_at)) my_new_model = my_new_model.to_dict() print(my_new_model) print("JSON of my_model:") for key in my_new_model.keys(): print("\t{}: ({}) - {}".format(key, type(my_new_model[key]), my_new_model[key])) print("--") print(my_model is my_new_model)