-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |