From 1d6c48d058e9bb86e2c4ef8206e92b29e04032ff Mon Sep 17 00:00:00 2001 From: Ainur Gimadeev Date: Mon, 25 Apr 2022 13:37:50 +0300 Subject: [PATCH 1/4] Run SQL tests --- tests/sql/test_db_connection/sqlite/test.py | 14 +++++++++++++- tests/sql/test_parsing_sql_files/parsing1/test.py | 12 ++++++++++++ .../execute/test_execute_with_plain_query/test.py | 12 ++++++++++++ .../execute/test_execute_with_query_name/test.py | 12 ++++++++++++ tests/sql/test_queries/sqlite/create_table/test.py | 12 ++++++++++++ .../sqlite/insert_and_select_data/test.py | 12 ++++++++++++ 6 files changed, 73 insertions(+), 1 deletion(-) diff --git a/tests/sql/test_db_connection/sqlite/test.py b/tests/sql/test_db_connection/sqlite/test.py index 47eac12e..f7570cc0 100644 --- a/tests/sql/test_db_connection/sqlite/test.py +++ b/tests/sql/test_db_connection/sqlite/test.py @@ -1,6 +1,8 @@ -from hstest import SQLTest, dynamic_test, correct, wrong +import unittest from sqlite3 import Connection +from hstest import SQLTest, dynamic_test, correct, wrong + class TestSQLProject(SQLTest): @dynamic_test @@ -9,3 +11,13 @@ def test_queries(self): return wrong('SQLite should be used by default') return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_parsing_sql_files/parsing1/test.py b/tests/sql/test_parsing_sql_files/parsing1/test.py index 95e89f6a..027f1eac 100644 --- a/tests/sql/test_parsing_sql_files/parsing1/test.py +++ b/tests/sql/test_parsing_sql_files/parsing1/test.py @@ -1,3 +1,5 @@ +import unittest + from hstest import SQLTest, dynamic_test, correct, wrong @@ -21,3 +23,13 @@ def test_queries(self): return wrong(f"'{query}' is wrong! \n Expected:\n {expected_queries[query]}\n" f"Found:\n{self.queries[query]}") return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_queries/execute/test_execute_with_plain_query/test.py b/tests/sql/test_queries/execute/test_execute_with_plain_query/test.py index 61e192a5..4d331778 100644 --- a/tests/sql/test_queries/execute/test_execute_with_plain_query/test.py +++ b/tests/sql/test_queries/execute/test_execute_with_plain_query/test.py @@ -1,3 +1,5 @@ +import unittest + from hstest import SQLTest, dynamic_test, correct, wrong @@ -22,3 +24,13 @@ def test_queries(self): return wrong("Can't find 'contacts' table in the database") return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_queries/execute/test_execute_with_query_name/test.py b/tests/sql/test_queries/execute/test_execute_with_query_name/test.py index 06c05e73..c43ea689 100644 --- a/tests/sql/test_queries/execute/test_execute_with_query_name/test.py +++ b/tests/sql/test_queries/execute/test_execute_with_query_name/test.py @@ -1,3 +1,5 @@ +import unittest + from hstest import SQLTest, dynamic_test, correct, wrong @@ -16,3 +18,13 @@ def test_queries(self): return wrong("Can't find 'contacts' table in the database") return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_queries/sqlite/create_table/test.py b/tests/sql/test_queries/sqlite/create_table/test.py index 02cdc13a..644b9faa 100644 --- a/tests/sql/test_queries/sqlite/create_table/test.py +++ b/tests/sql/test_queries/sqlite/create_table/test.py @@ -1,3 +1,5 @@ +import unittest + from hstest import SQLTest, dynamic_test, correct, wrong @@ -16,3 +18,13 @@ def test_queries(self): return wrong("Can't find 'contacts' table in the database") return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_queries/sqlite/insert_and_select_data/test.py b/tests/sql/test_queries/sqlite/insert_and_select_data/test.py index 735cb898..a005be51 100644 --- a/tests/sql/test_queries/sqlite/insert_and_select_data/test.py +++ b/tests/sql/test_queries/sqlite/insert_and_select_data/test.py @@ -1,3 +1,5 @@ +import unittest + from hstest import SQLTest, dynamic_test, correct, wrong @@ -29,3 +31,13 @@ def test_insert_data(self): return wrong('Wrong data was inserted!') return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, 0) + + +if __name__ == '__main__': + Test().test() From 30f45ca906a72d120484f65d87b6b670c8da5d98 Mon Sep 17 00:00:00 2001 From: Ainur Gimadeev Date: Mon, 25 Apr 2022 13:49:04 +0300 Subject: [PATCH 2/4] Refactor SQLTest execute method && add test cases --- hstest/stage/sql_test.py | 9 ++++-- .../main.sql | 9 ++++++ .../test.py | 28 +++++++++++++++++++ .../test.py | 2 +- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/main.sql create mode 100644 tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/test.py diff --git a/hstest/stage/sql_test.py b/hstest/stage/sql_test.py index 363599f0..8c06fbb1 100644 --- a/hstest/stage/sql_test.py +++ b/hstest/stage/sql_test.py @@ -12,12 +12,15 @@ def __init__(self, source: str = ''): self.runner = SQLRunner(self) def execute(self, query_name: str): - query = self.queries[query_name] if query_name in self.queries else query_name cursor = self.db.cursor() + + if query_name not in self.queries: + return cursor.execute(query_name) + try: - return cursor.execute(query) + return cursor.execute(self.queries[query_name]) except Exception as ex: raise WrongAnswer(str(ex)) - def executeAndFetchAll(self, query_name: str): + def execute_and_fetch_all(self, query_name: str): return self.execute(query_name).fetchall() diff --git a/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/main.sql b/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/main.sql new file mode 100644 index 00000000..fe445855 --- /dev/null +++ b/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/main.sql @@ -0,0 +1,9 @@ +create_table = "CRE TABLE contacts ( + contact_id INTEGER PRIMARY KEY, + first_name TEXT NOT NULL, + last_name TEXT NOT NULL, + email TEXT NOT NULL UNIQUE, + phone TEXT NOT NULL UNIQUE +);" + +insert_data = "INSERT INTO contacts VALUES(1, 'first_name', 'last_name', 'email', 'phone');" \ No newline at end of file diff --git a/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/test.py b/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/test.py new file mode 100644 index 00000000..ca4a9d57 --- /dev/null +++ b/tests/sql/test_queries/execute/test_execute_query_name_with_wrong_query/test.py @@ -0,0 +1,28 @@ +import unittest + +from hstest import SQLTest, dynamic_test, correct + + +class TestSQLProject(SQLTest): + queries = { + 'create_table': None, + 'insert_data': None + } + + @dynamic_test + def test_create_table(self): + self.execute('create_table') + + return correct() + + +class Test(unittest.TestCase): + def test(self): + status, feedback = TestSQLProject().run_tests() + self.assertEqual(status, -1) + self.assertIn('Wrong answer in test #1', feedback) + self.assertIn('near "CRE": syntax error', feedback) + + +if __name__ == '__main__': + Test().test() diff --git a/tests/sql/test_queries/execute/test_execute_with_wrong_plain_query/test.py b/tests/sql/test_queries/execute/test_execute_with_wrong_plain_query/test.py index dd530f90..69b5294a 100644 --- a/tests/sql/test_queries/execute/test_execute_with_wrong_plain_query/test.py +++ b/tests/sql/test_queries/execute/test_execute_with_wrong_plain_query/test.py @@ -26,5 +26,5 @@ class Test(unittest.TestCase): def test(self): result, feedback = TestSQLProject().run_tests() self.assertEqual(result, -1) - self.assertIn('Wrong answer in test #1', feedback) + self.assertIn('Unexpected error in test #1', feedback) self.assertIn('near "CRE": syntax error', feedback) From 4f660c724345613965c9b72e2525f8ba21439109 Mon Sep 17 00:00:00 2001 From: Ainur Gimadeev Date: Mon, 25 Apr 2022 14:04:49 +0300 Subject: [PATCH 3/4] Change WA feedback --- hstest/stage/sql_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hstest/stage/sql_test.py b/hstest/stage/sql_test.py index 8c06fbb1..03d324e8 100644 --- a/hstest/stage/sql_test.py +++ b/hstest/stage/sql_test.py @@ -20,7 +20,7 @@ def execute(self, query_name: str): try: return cursor.execute(self.queries[query_name]) except Exception as ex: - raise WrongAnswer(str(ex)) + raise WrongAnswer(f"Error while running '{query_name}': \n\n{str(ex)}") def execute_and_fetch_all(self, query_name: str): return self.execute(query_name).fetchall() From db02b1c69850f721c2abf153b1171573cd077d81 Mon Sep 17 00:00:00 2001 From: Ainur Gimadeev Date: Mon, 25 Apr 2022 14:09:45 +0300 Subject: [PATCH 4/4] Remove str convertation --- hstest/stage/sql_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hstest/stage/sql_test.py b/hstest/stage/sql_test.py index 03d324e8..dbd5b43c 100644 --- a/hstest/stage/sql_test.py +++ b/hstest/stage/sql_test.py @@ -20,7 +20,7 @@ def execute(self, query_name: str): try: return cursor.execute(self.queries[query_name]) except Exception as ex: - raise WrongAnswer(f"Error while running '{query_name}': \n\n{str(ex)}") + raise WrongAnswer(f"Error while running '{query_name}': \n\n{ex}") def execute_and_fetch_all(self, query_name: str): return self.execute(query_name).fetchall()