Skip to content

Commit

Permalink
Merge pull request #3 from baluyotraf/feature/tests
Browse files Browse the repository at this point in the history
Add initial test conditions
  • Loading branch information
baluyotraf authored Mar 21, 2024
2 parents b6d91fe + 2e4e54c commit bff36fa
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pydantic = "^2.6.4"

[tool.poetry.group.dev.dependencies]
pyright = "^1.1.354"
pytest = "^8.1.1"
pytest-sugar = "^1.0.0"

[tool.poetry.group.docs]
optional = true
Expand All @@ -38,6 +40,26 @@ mkdocstrings = { version="^0.24.1", extras = ["python"] }
mkdocs-include-markdown-plugin = "^6.0.4"
mkdocs-material = { version = "^9.5.14", extras = ["imaging"] }

[tool.pytest.ini_options]
minversion = 8.0
testpaths = ["tests"]

[tool.tox]
legacy_tox_ini = """
[tox]
min_version = 4.0
env_list = py{39,310,311,312}
[testenv]
description = run the tests with pytest
allowlist_externals =
poetry
commands_pre =
poetry install
commands =
pytest tests
"""

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Testing module."""
65 changes: 65 additions & 0 deletions tests/queries.py
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'""",
)
]
22 changes: 22 additions & 0 deletions tests/test_translation.py
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)
18 changes: 18 additions & 0 deletions tests/utils.py
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

0 comments on commit bff36fa

Please sign in to comment.