https://dagster.io/ logo
#ask-ai
Title
# ask-ai
j

Jérôme MONTIGNY

02/20/2024, 10:50 AM
How do I check what tests will be run using the define_asset_job function ?
s

Scout

02/20/2024, 10:50 AM
The documentation does not provide a direct method to check what tests will be run using the
define_asset_job
function. However, it does mention that Dagster has built-in support for testing, including the ability to test asset jobs. To test asset jobs, you would typically write unit tests for the individual assets and then possibly for the job as a whole. For testing individual assets, you can invoke them directly as if they were regular Python functions. For testing the job, you can use the
JobDefinition.execute_in_process
method to synchronously execute the job and inspect the results, such as checking for success or failure and examining the outputs produced by the assets. Here's an example from the documentation on how to write a test for an asset and a job:
Copy code
python from dagster import asset, define_asset_job, JobDefinition  @asset def my_asset():     # asset logic here     ...  my_job = define_asset_job("my_job", selection=["my_asset"])  def test_my_asset():     # Test the asset logic     assert my_asset() == expected_output  def test_my_job():     # Test the job execution     result = my_job.execute_in_process()     assert result.success     # Additional assertions can be made based on the job's execution results
To see which tests will be run, you would typically look at the test suite you have written for your assets and jobs. If you are using a test runner like pytest, you can also use its mechanisms to discover and list tests without running them, such as using the
--collect-only
flag. If you are looking for a way to programmatically inspect which tests are associated with a
define_asset_job
job definition, the documentation does not provide details on this. You would need to follow standard Python testing practices to organize and run your tests.
j

Jérôme MONTIGNY

02/20/2024, 10:58 AM
@Scout In the following case, how do I see which dbt tests will be run ?
Copy code
dbt_selection = build_dbt_asset_selection(
    [dbt_project_name_dbt_assets], dbt_select="fqn:some_folder"
)

selection = dbt_selection.upstream().required_multi_asset_neighbors()

# Define the job
job = define_asset_job(
    "job_name",
    selection,
)
s

Scout

02/20/2024, 10:58 AM
The
build_dbt_asset_selection
function is used to create an asset selection for a set of dbt resources based on a dbt selection string. In the code snippet you provided,
build_dbt_asset_selection
is used to select dbt assets that match the fully qualified name (fqn) within a specific folder (
"fqn:some_folder"
). The
upstream().required_multi_asset_neighbors()
method is then used to expand the selection to include all assets that are upstream of the selected dbt assets and are required for their computation. The
define_asset_job
function creates a job that targets the specified selection of assets. To see which dbt tests will be run as part of this job, you would need to refer to the dbt project's configuration and the tests that are associated with the models within
"some_folder"
. The dbt tests that will be executed are those that are associated with the dbt models selected by the
dbt_select
parameter. Dagster itself does not provide a direct way to list out the dbt tests that will be run as part of the job execution. However, you can use dbt's native mechanisms to list the tests associated with your selection. For example, you could run the following dbt command in your terminal to list the tests that would be run for the models in `"some_folder"`:
Copy code
sh dbt test --select 'fqn:some_folder'
This dbt command will show you the tests that dbt would run for the models in the specified folder. It's important to note that this command is part of dbt's CLI and is separate from Dagster's job execution. Dagster will run the dbt tests as part of the job execution if the dbt assets are configured to include tests as asset checks. If you want to ensure that only certain dbt tests are run as part of the Dagster job, you would need to configure your dbt project and assets accordingly, possibly using tags or other selection criteria that dbt supports.
j

Jérôme MONTIGNY

02/20/2024, 11:17 AM
@Scout it seems like the tests for the sources are not executed on dagster, but when use the dbt test command with the same select, they are executed. What could be happening ?
s

Scout

