|
| 1 | +"""Unit tests for the top-level ``metadata`` block in the wire payload. |
| 2 | +
|
| 3 | +Locks the shape of the dataset ``metadata`` array that |
| 4 | +``_build_check_collection_results_json_dict`` emits when a check collection |
| 5 | +runs a schema check. The block lets the backend refresh the dataset's column |
| 6 | +schema in Cloud, reusing the ``{columnName, sourceDataType}`` shape v3 discover |
| 7 | +emits (DTL-1807). |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from datetime import datetime, timezone |
| 13 | + |
| 14 | +from soda_core.common.logs import Location |
| 15 | +from soda_core.common.metadata_types import ColumnMetadata, SqlDataType |
| 16 | +from soda_core.common.soda_cloud import _build_check_collection_results_json_dict |
| 17 | +from soda_core.contracts.contract_verification import ( |
| 18 | + Check, |
| 19 | + CheckCollectionStatus, |
| 20 | + CheckOutcome, |
| 21 | + CheckResult, |
| 22 | + Contract, |
| 23 | + ContractVerificationResult, |
| 24 | + DataSource, |
| 25 | + YamlFileContentInfo, |
| 26 | +) |
| 27 | +from soda_core.contracts.impl.check_types.schema_check import SchemaCheckResult |
| 28 | + |
| 29 | + |
| 30 | +def _make_check(*, type: str, relative_path: str) -> Check: |
| 31 | + return Check( |
| 32 | + column_name=None, |
| 33 | + type=type, |
| 34 | + qualifier=None, |
| 35 | + name=type, |
| 36 | + relative_path=relative_path, |
| 37 | + identity="abc", |
| 38 | + definition=f"{type}: ...", |
| 39 | + contract_file_line=1, |
| 40 | + contract_file_column=1, |
| 41 | + threshold=None, |
| 42 | + attributes={}, |
| 43 | + location=Location(file_path="fake.yml", line=1, column=1), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _column(name: str, sql_data_type: SqlDataType) -> ColumnMetadata: |
| 48 | + return ColumnMetadata(column_name=name, sql_data_type=sql_data_type) |
| 49 | + |
| 50 | + |
| 51 | +def _make_schema_check_result(actual_columns: list[ColumnMetadata]) -> SchemaCheckResult: |
| 52 | + return SchemaCheckResult( |
| 53 | + check=_make_check(type="schema", relative_path="checks.schema"), |
| 54 | + outcome=CheckOutcome.PASSED, |
| 55 | + expected_columns=[], |
| 56 | + actual_columns=actual_columns, |
| 57 | + expected_column_names_not_actual=[], |
| 58 | + actual_column_names_not_expected=[], |
| 59 | + column_data_type_mismatches=[], |
| 60 | + are_columns_out_of_order=False, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def _make_row_count_check_result() -> CheckResult: |
| 65 | + return CheckResult( |
| 66 | + check=_make_check(type="row_count", relative_path="checks.row_count"), |
| 67 | + outcome=CheckOutcome.PASSED, |
| 68 | + diagnostic_metric_values={"check_rows_tested": 0, "dataset_rows_tested": 0}, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def _make_result( |
| 73 | + *, |
| 74 | + soda_qualified_dataset_name: str, |
| 75 | + dataset_prefix: list[str], |
| 76 | + dataset_name: str, |
| 77 | + check_results: list, |
| 78 | +) -> ContractVerificationResult: |
| 79 | + ts = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
| 80 | + return ContractVerificationResult( |
| 81 | + check_collection=Contract( |
| 82 | + data_source_name="test_ds", |
| 83 | + dataset_prefix=dataset_prefix, |
| 84 | + dataset_name=dataset_name, |
| 85 | + soda_qualified_dataset_name=soda_qualified_dataset_name, |
| 86 | + source=YamlFileContentInfo( |
| 87 | + source_content_str="# c", |
| 88 | + local_file_path="/fake/c.yml", |
| 89 | + soda_cloud_file_id="file-c", |
| 90 | + ), |
| 91 | + ), |
| 92 | + data_source=DataSource(name="test_ds", type="postgres"), |
| 93 | + data_timestamp=ts, |
| 94 | + started_timestamp=ts, |
| 95 | + ended_timestamp=ts, |
| 96 | + status=CheckCollectionStatus.PASSED, |
| 97 | + measurements=[], |
| 98 | + check_results=check_results, |
| 99 | + sending_results_to_soda_cloud_failed=False, |
| 100 | + log_records=[], |
| 101 | + post_processing_stages=[], |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def test_metadata_present_with_schema_of_column_name_and_source_data_type(): |
| 106 | + """A collection that ran a schema check → one ``metadata`` entry whose |
| 107 | + ``schema`` mirrors the discovered columns as ``{columnName, sourceDataType}``.""" |
| 108 | + result = _make_result( |
| 109 | + soda_qualified_dataset_name="postgres/public/customers", |
| 110 | + dataset_prefix=["public"], |
| 111 | + dataset_name="customers", |
| 112 | + check_results=[ |
| 113 | + _make_schema_check_result( |
| 114 | + actual_columns=[ |
| 115 | + _column("id", SqlDataType(name="integer")), |
| 116 | + _column("name", SqlDataType(name="character varying", character_maximum_length=255)), |
| 117 | + ] |
| 118 | + ) |
| 119 | + ], |
| 120 | + ) |
| 121 | + |
| 122 | + payload = _build_check_collection_results_json_dict([result], wire_source="soda-contract") |
| 123 | + |
| 124 | + assert payload["metadata"] == [ |
| 125 | + { |
| 126 | + "datasetQualifiedName": "postgres/public/customers", |
| 127 | + "schema": [ |
| 128 | + {"columnName": "id", "sourceDataType": "integer"}, |
| 129 | + {"columnName": "name", "sourceDataType": "character varying"}, |
| 130 | + ], |
| 131 | + } |
| 132 | + ] |
| 133 | + |
| 134 | + |
| 135 | +def test_metadata_source_data_type_is_unparameterized(): |
| 136 | + """``sourceDataType`` is the raw un-parameterized type (``character |
| 137 | + varying``), NOT the parameterized ``character varying(255)`` form.""" |
| 138 | + result = _make_result( |
| 139 | + soda_qualified_dataset_name="postgres/public/customers", |
| 140 | + dataset_prefix=["public"], |
| 141 | + dataset_name="customers", |
| 142 | + check_results=[ |
| 143 | + _make_schema_check_result( |
| 144 | + actual_columns=[_column("name", SqlDataType(name="character varying", character_maximum_length=255))] |
| 145 | + ) |
| 146 | + ], |
| 147 | + ) |
| 148 | + payload = _build_check_collection_results_json_dict([result], wire_source="soda-contract") |
| 149 | + assert payload["metadata"][0]["schema"][0]["sourceDataType"] == "character varying" |
| 150 | + |
| 151 | + |
| 152 | +def test_metadata_absent_when_no_schema_check(): |
| 153 | + """No schema check → no ``metadata`` key at all (``to_jsonnable`` strips it).""" |
| 154 | + result = _make_result( |
| 155 | + soda_qualified_dataset_name="postgres/public/customers", |
| 156 | + dataset_prefix=["public"], |
| 157 | + dataset_name="customers", |
| 158 | + check_results=[_make_row_count_check_result()], |
| 159 | + ) |
| 160 | + payload = _build_check_collection_results_json_dict([result], wire_source="soda-contract") |
| 161 | + assert "metadata" not in payload |
| 162 | + |
| 163 | + |
| 164 | +def test_metadata_absent_when_schema_check_has_no_columns(): |
| 165 | + """A schema check that discovered no columns (missing dataset) → no |
| 166 | + ``metadata`` entry. We never send an empty schema — that would soft-delete |
| 167 | + every column on the backend.""" |
| 168 | + result = _make_result( |
| 169 | + soda_qualified_dataset_name="postgres/public/customers", |
| 170 | + dataset_prefix=["public"], |
| 171 | + dataset_name="customers", |
| 172 | + check_results=[_make_schema_check_result(actual_columns=[])], |
| 173 | + ) |
| 174 | + payload = _build_check_collection_results_json_dict([result], wire_source="soda-contract") |
| 175 | + assert "metadata" not in payload |
| 176 | + |
| 177 | + |
| 178 | +def test_metadata_one_entry_per_dataset_in_session(): |
| 179 | + """A combined multi-file session emits one ``metadata`` entry per distinct |
| 180 | + dataset that ran a schema check — prefixed and non-prefixed dqns alike.""" |
| 181 | + prefixed = _make_result( |
| 182 | + soda_qualified_dataset_name="postgres/public/customers", |
| 183 | + dataset_prefix=["public"], |
| 184 | + dataset_name="customers", |
| 185 | + check_results=[_make_schema_check_result(actual_columns=[_column("id", SqlDataType(name="integer"))])], |
| 186 | + ) |
| 187 | + non_prefixed = _make_result( |
| 188 | + soda_qualified_dataset_name="postgres/orders", |
| 189 | + dataset_prefix=[], |
| 190 | + dataset_name="orders", |
| 191 | + check_results=[_make_schema_check_result(actual_columns=[_column("total", SqlDataType(name="numeric"))])], |
| 192 | + ) |
| 193 | + no_schema = _make_result( |
| 194 | + soda_qualified_dataset_name="postgres/public/events", |
| 195 | + dataset_prefix=["public"], |
| 196 | + dataset_name="events", |
| 197 | + check_results=[_make_row_count_check_result()], |
| 198 | + ) |
| 199 | + |
| 200 | + payload = _build_check_collection_results_json_dict( |
| 201 | + [prefixed, non_prefixed, no_schema], wire_source="data-standard" |
| 202 | + ) |
| 203 | + |
| 204 | + assert payload["metadata"] == [ |
| 205 | + { |
| 206 | + "datasetQualifiedName": "postgres/public/customers", |
| 207 | + "schema": [{"columnName": "id", "sourceDataType": "integer"}], |
| 208 | + }, |
| 209 | + { |
| 210 | + "datasetQualifiedName": "postgres/orders", |
| 211 | + "schema": [{"columnName": "total", "sourceDataType": "numeric"}], |
| 212 | + }, |
| 213 | + ] |
0 commit comments