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

[Feature] unit testing: dbt should tell me why it couldn't get columns (the model doesn't yet exist) #10014

Closed
2 tasks done
joellabes opened this issue Apr 23, 2024 · 8 comments · Fixed by dbt-labs/dbt-adapters#203
Labels
enhancement New feature or request good_first_issue Straightforward + self-contained changes, good for new contributors! unit tests Issues related to built-in dbt unit testing functionality

Comments

@joellabes
Copy link
Contributor

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

I tried making my first unit test from scratch (no using the docs!) and got the below error.

It turns out that the issue was that the models hadn't been built yet, but the error didn't explain why it couldn’t get the columns.

(dbt-dev) joel@Joel-Labes integration_tests % dbt test -s test_type:unit
02:56:57  Running with dbt=1.8.0-b3
02:56:57  Registered adapter: snowflake=1.8.0-b3
02:56:58  Found 22 models, 2 analyses, 24 seeds, 19 data tests, 576 macros, 1 unit test
02:56:58  
02:56:59  Concurrency: 8 threads (target='dev')
02:56:59  
02:56:59  1 of 1 START unit_test unit_compare_queries::identical_records_compare_queries . [RUN]
02:57:01  1 of 1 ERROR unit_compare_queries::identical_records_compare_queries ........... [ERROR in 1.62s]
02:57:01  
02:57:01  Finished running 1 unit test in 0 hours 0 minutes and 3.29 seconds (3.29s).
02:57:01  
02:57:01  Completed with 1 error and 0 warnings:
02:57:01  
02:57:01    Compilation Error in model unit_test_model_a (models/unit_test_wrappers/unit_tests.yml)
  Not able to get columns for unit test 'unit_test_model_a' from relation ANALYTICS_DEV.DBT_JLABES_INT_TESTS.unit_test_model_a
  
  > in macro get_fixture_sql (macros/unit_test_sql/get_fixture_sql.sql)
  > called by model unit_test_model_a (models/unit_test_wrappers/unit_tests.yml)
02:57:01  
02:57:01  Done. PASS=0 WARN=0 ERROR=1 SKIP=0 TOTAL=1

Expected Behavior

Ignoring the fact that there could be other causes for this (ephemeral models? permissions issues? idk), the general thing that I felt was missing was an explanation of what I could do to solve it. Something like

Not able to get columns for unit test 'unit_test_model_a' from relation ANALYTICS_DEV.DBT_JLABES_INT_TESTS.unit_test_model_a because the relation doesn't exist.

would be plenty to make me go "oh of course I need to do a build (or defer to prod).

Steps To Reproduce

  1. Add a new model
  2. Add a unit test to that model but don't build the new model
  3. Run dbt test -s test_type:unit

Relevant log output

No response

Environment

- OS:
- Python:
- dbt: dbt-core@2edd5b3335ddea4ca5e7dd8aaa3bfe3a5d30499a (using Core on main as of about 5 days ago, roughly equivalent to the 1.8 beta)

Which database adapter are you using with dbt?

No response

Additional Context

No response

@joellabes joellabes added bug Something isn't working triage labels Apr 23, 2024
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label Apr 23, 2024
@dbeatty10
Copy link
Contributor

I tried making my first unit test from scratch (no using the docs!) and got the below error.

That's awesome 🤩

Yeah, it would be nice to give more hints to our intrepid hikers on how to get back on the trail.

Re-labeling as a feature request and adding the refinement label to consider an alternative error message.

@dbeatty10 dbeatty10 added enhancement New feature or request Refinement Maintainer input needed and removed triage labels Apr 23, 2024
@dbeatty10 dbeatty10 changed the title [Bug] unit testing: dbt doesn’t tell me why it couldn't get columns (the model didn't exist) [Feature] unit testing: dbt should tell me _why_ it couldn't get columns (the model didn't exist) Apr 23, 2024
@dbeatty10 dbeatty10 changed the title [Feature] unit testing: dbt should tell me _why_ it couldn't get columns (the model didn't exist) [Feature] unit testing: dbt should tell me why it couldn't get columns (the model doesn't yet exist) Apr 23, 2024
@dbeatty10 dbeatty10 removed the bug Something isn't working label Apr 24, 2024
@graciegoheen
Copy link
Contributor

graciegoheen commented Apr 24, 2024

Hello! A few thoughts:

  • if you're using deferral, you should be fine here! (as long as the relation is built in the environment you're deferring to)
  • you can also use the --empty flag to build an empty relation, if you don't want to use less warehouse $$$
  Not able to get columns for unit test 'unit_test_model_a' from relation ANALYTICS_DEV.DBT_JLABES_INT_TESTS.unit_test_model_a

I agree this is a bit vague. You could not be able to get the columns from this relation because:

  • the relation doesn't exist (hasn't been built yet)
  • the relation doesn't exist (it never will, it's an ephemeral model)

I'm ok with updating it to:

  Not able to get columns for unit test 'unit_test_model_a' from relation ANALYTICS_DEV.DBT_JLABES_INT_TESTS.unit_test_model_a because the relation doesn't exist.

@graciegoheen graciegoheen added good_first_issue Straightforward + self-contained changes, good for new contributors! and removed Refinement Maintainer input needed labels Apr 24, 2024
@AlejandroBlanco2001
Copy link

Hey @graciegoheen @joellabes, I would like to take this issue to get started in the repository, can I take it ?

@graciegoheen
Copy link
Contributor

@AlejandroBlanco2001 That would be great!

@AlejandroBlanco2001
Copy link

I've traced the error down to the Jinja parser, snugly nested in jinja.py within the get_rendered method. It's throwing its own exception, so the idea is modify it to something like this:

rendered = None 
try:
    rendered = render_template(template, ctx, node)
except Exception as e:
    raise CustomException()

My idea is to use the already existing class CompilationError and create something like:

class ModelNotExist(CompilationError):
    def __init__(self, operation_name) -> None:
        super().__init__(msg=self.get_message())

    def get_message(self) -> str:
        msg = (
            f"some message"
        )
        return msg

If we have another mechanism to handle exceptions, I'm open to hear other approach. I reckon the proposed message fits ok for this scenario.

Not able to get columns for unit test 'unit_test_model_a' from relation ANALYTICS_DEV.DBT_JLABES_INT_TESTS.unit_test_model_a because the relation doesn't exist.

Thoughts @graciegoheen @dbeatty10?

@jtcohen6
Copy link
Contributor

@AlejandroBlanco2001 Nice detective work! I think this might be a bit simpler though — the compilation error is actually being raised directly by the get_fixture_sql macro in dbt-adapters. It sounds like the proposed resolution is to update the language in that exception message.

@AlejandroBlanco2001
Copy link

AlejandroBlanco2001 commented May 10, 2024

@jtcohen6 Oh, thanks for being even more specific about where the message is. I think that I wil update the message with the suggestion of @graciegoheen

@AlejandroBlanco2001
Copy link

@jtcohen6 @graciegoheen PR opened 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good_first_issue Straightforward + self-contained changes, good for new contributors! unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants