Skip to content

Commit 0889718

Browse files
authored
Merge pull request #211 from opsmill/dga-20250104-cont-int-tests
Add some integration tests in test_node and test_infrahub_client
2 parents e33444a + bd44e63 commit 0889718

File tree

6 files changed

+485
-284
lines changed

6 files changed

+485
-284
lines changed

infrahub_sdk/branch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BranchData(BaseModel):
4040

4141

4242
MUTATION_QUERY_DATA = {"ok": None, "object": BRANCH_DATA}
43+
MUTATION_QUERY_TASK = {"ok": None, "task": {"id": None}}
4344

4445
QUERY_ALL_BRANCHES_DATA = {"Branch": BRANCH_DATA}
4546

@@ -119,13 +120,14 @@ async def create(
119120
},
120121
}
121122

122-
query = Mutation(mutation="BranchCreate", input_data=input_data, query=MUTATION_QUERY_DATA)
123+
mutation_query = MUTATION_QUERY_TASK if background_execution else MUTATION_QUERY_DATA
124+
query = Mutation(mutation="BranchCreate", input_data=input_data, query=mutation_query)
123125
response = await self.client.execute_graphql(query=query.render(), tracker="mutation-branch-create")
124126

125127
# Make sure server version is recent enough to support background execution, as previously
126128
# using background_execution=True had no effect.
127129
if background_execution and "task" in response["BranchCreate"]:
128-
return BranchData(**response["BranchCreate"]["task"]["id"])
130+
return response["BranchCreate"]["task"]["id"]
129131
return BranchData(**response["BranchCreate"]["object"])
130132

131133
async def delete(self, branch_name: str) -> bool:

