<@U088WCP3B4P> i want a test thats checks if a tab...
# ask-elementary-ai
n
@kapa.ai i want a test thats checks if a table contains data and only triggers this test when a environment variable = acc
k
A message from kapa.ai
You can create a custom dbt test to check if a table is empty, as shown in the knowledge sources. Here’s an example of such a test:
Copy code
sql {% test empty_table(model) %}     select 1     from {{ model }}     limit 1 {% endtest %}
To ensure this test only runs when an environment variable (e.g.,
envt
) is set to
acc
, you can use dbt’s
config
with a
where
clause or conditionally include the test in your YAML using Jinja. For example, in your `schema.yml`:
Copy code
yaml models:   - name: your_table     tests:       - empty_table:           enabled: "{{ var('envt', 'dev') == 'acc' }}"
This way, the test will only be enabled and executed when the environment variable
envt
is set to
acc
. This approach is supported by dbt and works with Elementary, as Elementary will pick up and alert on any dbt test, including custom ones, as long as they are enabled and executed in your environment [Table/view exists test](https://github.com/elementary-data/elementary/issues/1388), [Can elementary pick up if table is empty?](https://slack.com/archives/C02CTC89LAX/p1661744454211599). If you need to set the variable, you can do so when running dbt:
Copy code
bash dbt test --vars 'envt:acc'
This ensures your test only triggers in the desired environment.