def test_get(self): """ tests the get method""" storage = FileStorage() state_ins = State() state_ins.id = 12345 state_ins.name = 'CA' storage.new(state_ins) storage.save() obj = storage.all() for k, v in obj.items(): if v.id == state_ins.id: st = v assertEqual(st.name, storage.get("State", 12345)) def test_count(self): """ tests the count method""" storage = FileStorage() state_ins = State() state_ins.id = 12345 state_ins.name = 'CA' storage.new(state_ins) storage.save() assertEqual(type(storage.count()), int) assertIsNotNone(storage.count("State")) assertGreater(storage.count("State"), 0)
def test_state(self): """ testing state class """ s = State() self.assertEqual(type(s), State) self.assertEqual(s.name, "") s.name = "CA" self.assertEqual(s.name, "CA")
def test_name(self): """ test the name class attribute """ bill = State() bill.name = "abdlfah" self.assertEqual(type(State.name), str)
def test_state(self): """testing State class""" s = State() self.assertEqual(type(s), State) self.assertEqual(s.name, "") s.name = "Arauca" self.assertEqual(s.name, "Arauca")
def test_class_attributes(self): """ Test User class attributes """ check = 0 my_state = State() """ Test keys in User dictionary """ self.assertTrue( sorted(list(my_state.__dict__.keys())) == [ 'created_at', 'id', 'updated_at' ], True) """ Test for class attributes in User """ self.assertEqual(my_state.name, '') my_state.name = "California" my_state.save() self.assertEqual(my_state.name, "California") """ Test file.json store the object just created """ if os.path.isfile(TestState.name): with open(TestState.name, 'r') as f: string = f.read() dic = json.loads(string) for key, value in dic.items(): if key.split('.')[1] == my_state.id: check = 1 break self.assertEqual(check, 1)
def test_db_insert(self): """ tests if database working """ database = MySQLdb.connect(host="localhost", port=3306, user="******", passwd="hbnb_dev_pwd", db="hbnb_dev_db") cursor = database.cursor() cursor.execute("SELECT * FROM states") rows = cursor.fetchall() before = len(rows) print(before) state = State() state.name = "brent" state.save() cursor.execute("SELECT * FROM states") rows = cursor.fetchall() after = len(rows) print(rows) self.assertTrue(before + 1 == after)
def test_for_get(self): """ Test for get method """ state = State() state.name = "name here" state.save() result = storage.get(State, state.id) self.assertIsNotNone(result)
def test_State(self): """Task 9 Tests `State` class. """ # Normal use: no args s1 = State() self.assertIsInstance(s1, State) # attr `name` defaults to empty string self.assertIsInstance(s1.name, str) self.assertEqual(s1.name, '') # State can be serialized to JSON by FileStorage s1.name = 'test' self.assertIn(s1, storage._FileStorage__objects.values()) s1.save() with open(storage._FileStorage__file_path, encoding='utf-8') as file: content = file.read() key = s1.__class__.__name__ + '.' + s1.id self.assertIn(key, json.loads(content)) # State can be deserialized from JSON by FileStorage self.assertIn(key, storage._FileStorage__objects.keys()) storage._FileStorage__objects = dict() storage.reload() self.assertIn(key, storage._FileStorage__objects.keys())
def test_basic(self): """Test for BaseModel """ s = State() s.name = "Holberton" s.number = 89 self.assertEqual([s.name, s.number], ["Holberton", 89])
def test_state(self): """ Test attributes of Class State """ my_state = State() my_state.name = "Atlantico" self.assertEqual(my_state.name, 'Atlantico')
def test_3_State(self): """ check if the State methods exists """ State_1 = State() State_1.name = "California" State_1.save() self.assertTrue(os.path.exists('file.json')) self.assertTrue(State_1.name, "California")
def test_correct_assignation(self): """Class State has attribute name, should be string.""" new_state = State() new_state.name = "Not Minnessota" self.assertIsInstance(new_state, State) self.assertIsInstance(new_state, BaseModel) self.assertIsInstance(new_state.name, str)
def test_get_state(self): """Test get method that retrieve one class object""" state = State() state.name = "ca" state.save() obj = storage.get("State", state.id) self.assertEqual(state, obj)
def test_state(self): """ Test attributes of Class State """ my_state = State() my_state.name = "Florida" self.assertEqual(my_state.name, 'Florida')
def test_state(self): """ Test attributes of Class State """ my_state = State() my_state.name = "Antioquia" self.assertEqual(my_state.name, 'Antioquia')
def test_all(self): """ __objects is properly returned """ # tests that storage.all() returns dictionary of objects new = BaseModel() new.save() temp = storage.all() self.assertIsInstance(temp, dict) dict_key = "{}.{}".format("BaseModel", new.id) self.assertIn(dict_key, temp) # tests storage.all() with cls argument, with no cls instances all_states = storage.all(State) self.assertEqual(len(all_states.keys()), 0) # creates cls instance and retests storage.all() with cls new_state = State() new_state.name = "California" new_state.save() all_states = storage.all(State) self.assertEqual(len(all_states.keys()), 1) for k, v in all_states.items(): self.assertEqual("California", v.name) # tests delete method storage.delete(new_state) all_states = storage.all(State) self.assertEqual(len(all_states.keys()), 0)
def test_state_error(self): """Test operation of saving a State object with an attribute that violates column constraints to db""" with self.assertRaises(sqlalchemy.exc.DataError): s = State() s.name = 'T' * 129 s.save()
def test_get(self): """Test that get properly gets object to deb storage""" x = models.storage newstate = State() newstate.name = "Californa" x.new(newstate) x.save() self.assertEqual(newstate, models.storage.get("State", newstate.id))
def test_get_method(self): """ Test get method in db_storage """ new_state = State() new_state.name = "Boyaca" storage.new(new_state) storage.save() obj_state = storage.get(State, new_state.id) self.assertEqual(obj_state, new_state, "Get method is working.")
def test_state_name(self): """test_state_name test Test instance class """ my_state = State() my_state.name = "Cartagena" self.assertEqual(my_state.name, "Cartagena")
def test_key_format(self): """ Key is properly formatted """ new = State() new.name = "Maine" new.save() for key in storage.all().keys(): if new.id in key: self.assertEqual(key, 'State' + '.' + new.id)
def test_basic_test(self): """Basic tests for State class""" self.assertTrue(issubclass(State, BaseModel)) my_model = State() my_model.name = "Holberton" my_model.my_number = 89 self.assertEqual([my_model.name, my_model.my_number], ["Holberton", 89])
def test_State_attr_types(self): """Test State instance attributes""" U = State() U.name = "State" self.assertEqual(type(U.id), str) self.assertTrue(type(U.created_at) is datetime.datetime) self.assertTrue(type(U.updated_at) is datetime.datetime) self.assertEqual(type(U.name), str)
def test_hasattribute(self): """test that instance of Base have been correctly made""" s2 = State() s2.name = "MA" self.assertTrue(hasattr(s2, "created_at")) self.assertTrue(hasattr(s2, "updated_at")) self.assertTrue(hasattr(s2, "id")) self.assertTrue(hasattr(s2, "name"))
def test_creation_with_dict(self): """ Check creation of instance with dictionary. """ new_state = State() new_state.name = "Minnesotta" another_state = State(new_state.to_dict()) self.assertEqual(new_state.name, another_state.name)
def test_State_basic_instance(self): """Test State instance""" A = State() A.name = "State" self.assertIsInstance(A, BaseModel) self.assertEqual(State, type(A)) self.assertTrue(hasattr(A, "created_at")) self.assertTrue(hasattr(A, "updated_at")) self.assertTrue(hasattr(A, "name"))
def test_count(self): """Test that count properly counts objects to dbstorage""" x = models.storage newstate = State() newstate.name = "Californa" x.new(newstate) x.save() self.assertEqual(len(models.storage.all("State")), models.storage.count("State"))
def test_new(self): """ Test new Method """ basM = State() basM.name = "NINGUNALANDIA" storage.new(basM) storage.save() dictTest = storage.all() strForm = "{}.{}".format(type(basM).__name__, basM.id) self.assertTrue(strForm in dictTest.keys())
def test_new(self): '''Tests new method''' s = FileStorage() dic = s.all() maine = State() maine.name = "Maine" s.new(maine) key = maine.__class__.__name__ + "." + str(maine.id) self.assertIsNotNone(dic[key])
def test_save_City(self): """test if the save works""" city = City() city.name = 'LA' state = State() state.name = 'CA' city.state_id = state.id city.save() self.assertNotEqual(city.created_at, city.updated_at)