def test_instanceattributes(self): """Tests storage for attributes""" Storage = FileStorage() Storage.reset() self.assertTrue(hasattr(Storage, "_FileStorage__file_path")) self.assertTrue(hasattr(Storage, "_FileStorage__objects"))
def test_02_working_save(self): """Test to validate save works.""" fs = FileStorage() fs.reset() fs.new(BaseModel()) fs.save() self.assertTrue(os.path.isfile("file.json"))
def test_01a_all_return_type(self): """Test to validate all() returns an empty dict""" fs = FileStorage() fs.reset() self.assertFalse(fs.all()) fs.new(BaseModel()) self.assertTrue(fs.all())
def test_save(self): """Tests the save method of File Storage class""" Storage = FileStorage() Storage.reset() b1 = BaseModel() Storage.new(b1) self.assertFalse(path.exists("file.json")) Storage.save() self.assertTrue(path.exists("file.json"))
def test_savebyreadingfile(self): """Tests the save method by reading file""" Storage = FileStorage() Storage.reset() b1 = BaseModel() Storage.new(b1) Storage.save() with open("file.json", "r", encoding='utf-8') as r: content = r.read() self.assertTrue("BaseModel.{}".format(b1.id) in content)
def test_reset(self): """ Tests if the reset method empties the database """ storage = FileStorage() storage.reload() jur1 = Jurisdiction(**{"name": "Cauca", "victims": 3}) storage.save() storage.reset() data = storage.all() self.assertTrue(data == {})
def test_03a_working_reload(self): """Checks reload functionality if file_path doesn't exist""" fs = FileStorage() b = BaseModel() key = "BaseModel" + '.' + b.id fs.new(b) fs.save() fs.reset() fs.reload() self.assertTrue(fs.all()[key])
def test_reload_2(self): '''Tests reload normal''' Storage = FileStorage() b1 = BaseModel() Storage.new(b1) Storage.save() Storage.reset() Storage.reload() self.assertTrue(Storage.all()["BaseModel.{}".format(b1.id)]) self.assertTrue(Storage._FileStorage__objects["BaseModel.{}".format( b1.id)])
def test_reloadbyclearingdictionary(self): """Tests the reload method of File Storage class""" Storage = FileStorage() b1 = BaseModel() Storage.new(b1) olddict = Storage.all() Storage.save() Storage.reset() Storage.reload() newdict = Storage.all() for key, value in olddict.items(): self.assertTrue(key in newdict)
def test_json_reload_times(self): '''Tests updated and created are same for json''' f1 = FileStorage() b1 = BaseModel() f1.reset() f1.new(b1) bcreate = datetime.isoformat(b1.created_at) bupdate = datetime.isoformat(b1.updated_at) f1.save() with open("file.json", "r", encoding='utf-8') as f: content = f.read() self.assertTrue(bcreate in content) self.assertTrue(bupdate in content)
def test_attributes(self): '''Tests storage for attributes''' Storage = FileStorage() Storage.reset() self.assertTrue(hasattr(FileStorage, "_FileStorage__file_path")) self.assertTrue(hasattr(FileStorage, "_FileStorage__objects"))