Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/dfo_pipelines/data_commons/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
recipe, so every export states its recipe, or that it has none, on its spec.
"""

import hashlib
import logging
import tempfile
from dataclasses import dataclass, field
Expand Down Expand Up @@ -178,6 +179,16 @@ def minted_provenance_dcid(self) -> str:
"""
return mint_dcid(prefix="provenance", token=self.provenance_dcid)

@property
def documentation_version(self) -> str:
"""Hex sha256 of the `DOCUMENTATION_FILES` alone, not of the export code"""
digest = hashlib.sha256()
for file_name in DOCUMENTATION_FILES.values():
content = (self.docs_dir / file_name).read_bytes()
# The length prefix keeps text moved between files from hashing the same.
digest.update(len(content).to_bytes(8, "big") + content)
return digest.hexdigest()


@dataclass(frozen=True, kw_only=True)
class ExportMetadata:
Expand Down
7 changes: 7 additions & 0 deletions src/dfo_pipelines/defs/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ def automation_sensor_default_status() -> dg.DefaultSensorStatus:
)
)
).with_label("eager_when_checks_pass")

# eager() ignores code versions, and an export's code version is its documentation.
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS = EAGER_WHEN_CHECKS_PASS.replace(
old=dg.AutomationCondition.any_deps_updated(),
new=dg.AutomationCondition.any_deps_updated()
| dg.AutomationCondition.code_version_changed(),
).with_label("eager_on_deps_or_code_change_when_checks_pass")
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/imf/weo/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -221,7 +224,8 @@ def fetch_weo_publication_date(version: tuple[str, int]) -> date:

@dg.asset(
group_name="imf_weo",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=IMF_WEO_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["imf_weo", "dc_weo_observations"]),
dg.AssetKey(["imf_weo", "dc_weo_provenance"]),
Expand Down
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/oecd/dac1/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -239,7 +242,8 @@ def oecd_dac1_raw_metadata(

@dg.asset(
group_name="oecd_dac1",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=OECD_DAC1_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["oecd_dac1", "dc_dac1_donor_observations"]),
dg.AssetKey(["oecd_dac1", "dc_dac1_flow_observations"]),
Expand Down
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/oecd/dac2a/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -229,7 +232,8 @@ def oecd_dac2a_raw_metadata(

@dg.asset(
group_name="oecd_dac2a",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=OECD_DAC2A_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["oecd_dac2a", "dc_dac2a_observations"]),
dg.AssetKey(["oecd_dac2a", "dc_dac2a_provenance"]),
Expand Down
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/oecd/dac2b/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -228,7 +231,8 @@ def oecd_dac2b_raw_metadata(

@dg.asset(
group_name="oecd_dac2b",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=OECD_DAC2B_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["oecd_dac2b", "dc_dac2b_observations"]),
dg.AssetKey(["oecd_dac2b", "dc_dac2b_provenance"]),
Expand Down
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/oecd/dac_crs/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -240,7 +243,8 @@ def oecd_dac_crs_raw_metadata(

@dg.asset(
group_name="oecd_dac_crs",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=CRS_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["oecd_dac_crs", "dc_dac_crs_observations"]),
dg.AssetKey(["oecd_dac_crs", "dc_dac_crs_provenance"]),
Expand Down
8 changes: 5 additions & 3 deletions src/dfo_pipelines/defs/one/conversion_tables/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -115,7 +115,8 @@ def _export(

@dg.asset(
group_name="one_conversion_tables",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=DAC_EXPORT_SPEC.documentation_version,
deps=_dbt_model_keys(table="dac"),
)
def one_conversion_tables_dac_dc_export(
Expand All @@ -134,7 +135,8 @@ def one_conversion_tables_dac_dc_export(

@dg.asset(
group_name="one_conversion_tables",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=IMF_EXPORT_SPEC.documentation_version,
deps=_dbt_model_keys(table="imf"),
)
def one_conversion_tables_imf_dc_export(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -529,7 +532,8 @@ def one_imputed_multilateral_sectors_raw_metadata(

@dg.asset(
group_name="one_imputed_multilateral_sectors",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=IMPUTED_MULTILATERAL_SECTORS_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(
[
Expand Down
8 changes: 6 additions & 2 deletions src/dfo_pipelines/defs/world_bank/ids/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
export_bundle,
)
from dfo_pipelines.data_commons.ingestion import ingest_bundle
from dfo_pipelines.defs.automation import EAGER_WHEN_CHECKS_PASS
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
EAGER_WHEN_CHECKS_PASS,
)
from dfo_pipelines.defs.dfo.ontology.assets import dfo_ontology_dc_ingestion
from dfo_pipelines.defs.dfo.shared.assets import dfo_shared_dc_ingestion
from dfo_pipelines.defs.resources import (
Expand Down Expand Up @@ -403,7 +406,8 @@ def combine_ids_bulk_data_files(input_folder: UPath, output_file: UPath) -> int:

@dg.asset(
group_name="world_bank_ids",
automation_condition=EAGER_WHEN_CHECKS_PASS,
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=IDS_EXPORT_SPEC.documentation_version,
deps=[
dg.AssetKey(["world_bank_ids", "dc_ids_debt_observations"]),
dg.AssetKey(["world_bank_ids", "dc_ids_economy_observations"]),
Expand Down
46 changes: 46 additions & 0 deletions tests/data_commons/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,52 @@ def test_export_bundle_fails_before_writing_when_a_documentation_file_is_missing
assert not any(out_dir.iterdir())


def spec_with_documentation(tmp_path: Path, name: str, texts: list[str]) -> ExportSpec:
"""The fixture spec over a docs directory holding `texts`, one per file"""
docs_dir = tmp_path / name
docs_dir.mkdir()
for file_name, text in zip(DOCUMENTATION_FILES.values(), texts, strict=True):
(docs_dir / file_name).write_text(text)
return replace(EXPORT_SPEC, docs_dir=docs_dir)


def documentation_texts() -> list[str]:
"""One distinct text per documentation file"""
return [f"text {index}" for index in range(len(DOCUMENTATION_FILES))]


# Each edit as the documentation texts before and after it.
DOCUMENTATION_EDITS = {
**{
f"{file_name} changed": (
documentation_texts(),
[
f"{text} changed" if index == changed else text
for index, text in enumerate(documentation_texts())
],
)
for changed, file_name in enumerate(DOCUMENTATION_FILES.values())
},
"text moved between files": (
["ab", "c", *documentation_texts()[2:]],
["a", "bc", *documentation_texts()[2:]],
),
}


@pytest.mark.parametrize(
("original", "edited"), DOCUMENTATION_EDITS.values(), ids=DOCUMENTATION_EDITS
)
def test_documentation_version_changes_with_each_documentation_edit(
tmp_path: Path, original: list[str], edited: list[str]
) -> None:
"""Test an edit to any documentation file, or text moved between them, changes the version"""
before = spec_with_documentation(tmp_path, "before", original)
after = spec_with_documentation(tmp_path, "after", edited)

assert after.documentation_version != before.documentation_version


def test_export_bundle_requires_exactly_one_provenance(tmp_path: Path) -> None:
"""Test the export fails when the provenance model holds two rows"""
connection = connect(tmp_path)
Expand Down
93 changes: 92 additions & 1 deletion tests/test_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import pytest

from dfo_pipelines.definitions import defs
from dfo_pipelines.defs.automation import automation_sensor_default_status
from dfo_pipelines.defs.automation import (
EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
automation_sensor_default_status,
)

SENSOR_NAME = "automation_condition_sensor"

Expand Down Expand Up @@ -961,3 +964,91 @@ def test_a_failed_rate_diagnostic_stops_every_export_reading_its_table(
key.to_user_string(): 0 if key in stopped_export_keys else 1
for key in export_keys
}


SYNTHETIC_PARENT_KEY = dg.AssetKey("parent")
SYNTHETIC_PARENT_CHECK_KEY = dg.AssetCheckKey(SYNTHETIC_PARENT_KEY, "parent_check")
SYNTHETIC_EXPORT_KEY = dg.AssetKey("export")


def _synthetic_export_definitions(code_version: str) -> dg.Definitions:
"""A parent with a blocking check, and an export at `code_version` below it"""

@dg.asset(key=SYNTHETIC_PARENT_KEY)
def parent() -> None: ...

@dg.asset_check(asset=parent, name=SYNTHETIC_PARENT_CHECK_KEY.name, blocking=True)
def parent_check() -> dg.AssetCheckResult:
return dg.AssetCheckResult(passed=True)

@dg.asset(
key=SYNTHETIC_EXPORT_KEY,
deps=[parent],
automation_condition=EAGER_ON_DEPS_OR_CODE_CHANGE_WHEN_CHECKS_PASS,
code_version=code_version,
)
def export() -> None: ...

return dg.Definitions(assets=[parent, export], asset_checks=[parent_check])


def _report_parent_check(instance: dg.DagsterInstance, *, passed: bool) -> None:
"""Report an evaluation of the parent's check, with no new materialization"""
instance.report_runless_asset_event(
dg.AssetCheckEvaluation(
asset_key=SYNTHETIC_PARENT_KEY,
check_name=SYNTHETIC_PARENT_CHECK_KEY.name,
passed=passed,
)
)


def _export_requests(
instance: dg.DagsterInstance,
code_version: str,
previous: dg.EvaluateAutomationConditionsResult,
) -> tuple[dg.EvaluateAutomationConditionsResult, int]:
"""Tick with the export at `code_version`, returning the result and its requests"""
result = dg.evaluate_automation_conditions(
defs=_synthetic_export_definitions(code_version),
instance=instance,
cursor=previous.cursor,
)
return result, result.get_num_requested(SYNTHETIC_EXPORT_KEY)


def _materialized_synthetic_graph(
instance: dg.DagsterInstance,
) -> dg.EvaluateAutomationConditionsResult:
"""Materialize the parent, with its check passing, then the export, at version 1"""
baseline = dg.evaluate_automation_conditions(
defs=_synthetic_export_definitions("1"), instance=instance
)
for key in (SYNTHETIC_PARENT_KEY, SYNTHETIC_EXPORT_KEY):
instance.report_runless_asset_event(dg.AssetMaterialization(asset_key=key))
_report_parent_check(instance, passed=True)
settled, requested = _export_requests(instance, "1", baseline)
assert requested == 0
return settled


def test_a_new_code_version_requests_the_export_once() -> None:
"""Test a changed code version alone requests an up-to-date export on one tick"""
with dg.DagsterInstance.ephemeral() as instance:
settled = _materialized_synthetic_graph(instance)
changed, requested_on_change = _export_requests(instance, "2", settled)
_, requested_after = _export_requests(instance, "2", changed)

assert (requested_on_change, requested_after) == (1, 0)


def test_a_new_code_version_waits_for_the_parent_checks_to_pass() -> None:
"""Test a version change during a failed blocking check is held until it passes"""
with dg.DagsterInstance.ephemeral() as instance:
settled = _materialized_synthetic_graph(instance)
_report_parent_check(instance, passed=False)
held, requested_while_failed = _export_requests(instance, "2", settled)
_report_parent_check(instance, passed=True)
_, requested_once_passed = _export_requests(instance, "2", held)

assert (requested_while_failed, requested_once_passed) == (0, 1)
Loading
Loading