-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from baluyotraf/feature/tests
Add initial test conditions
- Loading branch information
Showing
5 changed files
with
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Testing module.""" |
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,65 @@ | ||
"""Defines the queries for testing.""" | ||
|
||
import dataclasses as dc | ||
from typing import Any, Literal | ||
|
||
import altqq | ||
|
||
|
||
@dc.dataclass | ||
class TestQuery: | ||
"""Defines the conversion of query to different translations.""" | ||
|
||
query: altqq.Query | ||
pyodbc: altqq.PyODBCQuery | ||
plain_text: str | ||
|
||
|
||
class SelectTableByFilter(altqq.Query): | ||
"""Test query that selects and filters a table.""" | ||
|
||
__query__ = """ | ||
SELECT * FROM "{table}" | ||
WHERE "{filter_column}" = {filter_value} | ||
""" | ||
|
||
table: altqq.NonParameter[str] | ||
filter_column: altqq.NonParameter[str] | ||
filter_value: Any | ||
|
||
|
||
class OrderQuery(altqq.Query): | ||
"""Test query that orders a query.""" | ||
|
||
__query__ = """ | ||
SELECT * FROM ({subquery}) AS tbl | ||
ORDER BY "{order_column}" {order} | ||
""" | ||
|
||
subquery: altqq.Query | ||
order_column: altqq.NonParameter[str] | ||
order: altqq.NonParameter[Literal["asc", "desc"]] | ||
|
||
|
||
class UnionAllQuery(altqq.Query): | ||
"""Test query that union all two queries.""" | ||
|
||
__query__ = """ | ||
SELECT * FROM ({query1}) AS tbl1 | ||
UNION ALL | ||
SELECT * FROM ({query2}) AS tbl2 | ||
""" | ||
|
||
query1: altqq.Query | ||
query2: altqq.Query | ||
|
||
|
||
TEST_DATA = [ | ||
TestQuery( | ||
SelectTableByFilter("Users", "name", "arietta"), | ||
pyodbc=altqq.PyODBCQuery( | ||
query="""SELECT * FROM "Users" WHERE "name" = ?""", parameters=["arietta"] | ||
), | ||
plain_text="""SELECT * FROM "Users" WHERE "name" = 'arietta'""", | ||
) | ||
] |
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,22 @@ | ||
"""Tests the translations of queries.""" | ||
|
||
import altqq | ||
import pytest | ||
|
||
from tests.queries import TEST_DATA, TestQuery | ||
from tests.utils import clean_whitespaces as cws | ||
|
||
|
||
@pytest.mark.parametrize("test_query", TEST_DATA) | ||
def test_to_pyodbc__proper_query__correct_pyodbc_object(test_query: TestQuery): | ||
"""If the query parameters are correct, the pyodbc object is returned.""" | ||
res = altqq.to_pyodbc(test_query.query) | ||
assert cws(test_query.pyodbc.query) == cws(res.query) | ||
assert test_query.pyodbc.parameters == res.parameters | ||
|
||
|
||
@pytest.mark.parametrize("test_query", TEST_DATA) | ||
def test_to_plain_text__proper_query__correct_sqlt(test_query: TestQuery): | ||
"""If the query parameters are correct, the sql is returned.""" | ||
sql = altqq.to_plain_text(test_query.query) | ||
assert cws(test_query.plain_text) == cws(sql) |
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,18 @@ | ||
"""Utility test methods that do not belong to other groups.""" | ||
|
||
import re | ||
|
||
|
||
def clean_whitespaces(text: str) -> str: | ||
"""Standardize the white spaces in a text. | ||
Args: | ||
text (str): Text to standardize whitespace. | ||
Returns: | ||
str: Text with a standardized whitespace. | ||
""" | ||
strip = text.strip() | ||
split = re.split(r"\s+", strip) | ||
join = " ".join(split) | ||
return join |