infrahub_sdk/schema/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class BaseSchema(BaseModel):
259259
description: str | None = None
260260
include_in_menu: bool | None = None
261261
menu_placement: str | None = None
262+
display_labels: list[str] | None = None
263+
human_friendly_id: list[str] | None = None
262264
icon: str | None = None
263265
uniqueness_constraints: list[list[str]] | None = None
264266
documentation: str | None = None
@@ -286,7 +288,6 @@ class BaseNodeSchema(BaseSchema):
286288
inherit_from: list[str] = Field(default_factory=list)
287289
branch: BranchSupportType | None = None
288290
default_filter: str | None = None
289-
human_friendly_id: list[str] | None = None
290291
generate_profile: bool | None = None
291292
parent: str | None = None
292293
children: str | None = None
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import pytest
2+
3+
from infrahub_sdk import InfrahubClient
4+
from infrahub_sdk.exceptions import GraphQLError
5+
from infrahub_sdk.node import InfrahubNode
6+
from infrahub_sdk.schema.main import (
7+
AttributeKind,
8+
GenericSchema,
9+
NodeSchema,
10+
RelationshipDirection,
11+
RelationshipKind,
12+
SchemaRoot,
13+
)
14+
from infrahub_sdk.schema.main import AttributeSchema as Attr
15+
from infrahub_sdk.schema.main import RelationshipSchema as Rel
16+
17+
NAMESPACE = "Testing"
18+
19+
TESTING_ANIMAL = f"{NAMESPACE}Animal"
20+
TESTING_CAT = f"{NAMESPACE}Cat"
21+
TESTING_DOG = f"{NAMESPACE}Dog"
22+
TESTING_PERSON = f"{NAMESPACE}Person"
23+
24+
25+
class SchemaAnimal:
26+
@pytest.fixture(scope="class")
27+
def schema_animal(self) -> GenericSchema:
28+
return GenericSchema(
29+
name="Animal",
30+
namespace=NAMESPACE,
31+
include_in_menu=True,
32+
human_friendly_id=["owner__name__value", "name__value"],
33+
uniqueness_constraints=[
34+
["owner", "name__value"],
35+
],
36+
attributes=[
37+
Attr(name="name", kind=AttributeKind.TEXT),
38+
Attr(name="weight", kind=AttributeKind.NUMBER, optional=True),
39+
],
40+
relationships=[
41+
Rel(
42+
name="owner",
43+
kind=RelationshipKind.GENERIC,
44+
optional=False,
45+
peer=TESTING_PERSON,
46+
cardinality="one",
47+
identifier="person__animal",
48+
direction=RelationshipDirection.OUTBOUND,
49+
),
50+
Rel(
51+
name="best_friend",
52+
kind=RelationshipKind.GENERIC,
53+
optional=True,
54+
peer=TESTING_PERSON,
55+
cardinality="one",
56+
identifier="person__animal_friend",
57+
direction=RelationshipDirection.OUTBOUND,
58+
),
59+
],
60+
)
61+
62+
@pytest.fixture(scope="class")
63+
def schema_dog(self) -> NodeSchema:
64+
return NodeSchema(
65+
name="Dog",
66+
namespace=NAMESPACE,
67+
include_in_menu=True,
68+
inherit_from=[TESTING_ANIMAL],
69+
display_labels=["name__value", "breed__value"],
70+
attributes=[
71+
Attr(name="breed", kind=AttributeKind.TEXT, optional=False),
72+
Attr(name="color", kind=AttributeKind.COLOR, default_value="#444444", optional=True),
73+
],
74+
)
75+
76+
@pytest.fixture(scope="class")
77+
def schema_cat(self) -> NodeSchema:
78+
return NodeSchema(
79+
name="Cat",
80+
namespace=NAMESPACE,
81+
include_in_menu=True,
82+
inherit_from=[TESTING_ANIMAL],
83+
display_labels=["name__value", "breed__value", "color__value"],
84+
attributes=[
85+
Attr(name="breed", kind=AttributeKind.TEXT, optional=False),
86+
Attr(name="color", kind=AttributeKind.COLOR, default_value="#555555", optional=True),
87+
],
88+
)
89+
90+
@pytest.fixture(scope="class")
91+
def schema_person(self) -> NodeSchema:
92+
return NodeSchema(
93+
name="Person",
94+
namespace=NAMESPACE,
95+
include_in_menu=True,
96+
display_labels=["name__value"],
97+
default_filter="name__value",
98+
human_friendly_id=["name__value"],
99+
attributes=[
100+
Attr(name="name", kind=AttributeKind.TEXT, unique=True),
101+
Attr(name="height", kind=AttributeKind.NUMBER, optional=True),
102+
],
103+
relationships=[
104+
Rel(
105+
name="animals",
106+
peer=TESTING_ANIMAL,
107+
identifier="person__animal",
108+
cardinality="many",
109+
direction=RelationshipDirection.INBOUND,
110+
),
111+
Rel(
112+
name="best_friends",
113+
peer=TESTING_ANIMAL,
114+
identifier="person__animal_friend",
115+
cardinality="many",
116+
direction=RelationshipDirection.INBOUND,
117+
),
118+
],
119+
)
120+
121+
@pytest.fixture(scope="class")
122+
def schema_base(
123+
self,
124+
schema_animal: GenericSchema,
125+
schema_person: NodeSchema,
126+
schema_cat: NodeSchema,
127+
schema_dog: NodeSchema,
128+
) -> SchemaRoot:
129+
return SchemaRoot(version="1.0", generics=[schema_animal], nodes=[schema_person, schema_cat, schema_dog])
130+
131+
@pytest.fixture(scope="class")
132+
async def load_schema(self, client: InfrahubClient, schema_base: SchemaRoot) -> None:
133+
resp = await client.schema.load(schemas=[schema_base.to_schema_dict()], wait_until_converged=True)
134+
if resp.errors:
135+
raise GraphQLError(errors=[resp.errors])
136+
137+
@pytest.fixture(scope="class")
138+
async def person_liam(self, client: InfrahubClient) -> InfrahubNode:
139+
obj = await client.create(kind=TESTING_PERSON, name="Liam Walker", height=178)
140+
await obj.save()
141+
return obj
142+
143+
@pytest.fixture(scope="class")
144+
async def person_sophia(self, client: InfrahubClient) -> InfrahubNode:
145+
obj = await client.create(kind=TESTING_PERSON, name="Sophia Walker", height=168)
146+
await obj.save()
147+
return obj
148+
149+
@pytest.fixture(scope="class")
150+
async def person_ethan(self, client: InfrahubClient) -> InfrahubNode:
151+
obj = await client.create(kind=TESTING_PERSON, name="Ethan Carter", height=185)
152+
await obj.save()
153+
return obj
154+
155+
@pytest.fixture(scope="class")
156+
async def cat_bella(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
157+
obj = await client.create(kind=TESTING_CAT, name="Bella", breed="Bengal", color="#123456", owner=person_ethan)
158+
await obj.save()
159+
return obj
160+
161+
@pytest.fixture(scope="class")
162+
async def cat_luna(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
163+
obj = await client.create(kind=TESTING_CAT, name="Luna", breed="Siamese", color="#FFFFFF", owner=person_ethan)
164+
await obj.save()
165+
return obj
166+
167+
@pytest.fixture(scope="class")
168+
async def dog_daisy(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
169+
obj = await client.create(
170+
kind=TESTING_DOG, name="Daisy", breed="French Bulldog", color="#7B7D7D", owner=person_ethan
171+
)
172+
await obj.save()
173+
return obj
174+
175+
@pytest.fixture(scope="class")
176+
async def dog_rocky(self, client: InfrahubClient, person_sophia: InfrahubNode) -> InfrahubNode:
177+
obj = await client.create(
178+
kind=TESTING_DOG, name="Rocky", breed="German Shepherd", color="#784212", owner=person_sophia
179+
)
180+
await obj.save()
181+
return obj

infrahub_sdk/testing/schemas/car_person.py

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1+
from __future__ import annotations
2+
3+
from dataclasses import asdict, dataclass
4+
from typing import TYPE_CHECKING
5+
16
import pytest
27

3-
from infrahub_sdk import InfrahubClient
4-
from infrahub_sdk.node import InfrahubNode
58
from infrahub_sdk.schema.main import AttributeKind, NodeSchema, RelationshipKind, SchemaRoot
69
from infrahub_sdk.schema.main import AttributeSchema as Attr
710
from infrahub_sdk.schema.main import RelationshipSchema as Rel
811

12+
if TYPE_CHECKING:
13+
from infrahub_sdk import InfrahubClient
14+
from infrahub_sdk.node import InfrahubNode
15+
916
NAMESPACE = "Testing"
1017

1118
TESTING_MANUFACTURER = f"{NAMESPACE}Manufacturer"
1219
TESTING_PERSON = f"{NAMESPACE}Person"
1320
TESTING_CAR = f"{NAMESPACE}Car"
21+
BUILTIN_TAG = "BuiltinTag"
22+
23+
24+
@dataclass
25+
class TestingPersonData:
26+
name: str
27+
kind: str = TESTING_PERSON
28+
29+
30+
@dataclass
31+
class TestingManufacturerData:
32+
name: str
33+
kind: str = TESTING_MANUFACTURER
34+
35+
36+
@dataclass
37+
class TestingCarData:
38+
name: str
39+
color: str | None = None
40+
kind: str = TESTING_CAR
1441

1542

1643
class SchemaCarPerson:
@@ -63,6 +90,12 @@ def schema_car_base(self) -> NodeSchema:
6390
cardinality="one",
6491
identifier="car__manufacturer",
6592
),
93+
Rel(
94+
name="tags",
95+
optional=True,
96+
peer=BUILTIN_TAG,
97+
cardinality="many",
98+
),
6699
],
67100
)
68101

