You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docsource/faq.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@
5
5
6
6
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:
7
7
8
-
### Create your `MockTable` class
8
+
### Create your `TableMock` class
9
9
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`.
11
11
12
12
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.
13
13
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)).
15
15
16
16
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.
17
17
@@ -41,7 +41,6 @@ class String(MyFanceDatabaseColumnMock):
41
41
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.
42
42
Feel free to create a PR on this repository that we can start supporting your database system!
43
43
44
-
45
44
## I am missing a specific ColumnMock type for my model fields
46
45
47
46
We implemented some basic column types but it could happen that you don't find the one you need.
Copy file name to clipboardExpand all lines: docsource/getting_started/quickstart.md
+17-14Lines changed: 17 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@
6
6
7
7
Before diving into specific database scenarios, let's start with a simplified example of how SQL Mock works behind the scenes.
8
8
9
-
10
9
1. You have an original SQL query, for instance:
10
+
11
11
```sql
12
12
-- path/to/query_for_result_table.sql
13
13
SELECT id FROMdata.table1
14
14
```
15
15
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
16
17
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
18
18
```python
19
19
from sql_mock.clickhouse import column_mocks as col
20
20
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
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
+
34
35
```python
35
36
user_data = [
36
37
{}, # 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
41
42
input_table_mock = Table.from_dicts(user_data)
42
43
```
43
44
44
-
45
45
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
+
46
47
```python
47
48
res = ResultTable.from_mocks(input_data=[input_table_mock])
48
49
```
49
50
50
51
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
+
51
53
```sql
52
54
WITH data__table1 AS (
53
55
-- Mocked inputs
54
-
SELECT
55
-
cast('1'AS'String') ASid,
56
+
SELECT
57
+
cast('1'AS'String') ASid,
56
58
cast('Peter'AS'String') AS name
57
-
UNIONALL
58
-
SELECT
59
-
cast('2'AS'String') ASid,
59
+
UNIONALL
60
+
SELECT
61
+
cast('2'AS'String') ASid,
60
62
cast('Martin'AS'String') AS name
61
-
UNIONALL
62
-
SELECT
63
-
cast('3'AS'String') ASid,
63
+
UNIONALL
64
+
SELECT
65
+
cast('3'AS'String') ASid,
64
66
cast('Peter'AS'String') AS name
65
67
)
66
68
67
69
result AS (
68
70
-- Original query with replaced references
69
-
SELECTidFROM data__table1
71
+
SELECTidFROM data__table1
70
72
)
71
73
72
-
SELECT
74
+
SELECT
73
75
cast(idAS'String') ASid
74
76
FROM result
75
77
```
76
78
77
79
6. Finally, you can compare your results to some expected results using the `assert_equal` method.
Copy file name to clipboardExpand all lines: docsource/usage/default_values.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,17 @@
6
6
7
7
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.
8
8
9
-
## Utilizing Default Values in MockTable Fields
9
+
## Utilizing Default Values in TableMock Fields
10
10
11
11
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.
13
13
They are particularly useful for ensuring that joins and other query functionalities operate correctly.
Copy file name to clipboardExpand all lines: docsource/usage/defining_table_mocks.md
+13-10Lines changed: 13 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,28 +4,28 @@
4
4
5
5
# Defining table mocks
6
6
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`).
8
8
9
9
**We recommend to have a central `model.py` file where you create those models that you can easily reuse them across your tests**
10
10
11
11
```python
12
12
# models.py
13
13
14
14
from sql_mock.bigquery import column_mocks as col
15
-
from sql_mock.bigquery.table_mocks importBigQueryMockTable, table_meta
15
+
from sql_mock.bigquery.table_mocks importBigQueryTableMock, table_meta
16
16
17
17
# The models you are goign to use as inputs need to have a `table_ref` specified
18
18
@table_meta(table_ref='data.table1')
19
-
classTable(BigQueryMockTable):
19
+
classTable(BigQueryTableMock):
20
20
id= col.Int(default=1)
21
21
name = col.String(default='Peter')
22
22
23
23
@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',
26
26
default_inputs=[Table()] # You can provide default inputs on a class level
27
27
)
28
-
classResultTable(BigQueryMockTable):
28
+
classResultTable(BigQueryTableMock):
29
29
id= col.Int(default=1)
30
30
```
31
31
@@ -34,16 +34,19 @@ Some important things to mention:
34
34
**The models you are goign to use as inputs need to have a `table_ref` specified.**
35
35
The `table_ref` is how the table will be referenced in your production database (usually some pattern like `<schema>.<table>`)
36
36
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.
39
41
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.
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
+
47
50
```python
48
51
res = ResultTable.from_mocks(query='SELECT 1', input_data=[<your-input-mocks-table-instances>])
0 commit comments