Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions infrahub_sdk/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BranchData(BaseModel):


MUTATION_QUERY_DATA = {"ok": None, "object": BRANCH_DATA}
MUTATION_QUERY_TASK = {"ok": None, "task": {"id": None}}

QUERY_ALL_BRANCHES_DATA = {"Branch": BRANCH_DATA}

Expand Down Expand Up @@ -119,13 +120,14 @@ async def create(
},
}

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

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

async def delete(self, branch_name: str) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion infrahub_sdk/schema/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ class BaseSchema(BaseModel):
description: str | None = None
include_in_menu: bool | None = None
menu_placement: str | None = None
display_labels: list[str] | None = None
human_friendly_id: list[str] | None = None
icon: str | None = None
uniqueness_constraints: list[list[str]] | None = None
documentation: str | None = None
Expand Down Expand Up @@ -286,7 +288,6 @@ class BaseNodeSchema(BaseSchema):
inherit_from: list[str] = Field(default_factory=list)
branch: BranchSupportType | None = None
default_filter: str | None = None
human_friendly_id: list[str] | None = None
generate_profile: bool | None = None
parent: str | None = None
children: str | None = None
Expand Down
181 changes: 181 additions & 0 deletions infrahub_sdk/testing/schemas/animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import pytest

from infrahub_sdk import InfrahubClient
from infrahub_sdk.exceptions import GraphQLError
from infrahub_sdk.node import InfrahubNode
from infrahub_sdk.schema.main import (
AttributeKind,
GenericSchema,
NodeSchema,
RelationshipDirection,
RelationshipKind,
SchemaRoot,
)
from infrahub_sdk.schema.main import AttributeSchema as Attr
from infrahub_sdk.schema.main import RelationshipSchema as Rel

NAMESPACE = "Testing"

TESTING_ANIMAL = f"{NAMESPACE}Animal"
TESTING_CAT = f"{NAMESPACE}Cat"
TESTING_DOG = f"{NAMESPACE}Dog"
TESTING_PERSON = f"{NAMESPACE}Person"


class SchemaAnimal:
@pytest.fixture(scope="class")
def schema_animal(self) -> GenericSchema:
return GenericSchema(
name="Animal",
namespace=NAMESPACE,
include_in_menu=True,
human_friendly_id=["owner__name__value", "name__value"],
uniqueness_constraints=[
["owner", "name__value"],
],
attributes=[
Attr(name="name", kind=AttributeKind.TEXT),
Attr(name="weight", kind=AttributeKind.NUMBER, optional=True),
],
relationships=[
Rel(
name="owner",
kind=RelationshipKind.GENERIC,
optional=False,
peer=TESTING_PERSON,
cardinality="one",
identifier="person__animal",
direction=RelationshipDirection.OUTBOUND,
),
Rel(
name="best_friend",
kind=RelationshipKind.GENERIC,
optional=True,
peer=TESTING_PERSON,
cardinality="one",
identifier="person__animal_friend",
direction=RelationshipDirection.OUTBOUND,
),
],
)

@pytest.fixture(scope="class")
def schema_dog(self) -> NodeSchema:
return NodeSchema(
name="Dog",
namespace=NAMESPACE,
include_in_menu=True,
inherit_from=[TESTING_ANIMAL],
display_labels=["name__value", "breed__value"],
attributes=[
Attr(name="breed", kind=AttributeKind.TEXT, optional=False),
Attr(name="color", kind=AttributeKind.COLOR, default_value="#444444", optional=True),
],
)

@pytest.fixture(scope="class")
def schema_cat(self) -> NodeSchema:
return NodeSchema(
name="Cat",
namespace=NAMESPACE,
include_in_menu=True,
inherit_from=[TESTING_ANIMAL],
display_labels=["name__value", "breed__value", "color__value"],
attributes=[
Attr(name="breed", kind=AttributeKind.TEXT, optional=False),
Attr(name="color", kind=AttributeKind.COLOR, default_value="#555555", optional=True),
],
)

@pytest.fixture(scope="class")
def schema_person(self) -> NodeSchema:
return NodeSchema(
name="Person",
namespace=NAMESPACE,
include_in_menu=True,
display_labels=["name__value"],
default_filter="name__value",
human_friendly_id=["name__value"],
attributes=[
Attr(name="name", kind=AttributeKind.TEXT, unique=True),
Attr(name="height", kind=AttributeKind.NUMBER, optional=True),
],
relationships=[
Rel(
name="animals",
peer=TESTING_ANIMAL,
identifier="person__animal",
cardinality="many",
direction=RelationshipDirection.INBOUND,
),
Rel(
name="best_friends",
peer=TESTING_ANIMAL,
identifier="person__animal_friend",
cardinality="many",
direction=RelationshipDirection.INBOUND,
),
],
)

@pytest.fixture(scope="class")
def schema_base(
self,
schema_animal: GenericSchema,
schema_person: NodeSchema,
schema_cat: NodeSchema,
schema_dog: NodeSchema,
) -> SchemaRoot:
return SchemaRoot(version="1.0", generics=[schema_animal], nodes=[schema_person, schema_cat, schema_dog])

@pytest.fixture(scope="class")
async def load_schema(self, client: InfrahubClient, schema_base: SchemaRoot) -> None:
resp = await client.schema.load(schemas=[schema_base.to_schema_dict()], wait_until_converged=True)
if resp.errors:
raise GraphQLError(errors=[resp.errors])

Check warning on line 135 in infrahub_sdk/testing/schemas/animal.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/animal.py#L135

Added line #L135 was not covered by tests

