Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Schmidt committed Dec 30, 2023
1 parent 8b007d0 commit e064ab0
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 102 deletions.
1 change: 1 addition & 0 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ It provides a consistent and convenient way to test the execution of your query
:caption: Basic Usage

usage/defining_table_mocks
usage/dbt
usage/your_sql_query_to_test
usage/result_assertion
usage/default_values
Expand Down
130 changes: 130 additions & 0 deletions docs/_sources/usage/dbt.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Use with dbt

## Introduction

This guide will provide a quick start on how to use SQLMock with dbt (data build tool). You can use it to mock dbt models, sources, and seed models. We'll cover how to use these features effectively in your unit tests.

## Prerequisites

- A working dbt project with a `manifest.json` file **that has the latest compiled run.** (make sure to run `dbt compile`).
- The SQLMock library installed in your Python environment.

## Configuration

### Setting the dbt Manifest Path

Initialize your testing environment by setting the global path to your dbt manifest file:

```python
from sql_mock.config import SQLMockConfig

SQLMockConfig.set_dbt_manifest_path('/path/to/your/dbt/manifest.json')
```

## Creating Mock Tables

SQLMock offers specialized decorators for different dbt entities: models, sources, and seeds.

### dbt Model Mock Table

For dbt models, use the `dbt_model_meta` decorator from `sql_mock.dbt`. This decorator is suited for mocking the transformed data produced by dbt models.

```python
from sql_mock.dbt import dbt_model_meta
from sql_mock.bigquery.table_mocks import BigQueryMockTable

@dbt_model_meta(model_name="your_dbt_model_name")
class YourDBTModelTable(BigQueryMockTable):
# Define your table columns and other necessary attributes here
...
```

### dbt Source Mock Table

For dbt sources, use the `dbt_source_meta` decorator from `sql_mock.dbt`. This is ideal for mocking the raw data sources that dbt models consume.

```python
from sql_mock.dbt import dbt_source_meta
from sql_mock.bigquery.table_mocks import BigQueryMockTable

@dbt_source_meta(source_name="your_source_name", table_name="your_source_table")
class YourDBTSourceTable(BigQueryMockTable):
# Define your table columns and other necessary attributes here
...
```

### dbt Seed Mock Table

For dbt seeds, which are static data sets loaded into the database, use the `dbt_seed_meta` decorator from `sql_mock.dbt`.

```python
from sql_mock.dbt import dbt_seed_meta
from sql_mock.bigquery.table_mocks import BigQueryMockTable

@dbt_seed_meta(seed_name="your_dbt_seed_name")
class YourDBTSeedTable(BigQueryMockTable):
# Define your table columns and other necessary attributes here
...
```

## Example: Testing a dbt Model with Upstream Source and Seed Data

Let’s consider a dbt model named `monthly_user_spend` that aggregates data from a source `user_transactions` and a seed `user_categories`.

### Step 1: Define Your Source and Seed Mock Tables

```python
@dbt_source_meta(source_name="transactions", table_name="user_transactions")
class UserTransactionsTable(BigQueryMockTable):
transaction_id = col.Int(default=1)
user_id = col.Int(default=1)
amount = col.Float(default=1.0)
transaction_date = col.Date(default='2023-12-24')

@dbt_seed_meta(seed_name="user_categories")
class UserCategoriesTable(BigQueryMockTable):
user_id = col.Int(default=1)
category = col.String(default='foo')
```

### Step 2: Define Your Model Mock Table

```python
@dbt_model_meta(model_name="monthly_user_spend")
class MonthlyUserSpendTable(BigQueryMockTable):
user_id = col.Int(default=1)
month = col.String(default='foo')
total_spend = col.Float(default=1.0)
category = col.String(default='foo')
```

### Step 3: Write Your Test Case

```python
import datetime

def test_monthly_user_spend_model():
# Mock input data for UserTransactionsTable and UserCategoriesTable
transactions_data = [
{"transaction_id": 1, "user_id": 1, "amount": 120.0, "transaction_date": datetime.date(2023, 1, 10)},
{"transaction_id": 2, "user_id": 2, "amount": 150.0, "transaction_date": datetime.date(2023, 1, 20)},
]

categories_data = [
{"user_id": 1, "category": "Premium"},
{"user_id": 2, "category": "Standard"}
]

transactions_table = UserTransactionsTable.from_dicts(transactions_data)
categories_table = UserCategoriesTable.from_dicts(categories_data)

# Expected result
expected_output = [
{"user_id": 1, "month": "2023-01", "total_spend": 120.0, "category": "Premium"},
{"user_id": 2, "month": "2023-01", "total_spend": 150.0, "category": "Standard"},
]

monthly_spend_table = MonthlyUserSpendTable.from_mocks(input_data=[transactions_table, categories_table])

monthly_spend_table.assert_equal(expected_output)
```
2 changes: 2 additions & 0 deletions docs/faq.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/examples.html">Examples</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Database Specifics</span></p>
Expand Down
21 changes: 3 additions & 18 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Use with dbt</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down Expand Up @@ -121,8 +122,6 @@ <h2 id="_">_</h2>
</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>
</tr></table>
Expand Down Expand Up @@ -182,7 +181,7 @@ <h2 id="C">C</h2>
</li>
<li><a href="sql_mock.html#sql_mock.table_mocks.SQLMockData.columns">columns (sql_mock.table_mocks.SQLMockData attribute)</a>
</li>
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable.cte_name">cte_name (sql_mock.table_mocks.BaseMockTable property)</a>
<li><a href="sql_mock.html#sql_mock.table_mocks.MockTableMeta.cte_name">cte_name (sql_mock.table_mocks.MockTableMeta property)</a>
</li>
</ul></td>
</tr></table>
Expand Down Expand Up @@ -210,7 +209,7 @@ <h2 id="D">D</h2>
</ul></li>
<li><a href="sql_mock.html#id0">default (sql_mock.column_mocks.ColumnMock attribute)</a>, <a href="sql_mock.html#sql_mock.column_mocks.ColumnMock.default">[1]</a>
</li>
<li><a href="sql_mock.html#sql_mock.table_mocks.SQLMockData.default_inputs">default_inputs (sql_mock.table_mocks.SQLMockData attribute)</a>
<li><a href="sql_mock.html#sql_mock.table_mocks.MockTableMeta.default_inputs">default_inputs (sql_mock.table_mocks.MockTableMeta attribute)</a>
</li>
<li><a href="sql_mock.bigquery.html#sql_mock.bigquery.column_mocks.Date.dtype">dtype (sql_mock.bigquery.column_mocks.Date attribute)</a>

