def test_webpy_page(self): cursor = database_f.get_cursor() origional_require = user.require user.require = emulate_require for page_name in web.page_dict.keys(): if page_name in skip_pages: continue # Reset the CGI form each time so we're not sending data gui_test_utils.new_cgi_form({}) the_page = web.import_page(page_name, handle_exception=False) if page_name in expect_exception: try: self.assertRaises(Exception, the_page.main, cursor) except Exception: print("") print("Page name: %s" % page_name) raise else: try: the_page.main(cursor) except Exception: print("") print(page_name) raise user.require = origional_require
def test_cgi_form_functions(self): self.test_targets.append(common_f.get_val) self.test_targets.append(common_f.print_post_data) gui_test_utils.new_cgi_form(( ("a", 1), ("b", 2), ("c", 3), )) self.assertEqual(common_f.get_val("a"), 1) self.assertEqual(common_f.get_val("b"), 2) self.assertEqual(common_f.get_val("c"), 3) self.assertEqual(common_f.print_post_data(joiner="\n"), """a = 1 b = 2 c = 3""")
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_one_field(self): cursor = database_f.get_cursor() # Test string field gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "username"), ("where", "id = 1"), ("p", False), )) self.assertEqual("admin", get_one_field.main(cursor)) # Test boolean field gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("p", False), )) self.assertEqual(True, get_one_field.main(cursor)) # Now test setting it gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("value", "False"), ("p", False), )) self.assertEqual('False', edit_one_field.main(cursor)) # Get it again gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("p", False), )) self.assertEqual(False, get_one_field.main(cursor))
def test_DBConnectedObject_creation(self): self.test_targets.append(database_f.DBConnectedObject.__eq__) self.test_targets.append(database_f.DBConnectedObject.__str__) self.test_targets.append(database_f.DBConnectedObject.compare) self.test_targets.append(database_f.DBConnectedObject.get_from_form) self.test_targets.append(database_f.DBConnectedObject.get_from_dict) f1 = FakeConnected({ "s1": 1, "v1": "abc", "v2": "xyz", "i1": 1, "i2": 2, "t1": "abc abc", "t2": "xyz xyz", "d1": 1.1, "b1": False, "afield": [1,2,3], "bfield": [1,2,3], # This uses strings "date_field": common_f.convert_date("08-01-1999"), "time_field": common_f.convert_date("08-01-1999 04:05:06"), }) f2 = FakeConnected({ "s1": 2, "v1": "zabc", "v2": "zxyz", "i1": 3, "i2": 4, "t1": "zabc abc", "t2": "zxyz xyz", "d1": 1.2, "b1": True, "afield": [4,2,3], "bfield": [1,2,3], # This uses strings too "date_field": common_f.convert_date("20-02-2000"), "time_field": common_f.convert_date("20-02-2000 23:59:59"), }) gui_test_utils.new_cgi_form(( ("s1", 2), ("v1", "zabc"), ("v2", "zxyz"), ("i1", 3), ("i2", 4), ("t1", "zabc abc"), ("t2", "zxyz xyz"), ("d1", 1.2), ("b1", True), ("afield", "[4,2,3]"), ("bfield", "[1,2,3]"), # This convets strings ("date_field", datetime.date(2000, 2, 20)), ("time_field", datetime.datetime(year=2000, month=2, day=20, hour=23, minute=59, second=59)), )) f3 = FakeConnected() f3.get_from_form(cursor=None, form_list=common_f.cgi_form.list) # Test __eq__ self.assertNotEqual(f1, f2) if f2 != f3: print(f2.compare(f3)) self.assertEqual(f2, f3) # Test __str__ self.assertEqual(str(f1), """s1: 1 v1: abc v2: xyz i1: 1 i2: 2 t1: abc abc t2: xyz xyz d1: 1.1 b1: False afield: [1, 2, 3] bfield: [1, 2, 3] date_field: 1999-01-08 time_field: 1999-01-08 04:05:06""") self.assertEqual(str(f2), """s1: 2 v1: zabc v2: zxyz i1: 3 i2: 4 t1: zabc abc t2: zxyz xyz d1: 1.2 b1: True afield: [4, 2, 3] bfield: [1, 2, 3] date_field: 2000-02-20 time_field: 2000-02-20 23:59:59""") self.assertEqual(str(f3), str(f2)) # Test compare self.assertEqual(f2.compare(f3), "") self.assertEqual(f1.compare(f2), """s1 = 1 vs 2 v1 = abc vs zabc v2 = xyz vs zxyz i1 = 1 vs 3 i2 = 2 vs 4 t1 = abc abc vs zabc abc t2 = xyz xyz vs zxyz xyz d1 = 1.1 vs 1.2 b1 = False vs True afield = [1, 2, 3] vs [4, 2, 3] date_field = 1999-01-08 vs 2000-02-20 time_field = 1999-01-08 04:05:06 vs 2000-02-20 23:59:59""")