02/20/2024, 11:17 AM
If dbt tests for sources are being executed when you run the
dbt test
command with the same selection criteria directly in the terminal, but not when running through Dagster, there could be a few reasons for this discrepancy: 1. Dagster dbt Integration Configuration: Ensure that your Dagster job is configured to run dbt tests. Dagster's integration with dbt might require specific configuration to execute tests. For example, if you're using
load_assets_from_dbt_project
, you might need to set the
load_assets_from_dbt_project
configuration to use the
dbt build
command instead of
dbt run
, as
dbt build
includes both model materialization and test execution. 2. Asset Selection: The selection criteria used in the
build_dbt_asset_selection
function might not be including the dbt source tests. The selection string you use in Dagster should match the one you use in the terminal. If there's a mismatch, the tests might not be included in the Dagster job. 3. Dagster Version: There was an issue in earlier versions of Dagster where dbt tests would not run when materializing the data. This was addressed in a later version where the default command was changed to
dbt build
, which includes test execution. Make sure you are using a version of Dagster that includes this fix. 4. Execution Context: The context in which dbt tests are executed might differ between a direct dbt CLI invocation and a Dagster job execution. For instance, environment variables, profiles, or targets might be set differently, affecting which tests are run. 5. Dagster Job Execution: When executing the job in Dagster, it's possible that the job is not yielding or returning the expected outputs, which could skip the execution of certain steps, including tests. 6. Dbt Project Configuration: Check your dbt project configuration to ensure that tests for sources are properly defined and that there are no conditional configurations that might prevent tests from running in certain environments or contexts. To troubleshoot this issue, you can: - Review the Dagster job logs to see if there are any messages indicating why the tests were not run. - Check the version of Dagster you are using and update it if necessary to ensure it includes the latest fixes for dbt test execution. - Verify the dbt selection criteria used in Dagster match those you use when running dbt directly. - Ensure that your dbt project is correctly configured to include tests for sources and that these tests are not being excluded by any project-level configurations. If you continue to experience issues, you may want to consult the Dagster community or support channels for more specific guidance based on your setup.
j

Jérôme MONTIGNY

02/20/2024, 11:22 AM
@Scout do you know which Dagster versions were not using dbt build ?
s

Scout

02/20/2024, 11:22 AM
As of my last update, the specific versions of Dagster that did not use
dbt build
by default in the
dagster-dbt
integration are not explicitly listed in the documentation provided to me. However, there was a notable change in the
dagster-dbt
integration where the default behavior was updated to use
dbt build
instead of
dbt run
. The
dbt build
command was introduced in dbt
0.19.0
and combines the functionality of
dbt run
and
dbt test
, executing models, tests, snapshots, and seeds in the correct order. Prior to this change in dbt, users of Dagster would have had to manually configure their jobs to run tests after model execution. To find out when Dagster started using
dbt build
by default, you would typically look at the release notes or the changelog for the
dagster-dbt
library. If you are using a version of
dagster-dbt
that predates the introduction of
dbt build
in dbt itself, or if you are using an older version of Dagster, you may need to manually configure your Dagster job to run dbt tests. To ensure that you are using a version of Dagster that supports
dbt build
, you should update to the latest version of Dagster and the
dagster-dbt
library. Additionally, you can check the documentation or the release notes for the version of Dagster you are using to see if it defaults to
dbt build
or if you need to set it explicitly.
j

Jérôme MONTIGNY

02/20/2024, 1:01 PM
@Scout can you link he post of the issue about dbt build ?
s

Scout

02/20/2024, 1:01 PM
I'm sorry, but as a language model developed by OpenAI, I don't have the capability to provide direct links to external content, such as GitHub issues or posts. However, you can typically find information about specific issues or feature changes by visiting the GitHub repository for the project in question and searching through the issues or pull requests. For Dagster, you would go to the Dagster GitHub repository at:
Copy code
<https://github.com/dagster-io/dagster>
Once there, you can search the issues or pull requests for terms like "dbt build" to find discussions related to the integration of the
dbt build
command within Dagster's
dagster-dbt
library. The Dagster team may also document such changes in their release notes, which can be found in the repository or on the official Dagster documentation website. If you're looking for a specific issue and you know the issue number, you can directly navigate to it by appending the issue number to the URL like so:
Copy code
<https://github.com/dagster-io/dagster/issues/<issue_number>>
Replace
<issue_number>
with the actual number of the issue you're interested in.