def test_attributes_type(self): """Test attr types""" b = BaseModel() b.name = "empty" b.age = 13 self.assertEqual(type(b.id), str) self.assertEqual(type(b.created_at), datetime) self.assertEqual(type(b.updated_at), datetime)
def test_from_dict(self): """__init__(self, *args, **kwargs)""" from_dict = BaseModel() from_dict.name = "Pedro" from_dict.age = 95 from_dict_json = from_dict.to_dict() from_dict_new = BaseModel(**from_dict_json) self.assertEqual(from_dict_new.to_dict(), from_dict.to_dict())
def test_instance_kwargs(self): """ Test instantiation with kwargs""" b = BaseModel() b.name = "scoot" b.age = 98 model_json = b.to_dict() new_model = BaseModel(**model_json) self.assertEqual(new_model.to_dict(), b.to_dict())
def test_03_adding_extra_parameters(self): """Test for manually adding parameters""" b = BaseModel() b.first_name = "Ophelie" b.age = 24 self.assertTrue(hasattr(b, "first_name")) self.assertTrue(hasattr(b, "age")) self.assertEqual(type(b.first_name), str) self.assertEqual(type(b.age), int)
def test_value_attr_type(self): b = BaseModel() b.name = "empty" b.age = 13 f = "%Y-%m-%dT%H:%M:%S.%f" d = b.to_dict() self.assertEqual(d['created_at'], b.created_at.strftime(f)) self.assertEqual(d['updated_at'], b.updated_at.strftime(f)) self.assertEqual("empty", b.name) self.assertEqual(13, b.age)
def test_reload(self): """test reload""" objects = BaseModel() storage = FileStorage() objects.name = "sober" objects.age = 23.45 objects.save() storage._FileStorage__objects = {} storage.reload() self.assertEqual(storage.all(), {})
def test_to_dict(self): """test attributes types in dict""" b = BaseModel() b.name = "Mark" b.age = 12 dict_base = b.to_dict() self.assertEqual(dict_base['updated_at'], b.updated_at.isoformat()) self.assertEqual(dict_base['__class__'], "BaseModel") self.assertEqual(dict_base['name'], "Mark") self.assertEqual(dict_base['age'], 12) self.assertEqual(dict_base['created_at'], b.created_at.isoformat())
def test_to_dict(self): base = BaseModel() base.name = "Kobe" base.age = 40 convert = base.to_dict() self.assertEqual(convert["id"], base.id) self.assertEqual(convert["name"], base.name) self.assertEqual(convert["age"], base.age) self.assertEqual(convert["updated_at"], base.updated_at.isoformat()) self.assertEqual(convert["created_at"], base.created_at.isoformat()) self.assertEqual(convert["__class__"], type(base).__name__)
def test_to_dict(self): """Tests the public instance method to_dict()""" b = BaseModel() b.name = "Adonis" b.age = 22 d = b.to_dict() self.assertEqual(d["id"], b.id) self.assertEqual(d["__class__"], type(b).__name__) self.assertEqual(d["created_at"], b.created_at.isoformat()) self.assertEqual(d["updated_at"], b.updated_at.isoformat()) self.assertEqual(d["name"], b.name) self.assertEqual(d["age"], b.age)
def test_to_dict(self): """send to dict""" rat = BaseModel() rat.name = "labrat" rat.age = 100 thing = rat.to_dict() self.assertEqual(thing["id"], rat.id) self.assertEqual(thing["__class__"], type(rat).__name__) self.assertEqual(thing["created_at"], rat.created_at.isoformat()) self.assertEqual(thing["updated_at"], rat.updated_at.isoformat()) self.assertEqual(thing["name"], rat.name) self.assertEqual(thing["age"], rat.age)
def test_to_dict(self): """testing to_dict method""" b = BaseModel() b.name = "holberton" b.age = 25 dict_ = b.to_dict() self.assertEqual(dict_["id"], b.id) self.assertEqual(dict_["__class__"], type(b).__name__) self.assertEqual(dict_["created_at"], b.created_at.isoformat(sep=' ')) self.assertEqual(dict_["updated_at"], b.updated_at.isoformat(sep=' ')) self.assertEqual(dict_["name"], b.name) self.assertEqual(dict_["age"], b.age)
def test_04_to_dict(self): """Test to validate to_dict""" b = BaseModel() b.first_name = "Huy" b.age = 18 d = b.to_dict() self.assertTrue('first_name' in d) self.assertTrue('age' in d) self.assertTrue('id' in d) self.assertTrue('created_at' in d) self.assertTrue('updated_at' in d) self.assertTrue('__class__' in d)
def test_to_dict(self): """ test method to dict """ self.kill() base = BaseModel() base.name = "Maicol" base.age = 22 convert = base.to_dict() self.assertEqual(convert["id"], base.id) self.assertEqual(convert["name"], base.name) self.assertEqual(convert["age"], base.age) self.assertEqual(convert["updated_at"], base.updated_at.isoformat()) self.assertEqual(convert["created_at"], base.created_at.isoformat()) self.assertEqual(convert["__class__"], type(base).__name__)
def test_save_reload_file_storage(self): """ Check save / reload method""" obj = BaseModel() obj.name = "Manuel-12345" obj.age = 26 storage.save() FileStorage._FileStorage__objects = {} storage.reload() self.assertTrue(storage.all()["BaseModel" + "." + str(obj.id)].name == "Manuel-12345") self.assertTrue(storage.all()["BaseModel" + "." + str(obj.id)].age == 26)
def test_str(self): """__str__: should print: [<class name>] (<self.id>) <self.__dict__>""" str_model = BaseModel() str_model.name = "Holberton" str_model.age = 89 temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): print(str_model) output = temp_stdout.getvalue().strip() self.assertIn("[BaseModel]", output) self.assertIn("'age': 89", output) self.assertIn("'name': 'Holberton'", output) self.assertIn("'created_at': datetime.datetime", output) self.assertIn("'updated_at': datetime.datetime", output)
def test_to_dict_base(self): """Test for dict method""" obj = BaseModel() my_dict = obj.to_dict() self.assertTrue("__class__" in my_dict) self.assertTrue(str(type(my_dict["created_at"])) == "<class 'str'>") self.assertTrue(str(type(my_dict["updated_at"])) == "<class 'str'>") obj.name = "Manuel" obj.age = 26 my_dict = obj.to_dict() self.assertTrue(str(type(my_dict["age"])) == "<class 'int'>") self.assertEqual(my_dict["age"], 26) self.assertTrue(str(type(my_dict["name"])) == "<class 'str'>") self.assertEqual(my_dict["name"], "Manuel")
def test_save(self): """ Tests the save method """ obj = BaseModel() time1 = obj.updated_at obj.name = "Plankton" obj.age = 88.32 obj.save() time2 = obj.updated_at dict_obj = storage.all() obj_ref = storage.all().get("BaseModel.{}".format(obj.id)) self.assertNotEqual(time1, time2) self.assertEqual(obj.id, obj_ref.id) self.assertEqual(obj.name, obj_ref.name) self.assertEqual(obj.age, obj_ref.age) self.assertTrue(os.path.exists('file.json'))
def test_to_dict(self): """Test to_dict method""" ba = BaseModel() ba.age = 28 ba.size = "tall" for k, v in ba.__dict__.items(): if k != 'updated_at' and k != 'created_at': self.assertIn(k, ba.to_dict()) self.assertEqual(v, ba.to_dict()[k]) self.assertEqual(ba.to_dict()['__class__'], ba.__class__.__name__) self.assertEqual(ba.to_dict()['updated_at'], ba.updated_at.isoformat()) self.assertEqual(ba.to_dict()['created_at'], ba.created_at.isoformat()) self.assertEqual(ba.to_dict()['age'], 28) self.assertEqual(ba.to_dict()['size'], 'tall') self.assertIsInstance(ba.to_dict(), dict)
def test_to_dict(self): """Test the public instance method to_dict""" to_dict = BaseModel() to_dict.name = "Santiago" to_dict.age = 8 to_dict_json = to_dict.to_dict() self.assertEqual(to_dict_json["name"], to_dict.name) self.assertEqual(to_dict_json["id"], to_dict.id) self.assertEqual(to_dict_json["age"], to_dict.age) self.assertEqual(to_dict_json["__class__"], type(to_dict).__name__) self.assertEqual(to_dict_json["created_at"], to_dict.created_at.isoformat()) self.assertEqual(to_dict_json["created_at"], to_dict.created_at.isoformat()) self.assertEqual(to_dict_json["updated_at"], to_dict.updated_at.isoformat())
def test_save(self): """test save method for classname""" self.resetStorage() my_model = BaseModel() my_model.name = "Holberton" my_model.age = 89 my_model.save() self.assertTrue(os.access('file.json', os.R_OK)) allobjects = storage.all() for obj_id in allobjects.keys(): if my_model.id in obj_id: obj = allobjects[obj_id] self.assertEqual(my_model.id, obj.id) self.assertEqual(my_model.name, obj.name) self.assertEqual(my_model.age, obj.age) self.assertEqual(my_model.created_at, obj.created_at) self.assertEqual(my_model.updated_at, obj.updated_at)
def test_instantiation(self): """Test proper instantiation of object 'BaseModel()'""" ba = BaseModel() self.assertIsInstance(ba, BaseModel) self.assertIsInstance(ba.id, str) self.assertIsInstance(ba.created_at, datetime.datetime) self.assertIsInstance(ba.updated_at, datetime.datetime) self.assertIsInstance(ba.__class__, type) ba.size = "tall" l1 = dir(ba) self.assertIn('size', l1) self.assertEqual(ba.__dict__['size'], 'tall') ba.size = 'tall' l2 = dir(ba) self.assertIn('size', l2) self.assertEqual(ba.__dict__['size'], 'tall') ba.age = 28 l3 = dir(ba) self.assertIn('age', l3) self.assertEqual(ba.__dict__['age'], 28) ba.age = 28.5 l4 = dir(ba) self.assertIn('age', l4) self.assertEqual(ba.__dict__['age'], 28.5) ba.age = None l5 = dir(ba) self.assertIn('age', l5) self.assertEqual(ba.__dict__['age'], None) ba_kw1 = BaseModel(**{}) self.assertIsInstance(ba_kw1, BaseModel) self.assertIsInstance(ba_kw1.id, str) self.assertIsInstance(ba_kw1.created_at, datetime.datetime) self.assertIsInstance(ba_kw1.updated_at, datetime.datetime) self.assertIsInstance(ba_kw1.__class__, type) ba_kw2 = BaseModel(**{"first_name": "John"}) l6 = dir(ba_kw2) self.assertIn('first_name', l6) self.assertEqual(ba_kw2.__dict__['first_name'], 'John') # self.assertIsInstance(ba_kw2.created_at, datetime.datetime) # self.assertIsInstance(ba_kw2.updated_at, datetime.datetime) ba_kw3 = BaseModel(**{"first_name": 'John'}) l7 = dir(ba_kw3) self.assertIn('first_name', l7) self.assertEqual(ba_kw3.__dict__['first_name'], 'John') # self.assertIsInstance(ba_kw3.created_at, datetime.datetime) # self.assertIsInstance(ba_kw3.updated_at, datetime.datetime) ba_kw4 = BaseModel(**{"age": 28}) l8 = dir(ba_kw4) self.assertIn('age', l8) self.assertEqual(ba_kw4.__dict__['age'], 28) # self.assertIsInstance(ba_kw4.created_at, datetime.datetime) # self.assertIsInstance(ba_kw4.updated_at, datetime.datetime) ba_kw5 = BaseModel(**{"age": 28.5}) l9 = dir(ba_kw5) self.assertIn('age', l9) self.assertEqual(ba_kw5.__dict__['age'], 28.5) # self.assertIsInstance(ba_kw5.created_at, datetime.datetime) # self.assertIsInstance(ba_kw5.updated_at, datetime.datetime) ba_kw6 = BaseModel(**{"age": None}) l10 = dir(ba_kw6) self.assertIn('age', l10) self.assertEqual(ba_kw6.__dict__['age'], None) # self.assertIsInstance(ba_kw6.created_at, datetime.datetime) # self.assertIsInstance(ba_kw6.updated_at, datetime.datetime) ba_kw7 = BaseModel(**{"age": float('inf')}) l11 = dir(ba_kw7) self.assertIn('age', l11) self.assertEqual(ba_kw7.__dict__['age'], float('inf')) # self.assertIsInstance(ba_kw7.created_at, datetime.datetime) # self.assertIsInstance(ba_kw7.updated_at, datetime.datetime) # ba_kw8 = BaseModel(**{"age": float('nan')}) # l12 = dir(ba_kw8) # self.assertIn('age', l12) # self.assertEqual(ba_kw8.__dict__['age'], float('nan')) # self.assertIsInstance(ba_kw8.created_at, datetime.datetime) # self.assertIsInstance(ba_kw8.updated_at, datetime.datetime) ba_kw9 = BaseModel(**{"first_name": "John", "last_name": "Smith"}) l13 = dir(ba_kw9) self.assertIn('first_name', l13) self.assertIn('last_name', l13) self.assertEqual(ba_kw9.__dict__['first_name'], 'John') self.assertEqual(ba_kw9.__dict__['last_name'], 'Smith') # self.assertIsInstance(ba_kw9.created_at, datetime.datetime) # self.assertIsInstance(ba_kw9.updated_at, datetime.datetime) ba_kw11 = BaseModel({}) self.assertIsInstance(ba_kw11, BaseModel) self.assertIsInstance(ba_kw11.id, str) self.assertIsInstance(ba_kw11.created_at, datetime.datetime) self.assertIsInstance(ba_kw11.updated_at, datetime.datetime) self.assertIsInstance(ba_kw11.__class__, type) ba_kw12 = BaseModel([]) self.assertIsInstance(ba_kw12, BaseModel) self.assertIsInstance(ba_kw12.id, str) self.assertIsInstance(ba_kw12.created_at, datetime.datetime) self.assertIsInstance(ba_kw12.updated_at, datetime.datetime) self.assertIsInstance(ba_kw12.__class__, type) ba_kw13 = BaseModel(()) self.assertIsInstance(ba_kw13, BaseModel) self.assertIsInstance(ba_kw13.id, str) self.assertIsInstance(ba_kw13.created_at, datetime.datetime) self.assertIsInstance(ba_kw13.updated_at, datetime.datetime) self.assertIsInstance(ba_kw13.__class__, type) ba_kw14 = BaseModel(1) self.assertIsInstance(ba_kw14, BaseModel) self.assertIsInstance(ba_kw14.id, str) self.assertIsInstance(ba_kw14.created_at, datetime.datetime) self.assertIsInstance(ba_kw14.updated_at, datetime.datetime) self.assertIsInstance(ba_kw14.__class__, type) ba_kw15 = BaseModel("a") self.assertIsInstance(ba_kw15, BaseModel) self.assertIsInstance(ba_kw15.id, str) self.assertIsInstance(ba_kw15.created_at, datetime.datetime) self.assertIsInstance(ba_kw15.updated_at, datetime.datetime) self.assertIsInstance(ba_kw15.__class__, type) ba_kw16 = BaseModel(None) self.assertIsInstance(ba_kw16, BaseModel) self.assertIsInstance(ba_kw16.id, str) self.assertIsInstance(ba_kw16.created_at, datetime.datetime) self.assertIsInstance(ba_kw16.updated_at, datetime.datetime) self.assertIsInstance(ba_kw16.__class__, type) ba_kw17 = BaseModel({"first_name": "John"}) self.assertIsInstance(ba_kw17, BaseModel) self.assertIsInstance(ba_kw17.id, str) self.assertIsInstance(ba_kw17.created_at, datetime.datetime) self.assertIsInstance(ba_kw17.updated_at, datetime.datetime) self.assertIsInstance(ba_kw17.__class__, type) ba_kw18 = BaseModel(**{"my_list": [1, 2]}) l14 = dir(ba_kw18) self.assertIn('my_list', l14) self.assertEqual(ba_kw18.__dict__['my_list'], [1, 2]) # self.assertIsInstance(ba_kw18.created_at, datetime.datetime) # self.assertIsInstance(ba_kw18.updated_at, datetime.datetime) ba_kw19 = BaseModel(**{"my_tup": (1, 2)}) l15 = dir(ba_kw19) self.assertIn('my_tup', l15) self.assertEqual(ba_kw19.__dict__['my_tup'], (1, 2)) # self.assertIsInstance(ba_kw19.created_at, datetime.datetime) # self.assertIsInstance(ba_kw19.updated_at, datetime.datetime) ba_kw20 = BaseModel(**{"my_set": {1, 2}}) l16 = dir(ba_kw20) self.assertIn('my_set', l16) self.assertEqual(ba_kw20.__dict__['my_set'], {1, 2}) # self.assertIsInstance(ba_kw20.created_at, datetime.datetime) # self.assertIsInstance(ba_kw20.updated_at, datetime.datetime) ba_kw21 = BaseModel(**{"my_dict": {"a": 1}}) l17 = dir(ba_kw21) self.assertIn('my_dict', l17) self.assertEqual(ba_kw21.__dict__['my_dict'], {"a": 1})
def test_bm(self): """ Test BaseModel class id values. """ bs1 = BaseModel() bs2 = BaseModel() self.assertTrue(isinstance(bs1, BaseModel)) self.assertNotEqual(bs1.id, bs2.id) bs1.name = "Holberton" self.assertEqual(bs1.name, "Holberton") self.assertTrue(isinstance(bs1.name, str)) bs1.age = 5 self.assertEqual(bs1.age, 5) self.assertTrue(isinstance(bs1.age, int)) self.assertTrue(isinstance(bs1.created_at, datetime.datetime)) self.assertTrue(isinstance(bs1.updated_at, datetime.datetime)) self.assertTrue(isinstance(bs1.id, str)) self.assertTrue(isinstance(bs1.to_dict(), dict)) self.assertTrue(hasattr(bs1, 'id')) self.assertTrue(hasattr(bs1, 'created_at')) self.assertTrue(hasattr(bs1, 'updated_at')) self.assertTrue(hasattr(bs1, 'age')) self.assertTrue(hasattr(bs1, 'name')) self.assertTrue(hasattr(eval(bs1.__class__.__name__), '__str__')) dic = bs1.to_dict() self.assertTrue(hasattr(dic, '__class__')) self.assertTrue(hasattr(bs1, 'to_dict')) self.assertTrue(hasattr(bs1, '__str__')) self.assertTrue(callable(type(bs1.to_dict()))) self.assertTrue(callable(type(bs1.__str__()))) s0 = str(bs1) self.assertMultiLineEqual(bs1.__str__(), str(bs1)) my_dict = {} for k, v in bs1.__dict__.items(): if k == 'created_at': my_dict[k] = v.isoformat() elif k == 'updated_at': my_dict[k] = v.isoformat() else: my_dict[k] = v my_dict['__class__'] = bs1.__class__.__name__ self.assertDictEqual(bs1.to_dict(), my_dict) d1 = bs1.updated_at bs1.save() objs = models.storage.all() key = 'BaseModel' + '.' + bs1.id if key in objs: r = objs[key].to_dict() for k, v in r.items(): self.assertEqual(r[k], bs1.to_dict()[k]) d2 = bs1.updated_at self.assertNotEqual(d1, d2) my_model = BaseModel() my_model.name = "Holberton" my_model.my_number = 89 my_model_json = my_model.to_dict() for key in my_model_json.keys(): self.assertTrue(hasattr(my_model, key))
def test_to_dict_age(self): """ Test if age is implemented into BaseModel """ b = BaseModel() b.age = 108 d = b.to_dict() self.assertEqual(d["age"], b.age)