def test_read(self): """Test classmethod :meth:`plugins.user.UserDatabase.read` **Tested:** - The returned object is a UserDatabase object. - The contents of the returned user database are correct. """ userdb_fn = os.path.join(cfg.tmp_dir, 'testuserdb.txt') contents1 = '\n'.join([ "username;password", "un1;pw1", "un2;pw2", ]) + '\n' # Test reading of file that does not exist. userdb = user.UserDatabase.read(userdb_fn) self.assertEqual(userdb.users, []) # Test reading of correct file. with open(userdb_fn, 'w') as userdb_fil: userdb_fil.write(contents1) expected0 = user.User(username='******', password='******') expected1 = user.User(username='******', password='******') userdb = user.UserDatabase.read(userdb_fn) self.assertTrue(isinstance(userdb, user.UserDatabase)) self.assertEqual(len(userdb.users), 2) self.assertEqual(userdb.users[0], expected0) self.assertEqual(userdb.users[1], expected1)
def test_write(self): """Test method :meth:`plugins.user.User.write` **Tested:** - The contents of the generated file are correct. """ usr = user.User( name='John Smith', age=50, username='******', password='******', ) testfile_fn = os.path.join(cfg.tmp_dir, 'testoutput.txt') usr.write(testfile_fn) with open(testfile_fn, 'r') as testfile: result = testfile.read() expected = [ "Contents of User object:", "name: John Smith", "age: 50", "username: johnsmith", "password: password", ] self.assertTrue(all([part in result for part in expected]))
def test_is_valid_user(self): """Test method :meth:`plugins.user.UserDatabase.is_valid_user` **Tested:** - False is returned when a non-user object is passed. - False is returned when an invalid user is passed. - True is returned when a valid user is passed. """ userdb = user.UserDatabase() usr_invalid = user.User(name='name1', age=10) usr_valid = user.User(username='******', password='******') self.assertFalse(userdb.is_valid_user(0)) self.assertFalse(userdb.is_valid_user(usr_invalid)) self.assertTrue(userdb.is_valid_user(usr_valid))
def test___init__(self): """Test method :meth:`plugins.user.UserDatabase.__init__` **Tested:** - The amount of users in a user database is correct after initialization. - The users in a user database are correct after initialization. """ userdb = user.UserDatabase([ user.User(username='******', password='******'), user.User(username='******', password='******'), ], ) expected0 = user.User(username='******', password='******') expected1 = user.User(username='******', password='******') self.assertEqual(len(userdb.users), 2) self.assertEqual(userdb.users[0], expected0) self.assertEqual(userdb.users[1], expected1)
def test_get_users(self): """Test method :meth:`plugins.user.UserDatabase.get_users` **Tested:** - If there are matches, the returned list of users is correct. - If there are no matches, the returned list is empty. """ userdb = user.UserDatabase([ user.User(username='******', password='******'), user.User(username='******', password='******'), user.User(username='******', password='******'), ], ) expected1 = [ user.User(username='******', password='******'), user.User(username='******', password='******'), ] expected2 = [ user.User(username='******', password='******'), user.User(username='******', password='******'), ] self.assertEqual(userdb.get_users('username', 'un1'), expected1) self.assertEqual(userdb.get_users('password', 'pw2'), expected2) self.assertEqual(userdb.get_users('username', 'un3'), []) self.assertEqual(userdb.get_users('invalid_attr', 'val'), [])
def test_add_user(self): """Test method :meth:`plugins.user.UserDatabase.add_user` **Tested:** - The amount of users in the user database is correct before and after adding a user. - The last user in the user database is the one that was added. """ userdb = user.UserDatabase([ user.User(username='******', password='******'), ], ) user_added = user.User(username='******', password='******') expected = user.User(username='******', password='******') self.assertEqual(len(userdb.users), 1) userdb.add_user(user_added) self.assertEqual(len(userdb.users), 2) self.assertEqual(userdb.users[-1], expected)
def test___str__(self): """Test method :meth:`plugins.user.UserDatabase.__str__` **Tested:** - The returned string is correct. """ userdb = user.UserDatabase([ user.User(username='******', password='******'), user.User(username='******', password='******'), ], ) result = userdb.__str__() expected = '\n'.join([ "User database:", " Required user attributes: ['username', 'password']", " Amount of users inside database: 2" ]) self.assertEqual(result, expected)
def test___eq__(self): """Test method :meth:`plugins.user.User.__eq__` **Tested:** - True is returned when the two users have the same attributes and values. - False is returned when the two users have the same attributes but different values. - False is returned when the two users have different attributes, but the same values for the attributes that are the same. """ user1 = user.User(name='John Smith', age=50, extra='extra_attribute') user2 = user.User(name='John Smith', age=50, extra='extra_attribute') user3 = user.User(name='John Smith', age=60, extra='extra_attribute') user4 = user.User(name='John Smith', age=50) user5 = user.User(name='John Smith', age=50, other='extra_attribute') self.assertTrue(user1 == user2) self.assertFalse(user1 == user3) self.assertFalse(user1 == user4) self.assertFalse(user1 == user5)
def test_write(self): """Test method :meth:`plugins.user.UserDatabase.write` **Tested:** - The contents of the generated file are correct. """ userdb = user.UserDatabase([ user.User(username='******', password='******'), user.User(username='******', password='******'), ], ) userdb_fn = os.path.join(cfg.tmp_dir, 'testuserdb.txt') userdb.write(userdb_fn) with open(userdb_fn, 'r') as userdb_fil: result = userdb_fil.read() expected = [ "username;password", "un1;pw1", "un2;pw2", ] self.assertTrue(all([part in result for part in expected]))
def run(): """Execute the challenges.001e module.""" # Ask for input. name = utils.get_input("Name? > ") age = utils.get_input("Age? > ") reddit_username = utils.get_input("Reddit Username? > ") # Create PersonalInfo object, print and write to file. pi = user.User(name=name, age=age, reddit_username=reddit_username) print(pi) outputfile_fn = os.path.join(cfg.output_dir, '001e_example_output.txt') pi.write(outputfile_fn) print("Note: Data has been appended to file '{}'".format(outputfile_fn))
def test___init__(self): """Test method :meth:`plugins.user.User.__init__` **Tested:** - The attributes of a user are correct after initialization. """ usr = user.User( name='John Smith', age=50, username='******', password='******', ) self.assertEqual(usr.name, 'John Smith') self.assertEqual(usr.age, 50) self.assertEqual(usr.username, 'johnsmith') self.assertEqual(usr.password, 'password')
def test___str__(self): """Test method :meth:`plugins.user.User.__str__` **Tested:** - The returned string is correct. """ usr = user.User( name='John Smith', age=50, username='******', password='******', ) result = usr.__str__() expected = [ "Contents of User object:", "name: John Smith", "age: 50", "username: johnsmith", "password: password", ] self.assertTrue(all([part in result for part in expected]))