import json import unittest from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] CRM_PACK = REPO_ROOT / "salesforce-crm-standard" def load_json(path: Path): with path.open(encoding="utf-8") as handle: return json.load(handle) def object_spec(api_name: str): for spec in load_json(CRM_PACK / "objects" / "standard_objects_spec.json"): if spec.get("apiName") == api_name: return spec raise AssertionError(f"{api_name} object spec missing") def field_spec(spec: dict, api_name: str): for field in spec.get("fields", []): if field.get("apiName") == api_name: return field raise AssertionError(f"{spec.get('apiName')}.{api_name} field spec missing") def related_list_rule(key: str): payload = load_json(CRM_PACK / "related_lists" / "standard_related_list_rules.json") for rule in payload.get("rules", []): if rule.get("key") == key: return rule raise AssertionError(f"{key} related-list rule missing") class DisplayNameFieldContractsTest(unittest.TestCase): def assert_product_display_name(self, object_api_name: str): spec = object_spec(object_api_name) self.assertEqual(spec.get("displayNameField"), "Product2Id") product = field_spec(spec, "Product2Id") self.assertEqual(product.get("type"), "reference") self.assertEqual(product.get("referenceTo"), ["Product2"]) self.assertEqual(product.get("relationshipName"), "Product2") def test_line_item_objects_use_product_as_display_name(self): # These objects are central to Add Products / Create Quote flows. Product # identity is the user-facing label; PricebookEntry is implementation # detail unless a workflow explicitly needs pricebook-row inspection. for object_api_name in ("OpportunityLineItem", "QuoteLineItem", "OrderItem"): with self.subTest(object=object_api_name): self.assert_product_display_name(object_api_name) def test_product_related_lists_start_with_product_column(self): expected = { "opportunity-line-items": "Product2Id", "quote-line-items": "Product2Id", "order-line-items": "Product2Id", } for key, first_field in expected.items(): with self.subTest(related_list=key): rule = related_list_rule(key) self.assertGreater(len(rule.get("fields", [])), 0) self.assertEqual(rule["fields"][0], first_field) if __name__ == "__main__": unittest.main()