def test_str(self): """ Check str method. """ new_user = User() string = "[User] ({}) {}".format(new_user.id, new_user.__dict__) self.assertIsInstance(new_user.__str__(), str) self.assertEqual(new_user.__str__(), string)
def test_method_str(self): """Test method str""" new = User() 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 User str representation. """ new = User() trex = r"\[User\] \(.*\) .*" self.assertIsInstance(new, User) self.assertTrue(type(new.__str__()) == str) self.assertTrue(search(trex, new.__str__())) args = ["hola", 2, 3.2, "poads"] new = User(*args) self.assertIsInstance(new, User) new = User("hola", 2, 3.2, "poads") self.assertIsInstance(new, User)
def test_str(self): """Test __str__ method""" us = User() string = '[' + us.__class__.__name__ + ']' + ' (' + us.id + ') ' + str( us.__dict__) self.assertEqual(string, us.__str__())
def test_User12(self): """Tests str""" s = "[User] ({}) {}" my_user13 = User() my_user13printed = my_user13.__str__() self.assertEqual(my_user13printed, s.format(my_user13.id, my_user13.__dict__))
def test_str(self): """ review format: [<class name>] (<self.id>) <self.__dict__> """ my_class = User() 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 = User() 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_str_format(self): """Test str format""" instance = User() s = instance.__str__() _format = "[{}] ({}) {}".\ format(instance.__class__.__name__, instance.id, instance.__dict__) self.assertEqual(s, _format)
def test_user_str_full(self): user = User() user.email = "*****@*****.**" user.first_name = "John" user.first_name = "Nash" eq_str = "[User] {} - {} - {}".format(user.id, user.email, user.display_name()) self.assertEqual(eq_str, user.__str__())
def test_3_str(self): '''If str method work as expected''' my_model_0 = User() my_model_1 = User() my_model_2 = User() 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_user_str_method(self): """User str method creates accurate representation""" user = User() user_str = user.__str__() self.assertIsInstance(user_str, str) self.assertEqual(user_str[:6], '[User]') self.assertEqual(user_str[7:45], '({})'.format(user.id)) self.assertDictEqual(eval(user_str[46:]), user.__dict__)
def test_str(self): """ test print """ u1 = User() u2 = User(id='Betty') s = "[{}] ({}) {}".format("User", u1.id, u1.__dict__) self.assertEqual(print(s), print(u1)) self.assertIsInstance(u1.__str__(), str) s = "[{}] ({}) {}".format("User", u2.id, u2.__dict__) self.assertEqual(print(s), print(u2))
def test_str_representation(self): dt = datetime.today() dt_repr = repr(dt) us = User() us.id = "123456" us.created_at = us.updated_at = dt usstr = us.__str__() self.assertIn("[User] (123456)", usstr) self.assertIn("'id': '123456'", usstr) self.assertIn("'created_at': " + dt_repr, usstr) self.assertIn("'updated_at': " + dt_repr, usstr)
def testargs(self): datet = datetime.today() datei = datet.isoformat() dater = repr(datet) usr = User() usr.id = "931" usr.created_at = usr.updated_at = datet usr_str = usr.__str__() self.assertIn("[User] (931)", usr_str) self.assertIn("'id': '931'", usr_str) self.assertIn("'created_at': " + dater, usr_str) self.assertIn("'updated_at': " + dater, usr_str)
class TestUser(unittest.TestCase): def setUp(self): '''method to set up instance of BaseModel/json file''' self.user = User() def tearDown(self): '''method to tear down instance of BaseModel/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.user) def test_attributes(self): '''method to test if updates take place in save''' self.assertEqual(self.user.email, "") self.assertEqual(self.user.password, "") self.assertEqual(self.user.first_name, "") self.assertEqual(self.user.last_name, "") self.assertTrue(hasattr(self.user, "created_at")) self.assertFalse(hasattr(self.user, "updated_at")) self.assertTrue(hasattr(self.user, "id")) self.user.save() self.assertTrue(hasattr(self.user, "updated_at")) def test_to_json(self): '''method to check that the to_json function returns''' example_tojson = User.to_json(self.user) 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.user), print(example)) def test__repr__(self): '''method to print attributes of dictionary''' self.assertIsNotNone(self.user.__str__())
def test_User(self): """Test instance of User class""" new = User() new2 = User() self.assertIsInstance(new, BaseModel) self.assertEqual(issubclass(new.__class__, BaseModel), True) self.assertIs(type(new), User) 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.assertIn(User(), models.storage.all().values()) self.assertEqual(User, type(User())) self.assertEqual(str, type(User().id)) self.assertEqual(datetime, type(User().created_at)) self.assertEqual(datetime, type(User().updated_at)) self.assertEqual(str, type(User.email)) self.assertEqual(str, type(User.password)) self.assertEqual(str, type(User.first_name))
def test_str_method(self): """Tests to see if each method is printing accurately""" b1 = User() b1printed = b1.__str__() self.assertEqual(b1printed, "[User] ({}) {}".format(b1.id, b1.__dict__))
class TestUser(unittest.TestCase): def setUp(self): """ setUp user instance """ self.user = User() self.dict_user = User() self.dict_user.email = "*****@*****.**" self.dict_user.password = "******" self.dict_user.first_name = "Bob" self.dict_user.last_name = "Dylan" self.d_user = self.dict_user.to_dict() def test_no_value_display(self): """ testing displays empty if no user """ self.assertIs(self.user.display_name(), "") def test_email_display(self): """ testing email value display """ self.user.email = "*****@*****.**" self.assertIs(self.user.display_name(), "*****@*****.**") def test_firstname_display(self): """ testing first name value display """ self.user.first_name = "Bob" self.assertIs(self.user.display_name(), "Bob") def test_lastname_display(self): """ testing last name value display """ self.user.email = "*****@*****.**" self.user.last_name = "Dylan" self.assertIs(self.user.display_name(), "Dylan") def test_fullname_display(self): """ testing full name value display """ self.user.first_name = "Bob" self.user.last_name = "Dylan" self.assertEqual(self.user.display_name(), "Bob Dylan") def test__str__(self): """ testing reformatted user info display """ self.assertEqual( self.user.__str__(), "[User] {} - {} - {}".format(self.user.id, self.user.email, self.user.display_name())) def test_no_password(self): """ testing empty password """ self.assertIsNone(self.user.password) def test_password(self): """ testing password value """ self.user.password = "******" self.assertEqual(self.user.password, "5d41402abc4b2a76b9719d911017c592") def test_is_valid_password_none(self): """ testing password None """ self.assertFalse(self.user.is_valid_password(None)) def test_password_is_not_valid(self): """ testing false password """ self.assertFalse(self.user.is_valid_password(89)) self.assertFalse(self.user.is_valid_password("tutu1234")) def test_password_is_valid(self): """ testing valid password """ self.user.email = "*****@*****.**" self.user.password = "******" self.assertTrue(self.user.is_valid_password("toto1234")) def test_id_to_dict_id(self): """ testing id in dict """ self.assertIsInstance(self.d_user["id"], str) def test_to_dict_updated_at(self): """ testing updated_at in dict """ self.assertEqual( "{} ({})".format("updated_at", type(self.d_user["updated_at"])), "updated_at (<class 'str'>)") def test_to_dict_firstname(self): """ testing first_name in dict """ self.assertEqual( "{} ({}): {}".format("first_name", type(self.d_user["first_name"]), self.dict_user.first_name), "first_name (<class 'str'>): Bob") def test_to_dict_email(self): """ testing email in dict """ self.assertEqual( "{} ({}): {}".format("email", type(self.d_user["email"]), self.dict_user.email), "email (<class 'str'>): [email protected]") def test_to_dict_lastname(self): """ testing last_name in dict """ self.assertEqual( "{} ({}): {}".format("last_name", type(self.d_user["last_name"]), self.dict_user.last_name), "last_name (<class 'str'>): Dylan") def test_to_dict_created_at(self): """ testing created_at in dict """ self.assertEqual( "{} ({})".format("created_at", type(self.d_user["created_at"])), "created_at (<class 'str'>)")
def test_str_method(self): """Tests to see if each method is printing accurately""" i = User() printed = i.__str__() self.assertEqual(printed, "[User] ({}) {}".format(i.id, i.__dict__))
def test_str_(self): """ tests str method """ b1 = User() b1_str = b1.__str__() self.assertEqual(b1_str, "[User] ({}) {}".format(b1.id, b1.__dict__))
def test_str_method(self): """Tests to see if each method is printing accurately""" obj = User() objprinted = obj.__str__() self.assertEqual(objprinted, "[User] ({}) {}".format(obj.id, obj.__dict__))
class Test_User(unittest.TestCase): """ Class to test """ def setUp(self): """Function that execute before of each test function """ self.obj = User() def test_createUser(self): """Test that is instance """ self.assertIsInstance(self.obj, User) 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(User.__doc__) def test_pep8_user(self): """ Test for PEP8 """ pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/user.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['User.' + 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__'], "User") self.assertEqual(type(self.obj).__name__, "User")
class TestUser(unittest.TestCase): """Class test """ def setUp(self): """ setUp init for tests""" self.my_model = User() self.my_model2 = User() self.my_model.name = "Vale" self.my_model.age = 28 self.my_model.save() self.my_model2.save() def test_pep8_conformance_User(self): """Test that we conform to PEP8.""" pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['models/user.py']) self.assertEqual(result.total_errors, 0) def test_docstrings(self): """Test docstrings""" self.assertIsNotNone(User.__doc__) self.assertIsNotNone(User.__init__.__doc__) self.assertIsNotNone(User.__str__.__doc__) self.assertIsNotNone(User.save.__doc__) self.assertIsNotNone(User.to_dict.__doc__) def test_user(self): """Test for User """ self.assertIsInstance(self.my_model, User) 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), "[User] ({}) {}". format(self.my_model.id, self.my_model.__dict__)) self.assertEqual(self.my_model.__str__(), "[User] ({}) {}". 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 = User(**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): z = User() stringA = str(z) stringB = z.__str__() self.assertTrue(stringA, stringB)
class ModelTests(unittest.TestCase): """ tests for the User class """ def setUp(self): """ setup tests """ self.model = User() def test_class_doc(self): """ test class for doc """ self.assertGreater(len(self.model.__doc__), 0) def test_function_docs(self): """ test class methods for doc """ attr_list = inspect.getmembers(self.model) for attr in attr_list: attr_type = type(attr[1]).__name__ if type(attr_type) == "method": self.assertGreater(len(attr[1].__doc__, 0)) def test_model(self): """ test User """ self.assertIsNotNone(self.model) self.assertIsInstance(self.model, BaseModel) def test_id(self): """ test id """ self.assertIsNotNone(self.model.id) self.assertIsInstance(self.model.id, str) def test_created_at(self): """ test created_at """ self.assertIsNotNone(self.model.created_at) self.assertIsInstance(self.model.created_at, datetime) def test_updated_at(self): """ test created_at """ self.assertIsNotNone(self.model.created_at) self.assertIsInstance(self.model.updated_at, datetime) self.assertEqual(self.model.created_at, self.model.updated_at) def test_display_name(self): """ tests display_name() when first_name and last_name are not None """ self.model.first_name = "Bob" self.model.last_name = "Dylan" self.assertEqual(self.model.display_name(), "Bob Dylan") def test_display_name_all_None(self): """ tests display_name() when all attributes are None """ self.assertEqual(self.model.display_name(), "") def test_display_name_first_last_None(self): """ tests display_name() when first_name and last_name are None """ self.model.email = "*****@*****.**" self.assertEqual(self.model.display_name(), "*****@*****.**") def test_display_last_None(self): """ tests display_name() when last name is None """ self.model.first_name = "Bob" self.assertEqual(self.model.display_name(), "Bob") def test_display_name_first_None(self): """ tests display_name() when first_name is None """ self.model.last_name = "Dylan" self.assertEqual(self.model.display_name(), "Dylan") def test_str_email(self): """ test with valid email """ self.model.email = "*****@*****.**" self.assertEqual( self.model.__str__(), "[User] " + self.model.id + " - [email protected] - " + "*****@*****.**") def test_str_first_name(self): """ test with first name only """ self.model.first_name = "Bob" self.assertEqual(self.model.__str__(), "[User] " + self.model.id + " - None - Bob") def test_str_last_name(self): """ test with last name only """ self.model.last_name = "Dylan" self.assertEqual(self.model.__str__(), "[User] " + self.model.id + " - None - Dylan") def test_str_format(self): """ test with first and last name, and email """ self.model.first_name = "Bob" self.model.last_name = "Dylan" self.model.email = "*****@*****.**" self.assertEqual( self.model.__str__(), "[User] " + self.model.id + " - [email protected] - Bob Dylan") def test_password_set(self): """ test password set and get """ self.model.password = "******" expect = hashlib.md5(bytes("my_password".encode("utf8"))).hexdigest() self.assertEqual(self.model.password, expect) def test_is_valid_password_true(self): """ test a valid password """ pw = "my_password" self.model.password = pw self.assertEqual(self.model.is_valid_password(pw), True) def test_is_valid_password_false(self): """ test an invalid password """ pw = "my_password" self.model.password = pw self.assertEqual(self.model.is_valid_password("my_passw0rd"), False) def test_is_valid_password_none_param(self): """ test None pwd """ pw = "my_password" self.model.password = pw self.assertEqual(self.model.is_valid_password(None), False) def test_is_valid_password_none_self(self): """ test None self._password """ pw = None self.model.password = pw self.assertEqual(self.model.is_valid_password("my_password"), False) def test_to_dict(self): """ test to_dict class method """ str_format = "%Y-%m-%d %H:%M:%S" expected = { "id": self.model.id, "email": "*****@*****.**", "first_name": "Bob", "last_name": "Dylan", "created_at": self.model.created_at.strftime(str_format), "updated_at": self.model.updated_at.strftime(str_format) } self.model.first_name = "Bob" self.model.last_name = "Dylan" self.model.email = "*****@*****.**" self.assertEqual(expected, self.model.to_dict())
def addUser(): user = User() user = user.filteredObject(request.form) db.session.add(user) db.session.commit() return user.__str__()
def test_user_str_email(self): user = User() user.email = "*****@*****.**" eq_str = "[User] {} - {} - {}".format(user.id, user.email, user.email) self.assertEqual(eq_str, user.__str__())
def test_user_str_none(self): user = User() self.assertEqual("[User] {} - None - ".format(user.id), user.__str__())
def test_str_method(self): """test that each method is printing accurately""" user3 = User() user3printed = user3.__str__() self.assertEqual(user3printed, "[User] ({}) {}".format(user3.id, user3.__dict__))
def test_str(self): """Test str""" instance = User() self.assertEqual(type(instance.__str__()), str)