Expand Down Expand Up @@ -261,10 +260,6 @@ <h2 id="F">F</h2>

<h2 id="G">G</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.get_keys_from_list_of_dicts">get_keys_from_list_of_dicts() (in module sql_mock.table_mocks)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="sql_mock.bigquery.html#sql_mock.bigquery.settings.BigQuerySettings.google_application_credentials">google_application_credentials (sql_mock.bigquery.settings.BigQuerySettings attribute)</a>
</li>
Expand Down Expand Up @@ -398,19 +393,13 @@ <h2 id="R">R</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.SQLMockData.rendered_query">rendered_query (sql_mock.table_mocks.SQLMockData attribute)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="sql_mock.html#sql_mock.table_mocks.replace_original_table_references">replace_original_table_references() (in module sql_mock.table_mocks)</a>
</li>
</ul></td>
</tr></table>

<h2 id="S">S</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.select_from_cte">select_from_cte() (in module sql_mock.table_mocks)</a>
</li>
<li>
sql_mock

Expand Down Expand Up @@ -549,10 +538,6 @@ <h2 id="U">U</h2>

<h2 id="V">V</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.validate_input_mocks">validate_input_mocks() (in module sql_mock.table_mocks)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="sql_mock.html#sql_mock.exceptions.ValidationError">ValidationError</a>
</li>
Expand Down
2 changes: 2 additions & 0 deletions docs/getting_started/installation.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/default_values.html">Default values</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/examples.html">Examples</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Database Specifics</span></p>
Expand Down
1 change: 1 addition & 0 deletions docs/getting_started/quickstart.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usage/default_values.html">Default values</a></li>
Expand Down
22 changes: 22 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Use with dbt</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down Expand Up @@ -119,6 +120,27 @@ <h1>Welcome to SQL Mock’s documentation!<a class="headerlink" href="#welcome-t
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Use with dbt</a><ul>
<li class="toctree-l2"><a class="reference internal" href="usage/dbt.html#introduction">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="usage/dbt.html#prerequisites">Prerequisites</a></li>
<li class="toctree-l2"><a class="reference internal" href="usage/dbt.html#configuration">Configuration</a><ul>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#setting-the-dbt-manifest-path">Setting the dbt Manifest Path</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="usage/dbt.html#creating-mock-tables">Creating Mock Tables</a><ul>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#dbt-model-mock-table">dbt Model Mock Table</a></li>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#dbt-source-mock-table">dbt Source Mock Table</a></li>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#dbt-seed-mock-table">dbt Seed Mock Table</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="usage/dbt.html#example-testing-a-dbt-model-with-upstream-source-and-seed-data">Example: Testing a dbt Model with Upstream Source and Seed Data</a><ul>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#step-1-define-your-source-and-seed-mock-tables">Step 1: Define Your Source and Seed Mock Tables</a></li>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#step-2-define-your-model-mock-table">Step 2: Define Your Model Mock Table</a></li>
<li class="toctree-l3"><a class="reference internal" href="usage/dbt.html#step-3-write-your-test-case">Step 3: Write Your Test Case</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a><ul>
<li class="toctree-l2"><a class="reference internal" href="usage/your_sql_query_to_test.html#ways-to-provide-your-sql-query-to-be-tested">Ways to provide your SQL query to be tested</a><ul>
<li class="toctree-l3"><a class="reference internal" href="usage/your_sql_query_to_test.html#option-1-recommended-use-the-table-meta-decorator">Option 1 (recommended): Use the <code class="docutils literal notranslate"><span class="pre">table_meta</span></code> decorator</a></li>
Expand Down
1 change: 1 addition & 0 deletions docs/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
1 change: 1 addition & 0 deletions docs/py-modindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Use with dbt</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down
2 changes: 2 additions & 0 deletions docs/robots.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/examples.html">Examples</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Database Specifics</span></p>
Expand Down
1 change: 1 addition & 0 deletions docs/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Use with dbt</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down
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/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/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>
<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/usage/dbt.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>
1 change: 1 addition & 0 deletions docs/sql_mock.bigquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<p class="caption" role="heading"><span class="caption-text">Basic Usage</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="usage/defining_table_mocks.html">Defining table mocks</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/dbt.html">Enhanced SQLMock with dbt Integration Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/your_sql_query_to_test.html">Your SQL query to test</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/result_assertion.html">Result assertion</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage/default_values.html">Default values</a></li>
Expand Down
Loading

0 comments on commit e064ab0

Please sign in to comment.