def test_method_str(self): """Test method str""" new = Place() 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 Place str representation. """ new = Place() trex = r"\[Place\] \(.*\) .*" self.assertIsInstance(new, Place) self.assertTrue(type(new.__str__()) == str) self.assertTrue(search(trex, new.__str__())) args = ["hola", 2, 3.2, "poads"] new = Place(*args) self.assertIsInstance(new, Place) new = Place("hola", 2, 3.2, "poads") self.assertIsInstance(new, Place)
def test_str(self): """Test __str__ method""" pl = Place() string = '[' + pl.__class__.__name__ + ']' + ' (' + pl.id + ') ' + str( pl.__dict__) self.assertEqual(string, pl.__str__())
def test_4_str_method(self): """is the string representation is wrong""" model_1 = Place() 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_3_str(self): '''If str method work as expected''' my_model_0 = Place() my_model_1 = Place() my_model_2 = Place() 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_place_str_method(self): """Place str method creates accurate representation""" place = Place() place_str = place.__str__() self.assertIsInstance(place_str, str) self.assertEqual(place_str[:7], '[Place]') self.assertEqual(place_str[8:46], '({})'.format(place.id)) self.assertDictEqual(eval(place_str[47:]), place.__dict__)
def test_methodworks(self): """ Check the instance""" instancia = Place() time = instancia.updated_at instancia.save() self.assertTrue(time != instancia.updated_at) self.assertTrue(type(instancia.__str__()) is str) self.assertTrue(type(instancia.to_dict()) is dict)
def test_str_representation(self): dt = datetime.today() dt_repr = repr(dt) pl = Place() pl.id = "123456" pl.created_at = pl.updated_at = dt plstr = pl.__str__() self.assertIn("[Place] (123456)", plstr) self.assertIn("'id': '123456'", plstr) self.assertIn("'created_at': " + dt_repr, plstr) self.assertIn("'updated_at': " + dt_repr, plstr)
class TestAmenity(unittest.TestCase): def setUp(self): '''method to set up instance of Place/json file''' self.place = Place() def tearDown(self): '''method to tear down instance of Place/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.place) def test_attributes(self): '''method to test if updates take place in save''' self.assertEqual(self.place.city_id, "") self.assertEqual(self.place.user_id, "") self.assertEqual(self.place.name, "") self.assertEqual(self.place.description, "") self.assertEqual(self.place.number_rooms, 0) self.assertEqual(self.place.number_bathrooms, 0) self.assertEqual(self.place.max_guest, 0) self.assertEqual(self.place.price_by_night, 0) self.assertEqual(self.place.latitude, 0.0) self.assertEqual(self.place.longitude, 0.0) self.assertEqual(self.place.amenity_ids, "") self.assertTrue(hasattr(self.place, "created_at")) self.assertFalse(hasattr(self.place, "updated_at")) self.assertTrue(hasattr(self.place, "id")) self.place.save() self.assertTrue(hasattr(self.place, "updated_at")) def test_to_json(self): '''method to check that the to_json function returns''' example_tojson = Place.to_json(self.place) 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.place), print(example)) def test__repr__(self): '''method to print attributes of dictionary''' self.assertIsNotNone(self.place.__str__())
def test_Place(self): """Test instance of amenity class""" new = Place() new2 = Place() self.assertIsInstance(new, BaseModel) self.assertEqual(issubclass(new.__class__, BaseModel), True) self.assertIs(type(new), Place) self.assertTrue(hasattr(new, "id")) 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(Place, type(Place()))
def test_str_method(self): """test that each method is printing accurately""" pl3 = Place() pl3printed = pl3.__str__() self.assertEqual(pl3printed, "[Place] ({}) {}".format(pl3.id, pl3.__dict__))
def test_str_method(self): """Tests to see if each method is printing accurately""" b1 = Place() b1printed = b1.__str__() self.assertEqual(b1printed, "[Place] ({}) {}".format(b1.id, b1.__dict__))
def test_str(self): """ Tests the str repr. of an object """ base = Place() base_str = base.__str__() self.assertTrue(isinstance(base_str, str))
def test_str_method(self): """Tests to see if each method is printing accurately""" i = Place() printed = i.__str__() self.assertEqual(printed, "[Place] ({}) {}".format(i.id, i.__dict__))
class TestPlace(unittest.TestCase): """Class test """ def setUp(self): """ setUp init for tests""" self.my_model = Place() self.my_model2 = Place() self.my_model.name = "Vale" self.my_model.age = 28 def test_pep8_conformance_Place(self): """Test that we conform to PEP8.""" pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/place.py']) self.assertEqual(result.total_errors, 0) def test_docstrings(self): """Test docstrings""" self.assertIsNotNone(Place.__doc__) self.assertIsNotNone(Place.__init__.__doc__) self.assertIsNotNone(Place.__str__.__doc__) self.assertIsNotNone(Place.save.__doc__) self.assertIsNotNone(Place.to_dict.__doc__) def test_base(self): """Test for Place """ self.assertIsInstance(self.my_model, Place) 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), "[Place] ({}) {}".format(self.my_model.id, self.my_model.__dict__)) self.assertEqual( self.my_model.__str__(), "[Place] ({}) {}".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 = Place(**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): """ testing to see if the method is printing """ b1 = Place() b1_str = b1.__str__() self.assertEqual(b1_str, "[Place] ({}) {}".format(b1.id, b1.__dict__))
class Test_Place(unittest.TestCase): """ Class to test """ def setUp(self): """Function that execute before of each test function """ self.obj = Place() def test_createPlace(self): """Test that is instance """ self.assertIsInstance(self.obj, Place) 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(Place.__doc__) def test_pep8_place(self): """ Test for PEP8 """ pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/place.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['Place.' + 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__'], "Place") self.assertEqual(type(self.obj).__name__, "Place")
class TestBaseModel(unittest.TestCase): """Unit test for Place class""" @classmethod def setUp(cls): print('SetupClass') @classmethod def tearDown(cls): print('TearDownClass') def setUp(self): """Unit test setup""" print('setUp') self.p1 = Place() self.p2 = Place() def tearDown(self): """Unit test tear down""" del self.p1 del self.p2 def test_init(self): """Test for init method""" print("testing init...") self.assertIsNotNone(self.p1) self.assertIsInstance(self.p1, BaseModel) self.assertIs(type(self.p1), Place) def test_uuid(self): """Test for uuid attribute""" print("testing uuid...") self.assertTrue(hasattr(self.p1, "id")) self.assertNotEqual(self.p1.id, self.p2.id) self.assertIsInstance(self.p1.id, str) def test_city_id(self): """Test for city_id attribute""" print("testing city_id...") self.assertTrue(hasattr(self.p1, "city_id")) self.assertEqual(self.p1.city_id, "") self.assertIsInstance(self.p1.city_id, str) def test_user_id(self): """Test for user_id attribute""" print("testing user_id...") self.assertTrue(hasattr(self.p1, "user_id")) self.assertEqual(self.p1.user_id, "") self.assertIsInstance(self.p1.user_id, str) def test_name(self): """Test for name attribute""" print("testing name...") self.assertTrue(hasattr(self.p1, "name")) self.assertEqual(self.p1.name, "") self.assertIsInstance(self.p1.name, str) def test_description(self): """Test for description attribute""" print("testing description...") self.assertTrue(hasattr(self.p1, "description")) self.assertEqual(self.p1.description, "") self.assertIsInstance(self.p1.description, str) def test_number_rooms(self): """Test for number_rooms attribute""" print("testing number_rooms...") self.assertTrue(hasattr(self.p1, "number_rooms")) self.assertEqual(self.p1.number_rooms, 0) self.assertIsInstance(self.p1.number_rooms, int) def test_number_bathrooms(self): """Test for number_bathrooms attribute""" print("testing number_bathrooms...") self.assertTrue(hasattr(self.p1, "number_bathrooms")) self.assertEqual(self.p1.number_bathrooms, 0) self.assertIsInstance(self.p1.number_bathrooms, int) def test_max_guest(self): """Test for max_guest attribute""" print("testing max_guest...") self.assertTrue(hasattr(self.p1, "max_guest")) self.assertEqual(self.p1.max_guest, 0) self.assertIsInstance(self.p1.max_guest, int) def test_price_by_night(self): """Test for price_by_night attribute""" print("testing price_by_night...") self.assertTrue(hasattr(self.p1, "price_by_night")) self.assertEqual(self.p1.price_by_night, 0) self.assertIsInstance(self.p1.price_by_night, int) def test_latitude(self): """Test for latitude attribute""" print("testing latitude...") self.assertTrue(hasattr(self.p1, "latitude")) self.assertEqual(self.p1.latitude, 0.0) self.assertIsInstance(self.p1.latitude, float) def test_longitude(self): """Test for longitude attribute""" print("testing longitude...") self.assertTrue(hasattr(self.p1, "longitude")) self.assertEqual(self.p1.longitude, 0.0) self.assertIsInstance(self.p1.longitude, float) def test_amenity_ids(self): """Test for amenity_ids attribute""" print("testing amenity_ids...") self.assertTrue(hasattr(self.p1, "amenity_ids")) self.assertEqual(self.p1.amenity_ids, "") self.assertIsInstance(self.p1.amenity_ids, str) def test_str(self): """Test for __str__ method""" print("testing __str__method...") result = len(self.p1.__str__()) self.assertTrue(result, 172) def test_save(self): """Test for save method""" print("testing save method...") prechange = self.p1.updated_at self.p1.save() postchange = self.p1.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.p1, "created_at")) def test_updated_at(self): """Test for the updated at time attr""" print("Testing the updated at time attr") prechange = self.p1.updated_at self.p1.save() postchange = self.p1.updated_at self.assertNotEqual(prechange, postchange) def test_kwargs(self): """Test for kwargs""" print("Testing for kwargs") self.p1.name = "Holberton" self.p1.my_number = 89 p1_json = self.p1.to_dict() p2 = BaseModel(**p1_json) self.assertEqual(self.p1.id, p2.id) self.assertEqual(self.p1.created_at, p2.created_at) self.assertEqual(self.p1.updated_at, p2.updated_at) self.assertEqual(self.p1.name, p2.name) self.assertEqual(self.p1.my_number, p2.my_number) def test_module_docstring(self): """Test for existence of module docstring""" print("testing module docstring...") result = len(__import__('models.place').__doc__) self.assertTrue(result > 0, True) def test_class_docstring(self): """Place Class Docstring Test""" print("test_place_docstring") result = len(Place.__doc__) self.assertTrue(result > 0, True) def test_init_docstring(self): """Place init Docstring Test""" print("test_init_docstring") result = len(self.__init__.__doc__) self.assertTrue(result > 0, True) def test__str__docstring(self): """Place __str__ Docstring Test""" print("testing __str__ docstring...") result = len(Place.__str__.__doc__) self.assertTrue(result > 0, True) def test_save_docstring(self): """Place save method Docstring Test""" print("testing save docstring...") result = len(Place.save.__doc__) self.assertTrue(result > 0, True) def test_to_dict_docstring(self): """Place to_dict Docstring Test""" print("testing to_dict docstring...") result = len(Place.to_dict.__doc__) self.assertTrue(result > 0, True) if __name__ == "__main__": unittest.main()
def test_str(self): z = Place() stringA = str(z) stringB = z.__str__() self.assertTrue(stringA, stringB)