@@ -107,6 +140,68 @@ def schema_base(
107140
) -> SchemaRoot:
108141
return SchemaRoot(version="1.0", nodes=[schema_car_base, schema_person_base, schema_manufacturer_base])
109142

143+
@pytest.fixture(scope="class")
144+
async def person_joe_data(self) -> TestingPersonData:
145+
return TestingPersonData(name="Joe Doe")
146+
147+
@pytest.fixture(scope="class")
148+
async def person_jane_data(self) -> TestingPersonData:
149+
return TestingPersonData(name="Jane Doe")
150+
151+
@pytest.fixture(scope="class")
152+
async def manufacturer_vw_data(self) -> TestingManufacturerData:
153+
return TestingManufacturerData(name="Volkswagen")
154+
155+
@pytest.fixture(scope="class")
156+
async def manufacturer_renault_data(self) -> TestingManufacturerData:
157+
return TestingManufacturerData(name="Renault")
158+
159+
@pytest.fixture(scope="class")
160+
async def manufacturer_mercedes_data(self) -> TestingManufacturerData:
161+
return TestingManufacturerData(name="Mercedes")
162+
163+
@pytest.fixture(scope="class")
164+
async def car_golf_data(self) -> TestingCarData:
165+
return TestingCarData(name="Golf", color="Black")
166+
167+
@pytest.fixture(scope="class")
168+
async def person_joe(self, client: InfrahubClient, person_joe_data: TestingPersonData) -> InfrahubNode:
169+
obj = await client.create(**asdict(person_joe_data))
170+
await obj.save()
171+
return obj
172+
173+
@pytest.fixture(scope="class")
174+
async def manufacturer_mercedes(
175+
self, client: InfrahubClient, manufacturer_mercedes_data: TestingManufacturerData
176+
) -> InfrahubNode:
177+
obj = await client.create(**asdict(manufacturer_mercedes_data))
178+
await obj.save()
179+
return obj
180+
181+
@pytest.fixture(scope="class")
182+
async def car_golf(
183+
self,
184+
client: InfrahubClient,
185+
car_golf_data: TestingCarData,
186+
manufacturer_mercedes: InfrahubNode,
187+
person_joe: InfrahubNode,
188+
) -> InfrahubNode:
189+
obj = await client.create(**asdict(car_golf_data), manufacturer=manufacturer_mercedes, owner=person_joe)
190+
await obj.save()
191+
return obj
192+
193+
@pytest.fixture(scope="class")
194+
async def tag_blue(self, client: InfrahubClient) -> InfrahubNode:
195+
obj = await client.create(kind=BUILTIN_TAG, name="Blue")
196+
await obj.save()
197+
return obj
198+
199+
@pytest.fixture(scope="class")
200+
async def tag_red(self, client: InfrahubClient) -> InfrahubNode:
201+
obj = await client.create(kind=BUILTIN_TAG, name="Red")
202+
await obj.save()
203+
return obj
204+
110205
async def create_persons(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]:
111206
john = await client.create(kind=TESTING_PERSON, name="John Doe", branch=branch)
112207
await john.save()

0 commit comments

Comments
 (0)