Skip to content

Commit 531722a

Browse files
committed
Consinstent naming for TableMock
1 parent e61ad8d commit 531722a

File tree

29 files changed

+166
-163
lines changed

29 files changed

+166
-163
lines changed

docsource/faq.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
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:
77

8-
### Create your `MockTable` class
8+
### Create your `TableMock` class
99

10-
First, you need to create a `MockTable` class for your database system that inherits from `sql_mock.table_mocks.BaseMockTable`.
10+
First, you need to create a `TableMock` class for your database system that inherits from `sql_mock.table_mocks.BaseTableMock`.
1111

1212
That class needs to implement the `_get_results` method which should make sure to fetch the results of a query (e.g. produced by `self._generate_query()`) and return it as list of dictionaries.
1313

14-
Look at one of the existing client libraries to see how this could work (e.g. [BigQueryMockTable](https://github.com/DeepLcom/sql-mock/blob/main/src/sql_mock/bigquery/table_mocks.py)).
14+
Look at one of the existing client libraries to see how this could work (e.g. [BigQueryTableMock](https://github.com/DeepLcom/sql-mock/blob/main/src/sql_mock/bigquery/table_mocks.py)).
1515

1616
You might want to create a settings class as well in case you need some specific connection settings to be available within the `_get_results` method.
1717

@@ -41,7 +41,6 @@ class String(MyFanceDatabaseColumnMock):
4141
There will definitely be folks in the community that are in the need of support for the database you just created all the setup for.
4242
Feel free to create a PR on this repository that we can start supporting your database system!
4343

44-
4544
## I am missing a specific ColumnMock type for my model fields
4645

4746
We implemented some basic column types but it could happen that you don't find the one you need.

docsource/getting_started/quickstart.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
Before diving into specific database scenarios, let's start with a simplified example of how SQL Mock works behind the scenes.
88

9-
109
1. You have an original SQL query, for instance:
10+
1111
```sql
1212
-- path/to/query_for_result_table.sql
1313
SELECT id FROM data.table1
1414
```
1515

16+
2. Using SQL Mock, you define mock tables. You can use the built-in column types provided by SQL Mock. Available column types include `Int`, `String`, `Date`, and more. Each database type has their own column types. Define your tables by subclassing a mock table class that fits your database (e.g. `BigQueryTableMock`) and specifying the column types along with default values. In our example we use the `ClickHouseTableMock` class
1617

17-
2. Using SQL Mock, you define mock tables. You can use the built-in column types provided by SQL Mock. Available column types include `Int`, `String`, `Date`, and more. Each database type has their own column types. Define your tables by subclassing a mock table class that fits your database (e.g. `BigQueryMockTable`) and specifying the column types along with default values. In our example we use the `ClickHouseTableMock` class
1818
```python
1919
from sql_mock.clickhouse import column_mocks as col
2020
from sql_mock.clickhouse.table_mocks import ClickHouseTableMock
@@ -24,13 +24,14 @@ Before diving into specific database scenarios, let's start with a simplified ex
2424
class Table(ClickHouseTableMock):
2525
id = col.Int(default=1)
2626
name = col.String(default='Peter')
27-
27+
2828
@table_meta(table_ref='data.result_table', query_path='path/to/query_for_result_table.sql')
2929
class ResultTable(ClickHouseTableMock):
3030
id = col.Int(default=1)
3131
```
3232

3333
3. **Creating mock data:** Define mock data for your tables using dictionaries. Each dictionary represents a row in the table, with keys corresponding to column names. Table column keys that don't get a value will use the default.
34+
3435
```python
3536
user_data = [
3637
{}, # This will use the defaults for both id and name
@@ -41,40 +42,42 @@ Before diving into specific database scenarios, let's start with a simplified ex
4142
input_table_mock = Table.from_dicts(user_data)
4243
```
4344

44-
4545
4. **Getting results for a table mock:** Use the `from_mocks` method of the table mock object to generate mock query results based on your mock data.
46+
4647
```python
4748
res = ResultTable.from_mocks(input_data=[input_table_mock])
4849
```
4950

5051
5. Behind the scene SQL Mock replaces table references (e.g. `data.table1`) in your query with Common Table Expressions (CTEs) filled with dummy data. It can roughly be compared to something like this:
52+
5153
```sql
5254
WITH data__table1 AS (
5355
-- Mocked inputs
54-
SELECT
55-
cast('1' AS 'String') AS id,
56+
SELECT
57+
cast('1' AS 'String') AS id,
5658
cast('Peter' AS 'String') AS name
57-
UNION ALL
58-
SELECT
59-
cast('2' AS 'String') AS id,
59+
UNION ALL
60+
SELECT
61+
cast('2' AS 'String') AS id,
6062
cast('Martin' AS 'String') AS name
61-
UNION ALL
62-
SELECT
63-
cast('3' AS 'String') AS id,
63+
UNION ALL
64+
SELECT
65+
cast('3' AS 'String') AS id,
6466
cast('Peter' AS 'String') AS name
6567
)
6668

6769
result AS (
6870
-- Original query with replaced references
69-
SELECT id FROM data__table1
71+
SELECT id FROM data__table1
7072
)
7173

72-
SELECT
74+
SELECT
7375
cast(id AS 'String') AS id
7476
FROM result
7577
```
7678

7779
6. Finally, you can compare your results to some expected results using the `assert_equal` method.
80+
7881
```python
7982
expected = [{'id': '1'},{'id': '2'},{'id': '3'}]
8083
res.assert_equal(expected)

docsource/usage/bigquery/examples.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
```python
88
import datetime
99
from sql_mock.bigquery import column_mocks as col
10-
from sql_mock.bigquery.table_mocks import BigQueryMockTable
10+
from sql_mock.bigquery.table_mocks import BigQueryTableMock
1111
from sql_mock.table_mocks import table_meta
1212

13-
# Define mock tables for your data model that inherit from BigQueryMockTable
13+
# Define mock tables for your data model that inherit from BigQueryTableMock
1414
@table_meta(table_ref='data.users')
15-
class UserTable(BigQueryMockTable):
15+
class UserTable(BigQueryTableMock):
1616
user_id = col.Int(default=1)
1717
user_name = col.String(default='Mr. T')
1818

1919

2020
@table_meta(table_ref='data.subscriptions')
21-
class SubscriptionTable(BigQueryMockTable):
21+
class SubscriptionTable(BigQueryTableMock):
2222
subscription_id = col.Int(default=1)
2323
period_start_date = col.Date(default=datetime.date(2023, 9, 5))
2424
period_end_date = col.Date(default=datetime.date(2023, 9, 5))
2525
user_id = col.Int(default=1)
2626

2727

2828
# Define a mock table for your expected results
29-
class SubscriptionCountTable(BigQueryMockTable):
29+
class SubscriptionCountTable(BigQueryTableMock):
3030
subscription_count = col.Int(default=1)
3131
user_id = col.Int(default=1)
3232

@@ -50,13 +50,13 @@ subscriptions = SubscriptionTable.from_dicts([
5050

5151
# Define your expected results
5252
expected = [
53-
{'user_id': 1, 'subscription_count': 2},
53+
{'user_id': 1, 'subscription_count': 2},
5454
{'user_id': 2, 'subscription_count': 1}
5555
]
5656

5757
# Simulate the SQL query using SQL Mock
5858
res = SubscriptionCountTable.from_mocks(
59-
query=query,
59+
query=query,
6060
input_data=[users, subscriptions]
6161
)
6262

docsource/usage/dbt.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ For dbt models, use the `dbt_model_meta` decorator from `sql_mock.dbt`. This dec
3131

3232
```python
3333
from sql_mock.dbt import dbt_model_meta
34-
from sql_mock.bigquery.table_mocks import BigQueryMockTable
34+
from sql_mock.bigquery.table_mocks import BigQueryTableMock
3535

3636
@dbt_model_meta(model_name="your_dbt_model_name")
37-
class YourDBTModelTable(BigQueryMockTable):
37+
class YourDBTModelTable(BigQueryTableMock):
3838
# Define your table columns and other necessary attributes here
3939
...
4040
```
@@ -45,10 +45,10 @@ For dbt sources, use the `dbt_source_meta` decorator from `sql_mock.dbt`. This i
4545

4646
```python
4747
from sql_mock.dbt import dbt_source_meta
48-
from sql_mock.bigquery.table_mocks import BigQueryMockTable
48+
from sql_mock.bigquery.table_mocks import BigQueryTableMock
4949

5050
@dbt_source_meta(source_name="your_source_name", table_name="your_source_table")
51-
class YourDBTSourceTable(BigQueryMockTable):
51+
class YourDBTSourceTable(BigQueryTableMock):
5252
# Define your table columns and other necessary attributes here
5353
...
5454
```
@@ -59,10 +59,10 @@ For dbt seeds, which are static data sets loaded into the database, use the `dbt
5959

6060
```python
6161
from sql_mock.dbt import dbt_seed_meta
62-
from sql_mock.bigquery.table_mocks import BigQueryMockTable
62+
from sql_mock.bigquery.table_mocks import BigQueryTableMock
6363

6464
@dbt_seed_meta(seed_name="your_dbt_seed_name")
65-
class YourDBTSeedTable(BigQueryMockTable):
65+
class YourDBTSeedTable(BigQueryTableMock):
6666
# Define your table columns and other necessary attributes here
6767
...
6868
```
@@ -75,14 +75,14 @@ Let’s consider a dbt model named `monthly_user_spend` that aggregates data fro
7575

7676
```python
7777
@dbt_source_meta(source_name="transactions", table_name="user_transactions")
78-
class UserTransactionsTable(BigQueryMockTable):
78+
class UserTransactionsTable(BigQueryTableMock):
7979
transaction_id = col.Int(default=1)
8080
user_id = col.Int(default=1)
8181
amount = col.Float(default=1.0)
8282
transaction_date = col.Date(default='2023-12-24')
8383

8484
@dbt_seed_meta(seed_name="user_categories")
85-
class UserCategoriesTable(BigQueryMockTable):
85+
class UserCategoriesTable(BigQueryTableMock):
8686
user_id = col.Int(default=1)
8787
category = col.String(default='foo')
8888
```
@@ -91,7 +91,7 @@ class UserCategoriesTable(BigQueryMockTable):
9191

9292
```python
9393
@dbt_model_meta(model_name="monthly_user_spend")
94-
class MonthlyUserSpendTable(BigQueryMockTable):
94+
class MonthlyUserSpendTable(BigQueryTableMock):
9595
user_id = col.Int(default=1)
9696
month = col.String(default='foo')
9797
total_spend = col.Float(default=1.0)

docsource/usage/default_values.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
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.
88

9-
## Utilizing Default Values in MockTable Fields
9+
## Utilizing Default Values in TableMock Fields
1010

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

1515
Here's an example:
1616

17-
```python
17+
```python
1818
@table_meta(table_ref="data.users")
19-
class UserTable(BigQueryMockTable):
19+
class UserTable(BigQueryTableMock):
2020
user_id = col.Int(default=1)
2121
user_name = col.String(default="Mr. T")
2222

@@ -30,23 +30,22 @@ users = UserTable.from_dicts([
3030

3131
## Setting Mock Defaults with table_meta
3232

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

3736
Consider this example:
3837

39-
```python
38+
```python
4039
@table_meta(
4140
query_path="./examples/test_query.sql",
4241
default_inputs=[UserTable([]), SubscriptionTable([])] # We can provide defaults for the class if needed.
4342
)
44-
class MultipleSubscriptionUsersTable(BigQueryMockTable):
43+
class MultipleSubscriptionUsersTable(BigQueryTableMock):
4544
user_id = col.Int(default=1)
4645

4746
# Setting up different scenarios to demonstrate the use of defaults
4847
users = UserTable.from_dicts([
49-
{"user_id": 1},
48+
{"user_id": 1},
5049
{"user_id": 2}
5150
])
5251
subscriptions = SubscriptionTable.from_dicts(

docsource/usage/defining_table_mocks.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
# Defining table mocks
66

7-
When you want to provide mocked data to test your SQL model, you need to create MockTable classes for all upstream data that your model uses, as well as for the model you want to test. Those mock tables can be created by inheriting from a `BaseMockTable` class for the database provider you are using (e.g. `BigQueryMockTable`).
7+
When you want to provide mocked data to test your SQL model, you need to create TableMock classes for all upstream data that your model uses, as well as for the model you want to test. Those mock tables can be created by inheriting from a `BaseTableMock` class for the database provider you are using (e.g. `BigQueryTableMock`).
88

99
**We recommend to have a central `model.py` file where you create those models that you can easily reuse them across your tests**
1010

1111
```python
1212
# models.py
1313

1414
from sql_mock.bigquery import column_mocks as col
15-
from sql_mock.bigquery.table_mocks import BigQueryMockTable, table_meta
15+
from sql_mock.bigquery.table_mocks import BigQueryTableMock, table_meta
1616

1717
# The models you are goign to use as inputs need to have a `table_ref` specified
1818
@table_meta(table_ref='data.table1')
19-
class Table(BigQueryMockTable):
19+
class Table(BigQueryTableMock):
2020
id = col.Int(default=1)
2121
name = col.String(default='Peter')
2222

2323
@table_meta(
24-
table_ref='data.result_table',
25-
query_path='path/to/query_for_result_table.sql',
24+
table_ref='data.result_table',
25+
query_path='path/to/query_for_result_table.sql',
2626
default_inputs=[Table()] # You can provide default inputs on a class level
2727
)
28-
class ResultTable(BigQueryMockTable):
28+
class ResultTable(BigQueryTableMock):
2929
id = col.Int(default=1)
3030
```
3131

@@ -34,16 +34,19 @@ Some important things to mention:
3434
**The models you are goign to use as inputs need to have a `table_ref` specified.**
3535
The `table_ref` is how the table will be referenced in your production database (usually some pattern like `<schema>.<table>`)
3636

37-
**The result model needs to have a query.**
38-
There are currently 2 ways to provide a query to the model:
37+
**The result model needs to have a query.**
38+
There are currently 2 ways to provide a query to the model:
39+
40+
1. Pass a path to your query file in the class definition using the `table_meta` decorator. This allows us to only specify it once.
3941

40-
1. Pass a path to your query file in the class definition using the `table_meta` decorator. This allows us to only specify it once.
4142
```python
4243
@table_meta(table_ref='data.result_table', query_path='path/to/query_for_result_table.sql')
43-
class ResultTable(BigQueryMockTable):
44+
class ResultTable(BigQueryTableMock):
4445
...
4546
```
47+
4648
2. Pass it as `query` argument to the `from_mocks` method when you are using the model in your test. This will also overwrite whatever query was read from the `query_path` in the `table_meta` decorator.
49+
4750
```python
4851
res = ResultTable.from_mocks(query='SELECT 1', input_data=[<your-input-mocks-table-instances>])
4952
```

docsource/usage/redshift/examples.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
```python
88
import datetime
99
from sql_mock.redshift import column_mocks as col
10-
from sql_mock.redshift.table_mocks import RedshiftMockTable
10+
from sql_mock.redshift.table_mocks import RedshiftTableMock
1111
from sql_mock.table_mocks import table_meta
1212

13-
# Define mock tables for your data model that inherit from RedshiftMockTable
13+
# Define mock tables for your data model that inherit from RedshiftTableMock
1414
@table_meta(table_ref="data.users")
15-
class UserTable(RedshiftMockTable):
15+
class UserTable(RedshiftTableMock):
1616
user_id = col.INTEGER(default=1)
1717
user_name = col.VARCHAR(default="Mr. T")
1818

1919

2020
@table_meta(table_ref="data.subscriptions")
21-
class SubscriptionTable(RedshiftMockTable):
21+
class SubscriptionTable(RedshiftTableMock):
2222
subscription_id = col.INTEGER(default=1)
2323
period_start_date = col.DATE(default=datetime.date(2023, 9, 5))
2424
period_end_date = col.DATE(default=datetime.date(2023, 9, 5))
2525
user_id = col.INTEGER(default=1)
2626

2727
# Define a mock table for your expected results
28-
class SubscriptionCountTable(RedshiftMockTable):
28+
class SubscriptionCountTable(RedshiftTableMock):
2929
subscription_count = col.INTEGER(default=1)
3030
user_id = col.INTEGER(default=1)
3131

0 commit comments

Comments
 (0)