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 5 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.
65 changes: 65 additions & 0 deletions docs/_sources/usage/default_values.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
```{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, which are particularly useful for ensuring that joins and other query functionalities operate correctly.
Somtom marked this conversation as resolved.
Show resolved Hide resolved

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([
{}, # Uses default values --> {"user_id": 1, "user_name": "Mr. T"}
Somtom marked this conversation as resolved.
Show resolved Hide resolved
{"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}])
Somtom marked this conversation as resolved.
Show resolved Hide resolved
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?

* **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
2 changes: 2 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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
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/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/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/usage/default_values.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/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