|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from infrahub_sdk import InfrahubClient |
| 6 | +from infrahub_sdk.node import InfrahubNode |
| 7 | + |
| 8 | +NAMESPACE = "Testing" |
| 9 | + |
| 10 | +TESTING_MANUFACTURER = f"{NAMESPACE}Manufacturer" |
| 11 | +TESTING_PERSON = f"{NAMESPACE}Person" |
| 12 | +TESTING_CAR = f"{NAMESPACE}Car" |
| 13 | + |
| 14 | + |
| 15 | +class SchemaCarPerson: |
| 16 | + @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}, |
| 29 | + ], |
| 30 | + "relationships": [ |
| 31 | + {"name": "cars", "kind": "Generic", "optional": True, "peer": TESTING_CAR, "cardinality": "many"} |
| 32 | + ], |
| 33 | + } |
| 34 | + |
| 35 | + @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"}, |
| 48 | + ], |
| 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 | + }, |
| 65 | + ], |
| 66 | + } |
| 67 | + |
| 68 | + @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 | + }, |
| 94 | + ], |
| 95 | + } |
| 96 | + |
| 97 | + @pytest.fixture(scope="class") |
| 98 | + def schema_base( |
| 99 | + 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 | + } |
| 108 | + |
| 109 | + async def create_persons(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]: |
| 110 | + john = await client.create(kind=TESTING_PERSON, name="John Doe", branch=branch) |
| 111 | + await john.save() |
| 112 | + |
| 113 | + jane = await client.create(kind=TESTING_PERSON, name="Jane Doe", branch=branch) |
| 114 | + await jane.save() |
| 115 | + |
| 116 | + return [john, jane] |
| 117 | + |
| 118 | + async def create_manufacturers(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]: |
| 119 | + obj1 = await client.create(kind=TESTING_MANUFACTURER, name="Volkswagen", branch=branch) |
| 120 | + await obj1.save() |
| 121 | + |
| 122 | + obj2 = await client.create(kind=TESTING_MANUFACTURER, name="Renault", branch=branch) |
| 123 | + await obj2.save() |
| 124 | + |
| 125 | + obj3 = await client.create(kind=TESTING_MANUFACTURER, name="Mercedes", branch=branch) |
| 126 | + await obj3.save() |
| 127 | + |
| 128 | + return [obj1, obj2, obj3] |
| 129 | + |
| 130 | + async def create_initial_data(self, client: InfrahubClient, branch: str) -> dict[str, list[InfrahubNode]]: |
| 131 | + persons = await self.create_persons(client=client, branch=branch) |
| 132 | + manufacturers = await self.create_manufacturers(client=client, branch=branch) |
| 133 | + |
| 134 | + car10 = await client.create( |
| 135 | + kind=TESTING_CAR, name="Golf", color="Black", manufacturer=manufacturers[0].id, owner=persons[0].id |
| 136 | + ) |
| 137 | + await car10.save() |
| 138 | + |
| 139 | + car20 = await client.create( |
| 140 | + kind=TESTING_CAR, name="Megane", color="Red", manufacturer=manufacturers[1].id, owner=persons[1].id |
| 141 | + ) |
| 142 | + await car20.save() |
| 143 | + |
| 144 | + return {TESTING_PERSON: persons, TESTING_CAR: [car10, car20], TESTING_MANUFACTURER: manufacturers} |
0 commit comments