Skip to content

Commit b51d0b4

Browse files
committed
Add testing directory and fix some integration tests
1 parent 9366f01 commit b51d0b4

File tree

7 files changed

+1705
-1497
lines changed

7 files changed

+1705
-1497
lines changed

infrahub_sdk/schema/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ class SchemaRoot(BaseModel):
316316
nodes: list[NodeSchema] = Field(default_factory=list)
317317
node_extensions: list[NodeExtensionSchema] = Field(default_factory=list)
318318

319+
def to_schema_dict(self) -> dict[str, Any]:
320+
return self.model_dump(exclude_unset=True, exclude_defaults=True)
319321

320322
class SchemaRootAPI(BaseModel):
321323
version: str

infrahub_sdk/testing/docker.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
class TestInfrahubDockerClient(TestInfrahubDocker):
88
@pytest.fixture(scope="class")
9-
def infrahub_client(self, infrahub_port: int) -> InfrahubClient:
10-
return InfrahubClient(config=Config(address=f"http://localhost:{infrahub_port}"))
9+
def client(self, infrahub_port: int) -> InfrahubClient:
10+
return InfrahubClient(
11+
config=Config(username="admin", password="infrahub", address=f"http://localhost:{infrahub_port}") # noqa: S106
12+
)
1113

1214
@pytest.fixture(scope="class")
13-
def infrahub_client_sync(self, infrahub_port: int) -> InfrahubClientSync:
14-
return InfrahubClientSync(config=Config(address=f"http://localhost:{infrahub_port}"))
15+
def client_sync(self, infrahub_port: int) -> InfrahubClientSync:
16+
return InfrahubClientSync(
17+
config=Config(username="admin", password="infrahub", address=f"http://localhost:{infrahub_port}") # noqa: S106
18+
)

infrahub_sdk/testing/schemas/car_person.py

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from typing import Any
2-
31
import pytest
42

53
from infrahub_sdk import InfrahubClient
64
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
78

89
NAMESPACE = "Testing"
910

@@ -14,97 +15,97 @@
1415

1516
class SchemaCarPerson:
1617
@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),
2930
],
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")
3233
],
33-
}
34+
)
3435

3536
@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),
4849
],
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+
),
6566
],
66-
}
67+
)
6768

6869
@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),
9480
],
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+
)
96100

97101
@pytest.fixture(scope="class")
98102
def schema_base(
99103
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])
108109

109110
async def create_persons(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]:
110111
john = await client.create(kind=TESTING_PERSON, name="John Doe", branch=branch)

0 commit comments

Comments
 (0)