import json import subprocess import sys import tempfile import unittest from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] SCRIPT = REPO_ROOT / "scripts" / "check_object_specs_reserved_fields.py" def write_pack(root: Path, field_api_names): pack = root / "sample-pack" objects = pack / "objects" objects.mkdir(parents=True) (pack / "pack.json").write_text( json.dumps( { "manifestFormatVersion": "1", "name": "sample-pack", "displayName": "Sample Pack", "category": "app-pack", "version": "0.0.1", "components": [ { "kind": "object_spec", "businessKey": "sample", "manageability": "locked", "path": "objects/standard_objects_spec.json", }, { "kind": "list_fields", "businessKey": "lists", "manageability": "defaults_only", "path": "ux/standard_listfields.json", }, ], }, indent=2, ) ) (objects / "standard_objects_spec.json").write_text( json.dumps( [ { "apiName": "Campaign", "label": "Campaign", "tableType": "standard_object", "category": "crm", "sharingModel": "PublicRead", "enableHierarchySharing": True, "description": "Campaign", "fields": [ {"apiName": name, "label": name, "type": "string"} for name in field_api_names ], } ], indent=2, ) ) ux = pack / "ux" ux.mkdir() (ux / "standard_listfields.json").write_text( json.dumps({"Campaign": ["Name", "OwnerId"]}, indent=2) ) return pack class CheckObjectSpecsReservedFieldsTest(unittest.TestCase): def run_check(self, pack: Path): return subprocess.run( [sys.executable, str(SCRIPT), str(pack)], cwd=REPO_ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) def test_accepts_system_field_references_outside_object_spec_fields(self): with tempfile.TemporaryDirectory() as tmp: pack = write_pack(Path(tmp), ["Name"]) result = self.run_check(pack) self.assertEqual(result.returncode, 0, result.stderr + result.stdout) def test_rejects_ownerid_declared_as_pack_object_field(self): with tempfile.TemporaryDirectory() as tmp: pack = write_pack(Path(tmp), ["Name", "OwnerId"]) result = self.run_check(pack) self.assertNotEqual(result.returncode, 0) self.assertIn("reserved_field_name", result.stderr) self.assertIn("Campaign.OwnerId", result.stderr) if __name__ == "__main__": unittest.main()