Skip to content

Commit

Permalink
Merge pull request #25 from hyperskill/sql
Browse files Browse the repository at this point in the history
Fix running SQL tests
  • Loading branch information
Vladimir Turov authored Apr 25, 2022
2 parents 7120f5b + db02b1c commit d9d87de
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 6 deletions.
11 changes: 7 additions & 4 deletions hstest/stage/sql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
raise WrongAnswer(f"Error while running '{query_name}': \n\n{ex}")

def executeAndFetchAll(self, query_name: str):
def execute_and_fetch_all(self, query_name: str):
return self.execute(query_name).fetchall()
14 changes: 13 additions & 1 deletion tests/sql/test_db_connection/sqlite/test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
12 changes: 12 additions & 0 deletions tests/sql/test_parsing_sql_files/parsing1/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from hstest import SQLTest, dynamic_test, correct, wrong


Expand All @@ -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()
Original file line number Diff line number Diff line change
@@ -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');"
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from hstest import SQLTest, dynamic_test, correct, wrong


Expand All @@ -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()
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from hstest import SQLTest, dynamic_test, correct, wrong


Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions tests/sql/test_queries/sqlite/create_table/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from hstest import SQLTest, dynamic_test, correct, wrong


Expand All @@ -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()
12 changes: 12 additions & 0 deletions tests/sql/test_queries/sqlite/insert_and_select_data/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from hstest import SQLTest, dynamic_test, correct, wrong


Expand Down Expand Up @@ -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()

0 comments on commit d9d87de

Please sign in to comment.