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

Expect empty resultset #173

Closed
darian-heede opened this issue Sep 21, 2023 · 6 comments
Closed

Expect empty resultset #173

darian-heede opened this issue Sep 21, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@darian-heede
Copy link

Is there a way to expect no return values, i.e. an empty resultset? Leaving the dbt_unit_testing.expect() call empty will yield a can't execute an empty query error.

It would be great to be able to call dbt_unit_testing.expect_nothing() for such cases.

Another solution would be to be able to specify the expected row count to be returned, which might also be useful for other use cases as well and could solve this issue by expecting 0 rows returned.

@psousa50
Copy link
Collaborator

Hi @darian-heede, we can implement that; it makes perfect sense.

Meanwhile, you can use something like this to test an empty result set:

  {% call dbt_unit_testing.mock_ref ('some_model') %}
    select 1 where false
  {% endcall %}

Let me know if it works for you.

Thank you!

@darian-heede
Copy link
Author

darian-heede commented Sep 28, 2023

Hi @psousa50 , thank you for your reply!

Creating an empty mock ref makes sense and will work, but how do you use it to expect an empty resultset? Using

{% call dbt_unit_testing.expect() %}
  SELECT 1 WHERE FALSE
{% endcall %}

will return the can't execute an empty query error.

@real-soothsayer
Copy link

real-soothsayer commented Oct 16, 2023

For me, it works when you specify the column names, even if the query will return empty

{% call dbt_unit_testing.expect() %}
  SELECT '' as col1, '' as col2, ... WHERE FALSE
{% endcall %}

ps: I'm using dbt with automate-dv package

@psousa50 psousa50 added the enhancement New feature or request label Jan 11, 2024
@psousa50
Copy link
Collaborator

Please take a look at the latest release (0.4.0) and check if it fixes this issue.

Thank you!

@darian-heede
Copy link
Author

Hi @psousa50, I tested dbt_unit_testing.expect_no_rows() with v0.4.0 and still got the error can't execute an empty query. The issue seems to be fixed in v0.4.12. This is exactly what I need 😃 thank you very much for adding this feature!

@SheldonNunes
Copy link

SheldonNunes commented Jul 18, 2024

I got the following error when trying to use WHERE FALSE solution:
Query without FROM clause cannot have a WHERE clause

For those who are having this issue but haven't been able to update versions I found a workaround:
SELECT 1 FROM (SELECT 1) WHERE false

You may have to replace SELECT 1 with the actual column names in order for it to configure the expected result correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants