1515
1616import pytest
1717
18+ from infrahub_sdk .schema import InfrahubSchemaRead , InfrahubSchemaWrite
1819from infrahub_sdk .schema .generated import read as read_module
1920from infrahub_sdk .schema .generated import write as write_module
2021
2122if TYPE_CHECKING :
2223 from pydantic import BaseModel
2324
2425_GENERATED_DIR = Path (write_module .__file__ ).parent
25- _EXPECTED_FAMILIES = [
26- "GeneratedAttributeSchema" ,
27- "GeneratedRelationshipSchema" ,
28- "GeneratedBaseNodeSchema" ,
29- "GeneratedNodeSchema" ,
30- "GeneratedGenericSchema" ,
26+ # Each family pairs its write-variant class name with its read-variant class name.
27+ _FAMILY_PAIRS = [
28+ ("AttributeSchemaWrite" , "AttributeSchemaRead" ),
29+ ("RelationshipSchemaWrite" , "RelationshipSchemaRead" ),
30+ ("BaseNodeSchemaWrite" , "BaseNodeSchemaRead" ),
31+ ("NodeSchemaWrite" , "NodeSchemaRead" ),
32+ ("GenericSchemaWrite" , "GenericSchemaRead" ),
3133]
34+ _WRITE_FAMILIES = [write for write , _ in _FAMILY_PAIRS ]
3235
3336
3437@pytest .mark .parametrize ("filename" , ["write.py" , "read.py" ])
@@ -38,31 +41,39 @@ def test_generated_files_present_with_do_not_edit_header(filename: str) -> None:
3841 assert "do not edit" in path .read_text ().splitlines ()[0 ].lower ()
3942
4043
41- @pytest .mark .parametrize ("family " , _EXPECTED_FAMILIES )
42- def test_expected_model_families_present_in_both_variants ( family : str ) -> None :
43- assert hasattr (write_module , family ), f"write variant is missing { family } "
44- assert hasattr (read_module , family ), f"read variant is missing { family } "
44+ @pytest .mark .parametrize (( "write_family " , "read_family" ), _FAMILY_PAIRS )
45+ def test_expected_model_families_present_in_each_variant ( write_family : str , read_family : str ) -> None :
46+ assert hasattr (write_module , write_family ), f"write variant is missing { write_family } "
47+ assert hasattr (read_module , read_family ), f"read variant is missing { read_family } "
4548
4649
47- @pytest .mark .parametrize ("family" , _EXPECTED_FAMILIES )
50+ @pytest .mark .parametrize ("family" , _WRITE_FAMILIES )
4851def test_write_variant_forbids_extra_fields (family : str ) -> None :
4952 model : type [BaseModel ] = getattr (write_module , family )
5053 assert model .model_config .get ("extra" ) == "forbid" , (
5154 f"write variant { family } must set extra='forbid' so non-settable fields are rejected"
5255 )
5356
5457
55- @pytest .mark .parametrize ("family " , _EXPECTED_FAMILIES )
56- def test_read_variant_is_superset_of_write_variant (family : str ) -> None :
57- write_model : type [BaseModel ] = getattr (write_module , family )
58- read_model : type [BaseModel ] = getattr (read_module , family )
58+ @pytest .mark .parametrize (( "write_family " , "read_family" ), _FAMILY_PAIRS )
59+ def test_read_variant_is_superset_of_write_variant (write_family : str , read_family : str ) -> None :
60+ write_model : type [BaseModel ] = getattr (write_module , write_family )
61+ read_model : type [BaseModel ] = getattr (read_module , read_family )
5962 write_fields = set (write_model .model_fields )
6063 read_fields = set (read_model .model_fields )
6164 missing = write_fields - read_fields
62- assert not missing , f"read variant { family } must expose every write field; missing: { sorted (missing )} "
65+ assert not missing , f"read variant { read_family } must expose every write field; missing: { sorted (missing )} "
6366
6467
6568def test_write_variant_carries_read_level_fields_absent () -> None :
6669 # `inherited` is a read-level attribute field and must not exist on the write variant.
67- assert "inherited" not in write_module .GeneratedAttributeSchema .model_fields
68- assert "inherited" in read_module .GeneratedAttributeSchema .model_fields
70+ assert "inherited" not in write_module .AttributeSchemaWrite .model_fields
71+ assert "inherited" in read_module .AttributeSchemaRead .model_fields
72+
73+
74+ def test_document_root_models_import_with_nodes_and_generics () -> None :
75+ for root in (InfrahubSchemaWrite , InfrahubSchemaRead ):
76+ assert "nodes" in root .model_fields , f"{ root .__name__ } must expose a 'nodes' field"
77+ assert "generics" in root .model_fields , f"{ root .__name__ } must expose a 'generics' field"
78+ # The write root is not extra=forbid so schema-level keys outside the model are tolerated.
79+ assert InfrahubSchemaWrite .model_config .get ("extra" ) != "forbid"
0 commit comments