-
Notifications
You must be signed in to change notification settings - Fork 135
Parameterized queries: Add e2e tests for parameterized queries #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd070da
Add e2e tests for parameterized queries
3ce76ce
Run isort on test file for clarity
c37f237
pr check: black the codebase
b5e0726
pr check: black the codebase
3333ca0
Update tests to check for inferred dates.
554623f
Update type map to allow it to infer datetime.date objects
a8841f8
PR: Split out tests into individual units of work. One test per primi…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -24,9 +24,11 @@ def __some_example_requirement(self): | |
| import sqlalchemy.testing.exclusions | ||
|
|
||
| import logging | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a formatting change unrelated to the rest of this PR. |
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| logger.warning("requirements.py is not currently employed by Databricks dialect") | ||
|
|
||
|
|
||
| class Requirements(sqlalchemy.testing.requirements.SuiteRequirements): | ||
| pass | ||
| pass | ||
This file contains hidden or 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,144 @@ | ||
| import datetime | ||
| from decimal import Decimal | ||
| from typing import Dict, List, Tuple, Union | ||
|
|
||
| import pytz | ||
|
|
||
| from databricks.sql.client import Connection | ||
| from databricks.sql.utils import DbSqlParameter, DbSqlType | ||
|
|
||
|
|
||
| class PySQLParameterizedQueryTestSuiteMixin: | ||
| """Namespace for tests of server-side parameterized queries""" | ||
|
|
||
| QUERY = "SELECT :p AS col" | ||
|
|
||
| def _get_one_result(self, query: str, parameters: Union[Dict, List[Dict]]) -> Tuple: | ||
| with self.connection() as conn: | ||
| with conn.cursor() as cursor: | ||
| cursor.execute(query, parameters=parameters) | ||
| return cursor.fetchone() | ||
|
|
||
| def _quantize(self, input: Union[float, int], place_value=2) -> Decimal: | ||
|
|
||
| return Decimal(str(input)).quantize(Decimal("0." + "0" * place_value)) | ||
|
|
||
| def test_primitive_inferred_bool(self): | ||
|
|
||
| params = {"p": True} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
|
|
||
| def test_primitive_inferred_integer(self): | ||
|
|
||
| params = {"p": 1} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
|
|
||
| def test_primitive_inferred_double(self): | ||
|
|
||
| params = {"p": 3.14} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
|
|
||
| def test_primitive_inferred_date(self): | ||
|
|
||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = {"p": date_value} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_primitive_inferred_timestamp(self): | ||
|
|
||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = {"p": date_value} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_primitive_inferred_string(self): | ||
|
|
||
| params = {"p": "Hello"} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" | ||
|
|
||
| def test_dbsqlparam_inferred_bool(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=True, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
|
|
||
| def test_dbsqlparam_inferred_integer(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=1, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
|
|
||
| def test_dbsqlparam_inferred_double(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=3.14, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
|
|
||
| def test_dbsqlparam_inferred_date(self): | ||
|
|
||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_dbsqlparam_inferred_timestamp(self): | ||
|
|
||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_dbsqlparam_inferred_string(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value="Hello", type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" | ||
|
|
||
| def test_dbsqlparam_explicit_bool(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=True, type=DbSqlType.BOOLEAN)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
|
|
||
| def test_dbsqlparam_explicit_integer(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=1, type=DbSqlType.INTEGER)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
|
|
||
| def test_dbsqlparam_explicit_double(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value=3.14, type=DbSqlType.FLOAT)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
|
|
||
| def test_dbsqlparam_explicit_date(self): | ||
|
|
||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=DbSqlType.DATE)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_dbsqlparam_explicit_timestamp(self): | ||
|
|
||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=DbSqlType.TIMESTAMP)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
|
|
||
| def test_dbsqlparam_explicit_string(self): | ||
|
|
||
| params = [DbSqlParameter(name="p", value="Hello", type=DbSqlType.STRING)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a formatting change unrelated to the bulk of the PR.