def test_delete(self): """ Test the delete function in file storage by creating a new State, checking the contents of file.json to confirm it's there, then running delete and checking the file again """ new_state = State() fs = FileStorage() fs.new(new_state) fs.save() self.assertTrue(os.path.isfile("file.json")) with open("file.json", 'r') as f: file_read = f.read() if new_state.id in file_read: flag = 1 fs.delete(new_state) fs.save() with open("file.json", 'r') as f: file_read = f.read() if new_state.id not in file_read: flag = 0 self.assertTrue(flag == 0)
def test_delete_three(self): """ Tests the delete method by passing in garbage """ storage = FileStorage() all_users = storage.all(User) user_count_init = len(all_users.keys()) storage.delete("dogs") all_users = storage.all(User) user_count_after = len(all_users.keys()) self.assertEqual(user_count_init, user_count_after)
def test_delete_method(self): ''' tests delete method ''' fs = FileStorage() new_state = State() fs.new(new_state) fs.save() self.assertIn(new_state, fs.all(State).values()) fs.delete(new_state) self.assertNotIn(new_state, fs.all(State).values())
def remove_all(): """Function to remove all items from storage""" storage = FileStorage() objects = storage.all() objects = list(objects.values()) for element in objects: storage.delete(element) objects = storage.all()
def test_delete_one(self): """ Tests method. delete obj from current DB session """ storage = FileStorage() all_users = storage.all(User) user_count_init = len(all_users.keys()) user = User() storage.delete(user) all_users = storage.all(User) user_count_after = len(all_users.keys()) self.assertEqual(user_count_init, user_count_after)
def test_delete(self): """ Tests delete method to delete objects in __object """ storage = FileStorage() obj_dict = storage.all() usr = User() usr.id = 12345 storage.new(usr) storage.delete(usr) key = usr.__class__.__name__ + "." + str(usr.id) self.assertFalse(key in obj_dict.keys())
def test_delete(self): """tests if all works in File Storage""" storage = FileStorage() state_instance = State(name="California") self.assertIsNotNone(state_instance) storage.new(state_instance) storage.save() storage.delete(state_instance) obj_state = storage.all(State) self.assertEqual(len(obj_state), 0)
def test_delete_one(self): """ Tests the delete method """ storage = FileStorage() all_users = storage.all(User) user_count_init = len(all_users.keys()) user = User() storage.delete(user) all_users = storage.all(User) user_count_after = len(all_users.keys()) self.assertEqual(user_count_init, user_count_after)
def test_delete(self): """Tests the method delete (obj, called from destroy method)""" storage = FileStorage() state = State() state.id = 123455 state.name = "Medellin" key = state.__class__.__name__ + "." + str(state.id) storage.new(state) storage.delete(state) obj = storage.all() self.assertTrue(key not in obj.keys())
def test_delete(self): """Tests the delete method""" storage = FileStorage() obj = storage.all() user = User() user.id = 123455 user.name = "Kevin" storage.new(user) key = user.__class__.__name__ + "." + str(user.id) self.assertIsNotNone(obj[key]) storage.delete(user) self.assertNotIn(user, storage.all())
def test_delete(self): """ Tests if delete works """ fs = FileStorage() new_obj = User() fs.new(new_obj) fs.save() self.assertIsNotNone(new_obj) fs.delete(new_obj) all_users = fs.all(User) self.assertEqual(len(all_users.keys()), 0)
def test_delete_obj(self): """Test for delete method""" storage = FileStorage() state = State() state.name = "California" state.id = 123123 storage.new(state) obj = storage.all() key = state.__class__.__name__ + "." + str(state.id) self.assertIn(key, obj) storage.delete(state) self.assertNotIn(key, obj)
def test_delete(self): """Tests the method to delete obj from __objects if its inside """ storage = FileStorage() state = State() state.id = 123455 state.name = "California" key = state.__class__.__name__ + "." + str(state.id) storage.new(state) storage.delete(state) obj = storage.all() self.assertTrue(key not in obj.keys())
def test_for_delete(self): """test for delete""" fs = FileStorage() new_state = State() new_state.name = "California" fs.new(new_state) fs.save() all_states = fs.all(State) # placeholder. change later. self.assertEqual(len(all_states.keys()), 2) fs.delete(new_state) all_states = fs.all(State) self.assertEqual(len(all_states.keys()), 1)
def test_delete(self): """Tests the delete v2 method""" storage = FileStorage() new_state = State() new_state.name = "California" storage.new(new_state) storage.save() states = storage.all(State) self.assertIsNotNone(states) self.assertEqual(type(states), dict) storage.delete(new_state) states = storage.all(State) self.assertEqual(states, {})
def test_delete(self): ''' Test delete method ''' fs = FileStorage() new_state = State() fs.new(new_state) state_id = new_state.id fs.save() fs.delete(new_state) with open("file.json", encoding="UTF-8") as fd: state_dict = json.load(fd) for k, v in state_dict.items(): self.assertFalse(state_id == k.split('.')[1])
def test_delete(self): ''' Tests delete ''' fs = FileStorage() entry = State() fs.new(entry) state_id = entry.id fs.save() fs.delete(entry) with open("file.json", encoding="UTF-8") as f: entry_dict = json.load(f) for key, value in entry_dict.items(): self.assertFalse(state_id == key.split('.')[1])
def test_all_v2(self): """Tests for the updated all method""" try: os.remove("file.json") except: pass storage = FileStorage() city = City() city.name = "San Francisco" city.id = "98765" city.save() self.assertTrue(storage.all(City)) storage.delete(city) self.assertFalse(storage.all(city))
def test_z_delete(self): """Tests delete functionality """ try: os.remove("file.json") except Exception: pass storage = FileStorage() state = State() state.name = "Maine" storage.new(state) self.assertTrue(storage.all(State)) storage.delete(state) self.assertFalse(storage.all(State))
def test_deletion(self): ''' Tests for an object being deleted with the delete method ''' fs = FileStorage() new_state = State() new_state.name = "Polynesia" fs.new(new_state) my_id = new_state.id fs.save() fs.delete(new_state) with open("file.json", encoding="UTF-8") as fd: json_dict = json.load(fd) for key, value in json_dict.items(): self.assertTrue(value['id'] != my_id)
def test_delete(self): """Tests for if an object is deleted in FileStorage""" try: os.remove("file.json") except: pass storage = FileStorage() state = State() state.name = "California" state.id = "54321" state.save() self.assertTrue(storage.all(State)) storage.delete(state) self.assertFalse(storage.all(State)) self.assertNotIsInstance(state.id, dict)
def test_new_delete(self): """test for new method that deletes obj from __objects""" ffs = FileStorage() all_states = ffs.all(State) num_states = len(all_states) new_state = State() new_state.name = "Florida" ffs.new(new_state) ffs.save() all_states = ffs.all(State) new_num_states = len(all_states) self.assertNotEqual(new_num_states, num_states) ffs.delete(new_state) all_states = ffs.all(State) delete = len(all_states) self.assertEqual(delete, (new_num_states - 1))
def test_delete_obj(self): """deletes obj from storage """ from models.engine.file_storage import FileStorage from models.state import State from models.user import User fs = FileStorage() before_new = len(fs.all(State)) # create new obj State new_state = State() new_state.name = "California" fs.new(new_state) fs.save() # delete obj fs.delete(new_state) after_delete = len(fs.all(State)) self.assertEqual(after_delete, before_new)
def test_delete(self): new_state = State() new_state.name = "California***********" fs = FileStorage() fs.new(new_state) fs.save() self.assertTrue(os.path.isfile("file.json")) with open("file.json", encoding="UTF8") as fd: content = fd.read() flag = 1 if new_state.id in content: flag = 0 self.assertTrue(flag == 0) fs.delete(new_state) fs.save() with open("file.json", encoding="UTF8") as fd: content = fd.read() flag = 0 if new_state.id in content: flag = 1 self.assertTrue(flag == 0)
def test_file_storage_new_save_delete(self): """ Tests the methods new(), save() and delete() for serialization of objects """ obj = BaseModel() storage = FileStorage() storage.new(obj) objects = storage.all() self.assertTrue(obj in objects.values()) storage.save() with open("test_storage_file.json", encoding="UTF-8") as f: read_json = f.read() self.assertTrue('updated_at' in read_json) storage.delete(obj) self.assertTrue(obj not in objects.values()) with open("test_storage_file.json", encoding="UTF-8") as f: read_json = f.read() self.assertTrue("{}" == read_json)
class testFileStorage(unittest.TestCase): ''' Testing the FileStorage class ''' def setUp(self): ''' Initializing classes ''' self.storage = FileStorage() self.my_model = BaseModel() def tearDown(self): ''' Cleaning up. ''' try: os.remove("file.json") except FileNotFoundError: pass def test_all_return_type(self): ''' Tests the data type of the return value of the all method. ''' storage_all = self.storage.all() self.assertIsInstance(storage_all, dict) def test_new_method(self): ''' Tests that the new method sets the right key and value pair in the FileStorage.__object attribute ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__ + "." + self.my_model.id) self.assertTrue(key in self.storage._FileStorage__objects) def test_objects_value_type(self): ''' Tests that the type of value contained in the FileStorage.__object is of type obj.__class__.__name__ ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__ + "." + self.my_model.id) val = self.storage._FileStorage__objects[key] self.assertIsInstance(self.my_model, type(val)) def test_save_file_exists(self): ''' Tests that a file gets created with the name file.json ''' self.storage.save() self.assertTrue(os.path.isfile("file.json")) def test_save_file_read(self): ''' Testing the contents of the files inside the file.json ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = json.load(fd) self.assertTrue(type(content) is dict) def test_the_type_file_content(self): ''' testing the type of the contents inside the file. ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = fd.read() self.assertIsInstance(content, str) def test_reaload_without_file(self): ''' Tests that nothing happens when file.json does not exists and reload is called ''' try: self.storage.reload() self.assertTrue(True) except: self.assertTrue(False) def test_delete_method(self): ''' Tests that the delete method removes an object from the FileStorage.__object ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__ + "." + self.my_model.id) self.assertTrue(key in self.storage._FileStorage__objects) self.storage.delete(self.my_model) self.assertFalse(key in self.storage._FileStorage__objects)
class testFileStorage(unittest.TestCase): ''' Testing the FileStorage class ''' def setUp(self): ''' Initializing classes ''' self.storage = FileStorage() self.my_model = BaseModel() def tearDown(self): ''' Cleaning up. ''' self.file_storage_reloader() @staticmethod def file_storage_reloader(): ''' Deletes the file that represent the storage ''' try: os.remove("file.json") except FileNotFoundError: pass storage.reload() def test_all_return_type(self): ''' Tests the data type of the return value of the all method. ''' storage_all = self.storage.all() self.assertIsInstance(storage_all, dict) def test_new_method(self): ''' Tests that the new method sets the right key and value pair in the FileStorage.__object attribute ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__ + "." + self.my_model.id) self.assertTrue(key in self.storage._FileStorage__objects) def test_objects_value_type(self): ''' Tests that the type of value contained in the FileStorage.__object is of type obj.__class__.__name__ ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__ + "." + self.my_model.id) val = self.storage._FileStorage__objects[key] self.assertIsInstance(self.my_model, type(val)) def test_save_file_exists(self): ''' Tests that a file gets created with the name file.json ''' self.storage.save() self.assertTrue(os.path.isfile("file.json")) def test_save_file_read(self): ''' Testing the contents of the files inside the file.json ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = json.load(fd) self.assertTrue(type(content) is dict) def test_the_type_file_content(self): ''' testing the type of the contents inside the file. ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = fd.read() self.assertIsInstance(content, str) def test_reload_without_file(self): ''' Tests that nothing happens when file.json does not exists and reload is called ''' try: self.storage.reload() self.assertTrue(True) except: self.assertTrue(False) def test_delete(self): ''' Test delete method ''' fs = FileStorage() new_state = State() fs.new(new_state) state_id = new_state.id fs.save() fs.delete(new_state) with open("file.json", encoding="UTF-8") as fd: state_dict = json.load(fd) for k, v in state_dict.items(): self.assertFalse(state_id == k.split('.')[1]) def test_model_storage(self): ''' Test State model in Filestorage ''' self.assertTrue(isinstance(storage, FileStorage)) def test_get(self): ''' Testing get method ''' # new state for the test case state = State(name="California") self.storage.new(state) state_id = state.id # state is instance of State self.assertIsInstance(state, State) # state can be retrieved using storage.get() state = storage.get("State", state_id) self.assertEqual(state.id, state_id) # fake_state is None # when the given id doesn't exist fake_state = storage.get("State", "fake_id") self.assertIsNone(fake_state) # clean up self.storage.delete(state) def test_count(self): ''' Testing cout method ''' count = storage.count() # count is int self.assertIsInstance(count, int) # count == 6 self.assertEqual(count, 6) # count = 7 # after the new record has been created state = State({"name": "California"}) self.storage.new(state) count = self.storage.count() self.assertEqual(count, 7) # while counting only states count = self.storage.count("State") # count is int self.assertIsInstance(count, int) # count == 1 self.assertEqual(count, 1)
fs = FileStorage() Town = "Town" # All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key]) # Create a new State new_state = State() new_state.name = "California" fs.new(new_state) fs.save() print("New State: {}".format(new_state)) # All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key]) # Delete the new State fs.delete(new_state) # All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key])
class testFileStorage(unittest.TestCase): ''' Testing the FileStorage class ''' def setUp(self): ''' Initializing classes ''' os.environ['HBNB_TYPE_STORAGE'] = 'file' self.storage = FileStorage() self.my_model = BaseModel() def tearDown(self): ''' Cleaning up. ''' try: os.remove("file.json") except FileNotFoundError: pass def test_FileStorage_all_return_type(self): ''' Tests the data type of the return value of the all method. ''' storage_all = self.storage.all() self.assertIsInstance(storage_all, dict) def test_FileStorage_all_class_specific(self): ''' Test all method with a class specified ''' new_city = models.City() new_state = models.State() state_key = str(new_state.__class__.__name__) + "." + str(new_state.id) city_key = str(new_city.__class__.__name__) + "." + str(new_city.id) self.storage.new(new_city) self.storage.new(new_state) tmp = self.storage.all(models.City) state = tmp.get(state_key, None) city = tmp.get(city_key, None) self.assertTrue(city is not None, msg="\n{}\n{}".format(tmp, city)) self.assertTrue(state is None) def test_FileStorage_new_method(self): ''' Tests that the new method sets the right key and value pair in the FileStorage.__object attribute ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__) + "." + self.my_model.id self.assertTrue(key in self.storage._FileStorage__objects) def test_FileStorage_objects_value_type(self): ''' Tests that the type of value contained in the FileStorage.__object is of type obj.__class__.__name__ ''' self.storage.new(self.my_model) key = str(self.my_model.__class__.__name__) + "." + self.my_model.id val = self.storage._FileStorage__objects[key] self.assertIsInstance(self.my_model, type(val)) def test_FileStorage_save_file_exists(self): ''' Tests that a file gets created with the name file.json ''' self.storage.save() self.assertTrue(os.path.isfile("file.json")) def test_FileStorage_save_file_read(self): ''' Testing the contents of the files inside the file.json ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = json.load(fd) self.assertTrue(type(content) is dict) def test_FileStorage_the_type_file_content(self): ''' testing the type of the contents inside the file. ''' self.storage.save() self.storage.new(self.my_model) with open("file.json", encoding="UTF8") as fd: content = fd.read() self.assertIsInstance(content, str) def test_FileStorage_reaload_without_file(self): ''' Tests that nothing happens when file.json does not exists and reload is called ''' try: self.storage.reload() self.assertTrue(True) except: self.assertTrue(False) def test_FileStorage_delete(self): ''' Tests delete function works ''' new_state = State() key = str(new_state.__class__.__name__ + "." + new_state.id) self.storage.new(new_state) self.assertTrue(key in self.storage._FileStorage__objects, msg="Object wasn't saved to storage") self.storage.save() self.storage.delete(new_state) self.storage.save() self.assertTrue(key not in self.storage._FileStorage__objects, msg="Object wasn't deleted from storage") def test_FileStorage_delete_not_in(self): ''' Tests delete works for key not in storage ''' new_state = State() key = str(new_state.__class__.__name__ + "." + new_state.id) self.storage.delete(new_state) self.assertTrue(key not in self.storage._FileStorage__objects) def test_FileStorage_delete_None(self): ''' Tests delete function works for None - no change to __objects ''' old_storage = self.storage._FileStorage__objects self.storage.delete(None) self.assertTrue(old_storage == self.storage._FileStorage__objects)
new_state.name = "California" fs.new(new_state) fs.save() print("New State: {}".format(new_state)) # All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key])''' # Create another State another_state = State() another_state.name = "Nevada" fs.new(another_state) fs.save() print("Another State: {}".format(another_state)) '''# All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key]) ''' # Delete the new State fs.delete(another_state) # All States all_states = fs.all(State) print("All States: {}".format(len(all_states.keys()))) for state_key in all_states.keys(): print(all_states[state_key])