DeepLcom / sql-mock

A Python library to test your SQL models using mocked input data
MIT License
34 stars 5 forks source link

Provide default mocks in table_meta #19

Closed Somtom closed 10 months ago

Somtom commented 10 months ago

Problem

At the moment we still need a lot of repetitive code to define our tests: Each from_mocks call needs to specify all the input table mocks even if their data would not change between tests.

What changed

We added a default_inputs argument to the table_meta decorator. This allows to pass reasonable default to the model. Here is an example from the docs:

@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

What to look for

You can ignore the changes in docs/ since they are auto generated from the docsource/ markdown file changes.

Instead, have a look at:

  1. What do you think about the "API" of providing defaults this way
  2. Are the docs and examples understandable
  3. General correctness
Somtom commented 10 months ago

@karolinastawicka thanks for your review! I loved the suggestions and tried to add them with my changes.