class Test_2_DB_all_users(unittest.TestCase): def setUp(self): self.db = DB() self.connection = self.db.connect_db() with self.db.db_cursor(self.connection) as curs: for i in range(10): self.u = User() self.u.username = '******' + ('').join(random.choices(ALPHABET, k=3)) self.u.email = str(self.u.username) + '@email.com' self.u.hashed_password = {'password': ('').join(random.choices(ALPHABET, k=6)), 'salt': None} #save save = self.u.save_to_db(curs) self.connection.commit() def test_load_all_users(self): with self.db.db_cursor(self.connection) as curs: all_users = User.load_all_users(curs) self.assertTrue(len(all_users), 10) self.assertIsInstance(all_users, list) self.assertIsInstance(all_users[1], User) self.assertIsInstance(all_users[6], User) self.assertIsInstance(all_users[9], User) def tearDown(self): with self.db.db_cursor(self.connection) as curs: all_users = User.load_all_users(curs) for u in all_users: u.delete(curs) self.connection.commit() self.connection.close() print(self.connection)
#!/usr/bin/python3 from db_run import DB # create Database and tables users and messages if __name__ == '__main__': o = DB() o.clean() o.run() connection = o.connect_db() with o.db_cursor(connection) as curs: o.create_tables(curs) print(curs) print(curs) print(connection) connection.commit() connection.close() print(connection)
'--delete', action='store_true', help='delete user, use wit -u -p') parser.add_argument('-l', '--list', action='store_true', help='list of users') options = vars(parser.parse_args()) options = {k: v for k, v in options.items() if v not in (False, None)} # db connection cnx = None ob = DB() try: cnx = ob.connect_db() except Exception: raise Exception( 'Incorrect connection parameters or database not created!') with ob.db_cursor(cnx) as curs: if 'username' in options: if len(options) > 0 and 'password' not in options: parser.print_help() userX = User.load_user_by_username(curs, options['username']) # no user if not userX: if 'password' in options: if len(options) == 2: if password_atleast_8(options['password']): # create user