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

Add invocation_args_dict to ProviderContext #5782

Conversation

jared-rimmer
Copy link
Contributor

@jared-rimmer jared-rimmer commented Sep 7, 2022

resolves #5524

Description

  • Added a invocation_args_dict contextproperty to the ProviderContext class.
  • Added a test in test_context.py using a MacroContext
  • Added a test in test_builtin_functions that prints {{invocation_args_dict}} to the logs. Again using a MacroContext

Checklist

@jared-rimmer jared-rimmer requested review from a team as code owners September 7, 2022 10:14
@jared-rimmer jared-rimmer requested review from gshank and stu-k September 7, 2022 10:14
@cla-bot cla-bot bot added the cla:yes label Sep 7, 2022
Copy link
Contributor

@gshank gshank left a comment

Choose a reason for hiding this comment

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

Looks good.

@jared-rimmer
Copy link
Contributor Author

@gshank the Structured Logging Schema check failed but the integration tests pass when I run them locally.

I was looking into using --log-format-json in the test itself:

    def test_builtin_invocation_args_to_dict_function(self, project):
        _, log_output = run_dbt_and_capture(
            [
                "--debug",
                "--log-format=json",
                "run-operation",
                "validate_invocation",
                "--args",
                "{my_variable: test_variable}",
            ]
        )
        # Result is checked in two parts because profiles_dir is unique each test run
        parsed_logs = []
        for line in log_output.split("\n"):
            try:
                log = json.loads(line)
            except ValueError:
                continue

            parsed_logs.append(log)

And then running the assert on the parsed_logs list of dicts.

What are your thoughts on this?

Copy link
Contributor

@jtcohen6 jtcohen6 left a comment

Choose a reason for hiding this comment

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

Nice work @jared-rimmer! Just a teensy comment about naming, and one about fixing the failing test. Could you also create a changelog entry?

core/dbt/context/providers.py Outdated Show resolved Hide resolved
test/unit/test_context.py Show resolved Hide resolved
tests/functional/context_methods/test_builtin_functions.py Outdated Show resolved Hide resolved
@jtcohen6 jtcohen6 added Team:Language ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering labels Sep 7, 2022
@jared-rimmer jared-rimmer changed the title draft: Add invocation_args_to_dict to ProviderContext draft: Add invocation_args_dict to ProviderContext Sep 8, 2022
@jared-rimmer jared-rimmer changed the title draft: Add invocation_args_dict to ProviderContext Add invocation_args_dict to ProviderContext Sep 8, 2022
Comment on lines +66 to +104
def test_builtin_invocation_args_dict_function(self, project):
_, log_output = run_dbt_and_capture(
[
"--debug",
"--log-format=json",
"run-operation",
"validate_invocation",
"--args",
"{my_variable: test_variable}",
]
)

parsed_logs = []
for line in log_output.split("\n"):
try:
log = json.loads(line)
except ValueError:
continue

parsed_logs.append(log)

# Value of msg is a string
result = next(
(
item
for item in parsed_logs
if "invocation_result" in item["data"].get("msg", "msg")
),
False,
)

assert result

# Result is checked in two parts because profiles_dir is unique each test run
expected = "invocation_result: {'debug': True, 'log_format': 'json', 'write_json': True, 'use_colors': True, 'printer_width': 80, 'version_check': True, 'partial_parse': True, 'static_parser': True, 'profiles_dir': "
assert expected in str(result)

expected = "'send_anonymous_usage_stats': False, 'event_buffer_size': 100000, 'quiet': False, 'no_print': False, 'macro': 'validate_invocation', 'args': '{my_variable: test_variable}', 'which': 'run-operation', 'rpc_method': 'run-operation', 'indirect_selection': 'eager'}"
assert expected in str(result)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to add --log-format=json to the dbt method and parse the json logs then implement the asserts on this within the test rather than having it in the macro jinja.

Based on similar logic I found in test_query_comments

@gshank gshank merged commit 82c8d6a into dbt-labs:main Sep 8, 2022
@jared-rimmer jared-rimmer deleted the feat/add-invocation-args-dict-to-provider-context-class branch September 9, 2022 06:02
josephberni pushed a commit to Gousto/dbt-core that referenced this pull request Sep 16, 2022
* Add invocation_args_to_dict to ProviderContext

* Change invocation_args_to_dict contextproperty to invocation_args_dict

* Fix invocation_args_dict builtin test

* Add CHANGELOG entry

* Fix formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-898] [Feature] Make entire dbt invocation command available to Jinja context
3 participants