@pytest.fixture(scope="class")
async def person_liam(self, client: InfrahubClient) -> InfrahubNode:
obj = await client.create(kind=TESTING_PERSON, name="Liam Walker", height=178)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def person_sophia(self, client: InfrahubClient) -> InfrahubNode:
obj = await client.create(kind=TESTING_PERSON, name="Sophia Walker", height=168)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def person_ethan(self, client: InfrahubClient) -> InfrahubNode:
obj = await client.create(kind=TESTING_PERSON, name="Ethan Carter", height=185)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def cat_bella(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
obj = await client.create(kind=TESTING_CAT, name="Bella", breed="Bengal", color="#123456", owner=person_ethan)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def cat_luna(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
obj = await client.create(kind=TESTING_CAT, name="Luna", breed="Siamese", color="#FFFFFF", owner=person_ethan)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def dog_daisy(self, client: InfrahubClient, person_ethan: InfrahubNode) -> InfrahubNode:
obj = await client.create(
kind=TESTING_DOG, name="Daisy", breed="French Bulldog", color="#7B7D7D", owner=person_ethan
)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def dog_rocky(self, client: InfrahubClient, person_sophia: InfrahubNode) -> InfrahubNode:
obj = await client.create(
kind=TESTING_DOG, name="Rocky", breed="German Shepherd", color="#784212", owner=person_sophia
)
await obj.save()
return obj
99 changes: 97 additions & 2 deletions infrahub_sdk/testing/schemas/car_person.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
from __future__ import annotations

from dataclasses import asdict, dataclass
from typing import TYPE_CHECKING

import pytest

from infrahub_sdk import InfrahubClient
from infrahub_sdk.node import InfrahubNode
from infrahub_sdk.schema.main import AttributeKind, NodeSchema, RelationshipKind, SchemaRoot
from infrahub_sdk.schema.main import AttributeSchema as Attr
from infrahub_sdk.schema.main import RelationshipSchema as Rel

if TYPE_CHECKING:
from infrahub_sdk import InfrahubClient
from infrahub_sdk.node import InfrahubNode

NAMESPACE = "Testing"

TESTING_MANUFACTURER = f"{NAMESPACE}Manufacturer"
TESTING_PERSON = f"{NAMESPACE}Person"
TESTING_CAR = f"{NAMESPACE}Car"
BUILTIN_TAG = "BuiltinTag"


@dataclass
class TestingPersonData:
name: str
kind: str = TESTING_PERSON


@dataclass
class TestingManufacturerData:
name: str
kind: str = TESTING_MANUFACTURER


@dataclass
class TestingCarData:
name: str
color: str | None = None
kind: str = TESTING_CAR


class SchemaCarPerson:
Expand Down Expand Up @@ -63,6 +90,12 @@
cardinality="one",
identifier="car__manufacturer",
),
Rel(
name="tags",
optional=True,
peer=BUILTIN_TAG,
cardinality="many",
),
],
)

Expand Down Expand Up @@ -107,6 +140,68 @@
) -> SchemaRoot:
return SchemaRoot(version="1.0", nodes=[schema_car_base, schema_person_base, schema_manufacturer_base])

@pytest.fixture(scope="class")
async def person_joe_data(self) -> TestingPersonData:
return TestingPersonData(name="Joe Doe")

@pytest.fixture(scope="class")
async def person_jane_data(self) -> TestingPersonData:
return TestingPersonData(name="Jane Doe")

Check warning on line 149 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L149

Added line #L149 was not covered by tests

@pytest.fixture(scope="class")
async def manufacturer_vw_data(self) -> TestingManufacturerData:
return TestingManufacturerData(name="Volkswagen")

Check warning on line 153 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L153

Added line #L153 was not covered by tests

@pytest.fixture(scope="class")
async def manufacturer_renault_data(self) -> TestingManufacturerData:
return TestingManufacturerData(name="Renault")

Check warning on line 157 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L157

Added line #L157 was not covered by tests

@pytest.fixture(scope="class")
async def manufacturer_mercedes_data(self) -> TestingManufacturerData:
return TestingManufacturerData(name="Mercedes")

@pytest.fixture(scope="class")
async def car_golf_data(self) -> TestingCarData:
return TestingCarData(name="Golf", color="Black")

@pytest.fixture(scope="class")
async def person_joe(self, client: InfrahubClient, person_joe_data: TestingPersonData) -> InfrahubNode:
obj = await client.create(**asdict(person_joe_data))
await obj.save()
return obj

@pytest.fixture(scope="class")
async def manufacturer_mercedes(
self, client: InfrahubClient, manufacturer_mercedes_data: TestingManufacturerData
) -> InfrahubNode:
obj = await client.create(**asdict(manufacturer_mercedes_data))
await obj.save()
return obj

@pytest.fixture(scope="class")
async def car_golf(
self,
client: InfrahubClient,
car_golf_data: TestingCarData,
manufacturer_mercedes: InfrahubNode,
person_joe: InfrahubNode,
) -> InfrahubNode:
obj = await client.create(**asdict(car_golf_data), manufacturer=manufacturer_mercedes, owner=person_joe)
await obj.save()
return obj

@pytest.fixture(scope="class")
async def tag_blue(self, client: InfrahubClient) -> InfrahubNode:
obj = await client.create(kind=BUILTIN_TAG, name="Blue")
await obj.save()
return obj

@pytest.fixture(scope="class")
async def tag_red(self, client: InfrahubClient) -> InfrahubNode:
obj = await client.create(kind=BUILTIN_TAG, name="Red")
await obj.save()
return obj

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