def test_get_fs(self): """Testing the get method in file_storage """ fs = FileStorage() self.assertIs(fs.get("User", "test"), None) self.assertIs(fs.get("test", "randon"), None) new_state = State() new_user = User() new_user.save() self.assertIs(fs.get("User", new_user.id), new_user) fs.new(new_state) first_state_id = list(storage.all("State").values())[0].id self.assertEqual(type(storage.get("State", first_state_id)), State)
def test_get(self): ''' Test for get method in filestorage ''' fs = FileStorage() new_state = State() fs.new(new_state) new_id = new_state.id fs.save() return_state = fs.get(State, new_id) self.assertTrue(new_id == return_state.id) self.assertTrue(new_state.id == return_state.id) self.assertTrue(isinstance(return_state, State)) get_none = fs.get(State, 'no id') self.assertTrue(get_none is None) get_none2 = fs.get('no state', new_id) self.assertTrue(get_none2 is None)
def test_get(self): """Tests the get() method, retrieves an object by its name """ storage = FileStorage() storage.reload() jur1 = Jurisdiction(**{"name": "Cauca", "victims": 3}) storage.save() jur1_clone = storage.get(Jurisdiction, "Cauca") self.assertTrue(jur1_clone is jur1)
def test_get(self): ''' Tests that the get method returns an object from file storage ''' fs = FileStorage() new_state = State() new_state.name = "Florida" fs.new(new_state) my_id = new_state.id fs.save() obj = fs.get("State", my_id) self.assertEqual(obj.name, "Florida")
def test_filestorage_get(self): ''' Testing Get method ''' filestor = FileStorage() new = State() new.name = "Alabama" filestor.new(new) new_id = new.id filestor.save() state = filestor.get("State", new_id) self.assertEqual(state.name, "Alabama")
def test_persistence(self): """Test saving and loading data models to storage""" if os.path.exists('storage.json'): os.remove('storage.json') storage = FileStorage() storage.reload() with self.subTest(msg='new models added to storage'): obj = self._cls() self.assertTrue(self._name + '.' + obj.id in storage.all()) with self.subTest(msg='instances can be saved and loaded'): obj.save() old = obj.to_dict() del obj, storage storage = FileStorage() storage.reload() self.assertEqual(storage.get(self._cls, old['id']).to_dict(), old)
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. ''' pass 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_parameter_validity(self): ''' Tests whether or not the parameter passed is an instance of the class ''' console = HBNBCommand() capturedOutput = io.StringIO() sys.stdout = capturedOutput my_id = console.onecmd("create State name='California'") sys.stdout = sys.__stdout__ with open("file.json", encoding="UTF-8") as fd: json_dict = json.load(fd) for key, value in json_dict.items(): my_key = 'State.' + str(my_id) if key == my_key: self.assertTrue(value['name'] == 'California') def test_parameter_lack_of_validity(self): ''' Tests whether or not the parameter passes is an instance of the class ''' console = HBNBCommand() capturedOutput = io.StringIO() sys.stdout = capturedOutput my_id = console.onecmd("create State address=98") sys.stdout = sys.__stdout__ self.assertIsNone(my_id) 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_get_file_storage(self): ''' Tests the get method of file storage ''' state = State(name="Cali") state_id = state.id self.storage.new(state) state_obj = self.storage.get("State", state_id) self.assertEqual(state_obj, state) def test_count_db_storage_works(self): ''' Tests if the count method in file storage is working ''' all_dict = self.storage.all() all_count = len(all_dict) count = self.storage.count() self.assertEqual(all_count, count) def test_count_file_storage_no_class(self): ''' Tests the count method in file storage when no class is passed ''' first_count = self.storage.count() state = State(name="Colorado") state.save() second_count = self.storage.count() self.assertTrue(first_count + 1, second_count) def test_count_file_storage_class(self): ''' Tests the count method in file storage when passing a class ''' first_count = self.storage.count("State") state = State(name="Colorado") state.save() second_count = self.storage.count("State") self.assertTrue(first_count + 1, second_count)
class Test_FileStorage(unittest.TestCase): """ Test the file storage class """ def setUp(self): self.store = FileStorage() test_args = { 'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997), 'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900', 'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900) } self.model = BaseModel(**test_args) self.test_len = len(self.store.all()) # @classmethod # def tearDownClass(cls): # import os # if os.path.isfile("test_file.json"): # os.remove('test_file.json') def test_all(self): self.assertEqual(len(self.store.all()), self.test_len) def test_all_arg(self): """test all(State)""" new_obj = State() new_obj.save() everything = self.store.all() nb_states = 0 for e in everything.values(): if e.__class__.__name__ == "State": nb_states += 1 self.assertEqual(len(self.store.all("State")), nb_states) # should test with a bad class name def test_new(self): # note: we cannot assume order of test is order written test_len = len(self.store.all()) # self.assertEqual(len(self.store.all()), self.test_len) new_obj = State() new_obj.save() self.assertEqual(len(self.store.all()), test_len + 1) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_save(self): self.test_len = len(self.store.all()) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 1) b = User() self.assertNotEqual(len(self.store.all()), self.test_len + 2) b.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_reload(self): self.model.save() a = BaseModel() a.save() self.store.reload() for value in self.store.all().values(): self.assertIsInstance(value.created_at, datetime) def test_state(self): """test State creation with an argument""" a = State(name="Kamchatka", id="Kamchatka666") a.save() self.assertIn("Kamchatka666", self.store.all("State").keys()) def test_count(self): """test count all""" test_len = len(self.store.all()) a = Amenity(name="test_amenity") a.save() self.assertEqual(test_len + 1, self.store.count()) def test_count_arg(self): """test count with an argument""" test_len = len(self.store.all("Amenity")) a = Amenity(name="test_amenity_2") a.save() self.assertEqual(test_len + 1, self.store.count("Amenity")) def test_count_bad_arg(self): """test count with dummy class name""" self.assertEqual(-1, self.store.count("Dummy")) def test_get(self): """test get with valid cls and id""" a = Amenity(name="test_amenity3", id="test_3") a.save() result = self.store.get("Amenity", "test_3") self.assertEqual(a.name, result.name) self.assertEqual(a.created_at, result.created_at) def test_get_bad_cls(self): """test get with invalid cls""" result = self.store.get("Dummy", "test") self.assertIsNone(result) def test_get_bad_id(self): """test get with invalid id""" result = self.store.get("State", "very_bad_id") self.assertIsNone(result)
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 Exception: 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_count(self): ''' Test count method in db_storage ''' self.assertEqual(self.storage.count(), len(self.storage.all())) def test_count_state(self): ''' Test count method in db_storage using states ''' self.assertEqual(self.storage.count("State"), len(self.storage.all("State"))) def test_get(self): ''' Test get method in db_storage ''' new_state = State(name="NewYork") self.storage.new(new_state) self.storage.save() self.assertEqual(new_state, self.storage.get("State", new_state.id))
class Test_FileStorage(unittest.TestCase): """ Test the file storage class """ def setUp(self): self.store = FileStorage() test_args = { 'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997), 'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900', 'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900) } self.model = BaseModel(test_args) self.test_len = len(self.store.all()) # @classmethod # def tearDownClass(cls): # import os # if os.path.isfile("test_file.json"): # os.remove('test_file.json') def test_all(self): self.assertEqual(len(self.store.all()), self.test_len) def test_get(self): a = Amenity(name='internet') output = self.store.get('Amenity', a.id) assertTrue(a.id in output) def test_count(self): output = self.store.count('Amenity') assertEqual(output, len(self.store.all('Amenity'))) def test_new(self): # note: we cannot assume order of test is order written test_len = len(self.store.all()) # self.assertEqual(len(self.store.all()), self.test_len) new_obj = State() new_obj.save() self.assertEqual(len(self.store.all()), test_len + 1) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_save(self): self.test_len = len(self.store.all()) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 1) b = User() self.assertNotEqual(len(self.store.all()), self.test_len + 2) b.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_reload(self): self.model.save() a = BaseModel() a.save() self.store.reload() for value in self.store.all().values(): self.assertIsInstance(value.created_at, datetime) def test_state(self): """test State creation with an argument""" pass
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 Exception: 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_method(self): """Tests FileStorage's get method that retrieves one object""" state_dict = {'name': 'Nevada', 'id': 'thisismyid'} state = State(**state_dict) state.save() new_state = self.storage.get('State', 'thisismyid') self.assertEqual(state.id, new_state.id) def test_count_method_without_filter(self): """Tests FileStorage's count method which retrieves a count of objects with an optional class filter """ self.assertEqual(len(self.storage.all()), self.storage.count()) def test_count_method_with_filter(self): """Tests FileStorage's count method which retrieves a count of objects with an optional class filter """ objs_dict = self.storage.all() count = 0 for obj in objs_dict.values(): if type(obj) is User: count += 1 self.assertEqual(count, self.storage.count('User'))
class Test_FileStorage(unittest.TestCase): """ Test the file storage class """ def setUp(self): self.store = FileStorage() test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997), 'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900', 'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)} self.model = BaseModel(**test_args) self.test_len = len(self.store.all()) # @classmethod # def tearDownClass(cls): # import os # if os.path.isfile("test_file.json"): # os.remove('test_file.json') def test_all(self): self.assertEqual(len(self.store.all()), self.test_len) def test_all_arg(self): """test all(State)""" new_obj = State() new_obj.save() everything = self.store.all() nb_states = 0 for e in everything.values(): if e.__class__.__name__ == "State": nb_states += 1 self.assertEqual(len(self.store.all("State")), nb_states) # should test with a bad class name def test_new(self): # note: we cannot assume order of test is order written test_len = len(self.store.all()) # self.assertEqual(len(self.store.all()), self.test_len) new_obj = State() new_obj.save() self.assertEqual(len(self.store.all()), test_len + 1) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_save(self): self.test_len = len(self.store.all()) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 1) b = User() self.assertNotEqual(len(self.store.all()), self.test_len + 2) b.save() self.assertEqual(len(self.store.all()), self.test_len + 2) def test_reload(self): self.model.save() a = BaseModel() a.save() self.store.reload() for value in self.store.all().values(): self.assertIsInstance(value.created_at, datetime) def test_state(self): """test State creation with an argument""" a = State(name="Kamchatka", id="Kamchatka666") a.save() self.assertIn("Kamchatka666", self.store.all("State").keys()) def test_count(self): """test count all""" test_len = len(self.store.all()) a = Amenity(name="test_amenity") a.save() self.assertEqual(test_len + 1, self.store.count()) def test_count_arg(self): """test count with an argument""" test_len = len(self.store.all("Amenity")) a = Amenity(name="test_amenity_2") a.save() self.assertEqual(test_len + 1, self.store.count("Amenity")) def test_count_bad_arg(self): """test count with dummy class name""" self.assertEqual(-1, self.store.count("Dummy")) def test_get(self): """test get with valid cls and id""" a = Amenity(name="test_amenity3", id="test_3") a.save() result = self.store.get("Amenity", "test_3") self.assertEqual(a.name, result.name) self.assertEqual(a.created_at, result.created_at) def test_get_bad_cls(self): """test get with invalid cls""" result = self.store.get("Dummy", "test") self.assertIsNone(result) def test_get_bad_id(self): """test get with invalid id""" result = self.store.get("State", "very_bad_id") self.assertIsNone(result)
class Test_FileStorage(unittest.TestCase): """ Test the file storage class """ def setUp(self): self.store = FileStorage() test_args = { 'name': 'wifi', 'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997), 'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900', 'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900) } self.model = Amenity(test_args) self.model.save() self.test_len = len(self.store.all()) def tearDown(self): self.store.delete(self.model) # @classmethod # def tearDownClass(cls): # import os # if os.path.isfile("test_file.json"): # os.remove('test_file.json') def test_all(self): self.assertEqual(len(self.store.all()), self.test_len) def test_new(self): # note: we cannot assume order of test is order written test_len = len(self.store.all()) # self.assertEqual(len(self.store.all()), self.test_len) new_obj = State() new_obj.save() self.assertEqual(len(self.store.all()), test_len + 1) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 2) self.store.delete(a) self.store.delete(new_obj) def test_save(self): self.test_len = len(self.store.all()) a = BaseModel() a.save() self.assertEqual(len(self.store.all()), self.test_len + 1) b = User() self.assertNotEqual(len(self.store.all()), self.test_len + 2) b.save() self.assertEqual(len(self.store.all()), self.test_len + 2) self.store.delete(a) self.store.delete(b) def test_reload(self): a = BaseModel() a.save() self.store.reload() for value in self.store.all().values(): self.assertIsInstance(value.created_at, datetime) self.store.delete(a) def test_state(self): """test State creation with an argument""" pass def test_get(self): """Test retrieving an object""" my_list = ["name", "id", "created_at", "updated_at"] a = self.store.get("Amenity", 'f519fb40-1f5c-458b-945c-2ee8eaaf4900') for k in my_list: self.assertIsNotNone(getattr(a, k, None)) self.assertIsNone(getattr(a, "fake_key", None)) b = self.store.get(None, 'invalid-id') self.assertIsNone(b) self.store.delete(self.model) def test_count(self): """Test storage count""" self.assertEqual(len(self.store.all()), 1) a = Amenity(name="Bob") a.save() self.assertEqual(len(self.store.all()), 2) self.store.delete(a)