Skip to content

Commit

Permalink
SQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
inuur committed Mar 23, 2022
1 parent 85e3161 commit 1c03e3c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hstest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'DjangoTest',
'FlaskTest',
'PlottingTest',
'SQLTest',

'TestCase',
'SimpleTestCase',
Expand All @@ -24,6 +25,7 @@
from hstest.stage import DjangoTest
from hstest.stage import FlaskTest
from hstest.stage import StageTest
from hstest.stage import SQLTest
from hstest.test_case import CheckResult
from hstest.test_case import SimpleTestCase
from hstest.test_case import TestCase
Expand Down
2 changes: 2 additions & 0 deletions hstest/stage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
'DjangoTest',
'FlaskTest',
'PlottingTest',
'SQLTest'
]

from hstest.stage.django_test import DjangoTest
from hstest.stage.flask_test import FlaskTest
from hstest.stage.stage_test import StageTest
from hstest.stage.sql_test import SQLTest

try:
from hstest.stage.plotting_test import PlottingTest
Expand Down
39 changes: 39 additions & 0 deletions hstest/stage/sql_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

from exception.outcomes import ErrorWithFeedback, WrongAnswer
from hstest.stage.stage_test import StageTest
from hstest.testing.runner.sql_runner import SQLRunner
import re


class SQLTest(StageTest):
runner = SQLRunner()
queries = dict()

def run_tests(self, *, debug=False, is_unittest: bool = False):
self.find_sql_files()
return super(SQLTest, self).run_tests()

def find_sql_files(self):
root_folder = os.getcwd()

is_sql_file_found: bool = False

for file in os.listdir(root_folder):
if file.endswith('.sql'):
is_sql_file_found = True
self.parse_sql_file(file)
if not is_sql_file_found:
raise ErrorWithFeedback("Can't find any SQL file!")

def parse_sql_file(self, file_name: str) -> None:
file_path = os.path.join(os.getcwd(), file_name)

with open(file_path, 'r') as file:
lines = file.readlines()
sql_content = " ".join(lines).replace("\n", "")
commands = re.findall("(\\w+)\\s+?=\\s+?\"(.+?)\"", sql_content)

for (name, query) in commands:
if name in self.queries:
self.queries[name] = query
21 changes: 21 additions & 0 deletions hstest/testing/runner/sql_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import typing

from hstest.test_case.check_result import CheckResult
from hstest.testing.runner.test_runner import TestRunner

if typing.TYPE_CHECKING:
from hstest.testing.test_run import TestRun


class SQLRunner(TestRunner):

def test(self, test_run: 'TestRun'):
test_case = test_run.test_case

try:
result = test_case.dynamic_testing()
return result
except BaseException as ex:
test_run.set_error_in_test(ex)

return CheckResult.from_error(test_run.error_in_test)
5 changes: 5 additions & 0 deletions tests/projects/sql/main.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test = "UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1";

create_table = "CREATE table";
16 changes: 16 additions & 0 deletions tests/projects/sql/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from hstest import SQLTest, dynamic_test, correct, wrong


class TestSQLProject(SQLTest):

queries = {
'create_table': None,
'test': None
}

@dynamic_test()
def simple_test(self):
for query in self.queries:
if self.queries[query] is None:
return wrong(f"Can't find '{query}' query from SQL files!")
return correct()

0 comments on commit 1c03e3c

Please sign in to comment.