def test_str(self): """ Check str method. """ new_city = City() string = "[City] ({}) {}".format(new_city.id, new_city.__dict__) self.assertIsInstance(new_city.__str__(), str) self.assertEqual(new_city.__str__(), string)
def test_method_str(self): """Test method str""" new = City() self.assertEqual(new.__str__(), "[{}] ({}) {}".format (new.__class__.__name__, new.id, new.__dict__)) self.assertTrue(type(new.__str__()), str) self.assertTrue(len(new.__str__()))
def test_str(self): """Test City str representation. """ new = City() trex = r"\[City\] \(.*\) .*" self.assertIsInstance(new, City) self.assertTrue(type(new.__str__()) == str) self.assertTrue(search(trex, new.__str__())) args = ["hola", 2, 3.2, "poads"] new = City(*args) self.assertIsInstance(new, City) new = City("hola", 2, 3.2, "poads") self.assertIsInstance(new, City)
def test_City14N(self): """str""" s = "[City] ({}) {}" my_city14 = City() my_city14printed = my_city14.__str__() self.assertEqual(my_city14printed, s.format(my_city14.id, my_city14.__dict__))
def test_str(self): """Test __str__ method""" ci = City() string = '[' + ci.__class__.__name__ + ']' + ' (' + ci.id + ') ' + str( ci.__dict__) self.assertEqual(string, ci.__str__())
def test_str(self): """ review format: [<class name>] (<self.id>) <self.__dict__> """ my_class = City() string = "[{:s}] ({:s}) {}".format(my_class.__class__.__name__, my_class.id, my_class.__dict__) self.assertEqual(string, my_class.__str__())
def test_4_str_method(self): """is the string representation is wrong""" model_1 = City() model_1.save() str_model = "[" + str(model_1.__class__.__name__) + "] (" + \ str(model_1.id) + ") " + str(model_1.__dict__) self.assertEqual(model_1.__str__(), str_model)
def test_city_str_method(self): """City str method creates accurate representation""" city = City() city_str = city.__str__() self.assertIsInstance(city_str, str) self.assertEqual(city_str[:6], '[City]') self.assertEqual(city_str[7:45], '({})'.format(city.id)) self.assertDictEqual(eval(city_str[46:]), city.__dict__)
def test_3_str(self): '''If str method work as expected''' my_model_0 = City() my_model_1 = City() my_model_2 = City() str_model_0 = "[" + str(type(my_model_0).__name__) + "] (" + \ str(my_model_0.id) + ") " + str(my_model_0.__dict__) str_model_1 = "[" + str(type(my_model_1).__name__) + "] (" + \ str(my_model_1.id) + ") " + str(my_model_1.__dict__) str_model_2 = "[" + str(type(my_model_2).__name__) + "] (" + \ str(my_model_2.id) + ") " + str(my_model_2.__dict__) self.assertEqual(str(my_model_0), str_model_0) self.assertEqual(my_model_1.__str__(), str_model_1) self.assertEqual(my_model_2.__str__(), str_model_2)
def test_str(self): """ test print """ u1 = City() u2 = City(id='Betty') s = "[{}] ({}) {}".format("City", u1.id, u1.__dict__) self.assertEqual(print(s), print(u1)) self.assertIsInstance(u1.__str__(), str) s = "[{}] ({}) {}".format("City", u2.id, u2.__dict__) self.assertEqual(print(s), print(u2))
def test_str_representation(self): dt = datetime.today() dt_repr = repr(dt) cy = City() cy.id = "123456" cy.created_at = cy.updated_at = dt cystr = cy.__str__() self.assertIn("[City] (123456)", cystr) self.assertIn("'id': '123456'", cystr) self.assertIn("'created_at': " + dt_repr, cystr) self.assertIn("'updated_at': " + dt_repr, cystr)
def test_City(self): """Test instance of city class""" new = City() new2 = City() self.assertIsInstance(new, BaseModel) self.assertEqual(issubclass(new.__class__, BaseModel), True) self.assertIs(type(new), City) self.assertNotEqual(new, new2) self.assertNotEqual(new.id, new2.id) self.assertEqual(new.__str__(), "[{}] ({}) {}".format (new.__class__.__name__, new.id, new.__dict__)) self.assertEqual(type(new.id), str) self.assertEqual(City, type(City()))
class TestCity(unittest.TestCase): def setUp(self): '''method to set up instance of city/json file''' self.city = City() def tearDown(self): '''method to tear down instance of city/json file''' if os.path.exists("file.json"): try: os.remove("file.json") except: pass def test___init__(self): '''method to check if instance initializes''' self.assertIsNotNone(self.city) def test_attributes(self): '''method to test if updates take place in save''' self.assertEqual(self.city.name, "") self.assertTrue(hasattr(self.city, "created_at")) self.assertFalse(hasattr(self.city, "updated_at")) self.assertTrue(hasattr(self.city, "id")) self.city.save() self.assertTrue(hasattr(self.city, "updated_at")) def test_to_json(self): '''method to check that the to_json function returns''' example_tojson = City.to_json(self.city) self.assertEqual(type(example_tojson), dict) def test___str__(self): '''method to check that dict printing instance''' example = "[{}] ({}) {}".format(self.__class__.__name__, self.id, self.__dict__) self.assertEqual(print(self.city), print(example)) def test__repr__(self): '''method to print attributes of dictionary''' self.assertIsNotNone(self.city.__str__())
def test_str_method(self): """Tests to see if each method is printing accurately""" i = City() printed = i.__str__() self.assertEqual(printed, "[City] ({}) {}".format(i.id, i.__dict__))
def test_str_method(self): """test that each method is printing accurately""" c3 = City() c3printed = c3.__str__() self.assertEqual(c3printed, "[City] ({}) {}".format(c3.id, c3.__dict__))
def test_str(self): z = City() stringA = str(z) stringB = z.__str__() self.assertTrue(stringA, stringB)
def test_str_(self): """ testing to see if the method is printing """ b1 = City() b1_str = b1.__str__() self.assertEqual(b1_str, "[City] ({}) {}".format(b1.id, b1.__dict__))
class TestCity(unittest.TestCase): """Class test """ def setUp(self): """ setUp init for tests""" self.my_model = City() self.my_model2 = City() self.my_model.name = "Vale" self.my_model.age = 28 def test_pep8_conformance_City(self): """Test that we conform to PEP8.""" pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/city.py']) self.assertEqual(result.total_errors, 0) def test_docstrings(self): """Test docstrings""" self.assertIsNotNone(City.__doc__) self.assertIsNotNone(City.__init__.__doc__) self.assertIsNotNone(City.__str__.__doc__) self.assertIsNotNone(City.save.__doc__) self.assertIsNotNone(City.to_dict.__doc__) def test_city(self): """Test for City """ self.assertIsInstance(self.my_model, City) self.assertEqual(self.my_model.name, "Vale") self.assertEqual(self.my_model.age, 28) self.assertTrue(hasattr(self.my_model, "id")) self.assertTrue(hasattr(self.my_model, "created_at")) self.assertTrue(hasattr(self.my_model, "updated_at")) self.assertTrue(hasattr(self.my_model, "__class__")) model1 = self.my_model.created_at model2 = self.my_model2.created_at self.assertTrue(model1 != model2) model1 = self.my_model.id model2 = self.my_model2.id self.assertNotEqual(model1, model2) def test_to_dict(self): """test_to_dict - test the return of a dict containing all the key/values of __dict__""" dict_json = self.my_model.to_dict() self.assertEqual(type(dict_json), dict) self.assertTrue(type(dict_json['created_at']) is str) self.assertTrue(type(dict_json['updated_at']) is str) def test_save(self): """Test for save method """ previous_update = self.my_model.updated_at self.my_model.save() self.assertNotEqual(previous_update, self.my_model.updated_at) def test_str(self): """Test for __str__ method """ self.assertEqual( str(self.my_model), "[City] ({}) {}".format(self.my_model.id, self.my_model.__dict__)) self.assertEqual( self.my_model.__str__(), "[City] ({}) {}".format(self.my_model.id, self.my_model.__dict__)) def test_kwargs(self): """Test for recreate instance from a instance old """ dict_json = self.my_model.to_dict() my_model2 = City(**dict_json) self.assertFalse(my_model2 is self.my_model) self.assertEqual(self.my_model.id, my_model2.id) self.assertEqual(self.my_model.created_at, my_model2.created_at) self.assertEqual(self.my_model.updated_at, my_model2.updated_at)
def test_str(self): """ Tests the str repr. of an object """ base = City() base_str = base.__str__() self.assertTrue(isinstance(base_str, str))
def test_str_method(self): """Tests to see if each method is printing accurately""" b1 = City() b1printed = b1.__str__() self.assertEqual(b1printed, "[City] ({}) {}".format(b1.id, b1.__dict__))
class Test_City(unittest.TestCase): """ Class to test """ def setUp(self): """Function that execute before of each test function """ self.obj = City() def test_createCity(self): """Test that is instance """ self.assertIsInstance(self.obj, City) def test_Id_Is_String(self): """Id is string """ self.assertEqual(type(self.obj.id), str) def test_add_Atrribute(self): self.obj.name = "Holberton" self.obj.my_number = 89 self.assertTrue(self.obj.name) self.assertTrue(self.obj.my_number) def test_docString(self): """Test if function and class have docString """ self.assertIsNotNone(City.__doc__) def test_pep8_base_model(self): """ Test for PEP8 """ pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/city.py']) self.assertEqual(result.total_errors, 0, "Please fix pep8") def test_id_uuid_v4(self): """Test version 4 of UUID """ test__version = uuid.UUID(self.obj.id).version self.assertEqual(test__version, 4, "Error: Different version") def test_created_at_Is_datatime(self): """Test that created_at is instance of datetime """ self.assertIsInstance(self.obj.created_at, datetime) def test_updated_at_Is_datatime(self): """Test that updated_at is instance of datetime """ self.assertIsInstance(self.obj.updated_at, datetime) def test_str(self): """ Test function __str__ """ s = "[{}] ({}) {}".format( self.obj.__class__.__name__, self.obj.id, self.obj.__dict__) self.assertEqual(self.obj.__str__(), s) def test_save(self): """Test save objects in a json file """ self.obj.save() key = self.obj.__class__.__name__+"."+self.obj.id self.assertEqual(self.obj, models.storage.all()[key]) self.assertNotEqual(self.obj.created_at, self.obj.updated_at) self.assertTrue(os.path.exists("file.json")) def test_save_content(self): """Test to compare the saved in json file with to_dic()""" self.obj.save() dict_to_load = {} with open("file.json", 'r') as f: dict_to_load = json.loads(f.read()) self.assertDictEqual( self.obj.to_dict(), dict_to_load['City.' + self.obj.id]) def test_to_dict(self): """Test to compare to two dictonary """ new_dict = self.obj.__dict__.copy() new_dict["__class__"] = self.obj.__class__.__name__ new_dict["created_at"] = new_dict["created_at"].isoformat() new_dict["updated_at"] = new_dict["updated_at"].isoformat() self.assertDictEqual(new_dict, self.obj.to_dict()) self.assertEqual(self.obj.to_dict()['__class__'], "City") self.assertEqual(type(self.obj).__name__, "City")
class TestBaseModel(unittest.TestCase): """Unit test for City class""" @classmethod def setUp(cls): print('SetupClass') @classmethod def tearDown(cls): print('TearDownClass') def setUp(self): """Unit test setup""" print('setUp') self.c1 = City() self.c2 = City() def tearDown(self): """Unit test tear down""" del self.c1 del self.c2 def test_init(self): """Test for init method""" print("testing init...") self.assertIsNotNone(self.c1) self.assertIsInstance(self.c1, BaseModel) self.assertIs(type(self.c1), City) def test_uuid(self): """Test for uuid attribute""" print("testing uuid...") self.assertTrue(hasattr(self.c1, "id")) self.assertNotEqual(self.c1.id, self.c2.id) self.assertIsInstance(self.c1.id, str) def test_state_id(self): """Test for state_id attribute""" print("testing state_id...") self.assertTrue(hasattr(self.c1, "state_id")) self.assertEqual(self.c1.state_id, "") self.assertIsInstance(self.c1.state_id, str) def test_name(self): """Test for name attribute""" print("testing name...") self.assertTrue(hasattr(self.c1, "name")) self.assertEqual(self.c1.name, "") self.assertIsInstance(self.c1.name, str) def test_str(self): """Test for __str__ method""" print("testing __str__method...") result = len(self.c1.__str__()) self.assertTrue(result, 172) def test_save(self): """Test for save method""" print("testing save method...") prechange = self.c1.updated_at self.c1.save() postchange = self.c1.updated_at self.assertNotEqual(prechange, postchange) def test_created_at(self): """Test for created at time""" print("Testing the created at time attr") self.assertTrue(hasattr(self.c1, "created_at")) def test_updated_at(self): """Test for the updated at time attr""" print("Testing the updated at time attr") prechange = self.c1.updated_at self.c1.save() postchange = self.c1.updated_at self.assertNotEqual(prechange, postchange) def test_kwargs(self): """Test for kwargs""" print("Testing for kwargs") self.c1.name = "Holberton" self.c1.my_number = 89 c1_json = self.c1.to_dict() c2 = BaseModel(**c1_json) self.assertEqual(self.c1.id, c2.id) self.assertEqual(self.c1.created_at, c2.created_at) self.assertEqual(self.c1.updated_at, c2.updated_at) self.assertEqual(self.c1.name, c2.name) self.assertEqual(self.c1.my_number, c2.my_number) def test_module_docstring(self): """Test for existence of module docstring""" print("testing module docstring...") result = len(__import__('models.city').__doc__) self.assertTrue(result > 0, True) def test_class_docstring(self): """City Class Docstring Test""" print("test_class_docstring") result = len(City.__doc__) self.assertTrue(result > 0, True) def test_init_docstring(self): """City init Docstring Test""" print("test_init_docstring") result = len(self.__init__.__doc__) self.assertTrue(result > 0, True) def test__str__docstring(self): """City __str__ Docstring Test""" print("testing __str__ docstring...") result = len(City.__str__.__doc__) self.assertTrue(result > 0, True) def test_save_docstring(self): """City save method Docstring Test""" print("testing save docstring...") result = len(City.save.__doc__) self.assertTrue(result > 0, True) def test_to_dict_docstring(self): """City to_dict Docstring Test""" print("testing to_dict docstring...") result = len(City.to_dict.__doc__) self.assertTrue(result > 0, True) if __name__ == "__main__": unittest.main()
def test_str_method(self): """Tests to see if each method is printing accurately""" obj = City() objprinted = obj.__str__() self.assertEqual(objprinted, "[City] ({}) {}".format(obj.id, obj.__dict__))