Skip to content

Commit

Permalink
Merge pull request #18 from DeepLcom/fix/sql-dialect
Browse files Browse the repository at this point in the history
Fix/sql dialect
  • Loading branch information
Somtom authored Nov 17, 2023
2 parents e324303 + eaca4f5 commit a12d9b5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 12 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed


## [0.3.1]

### Fixed
- MockTable classes now have a `_sql_dialect` attribute that is used with `sglglot` for more reliable dialect conversions


## [0.3.0]

**Full Changelog**: https://github.com/DeepLcom/sql-mock/compare/v0.2.0...v0.3.0
Expand Down Expand Up @@ -47,7 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.2] - 2023-10-26
Initial version.

[Unreleased]: https://github.com/DeepLcom/sql-mock/compare/v0.3.0...HEAD
[Unreleased]: https://github.com/DeepLcom/sql-mock/compare/v0.3.1...HEAD
[0.3.1]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.3.1
[0.3.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.3.0
[0.2.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.2.0
[0.1.2]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.1.2
4 changes: 3 additions & 1 deletion docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ <h1 id="index">Index</h1>
<h2 id="_">_</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_data">_sql_mock_data (sql_mock.table_mocks.BaseMockTable attribute)</a>
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_dialect">_sql_dialect (sql_mock.table_mocks.BaseMockTable attribute)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_data">_sql_mock_data (sql_mock.table_mocks.BaseMockTable attribute)</a>
</li>
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_meta">_sql_mock_meta (sql_mock.table_mocks.BaseMockTable attribute)</a>
</li>
</ul></td>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/sitemap.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version='1.0' encoding='utf-8'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://deeplcom.github.io/sql-mock/en/faq.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/getting_started/installation.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/getting_started/quickstart.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/modules.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/robots.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/settings.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/settings.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/defining_table_mocks.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/result_assertion.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/your_sql_query_to_test.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/genindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/py-modindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/search.html</loc></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://deeplcom.github.io/sql-mock/en/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/modules.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/genindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/py-modindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/search.html</loc></url></urlset>
14 changes: 13 additions & 1 deletion docs/sql_mock.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Link to this headi
</dl>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="sql_mock.table_mocks.BaseMockTable._sql_dialect">
<span class="sig-name descname"><span class="pre">_sql_dialect</span></span><a class="headerlink" href="#sql_mock.table_mocks.BaseMockTable._sql_dialect" title="Link to this definition"></a></dt>
<dd><p>The sql dialect that the mock model uses. It will be leveraged by sqlglot.</p>
<dl class="field-list simple">
<dt class="field-odd">Type<span class="colon">:</span></dt>
<dd class="field-odd"><p>str</p>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sql_mock.table_mocks.BaseMockTable.as_sql_input">
<span class="sig-name descname"><span class="pre">as_sql_input</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.BaseMockTable.as_sql_input" title="Link to this definition"></a></dt>
Expand Down Expand Up @@ -529,14 +540,15 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Link to this headi

<dl class="py function">
<dt class="sig sig-object py" id="sql_mock.table_mocks.select_from_cte">
<span class="sig-prename descclassname"><span class="pre">sql_mock.table_mocks.</span></span><span class="sig-name descname"><span class="pre">select_from_cte</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">query</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.select_from_cte" title="Link to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">sql_mock.table_mocks.</span></span><span class="sig-name descname"><span class="pre">select_from_cte</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">query</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sql_dialect</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.select_from_cte" title="Link to this definition"></a></dt>
<dd><p>If selecting from a CTE, we need to replace the the final SELECT statement
with a SELECT * FROM select_cte</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>query</strong> (<em>str</em>) – Original SQL query</p></li>
<li><p><strong>cte_name</strong> (<em>str</em>) – Name of the CTE to select from</p></li>
<li><p><strong>sql_dialect</strong> (<em>str</em>) – The sql dialect to use for generating the query</p></li>
</ul>
</dd>
</dl>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[tool.poetry]
name = "sql-mock"
version = "0.3.0"
version = "0.3.1"
description = "Simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios. It provides a consistent and convenient way to test the execution of your query without the need to process a massive amount of data."
repository = "https://github.com/DeepLcom/sql-mock"
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions src/sql_mock/bigquery/table_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


class BigQueryMockTable(BaseMockTable):
_sql_dialect = "bigquery"

def __init__(
self,
*args,
Expand Down
2 changes: 2 additions & 0 deletions src/sql_mock/clickhouse/table_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


class ClickHouseTableMock(BaseMockTable):
_sql_dialect = "clickhouse"

def __init__(
self,
*args,
Expand Down
9 changes: 6 additions & 3 deletions src/sql_mock/table_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ def replace_original_table_references(query: str, mock_tables: list["BaseMockTab
return query


def select_from_cte(query: str, cte_name: str):
def select_from_cte(query: str, cte_name: str, sql_dialect: str):
"""
If selecting from a CTE, we need to replace the the final SELECT statement
with a SELECT * FROM select_cte
Args:
query (str): Original SQL query
cte_name (str): Name of the CTE to select from
sql_dialect (str): The sql dialect to use for generating the query
"""
ast = sqlglot.parse_one(query)

Expand All @@ -52,7 +53,7 @@ def select_from_cte(query: str, cte_name: str):
col.pop()

# Change the final select statement to SELECT * FROM <cte_name>
adjusted_query = ast.select("*").from_(cte_name).sql(pretty=True)
adjusted_query = ast.select("*").from_(cte_name).sql(pretty=True, dialect=sql_dialect)
return adjusted_query


Expand Down Expand Up @@ -131,10 +132,12 @@ class BaseMockTable:
Attributes:
_sql_mock_data (SQLMockData): A class that stores data which is for processing. This is automatcially created on instantiation.
_sql_mock_meta (MockTableMeta): A class attribute to store table metadata. It is created using the `table_meta` decorator.
_sql_dialect (str): The sql dialect that the mock model uses. It will be leveraged by sqlglot.
"""

_sql_mock_meta: MockTableMeta = None
_sql_mock_data: SQLMockData = None
_sql_dialect: str = None

def __init__(self, data: list[dict] = None) -> None:
"""
Expand Down Expand Up @@ -213,7 +216,7 @@ def _generate_query(
result_query = self._sql_mock_data.rendered_query

if cte_to_select is not None:
result_query = select_from_cte(result_query, cte_to_select)
result_query = select_from_cte(result_query, cte_to_select, sql_dialect=self._sql_dialect)
final_columns_to_select = "*"
else:
final_columns_to_select = ",\n".join(
Expand Down
9 changes: 6 additions & 3 deletions tests/test_table_mocks/test_generate_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class StringTestColumn(ColumnMock):
class MockTestTable(BaseMockTable):
col1 = int_col
col2 = string_col
_sql_dialect = "bigquery"


def test_replace_original_table_references_when_reference_exists():
Expand Down Expand Up @@ -74,7 +75,7 @@ def test_select_from_cte_when_cte_exists():
# Make sure we match the query format
expected = expected.sql(pretty=True)

assert expected == select_from_cte(query, cte_name)
assert expected == select_from_cte(query, cte_name, sql_dialect="bigquery")


def test_select_from_cte_when_cte_does_not_exist():
Expand All @@ -91,7 +92,7 @@ def test_select_from_cte_when_cte_does_not_exist():
"""

with pytest.raises(ValueError):
select_from_cte(query, cte_name)
select_from_cte(query, cte_name, sql_dialect="bigquery")


# Test the _generate_query method
Expand Down Expand Up @@ -176,7 +177,9 @@ def test_generate_query_cte_provided(mocker):
query = mock_table_instance._generate_query(cte_to_select=cte_to_select)

# Asserts
mocked_select_from_cte.assert_called_once_with(original_query, cte_to_select)
mocked_select_from_cte.assert_called_once_with(
original_query, cte_to_select, sql_dialect=MockTestTable._sql_dialect
)
mocked_replace_original_table_references.assert_called_once_with(
expected_query_template_result, mock_tables=[mock_table_instance]
)
Expand Down

0 comments on commit a12d9b5

Please sign in to comment.