from __future__ import annotations from copy import deepcopy import pytest from pingcap_datapipelines.month_end_arr import ( EXTERNAL_ID_FIELD, TARGET_FIELDS, RowValidationError, map_month_end_arr_row, validate_target_contract, ) def test_blank_account_is_skipped() -> None: assert map_month_end_arr_row({"account_id": " "}) is None def test_month_end_arr_mapping_is_explicit_and_deterministic() -> None: source = { "region": " APAC ", "product_family": "TiDB Cloud", "product_type": "Cloud", "account_id": "001-example", "revenue_month": "2026-07", "month_end_arr_amount": "$1,234.50", } first = map_month_end_arr_row(source) second = map_month_end_arr_row(source) assert first is not None assert second is not None assert first.external_key == second.external_key assert len(first.external_key) == 64 assert first.payload == { "attributes": {"type": "Month_End_ARR__c"}, "Outbound_Key__c": first.external_key, "Account_ID__c": "001-example", "Order_Region__c": "APAC", "Product_Family__c": "TiDB Cloud", "Product_Type__c": "Cloud", "Revenue_Month__c": "2026-07-01", "Month_End_ARR_Amount__c": 1234.5, } assert first.source_account_id == "001-example" def test_invalid_amount_fails_source_preflight() -> None: with pytest.raises(RowValidationError, match="not numeric"): map_month_end_arr_row( { "region": "APAC", "product_family": "TiDB Cloud", "product_type": "Cloud", "account_id": "001-example", "revenue_month": "2026-07-01", "month_end_arr_amount": "not-money", } ) def test_target_contract_requires_unique_external_key_and_valid_picklists() -> None: record = map_month_end_arr_row( { "region": "APAC", "product_family": "TiDB Cloud", "product_type": "Cloud", "account_id": "001-example", "revenue_month": "2026-07-01", "month_end_arr_amount": "10", } ) assert record is not None describe = _target_describe() validate_target_contract(describe, [record]) non_unique = deepcopy(describe) next(field for field in non_unique["fields"] if field["name"] == EXTERNAL_ID_FIELD)["unique"] = False with pytest.raises(RowValidationError, match="must be unique"): validate_target_contract(non_unique, [record]) bad_region = deepcopy(record) bad_region.payload["Order_Region__c"] = "LATAM" with pytest.raises(RowValidationError, match="invalid Order_Region__c"): validate_target_contract(describe, [bad_region]) def _target_describe() -> dict[str, object]: fields = [{"name": name, "type": "string", "createable": True, "updateable": True} for name in TARGET_FIELDS] next(field for field in fields if field["name"] == "Order_Region__c").update( {"type": "picklist", "picklistValues": [{"value": "APAC", "active": True}]} ) next(field for field in fields if field["name"] == "Product_Type__c").update( {"type": "picklist", "picklistValues": [{"value": "Cloud", "active": True}]} ) fields.append( { "name": EXTERNAL_ID_FIELD, "type": "string", "unique": True, "createable": True, "updateable": True, } ) return {"fields": fields}