def setup_test_db(): cursor = database_f.get_test_cursor() # Strip it down tables = [] query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: tables.append(row['tablename']) for t in tables: query = """DROP TABLE {} CASCADE""".format(t) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: raise Exception("Not all tables deleted, found table %s" % row['tablename']) # Build structure sync_module.main(fix=True, print_output=False) # Populate it with the test data path = "{}/test_lib/test_setup.sql".format(sys.path[0]) with open(path) as f: query = dummy_data + f.read() if query != "": try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # Test to ensure we list the right number of tables tables = 0 query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: tables += 1 if tables != len(sync_module.table_list): raise Exception("Error: Tables listed ({}) does not match the length of sync.table_list: {}".format(tables, len(sync_module.table_list)))
def test_try_transaction(self): self.test_targets.append(database_f.try_transaction) cursor = database_f.get_test_cursor() # Set it all up results = [] original_execute = cursor.execute cursor.execute = results.append def without_try(c): c.execute("without_try") @database_f.try_transaction def with_try(c): c.execute("with_try") @database_f.try_transaction def with_try_execption(c): c.execute("with_try_execption") raise Exception("") # Now we make it fall over without_try(cursor) with_try(cursor) try: with_try_execption(cursor) except Exception: pass self.assertEqual(results, [ "without_try", "BEGIN", "with_try", "COMMIT", "BEGIN", "with_try_execption", "ROLLBACK", ]) cursor.execute = original_execute
def ttest_queries(self): cursor = database_f.get_test_cursor() self.test_targets.extend([common_q._make_query, common_q.id_list, common_q.get_one, common_q.get_all, common_q.get_where, common_q.get_last]) # ID List self.assertEqual(common_q.id_list(cursor, error.Error), [1,2,3,4,5]) # All self.assertEqual(len(common_q.get_all(cursor, error.Error)), 5) self.assertEqual(type(common_q.get_all(cursor, error.Error, where="id=1")[1]), error.Error) self.assertEqual(len(common_q.get_all(cursor, error.Error, where="id>3")), 2) # One result = common_q.get_one(cursor, error.Error, id=1) fake = error.Error({ "id": 1, "timestamp": 1000, "args": "a=1", "mode": "list_users", "user_id": 1, "exception_type": "Exception", "traceback": "traceback", }) if result != fake: print(result.compare(fake)) self.assertEqual(result, fake) # Where self.assertEqual( common_q.get_all(cursor, error.Error, where='"timestamp" = 3000'), common_q.get_where(cursor, error.Error, timestamp=3000) ) # Last self.assertEqual( common_q.get_all(cursor, error.Error, where='id = 5')[5], common_q.get_last(cursor, error.Error) )
def test_log_error(self): self.test_targets.append(error.log_error) cursor = database_f.get_test_cursor() # First we make sure there is no error query = """DELETE FROM errors""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # We need to fake a user for the logging to work common_f.cache['user'] = user.User(id=1, username="******") # Fake args gui_test_utils.new_cgi_form(( ("mode", "list_users"), ("arg_1", 1), ("arg_3", 3), )) # Now we log the error try: raise Exception("This is an exception") except Exception as e: error.log_error(cursor, e, context=5) query = """SELECT * FROM errors""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) errors_found = [] for row in cursor: errors_found.append(row) self.assertEqual(len(errors_found), 1) self.assertEqual(row['user_id'], 1) self.assertEqual(row['args'], "mode = list_users\n\narg_1 = 1\n\narg_3 = 3") self.assertEqual(row['exception_type'], "Exception") self.assertEqual(row['mode'], "list_users")
def test_check_default(self): self.test_targets.extend([ database_f.DBField.data_type_syntax, database_f.VarcharField.data_type_syntax, database_f.DBField.check_default, database_f.BooleanField.check_default, database_f.DateField.check_default, database_f.DoubleField.check_default, database_f.IntegerField.check_default, database_f.SerialField.check_default, database_f.TextField.check_default, database_f.TimestampField.check_default, database_f.VarcharField.check_default, ]) cursor = database_f.get_test_cursor() # We actually want to make our table sync_f.check_table(cursor, fake_info, fix=True) # Use this query to grab info about the rows db_info_dict = {} query = """SELECT column_default, column_name FROM INFORMATION_SCHEMA.COLUMNS where table_name = 'fake_connected'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: db_info_dict[row['column_name']] = row['column_default'] # Have one correct and one incorrect default to ensure it's not replacing the correct ones # BooleanField self.assertEqual(database_f.BooleanField("the_field", default=False).check_default( db_info_dict['b1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT 'False';"]) self.assertEqual(database_f.BooleanField("the_field", default=True).check_default( db_info_dict['b1'], fake_info ), []) # DateField self.assertEqual(database_f.DateField("the_field", default="1000-01-01").check_default( db_info_dict['date_field'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT '1000-01-01';"]) self.assertEqual(database_f.DateField("the_field", default="2000-10-30").check_default( db_info_dict['date_field'], fake_info ), []) # DoubleField self.assertEqual(database_f.DoubleField("the_field", default=1.5).check_default( db_info_dict['d1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT '1.5';"]) self.assertEqual(database_f.DoubleField("the_field", default=1.1).check_default( db_info_dict['d1'], fake_info ), []) # IntegerField self.assertEqual(database_f.IntegerField("the_field", default=5).check_default( db_info_dict['i1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT '5';"]) self.assertEqual(database_f.IntegerField("the_field", default=1).check_default( db_info_dict['i1'], fake_info ), []) self.assertEqual(database_f.IntegerField("the_field", default=-1).check_default( db_info_dict['i1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT '-1';"]) # SerialField self.assertEqual(database_f.SerialField("the_field", default=5).check_default( db_info_dict['s1'], fake_info ), []) self.assertEqual(database_f.SerialField("the_field", default=1).check_default( db_info_dict['s1'], fake_info ), []) # TextField self.assertEqual(database_f.TextField("the_field", default='tt').check_default( db_info_dict['t1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT 'tt';"]) self.assertEqual(database_f.TextField("the_field", default='t1').check_default( db_info_dict['t1'], fake_info ), []) # TimestampField self.assertEqual(database_f.TimestampField("the_field", default='2012-1-5 10:00:00').check_default( db_info_dict['time_field'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT '2012-1-5 10:00:00';"]) self.assertEqual(database_f.TimestampField("the_field", default='2000-10-30 12:50:59').check_default( db_info_dict['time_field'], fake_info ), []) # VarcharField self.assertEqual(database_f.VarcharField("the_field", default='vvv').check_default( db_info_dict['v1'], fake_info ), ["ALTER TABLE fake_connected ALTER COLUMN the_field SET DEFAULT 'vvv';"]) self.assertEqual(database_f.VarcharField("the_field", default='v1').check_default( db_info_dict['v1'], fake_info ), []) # Drop it incase it affects any other tests try: cursor.execute("DROP TABLE fake_connected") except Exception: pass
def test_check_type(self): self.test_targets.extend([ database_f.DBField.data_type_syntax, database_f.VarcharField.data_type_syntax, database_f.DBField.check_type, database_f.BooleanField.check_type, database_f.DateField.check_type, database_f.DoubleField.check_type, database_f.IntegerField.check_type, database_f.SerialField.check_type, database_f.TextField.check_type, database_f.TimestampField.check_type, database_f.VarcharField.check_type, ]) cursor = database_f.get_test_cursor() # We actually want to make our table sync_f.check_table(cursor, fake_info, fix=True) # Use this query to grab info about the rows db_info_dict = {} query = """SELECT data_type, column_name FROM INFORMATION_SCHEMA.COLUMNS where table_name = 'fake_connected'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: db_info_dict[row['column_name']] = row['data_type'] # BooleanField the_field = database_f.BooleanField("the_field") self.assertEqual(the_field.check_type(db_info_dict['b1'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE boolean;"] ) # DateField the_field = database_f.DateField("the_field") self.assertEqual(the_field.check_type(db_info_dict['date_field'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE date;"] ) # DoubleField the_field = database_f.DoubleField("the_field") self.assertEqual(the_field.check_type(db_info_dict['d1'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE double precision;"] ) # IntegerField the_field = database_f.IntegerField("the_field") self.assertEqual(the_field.check_type(db_info_dict['i1'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE integer;"] ) # SerialField the_field = database_f.SerialField("the_field") self.assertEqual(the_field.check_type(db_info_dict['s1'], fake_info), []) self.assertRaises(Exception, the_field.check_type, (db_info_dict['v1'], fake_info)) # TextField the_field = database_f.TextField("the_field") self.assertEqual(the_field.check_type(db_info_dict['t1'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE text;"] ) # TimestampField the_field = database_f.TimestampField("the_field") self.assertEqual(the_field.check_type(db_info_dict['time_field'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE timestamp without time zone;"] ) # VarcharField the_field = database_f.VarcharField("the_field") self.assertEqual(the_field.check_type(db_info_dict['v1'], fake_info), []) self.assertEqual(the_field.check_type(db_info_dict['i1'], fake_info), ["ALTER TABLE fake_connected ALTER COLUMN the_field TYPE varchar(255);"] ) # Drop it incase it affects any other tests try: cursor.execute("DROP TABLE fake_connected") except Exception: pass
def test_check_structure(self): self.test_targets.append(sync_f.check_table) self.test_targets.append(sync_f.check_table_fields) self.test_targets.append(sync_f.create_table) cursor = database_f.get_test_cursor() # Just incase it's lingering from a previous test try: cursor.execute("DROP TABLE fake_connected") except Exception: pass r = sync_f.check_table(cursor, fake_info, fix=False, show_fixes=True, die_queitly=False) self.assertEqual(r, """create table fake_connected ( s1 Serial NOT NULL, v1 varchar(255) NOT NULL default 'v1', v2 varchar(255) NOT NULL default 'v2', i1 integer NOT NULL default '1', i2 integer NOT NULL default '2', t1 text NOT NULL default 't1', t2 text NOT NULL default 't2', d1 double precision NOT NULL default '1.1', b1 boolean NOT NULL default 'True', afield integer[] NOT NULL default '{0}', bfield integer[] NOT NULL default '{1,2}', date_field date NOT NULL default '2000-10-30', time_field timestamp without time zone NOT NULL default '2000-10-30 12:50:59', primary key (s1) ) \033[31mTable missing\033[30;0m (fake_connected)""") # Now we actually fix it sync_f.check_table(cursor, fake_info, fix=True, show_fixes=False, die_queitly=False) # And make sure it's done properly r = sync_f.check_table(cursor, fake_info, fix=False, show_fixes=True, die_queitly=False) self.assertEqual("\033[32mfake_connected is correct\033[30;0m", r) # TEST CHECK_TABLE_FIELDS #------------------------ # Start by making sure it shows as correct r = sync_f.check_table_fields(cursor, fake_info, fix=False, show_fixes=False) self.assertEqual("\033[32mfake_connected is correct\033[30;0m", r) # Drop a field from the table to make sure we'll fix it query = """ALTER TABLE fake_connected DROP COLUMN t1;""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) r = sync_f.check_table_fields(cursor, fake_info, fix=False, show_fixes=True) self.assertEqual("""\033[31mcolumn missing in fake_connected\033[30;0m (t1) Fixes: alter table fake_connected add column t1 text NOT NULL default 't1' """, r) # Drop the table, just to make sure it doesn't mess with anything else query = """DROP TABLE fake_connected""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))