-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOC-348] Docs for asset and op unit testing
- Loading branch information
1 parent
23beba5
commit 920cadc
Showing
14 changed files
with
308 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...pets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-combo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import dagster as dg | ||
|
||
|
||
class SeparatorConfig(dg.Config): | ||
separator: str | ||
|
||
|
||
@dg.asset | ||
def processed_file( | ||
primary_file: str, secondary_file: str, config: SeparatorConfig | ||
) -> str: | ||
return f"{primary_file}{config.separator}{secondary_file}" | ||
|
||
|
||
# highlight-start | ||
def test_processed_file() -> None: | ||
assert ( | ||
processed_file( | ||
primary_file="abc", | ||
secondary_file="def", | ||
config=SeparatorConfig(separator=","), | ||
) | ||
== "abc,def" | ||
) | ||
# highlight-end |
18 changes: 18 additions & 0 deletions
18
...ets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dagster as dg | ||
|
||
|
||
class FilepathConfig(dg.Config): | ||
path: str | ||
|
||
|
||
@dg.asset | ||
def loaded_file(config: FilepathConfig) -> str: | ||
with open(config.path) as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_loaded_file() -> None: | ||
assert loaded_file(FilepathConfig(path="path1.txt")) == "contents1" | ||
assert loaded_file(FilepathConfig(path="path2.txt")) == "contents2" | ||
# highlight-end |
14 changes: 14 additions & 0 deletions
14
...ts/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-context.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.asset(partitions_def=dg.DailyPartitionsDefinition("2024-01-01")) | ||
def loaded_file(context: dg.AssetExecutionContext) -> str: | ||
with open(f"path_{context.partition_key}.txt") as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_loaded_file() -> None: | ||
context = dg.build_asset_context(partition_key="2024-08-16") | ||
assert loaded_file(context) == "Contents for August 16th, 2024" | ||
# highlight-end |
18 changes: 18 additions & 0 deletions
18
...docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-dependency.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.asset | ||
def loaded_file() -> str: | ||
with open("path.txt") as file: | ||
return file.read() | ||
|
||
|
||
@dg.asset | ||
def processed_file(loaded_file: str) -> str: | ||
return loaded_file.strip() | ||
|
||
|
||
# highlight-start | ||
def test_processed_file() -> None: | ||
assert processed_file(" contents ") == "contents" | ||
# highlight-end |
13 changes: 13 additions & 0 deletions
13
...ocs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-no-argument.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.asset | ||
def loaded_file() -> str: | ||
with open("path.txt") as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_loaded_file() -> None: | ||
assert loaded_file() == "contents" | ||
# highlight-end |
21 changes: 21 additions & 0 deletions
21
...s/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/asset-resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mock | ||
from dagster_aws.s3 import S3FileHandle, S3FileManager | ||
|
||
import dagster as dg | ||
|
||
|
||
@dg.asset | ||
def loaded_file(file_manager: S3FileManager) -> str: | ||
return file_manager.read_data(S3FileHandle("bucket", "path.txt")) | ||
|
||
|
||
# highlight-start | ||
def test_file() -> None: | ||
mocked_resource = mock.Mock(spec=S3FileManager) | ||
mocked_resource.read_data.return_value = "contents" | ||
|
||
assert loaded_file(mocked_resource) == "contents" | ||
assert mocked_resource.read_data.called_once_with( | ||
S3FileHandle("bucket", "path.txt") | ||
) | ||
# highlight-end |
25 changes: 25 additions & 0 deletions
25
...nippets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-combo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import dagster as dg | ||
|
||
|
||
class SeparatorConfig(dg.Config): | ||
separator: str | ||
|
||
|
||
@dg.op | ||
def process_file( | ||
primary_file: str, secondary_file: str, config: SeparatorConfig | ||
) -> str: | ||
return f"{primary_file}{config.separator}{secondary_file}" | ||
|
||
|
||
# highlight-start | ||
def test_process_file() -> None: | ||
assert ( | ||
process_file( | ||
primary_file="abc", | ||
secondary_file="def", | ||
config=SeparatorConfig(separator=","), | ||
) | ||
== "abc,def" | ||
) | ||
# highlight-end |
18 changes: 18 additions & 0 deletions
18
...ippets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dagster as dg | ||
|
||
|
||
class FilepathConfig(dg.Config): | ||
path: str | ||
|
||
|
||
@dg.op | ||
def load_file(config: FilepathConfig) -> str: | ||
with open(config.path) as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_load_file() -> None: | ||
assert load_file(FilepathConfig(path="path1.txt")) == "contents1" | ||
assert load_file(FilepathConfig(path="path2.txt")) == "contents2" | ||
# highlight-end |
14 changes: 14 additions & 0 deletions
14
...ppets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-context.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.op | ||
def load_file(context: dg.OpExecutionContext) -> str: | ||
with open(f"path_{context.partition_key}.txt") as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_load_file() -> None: | ||
context = dg.build_asset_context(partition_key="2024-08-16") | ||
assert load_file(context) == "Contents for August 16th, 2024" | ||
# highlight-end |
12 changes: 12 additions & 0 deletions
12
...ts/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-dependency.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.op | ||
def process_file(loaded_file: str) -> str: | ||
return loaded_file.strip() | ||
|
||
|
||
# highlight-start | ||
def test_process_file() -> None: | ||
assert process_file(" contents ") == "contents" | ||
# highlight-end |
13 changes: 13 additions & 0 deletions
13
...s/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-no-argument.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import dagster as dg | ||
|
||
|
||
@dg.op | ||
def load_file() -> str: | ||
with open("path.txt") as file: | ||
return file.read() | ||
|
||
|
||
# highlight-start | ||
def test_load_file() -> None: | ||
assert load_file() == "contents" | ||
# highlight-end |
21 changes: 21 additions & 0 deletions
21
...pets/docs_beta_snippets/guides/quality-testing/unit-testing-assets-and-ops/op-resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mock | ||
from dagster_aws.s3 import S3FileHandle, S3FileManager | ||
|
||
import dagster as dg | ||
|
||
|
||
@dg.op | ||
def load_file(file_manager: S3FileManager) -> str: | ||
return file_manager.read_data(S3FileHandle("bucket", "path.txt")) | ||
|
||
|
||
# highlight-start | ||
def test_load_file() -> None: | ||
mocked_resource = mock.Mock(spec=S3FileManager) | ||
mocked_resource.read_data.return_value = "contents" | ||
|
||
assert load_file(mocked_resource) == "contents" | ||
assert mocked_resource.read_data.called_once_with( | ||
S3FileHandle("bucket", "path.txt") | ||
) | ||
# highlight-end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters