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

Provide default mocks in table_meta #19

merged 6 commits into from
Dec 21, 2023

Conversation

Somtom
Copy link
Collaborator

@Somtom Somtom commented Dec 20, 2023

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

Thomas Schmidt added 5 commits December 20, 2023 07:11
* The table_meta decorator now accepts default input mock tables
* Bonus: Fix bug with empty inputs in BigQuery
@Somtom Somtom self-assigned this Dec 20, 2023
@Somtom Somtom requested a review from thecasper2 December 20, 2023 08:05
Copy link

@karolinastawicka karolinastawicka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this solution! It saves a lot of time and allows you to only add/change the specific fields you want - especially useful if it's more than 3 column like in example.

Maybe worth adding in the default_values.md.txt that another plus of it is that it allows you to change only one column (or only those that you really need to change in your test case, not redefine all). If it is clear somewhere else in the documentation, and I didn't spot it, then ignore this comment.

🦐

docs/_sources/usage/default_values.md.txt Outdated Show resolved Hide resolved
docsource/usage/default_values.md Outdated Show resolved Hide resolved
docsource/usage/default_values.md Outdated Show resolved Hide resolved
examples/bigquery/test_example.py Outdated Show resolved Hide resolved
docs/_sources/usage/default_values.md.txt Outdated Show resolved Hide resolved
docs/_sources/usage/default_values.md.txt Outdated Show resolved Hide resolved
@Somtom Somtom force-pushed the better-default-mocks branch from c23c7f1 to 114a232 Compare December 20, 2023 13:09
@Somtom
Copy link
Collaborator Author

Somtom commented Dec 20, 2023

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

@Somtom Somtom merged commit bf4f923 into main Dec 21, 2023
3 checks passed
@Somtom Somtom deleted the better-default-mocks branch December 21, 2023 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants