-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
2,480 additions
and
56 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
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
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,37 @@ | ||
sql\_mock.redshift package | ||
========================== | ||
|
||
Submodules | ||
---------- | ||
|
||
sql\_mock.redshift.column\_mocks module | ||
--------------------------------------- | ||
|
||
.. automodule:: sql_mock.redshift.column_mocks | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
sql\_mock.redshift.settings module | ||
---------------------------------- | ||
|
||
.. automodule:: sql_mock.redshift.settings | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
sql\_mock.redshift.table\_mocks module | ||
-------------------------------------- | ||
|
||
.. automodule:: sql_mock.redshift.table_mocks | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: sql_mock.redshift | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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,37 @@ | ||
sql\_mock.snowflake package | ||
=========================== | ||
|
||
Submodules | ||
---------- | ||
|
||
sql\_mock.snowflake.column\_mocks module | ||
---------------------------------------- | ||
|
||
.. automodule:: sql_mock.snowflake.column_mocks | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
sql\_mock.snowflake.settings module | ||
----------------------------------- | ||
|
||
.. automodule:: sql_mock.snowflake.settings | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
sql\_mock.snowflake.table\_mocks module | ||
--------------------------------------- | ||
|
||
.. automodule:: sql_mock.snowflake.table_mocks | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: sql_mock.snowflake | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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,61 @@ | ||
```{toctree} | ||
:maxdepth: 2 | ||
``` | ||
|
||
# Example: Testing Subscription Counts in ClickHouse | ||
|
||
```python | ||
import datetime | ||
from sql_mock.redshift import column_mocks as col | ||
from sql_mock.redshift.table_mocks import RedshiftMockTable | ||
from sql_mock.table_mocks import table_meta | ||
|
||
# Define mock tables for your data model that inherit from RedshiftMockTable | ||
@table_meta(table_ref="data.users") | ||
class UserTable(RedshiftMockTable): | ||
user_id = col.INTEGER(default=1) | ||
user_name = col.VARCHAR(default="Mr. T") | ||
|
||
|
||
@table_meta(table_ref="data.subscriptions") | ||
class SubscriptionTable(RedshiftMockTable): | ||
subscription_id = col.INTEGER(default=1) | ||
period_start_date = col.DATE(default=datetime.date(2023, 9, 5)) | ||
period_end_date = col.DATE(default=datetime.date(2023, 9, 5)) | ||
user_id = col.INTEGER(default=1) | ||
|
||
# Define a mock table for your expected results | ||
class SubscriptionCountTable(RedshiftMockTable): | ||
subscription_count = col.INTEGER(default=1) | ||
user_id = col.INTEGER(default=1) | ||
|
||
# Your original SQL query | ||
query = """ | ||
SELECT | ||
count(*) AS subscription_count, | ||
user_id | ||
FROM data.users | ||
LEFT JOIN data.subscriptions USING(user_id) | ||
GROUP BY user_id | ||
""" | ||
|
||
# Create mock data for the 'data.users' and 'data.subscriptions' tables | ||
users = UserTable.from_dicts([{'user_id': 1}, {'user_id': 2}]) | ||
subscriptions = SubscriptionTable.from_dicts([ | ||
{'subscription_id': 1, 'user_id': 1}, | ||
{'subscription_id': 2, 'user_id': 1}, | ||
{'subscription_id': 2, 'user_id': 2}, | ||
]) | ||
|
||
# Define your expected results | ||
expected = [ | ||
{'user_id': 1, 'subscription_count': 2}, | ||
{'user_id': 2, 'subscription_count': 1} | ||
] | ||
|
||
# Simulate the SQL query using SQL Mock | ||
res = SubscriptionCountTable.from_mocks(query=query, input_data=[users, subscriptions]) | ||
|
||
# Assert the results | ||
res.assert_equal(expected) | ||
``` |
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,10 @@ | ||
Redshift | ||
===================== | ||
|
||
This section documents the specifics on how to use SQL Mock with Redshift | ||
|
||
.. toctree:: | ||
:maxdepth: 4 | ||
|
||
./settings.md | ||
./examples.md |
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,14 @@ | ||
```{toctree} | ||
:maxdepth: 2 | ||
``` | ||
|
||
# Settings | ||
|
||
In order to use SQL Mock with Redshift, you need to provide the following environment variables when you run tests: | ||
|
||
* `SQL_MOCK_REDSHIFT_HOST`: The host of your Redshift instance | ||
* `SQL_MOCK_REDSHIFT_USER`: The user of your Redshift instance | ||
* `SQL_MOCK_REDSHIFT_PASSWORD`: The password of your Redshift instance | ||
* `SQL_MOCK_REDSHIFT_PORT`: The port of your Redshift instance | ||
|
||
Having those environment variables enables SQL Mock to connect to your Redshift instance. |
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,63 @@ | ||
```{toctree} | ||
:maxdepth: 2 | ||
``` | ||
|
||
# Example: Testing Subscription Counts in Snowflake | ||
|
||
```python | ||
import datetime | ||
from sql_mock.snowflake import column_mocks as col | ||
from sql_mock.snowflake.table_mocks import SnowflakeMockTable | ||
from sql_mock.table_mocks import table_meta | ||
|
||
# Define mock tables for your data model that inherit from SnowflakeMockTable | ||
@table_meta(table_ref="data.users") | ||
class UserTable(SnowflakeMockTable): | ||
user_id = col.INTEGER(default=1) | ||
user_name = col.STRING(default="Mr. T") | ||
|
||
|
||
@table_meta(table_ref="data.subscriptions") | ||
class SubscriptionTable(SnowflakeMockTable): | ||
subscription_id = col.INTEGER(default=1) | ||
period_start_date = col.DATE(default=datetime.date(2023, 9, 5)) | ||
period_end_date = col.DATE(default=datetime.date(2023, 9, 5)) | ||
user_id = col.INTEGER(default=1) | ||
|
||
|
||
# Define a mock table for your expected results | ||
class SubscriptionCountTable(SnowflakeMockTable): | ||
subscription_count = col.INTEGER(default=1) | ||
user_id = col.INTEGER(default=1) | ||
|
||
# Your original SQL query | ||
query = """ | ||
SELECT | ||
count(*) AS subscription_count, | ||
user_id | ||
FROM data.users | ||
LEFT JOIN data.subscriptions USING(user_id) | ||
GROUP BY user_id | ||
""" | ||
|
||
def test_something(): | ||
# Create mock data for the 'data.users' and 'data.subscriptions' tables | ||
users = UserTable.from_dicts([{'user_id': 1}, {'user_id': 2}]) | ||
subscriptions = SubscriptionTable.from_dicts([ | ||
{'subscription_id': 1, 'user_id': 1}, | ||
{'subscription_id': 2, 'user_id': 1}, | ||
{'subscription_id': 2, 'user_id': 2}, | ||
]) | ||
|
||
# Define your expected results | ||
expected = [ | ||
{"USER_ID": 1, "SUBSCRIPTION_COUNT": 2}, | ||
{"USER_ID": 2, "SUBSCRIPTION_COUNT": 1}, | ||
] | ||
|
||
# Simulate the SQL query using SQL Mock | ||
res = SubscriptionCountTable.from_mocks(query=query, input_data=[users, subscriptions]) | ||
|
||
# Assert the results | ||
res.assert_equal(expected) | ||
``` |
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,10 @@ | ||
Snowflake | ||
===================== | ||
|
||
This section documents the specifics on how to use SQL Mock with Snowflake | ||
|
||
.. toctree:: | ||
:maxdepth: 4 | ||
|
||
./settings.md | ||
./examples.md |
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,13 @@ | ||
```{toctree} | ||
:maxdepth: 2 | ||
``` | ||
|
||
# Settings | ||
|
||
In order to use SQL Mock with Snowflake, you need to provide the following environment variables when you run tests: | ||
|
||
* `SQL_MOCK_SNOWFLAKE_ACCOUNT`: The name of your Snowflake account | ||
* `SQL_MOCK_SNOWFLAKE_USER`: The name of your Snowflake user | ||
* `SQL_MOCK_SNOWFLAKE_PASSWORD`: The password for your Snowflake user | ||
|
||
Having those environment variables enables SQL Mock to connect to your Snowflake instance. |
Oops, something went wrong.