Skip to content

Commit

Permalink
Release/v0.5.0 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Somtom authored Jan 7, 2024
1 parent 306394f commit 34c3723
Show file tree
Hide file tree
Showing 49 changed files with 2,480 additions and 56 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed

### Fixed

## [0.5.0]

**Full Changelog**: https://github.com/DeepLcom/sql-mock/compare/v0.4.0...v0.5.0

### Added

* Support for Redshift
* Support for Snowflake

### Changed

## [0.4.0]

### Added
Expand Down Expand Up @@ -71,7 +79,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.4.0...HEAD
[Unreleased]: https://github.com/DeepLcom/sql-mock/compare/v0.5.0...HEAD
[0.5.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.5.0
[0.4.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.4.0
[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
Expand Down
8 changes: 4 additions & 4 deletions docs/_sources/faq.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

We are planning to add more and more supported database systems. However, if your system is not supported yet, you can still use SQL Mock. There are only 2 things you need to do:

### Create your `MockTable` class
### Create your `MockTable` class

First, you need to create a `MockTable` class for your database system that inherits from `sql_mock.table_mocks.BaseMockTable`.

Expand All @@ -27,7 +27,7 @@ from sql_mock.column_mocks import ColumnMock

class MyFanceDatabaseColumnMock(ColumnMock):
# In case you need some specific logic that overwrites the default behavior, you can do so here
pass
pass

class Int(MyFanceDatabaseColumnMock):
dtype = "Integer"
Expand All @@ -44,13 +44,13 @@ Feel free to create a PR on this repository that we can start supporting your da

## I am missing a specific ColumnMock type for my model fields

We implementd some basic column types but it could happen that you don't find the one you need.
We implemented some basic column types but it could happen that you don't find the one you need.
Luckily, you can easily create those with the tools provided.
The only thing you need to do is to inherit from the `ColumnMock` that is specific to your database system (e.g. `BigQueryColumnMock`) and write classes for the column mocks you are missing. Usually you only need to set the correct `dtype`. This would later be used in the `cast(col to <dtype>)` expression.

```python
# Replace the import with the database system you are using
from sql_mock.bigquery.column_mock import BigQueryColumnMock
from sql_mock.bigquery.column_mock import BigQueryColumnMock

class MyFancyMissingColType(BigQueryColumnMock):
dtype = "FancyMissingColType"
Expand Down
6 changes: 6 additions & 0 deletions docs/_sources/getting_started/installation.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pip install --upgrade "sql-mock[bigquery]"

# Clickhouse
pip install --upgrade "sql-mock[clickhouse]"

# Redshift
pip install --upgrade "sql-mock[redshift]"

# Snowflake
pip install --upgrade "sql-mock[snowflake]"
```

If you need to modify this source code, install the dependencies using poetry:
Expand Down
8 changes: 5 additions & 3 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Welcome to SQL Mock's documentation!
====================================

The primary purpose of this library is to simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios.
The primary purpose of this library is to 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.

.. meta::
Expand All @@ -27,7 +27,7 @@ It provides a consistent and convenient way to test the execution of your query

.. toctree::
:maxdepth: 3
:caption: Basic Usage
:caption: Basic Usage

usage/defining_table_mocks
usage/dbt
Expand All @@ -38,10 +38,12 @@ It provides a consistent and convenient way to test the execution of your query

.. toctree::
:maxdepth: 3
:caption: Database Specifics
:caption: Database Specifics

usage/bigquery/index
usage/clickhouse/index
usage/redshift/index
usage/snowflake/index

.. toctree::
:maxdepth: 3
Expand Down
37 changes: 37 additions & 0 deletions docs/_sources/sql_mock.redshift.rst.txt
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:
37 changes: 37 additions & 0 deletions docs/_sources/sql_mock.snowflake.rst.txt
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:
61 changes: 61 additions & 0 deletions docs/_sources/usage/redshift/examples.md.txt
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)
```
10 changes: 10 additions & 0 deletions docs/_sources/usage/redshift/index.rst.txt
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
14 changes: 14 additions & 0 deletions docs/_sources/usage/redshift/settings.md.txt
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.
63 changes: 63 additions & 0 deletions docs/_sources/usage/snowflake/examples.md.txt
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)
```
10 changes: 10 additions & 0 deletions docs/_sources/usage/snowflake/index.rst.txt
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
13 changes: 13 additions & 0 deletions docs/_sources/usage/snowflake/settings.md.txt
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.
Loading

0 comments on commit 34c3723

Please sign in to comment.