-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A couple of sa dialect tests for postgresdql and mysql from legacy (#116
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
lib/dl_connector_mysql/dl_connector_mysql_tests/db/core/test_sa_dialect.py
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,42 @@ | ||
import datetime | ||
|
||
import pytest | ||
import sqlalchemy as sa | ||
import sqlalchemy.dialects.mysql | ||
import sqlalchemy.sql.sqltypes | ||
|
||
from dl_connector_mysql_tests.db.core.base import BaseMySQLTestClass | ||
|
||
|
||
class TestMySQLSaDialect(BaseMySQLTestClass): | ||
@pytest.mark.parametrize( | ||
("value", "type_", "expected"), | ||
( | ||
pytest.param( | ||
datetime.date(2022, 1, 2), sqlalchemy.sql.sqltypes.Date(), datetime.date(2022, 1, 2), id="date-as-date" | ||
), | ||
pytest.param( | ||
"2022-01-02", sqlalchemy.dialects.mysql.DATE(), datetime.date(2022, 1, 2), id="date-as-string" | ||
), | ||
pytest.param( | ||
datetime.datetime(2022, 1, 2, 12, 59, 59), | ||
sqlalchemy.sql.sqltypes.DateTime(), | ||
datetime.datetime(2022, 1, 2, 12, 59, 59), | ||
id="datetime-as-datetime", | ||
), | ||
pytest.param( | ||
"2022-01-02T12:59:59", | ||
sqlalchemy.dialects.mysql.DATETIME(fsp=6), | ||
datetime.datetime(2022, 1, 2, 12, 59, 59), | ||
id="datetime-as-string", | ||
), | ||
), | ||
) | ||
def test_mysql_literal_bind_datetimes(self, value, type_, expected, db): | ||
execute = db.execute | ||
dialect = db._engine_wrapper.dialect | ||
|
||
query = sa.select([sa.literal(value, type_=type_)]) | ||
compiled = str(query.compile(dialect=dialect, compile_kwargs={"literal_binds": True})) | ||
res_literal = list(execute(compiled)) | ||
assert res_literal[0][0] == expected |
40 changes: 40 additions & 0 deletions
40
lib/dl_connector_postgresql/dl_connector_postgresql_tests/db/core/test_sa_dialect.py
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,40 @@ | ||
import datetime | ||
|
||
import pytest | ||
import pytz | ||
import sqlalchemy as sa | ||
|
||
from dl_connector_postgresql_tests.db.core.base import BasePostgreSQLTestClass | ||
|
||
|
||
TEST_VALUES = [datetime.date(2020, 1, 1)] + [ | ||
datetime.datetime(2020, idx1 + 1, idx2 + 1, 3, 4, 5, us).replace(tzinfo=tzinfo) | ||
for idx1, us in enumerate((0, 123356)) | ||
for idx2, tzinfo in enumerate( | ||
( | ||
None, | ||
datetime.timezone.utc, | ||
pytz.timezone("America/New_York"), | ||
) | ||
) | ||
] | ||
|
||
|
||
class TestPostgresqlSaDialect(BasePostgreSQLTestClass): | ||
@pytest.mark.parametrize("value", TEST_VALUES, ids=[val.isoformat() for val in TEST_VALUES]) | ||
def test_pg_literal_bind_datetimes(self, value, db): | ||
""" | ||
Test that query results for literal_binds matches the query results without, | ||
for the custom dialect code. | ||
This test should be in the bi_postgresql dialect itself, but it doesn't have | ||
a postgres-using test at the moment. | ||
""" | ||
execute = db.execute | ||
dialect = db._engine_wrapper.dialect | ||
|
||
query = sa.select([sa.literal(value)]) | ||
compiled = str(query.compile(dialect=dialect, compile_kwargs={"literal_binds": True})) | ||
res_direct = list(execute(query)) | ||
res_literal = list(execute(compiled)) | ||
assert res_direct == res_literal, dict(literal_query=compiled) |