Skip to content
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

Provide default mocks in table_meta #19

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ Make your code changes, commit them, and create a pull request to the project's
### 7. Code Formatting and Linting

As part of the pre-commit hooks, code formatting and linting will be automatically checked before each commit. Be sure to address any issues reported by the hooks.

### 8. Update documentation

We are using sphinx to generate our documentation.
The documentation pages can be found in `docsource`. Go there and add / adjust the files.
After that, we need to run `make build-docs-github` in order to populate the changes in the documentation and commit those.
1 change: 1 addition & 0 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ It provides a consistent and convenient way to test the execution of your query
usage/defining_table_mocks
usage/your_sql_query_to_test
usage/result_assertion
usage/default_values
usage/examples

.. toctree::
Expand Down
71 changes: 71 additions & 0 deletions docs/_sources/usage/default_values.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
```{toctree}
:maxdepth: 2
```

# Default values

Testing SQL queries can often involve repetitive setup for mock tables. In SQLMock, one effective way to streamline this process is by using default values. By setting reasonable defaults, you can significantly reduce the boilerplate code in your tests, especially when dealing with multiple input tables or complex queries. Let’s explore how you can efficiently implement this.

## Utilizing Default Values in MockTable Fields

Defining default values at the field level in your mock tables is straightforward.
The default argument in the field definition allows you to set default values consistency across all test scenarios in one step.
They are particularly useful for ensuring that joins and other query functionalities operate correctly.

Here's an example:

```python
@table_meta(table_ref="data.users")
class UserTable(BigQueryMockTable):
user_id = col.Int(default=1)
user_name = col.String(default="Mr. T")

# Create instances of the UserTable with various combinations of defaults and specified values
users = UserTable.from_dicts([
{}, # Left empty {} uses default values --> {"user_id": 1, "user_name": "Mr. T"}
{"user_id": 2}, # Overrides user_id but uses default for user_name
{"user_id": 3, "user_name": "Nala"} # No defaults used here
])
```

## Setting Mock Defaults with table_meta


When defining your MockTable classes, the `table_meta` decorator accepts a `default_inputs` argument.
The Mock instances passed here, will be used as defaults in the `from_mocks` method.

Consider this example:

```python
@table_meta(
query_path="./examples/test_query.sql",
default_inputs=[UserTable([]), SubscriptionTable([])] # We can provide defaults for the class if needed.
)
class MultipleSubscriptionUsersTable(BigQueryMockTable):
user_id = col.Int(default=1)

# Setting up different scenarios to demonstrate the use of defaults
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},
]
)

# Utilizing the default inputs set in the table_meta
res = MultipleSubscriptionUsersTable.from_mocks(input_data=[])
res = MultipleSubscriptionUsersTable.from_mocks(input_data=[users]) # Using only users, defaults for others
res = MultipleSubscriptionUsersTable.from_mocks(input_data=[users, subscriptions]) # Overriding defaults
```

## When is this useful?

* **Safe time and code by changing only the data you need for your test case:** You can only change single columns for the data you provide for a test case. The rest will be filled by defaults.
* **Simplifying Happy Path Testing:** Validate basic functionality and syntax correctness of your SQL queries with minimal setup.
* **Testing Subset Logic:** When certain tables in your query don't require data, default values can help focus on specific test scenarios.
* **Provide reasonable defaults for joins:** In tests with numerous input tables you can specify inputs that joins between tables work. For frequent addition of new tables, defaults can prevent the need for extensive refactoring.
6 changes: 5 additions & 1 deletion docs/_sources/usage/defining_table_mocks.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class Table(BigQueryMockTable):
id = col.Int(default=1)
name = col.String(default='Peter')

@table_meta(table_ref='data.result_table', query_path='path/to/query_for_result_table.sql')
@table_meta(
table_ref='data.result_table',
query_path='path/to/query_for_result_table.sql',
default_inputs=[Table()] # You can provide default inputs on a class level
)
class ResultTable(BigQueryMockTable):
id = col.Int(default=1)
```
Expand Down
3 changes: 3 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<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/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 Expand Up @@ -200,6 +201,8 @@ <h2 id="D">D</h2>
</li>
</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>
<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
7 changes: 7 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<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/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 Expand Up @@ -128,6 +129,12 @@ <h1>Welcome to SQL Mock’s documentation!<a class="headerlink" href="#welcome-t
</ul>
</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><ul>
<li class="toctree-l2"><a class="reference internal" href="usage/default_values.html#utilizing-default-values-in-mocktable-fields">Utilizing Default Values in MockTable Fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="usage/default_values.html#setting-mock-defaults-with-table-meta">Setting Mock Defaults with table_meta</a></li>
<li class="toctree-l2"><a class="reference internal" href="usage/default_values.html#when-is-this-useful">When is this useful?</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="usage/examples.html">Examples</a></li>
</ul>
</div>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
3 changes: 2 additions & 1 deletion docs/py-modindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<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/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 Expand Up @@ -104,7 +105,7 @@ <h1>Python Module Index</h1>
<td><img src="_static/minus.png" class="toggler"
id="toggle-1" style="display: none" alt="-" /></td>
<td>
<a href="sql_mock.html#module-sql_mock"><code class="xref">sql_mock</code></a></td><td>
<a href="index.html#module-sql_mock"><code class="xref">sql_mock</code></a></td><td>
<em></em></td></tr>
<tr class="cg-1">
<td></td>
Expand Down
1 change: 1 addition & 0 deletions docs/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<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/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
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/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/default_values.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>
Loading