|
1 | | -from typing import Any |
2 | | - |
3 | 1 | import pytest |
4 | 2 |
|
5 | 3 | from infrahub_sdk import InfrahubClient |
6 | 4 | from infrahub_sdk.node import InfrahubNode |
| 5 | +from infrahub_sdk.schema.main import AttributeKind, NodeSchema, RelationshipKind, SchemaRoot |
| 6 | +from infrahub_sdk.schema.main import AttributeSchema as Attr |
| 7 | +from infrahub_sdk.schema.main import RelationshipSchema as Rel |
7 | 8 |
|
8 | 9 | NAMESPACE = "Testing" |
9 | 10 |
|
|
14 | 15 |
|
15 | 16 | class SchemaCarPerson: |
16 | 17 | @pytest.fixture(scope="class") |
17 | | - def schema_person_base(self) -> dict[str, Any]: |
18 | | - return { |
19 | | - "name": "Person", |
20 | | - "namespace": NAMESPACE, |
21 | | - "include_in_menu": True, |
22 | | - "label": "Person", |
23 | | - "human_friendly_id": ["name__value"], |
24 | | - "attributes": [ |
25 | | - {"name": "name", "kind": "Text", "unique": True}, |
26 | | - {"name": "description", "kind": "Text", "optional": True}, |
27 | | - {"name": "height", "kind": "Number", "optional": True}, |
28 | | - {"name": "age", "kind": "Number", "optional": True}, |
| 18 | + def schema_person_base(self) -> NodeSchema: |
| 19 | + return NodeSchema( |
| 20 | + name="Person", |
| 21 | + namespace=NAMESPACE, |
| 22 | + include_in_menu=True, |
| 23 | + label="Person", |
| 24 | + human_friendly_id=["name__value"], |
| 25 | + attributes=[ |
| 26 | + Attr(name="name", kind=AttributeKind.TEXT, unique=True), |
| 27 | + Attr(name="description", kind=AttributeKind.TEXT, optional=True), |
| 28 | + Attr(name="height", kind=AttributeKind.NUMBER, optional=True), |
| 29 | + Attr(name="age", kind=AttributeKind.NUMBER, optional=True), |
29 | 30 | ], |
30 | | - "relationships": [ |
31 | | - {"name": "cars", "kind": "Generic", "optional": True, "peer": TESTING_CAR, "cardinality": "many"} |
| 31 | + relationships=[ |
| 32 | + Rel(name="cars", kind=RelationshipKind.GENERIC, optional=True, peer=TESTING_CAR, cardinality="many") |
32 | 33 | ], |
33 | | - } |
| 34 | + ) |
34 | 35 |
|
35 | 36 | @pytest.fixture(scope="class") |
36 | | - def schema_car_base(self) -> dict[str, Any]: |
37 | | - return { |
38 | | - "name": "Car", |
39 | | - "namespace": NAMESPACE, |
40 | | - "include_in_menu": True, |
41 | | - "default_filter": "name__value", |
42 | | - "human_friendly_id": ["owner__name__value", "name__value"], |
43 | | - "label": "Car", |
44 | | - "attributes": [ |
45 | | - {"name": "name", "kind": "Text"}, |
46 | | - {"name": "description", "kind": "Text", "optional": True}, |
47 | | - {"name": "color", "kind": "Text"}, |
| 37 | + def schema_car_base(self) -> NodeSchema: |
| 38 | + return NodeSchema( |
| 39 | + name="Car", |
| 40 | + namespace=NAMESPACE, |
| 41 | + include_in_menu=True, |
| 42 | + default_filter="name__value", |
| 43 | + human_friendly_id=["owner__name__value", "name__value"], |
| 44 | + label="Car", |
| 45 | + attributes=[ |
| 46 | + Attr(name="name", kind=AttributeKind.TEXT), |
| 47 | + Attr(name="description", kind=AttributeKind.TEXT, optional=True), |
| 48 | + Attr(name="color", kind=AttributeKind.TEXT), |
48 | 49 | ], |
49 | | - "relationships": [ |
50 | | - { |
51 | | - "name": "owner", |
52 | | - "kind": "Attribute", |
53 | | - "optional": False, |
54 | | - "peer": TESTING_PERSON, |
55 | | - "cardinality": "one", |
56 | | - }, |
57 | | - { |
58 | | - "name": "manufacturer", |
59 | | - "kind": "Attribute", |
60 | | - "optional": False, |
61 | | - "peer": TESTING_MANUFACTURER, |
62 | | - "cardinality": "one", |
63 | | - "identifier": "car__manufacturer", |
64 | | - }, |
| 50 | + relationships=[ |
| 51 | + Rel( |
| 52 | + name="owner", |
| 53 | + kind=RelationshipKind.ATTRIBUTE, |
| 54 | + optional=False, |
| 55 | + peer=TESTING_PERSON, |
| 56 | + cardinality="one", |
| 57 | + ), |
| 58 | + Rel( |
| 59 | + name="manufacturer", |
| 60 | + kind=RelationshipKind.ATTRIBUTE, |
| 61 | + optional=False, |
| 62 | + peer=TESTING_MANUFACTURER, |
| 63 | + cardinality="one", |
| 64 | + identifier="car__manufacturer", |
| 65 | + ), |
65 | 66 | ], |
66 | | - } |
| 67 | + ) |
67 | 68 |
|
68 | 69 | @pytest.fixture(scope="class") |
69 | | - def schema_manufacturer_base(self) -> dict[str, Any]: |
70 | | - return { |
71 | | - "name": "Manufacturer", |
72 | | - "namespace": NAMESPACE, |
73 | | - "include_in_menu": True, |
74 | | - "label": "Manufacturer", |
75 | | - "human_friendly_id": ["name__value"], |
76 | | - "attributes": [{"name": "name", "kind": "Text"}, {"name": "description", "kind": "Text", "optional": True}], |
77 | | - "relationships": [ |
78 | | - { |
79 | | - "name": "cars", |
80 | | - "kind": "Generic", |
81 | | - "optional": True, |
82 | | - "peer": TESTING_CAR, |
83 | | - "cardinality": "many", |
84 | | - "identifier": "car__manufacturer", |
85 | | - }, |
86 | | - { |
87 | | - "name": "customers", |
88 | | - "kind": "Generic", |
89 | | - "optional": True, |
90 | | - "peer": TESTING_PERSON, |
91 | | - "cardinality": "many", |
92 | | - "identifier": "person__manufacturer", |
93 | | - }, |
| 70 | + def schema_manufacturer_base(self) -> NodeSchema: |
| 71 | + return NodeSchema( |
| 72 | + name="Manufacturer", |
| 73 | + namespace=NAMESPACE, |
| 74 | + include_in_menu=True, |
| 75 | + label="Manufacturer", |
| 76 | + human_friendly_id=["name__value"], |
| 77 | + attributes=[ |
| 78 | + Attr(name="name", kind=AttributeKind.TEXT), |
| 79 | + Attr(name="description", kind=AttributeKind.TEXT, optional=True), |
94 | 80 | ], |
95 | | - } |
| 81 | + relationships=[ |
| 82 | + Rel( |
| 83 | + name="cars", |
| 84 | + kind=RelationshipKind.GENERIC, |
| 85 | + optional=True, |
| 86 | + peer=TESTING_CAR, |
| 87 | + cardinality="many", |
| 88 | + identifier="car__manufacturer", |
| 89 | + ), |
| 90 | + Rel( |
| 91 | + name="customers", |
| 92 | + kind=RelationshipKind.GENERIC, |
| 93 | + optional=True, |
| 94 | + peer=TESTING_PERSON, |
| 95 | + cardinality="many", |
| 96 | + identifier="person__manufacturer", |
| 97 | + ), |
| 98 | + ], |
| 99 | + ) |
96 | 100 |
|
97 | 101 | @pytest.fixture(scope="class") |
98 | 102 | def schema_base( |
99 | 103 | self, |
100 | | - schema_car_base: dict[str, Any], |
101 | | - schema_person_base: dict[str, Any], |
102 | | - schema_manufacturer_base: dict[str, Any], |
103 | | - ) -> dict[str, Any]: |
104 | | - return { |
105 | | - "version": "1.0", |
106 | | - "nodes": [schema_person_base, schema_car_base, schema_manufacturer_base], |
107 | | - } |
| 104 | + schema_car_base: NodeSchema, |
| 105 | + schema_person_base: NodeSchema, |
| 106 | + schema_manufacturer_base: NodeSchema, |
| 107 | + ) -> SchemaRoot: |
| 108 | + return SchemaRoot(version="1.0", nodes=[schema_car_base, schema_person_base, schema_manufacturer_base]) |
108 | 109 |
|
109 | 110 | async def create_persons(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]: |
110 | 111 | john = await client.create(kind=TESTING_PERSON, name="John Doe", branch=branch) |
|
0 commit comments