Hi All, I am trying to add a custom Python test, b...
# community-support
a
Hi All, I am trying to add a custom Python test, but getting the following unexpected error:
Copy code
Internal Error
  popped ['materialization'], expected macro.elementary.test_python
Any resolution for this?
e
Hey there @Anil! Could you share the test you're trying to add?
a
So in the model config yml file, I have declared the test macro like this:
Copy code
- elementary.python:
          code_macro: mean_test
And in the macros folder I have added the sample test as:
Copy code
{% macro mean_test(args) %}
import pandas as pd

def test(model_df, ref, session):
    original_table = model_df.toPandas()
    return True
{% endmacro %}
Anything I am doing wrong here?
e
@Anil What version of elementary and dbt are you using?
a
Core: - installed: 1.7.9 - latest: 1.7.9 - Up to date! Plugins: - bigquery: 1.7.6 - Up to date! - package: elementary-data/elementary version: 0.14.1
All up to date! But I have not done any other setup apart from mentioning in config file as:
Copy code
- elementary.python:
          code_macro: mean_test
Any other configuration I need to do to run python custom test? Normal elementary volume and anomaly tests are working fine.
e
Is that under the tests of your module? Besides a bit of an over indent, it looks ok.
Copy code
version:2
models:
  - name: orders
    description: This table has basic information about orders, as well as some derived facts based on payments
    config:
      tags: ["finance"]
      elementary:
        timestamp_column: "order_date"

    tests:
      - elementary.python:
          tags: [anil_test]
          code_macro: compare_tables
          macro_args:
            other_table: raw_orders
and:
Copy code
{% macro compare_tables(args) %}
# in macros/test_for_anil.sql
def test(model_df, ref, session):
    return 0
{% endmacro %}
Then I run it with:
dbt test -s 'tag:anil_test'
a
Yes that is under tests of my model and under the same model other inbuilt tests like volume anomaly are working fine. Do I need to configure any other things ? Like Do I need to give Dataproc or any other compute for this test calculation?
Any suggestions or resolutions on this? @Idan Shavit @Erik Zaadi
e
@Anil did you try to run my simple example to verify if it works?
a
Yes yes, I tried same thing with my model but no luck!
Is there any way, where I can check if it is even reaching the function defined in the macro? From the error, it looks like some path issue:
Copy code
Internal Error
  popped ['materialization'], expected macro.elementary.test_python
e
Are you running in a virtual env?
a
Yes I am running in a virtual environment in windows in local machine
Any specific problem with running in virtual env?
??
o
Hey @Anil, this issue happens when there is an exception in the python code, sadly, due to a limitation in the way that
dbt
allows for python code execution, the exception itself cannot be retrieved and displayed in the command output, so you would need to check it in your data warehouse.
a
But I have nothing in my python code:
Copy code
{% macro mean_test_(args) %}
def test(model_df, ref, session):
    return True
{% endmacro %}
I think is is not able to find the macro itself. I place it in the macros of whole project, do I need to put in macros of elementary package?
o
Hmm interesting. i think there might be an error in our docs, i dont thing that returning a
bool
is supported, only
int
(number of failing rows) or
DataFrame
(failing rows). can you try to change it to
return 0
? also, can you share how your test is configured in the
yml
file?
a
Hi @Ofek Weiss, thanks for replying. Even with returning 0, getting same error. Attaching yml config below:
o
What dwh are you using? did you go through the necessary setup for dbt python - https://docs.getdbt.com/docs/build/python-models#specific-data-platforms