Skip to content

How to mock JSON column #222

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

Open
HansBambel opened this issue Apr 1, 2025 · 0 comments
Open

How to mock JSON column #222

HansBambel opened this issue Apr 1, 2025 · 0 comments

Comments

@HansBambel
Copy link

Hey, really cool project!
I was wondering how would I mock a JSON in a column. I've tried different things already, but none seem to work. I mostly get a Syntax error when I run the test and the (mocked) JSON is fed in.

This is my latest version:

{{ config(tags=["unit-test"]) }}

-- Tests that output produced by the plugins are correctly aggregated
{% call dbt_unit_testing.test("session_plugin_output", "data aggregations correct") %}
    {%- set session_all_valid = "'00000000-0000-0000-0000-000000000001'" -%}
    {%- set session_mixed = "'00000000-0000-0000-0000-0000000mixed'" -%}
    {%- set session_no_output = "'00000000-0000-0000-0000-00000missing'" -%}
    {%- set session_no_json = "'00000000-0000-0000-0000-000000nojson'" -%}
    {%- set valid_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": ["Unit 1"]} -%}
    {%- set empty_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": []} -%}
    {%- set null_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": null} -%}
    {%- set missing_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}} -%}
    {% set column_transformations = {
      "value": "to_json_string(value)"
      }
    %}

    {% call dbt_unit_testing.mock_source(
        "event_log", "personalizationpluginrun_events", options={"column_transformations": column_transformations}
    ) %}
    {{ session_all_valid }} as log_id, {{ valid_output }} as value::jsonb
    UNION ALL
    {{ session_all_valid }} as log_id, {{ valid_output }} as value::jsonb
    UNION ALL
    {{ session_mixed }} as log_id, {{ valid_output }} as value::jsonb
    UNION ALL
    {{ session_mixed }} as log_id, {{ empty_output }} as value::jsonb
    UNION ALL
    {{ session_mixed }} as log_id, {{ empty_output }} as value::jsonb
    UNION ALL
    {{ session_mixed }} as log_id, {{ valid_output }} as value::jsonb
    UNION ALL
    {{ session_no_output }} as log_id, {{ empty_output }} as value::jsonb
    UNION ALL
    {{ session_no_output }} as log_id, {{ null_output }} as value::jsonb
    UNION ALL
    {{ session_no_output }} as log_id, {{ missing_output }} as value::jsonb
    UNION ALL
    {{ session_no_json }} as log_id, null as value::jsonb
    {% endcall %}

    {% call dbt_unit_testing.expect({"input_format": "csv"}) %}
          session_id::varchar(36), plugin_output_produced::int, plugin_runs::int
            {{ session_all_valid }}, 2, 2
            {{ session_mixed }}, 2, 4
            {{ session_no_output }}, 0, 3
            {{ session_no_json }}, 0, 1
    {% endcall %}
{% endcall %}

This results in the following error:

Database Error in test session_plugin_output (tests/legacy/personalization/session_plugin_output.sql)
  syntax error at or near "'00000000-0000-0000-0000-000000000001'" in context "( 
        with
        "personalizationpluginrun_events" as (
      '00000000-0000-0000-0000-000000000001'", at line 18, column 5

Before that I also tried it with CSV input, with or without the column_transformation which would make the test way easier to read:

{{ config(tags=["unit-test"]) }}

-- Tests that output produced by the plugins are correctly aggregated
{% call dbt_unit_testing.test("session_plugin_output", "data aggregations correct") %}
    {%- set session_all_valid = "'00000000-0000-0000-0000-000000000001'" -%}
    {%- set session_mixed = "'00000000-0000-0000-0000-0000000mixed'" -%}
    {%- set session_no_output = "'00000000-0000-0000-0000-00000missing'" -%}
    {%- set session_no_json = "'00000000-0000-0000-0000-000000nojson'" -%}
    {%- set valid_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": ["Unit 1"]} -%}
    {%- set empty_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": []} -%}
    {%- set null_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}, "outputContentIds": null} -%}
    {%- set missing_output = {"plugin": {"name": "expectedPlugin", "version": "1.0"}} -%}
    

    {% call dbt_unit_testing.mock_source(
        "event_log", "personalizationpluginrun_events", options={"input_format": "csv"}
    ) %}
    log_id::varchar(36), value::jsonb
    {{ session_all_valid }}, {{ valid_output }}
    {{ session_all_valid }}, {{ valid_output }}
    {{ session_mixed }}, {{ valid_output }}
    {{ session_mixed }}, {{ empty_output }}
    {{ session_mixed }}, {{ empty_output }}
    {{ session_mixed }}, {{ valid_output }}
    {{ session_no_output }}, {{ empty_output }}
    {{ session_no_output }}, {{ null_output }}
    {{ session_no_output }}, {{ missing_output }}
    {{ session_no_json }}, null
    {% endcall %}

    {% call dbt_unit_testing.expect({"input_format": "csv"}) %}
          session_id::varchar(36), plugin_output_produced::int, plugin_runs::int
            {{ session_all_valid }}, 2, 2
            {{ session_mixed }}, 2, 4
            {{ session_no_output }}, 0, 3
            {{ session_no_json }}, 0, 1
    {% endcall %}
{% endcall %}

This is results in the following error:

Database Error in test session_plugin_output (tests/legacy/personalization/session_plugin_output.sql)
  syntax error at or near "'plugin'" in context ",CAST({'plugin'", at line 17, column 128

Is mocking such a JSON column supported in the first place? If so, how would I mock it?

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

No branches or pull requests

1 participant