Skip to content

Commit 14e064c

Browse files
committed
migrate test
1 parent 51b7c90 commit 14e064c

File tree

12 files changed

+274
-364
lines changed

12 files changed

+274
-364
lines changed

backend/infrahub/computed_attribute/tasks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ async def process_transform(
4949
branch_name: str,
5050
node_kind: str,
5151
object_id: str,
52+
computed_attribute_name: str, # pylint: disable=unused-argument
53+
computed_attribute_kind: str, # pylint: disable=unused-argument
5254
updated_fields: list[str] | None = None, # pylint: disable=unused-argument
5355
) -> None:
5456
"""Request to the creation of git branches in available repositories."""
@@ -355,6 +357,8 @@ async def computed_attribute_setup_python() -> None:
355357
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
356358
"node_kind": "{{ event.resource['infrahub.node.kind'] }}",
357359
"object_id": "{{ event.resource['infrahub.node.id'] }}",
360+
"computed_attribute_name": computed_attribute.computed_attribute.attribute.name,
361+
"computed_attribute_kind": computed_attribute.computed_attribute.kind,
358362
},
359363
job_variables={},
360364
)

backend/tests/fixtures/schemas/infra_simple_01.json

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"nodes": [
44
{
55
"name": "Device",
6-
"namespace": "Infra",
6+
"namespace": "Test",
77
"default_filter": "name__value",
88
"display_labels": ["name__value"],
99
"attributes": [
@@ -16,7 +16,7 @@
1616
"relationships": [
1717
{
1818
"name": "interfaces",
19-
"peer": "InfraInterface",
19+
"peer": "TestInterface",
2020
"optional": true,
2121
"cardinality": "many",
2222
"kind": "Component",
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"name": "Interface",
30-
"namespace": "Infra",
30+
"namespace": "Test",
3131
"display_labels": ["name__value"],
3232
"attributes": [
3333
{"name": "name", "kind": "Text"},
@@ -36,11 +36,11 @@
3636
{"name": "enabled", "kind": "Boolean", "default_value": true}
3737
],
3838
"relationships": [
39-
{"name": "device", "peer": "InfraDevice", "optional": false, "cardinality": "one", "kind": "Parent"},
39+
{"name": "device", "peer": "TestDevice", "optional": false, "cardinality": "one", "kind": "Parent"},
4040
{"name": "tags", "peer": "BuiltinTag", "optional": true, "cardinality": "many", "kind": "Attribute"},
4141
{
4242
"name": "ip_addresses",
43-
"peer": "InfraIPAddress",
43+
"peer": "TestIPAddress",
4444
"optional": true,
4545
"cardinality": "many",
4646
"kind": "Component"
@@ -49,15 +49,66 @@
4949
},
5050
{
5151
"name": "IPAddress",
52-
"namespace": "Infra",
52+
"namespace": "Test",
5353
"default_filter": "address__value",
5454
"display_labels": ["address__value"],
5555
"attributes": [
5656
{"name": "address", "kind": "Text"},
5757
{"name": "description", "kind": "Text", "optional": true}
5858
],
5959
"relationships": [
60-
{"name": "interface", "peer": "InfraInterface", "optional": false, "cardinality": "one", "kind": "Parent"}
60+
{"name": "interface", "peer": "TestInterface", "optional": false, "cardinality": "one", "kind": "Parent"}
61+
]
62+
},
63+
{
64+
"name": "Organization",
65+
"namespace": "Testing",
66+
"description": "An organization represent a legal entity, a company.",
67+
"include_in_menu": true,
68+
"label": "Organization",
69+
"icon": "mdi:domain",
70+
"default_filter": "name__value",
71+
"order_by": ["name__value"],
72+
"display_labels": ["label__value"],
73+
"branch": "aware",
74+
"attributes": [
75+
{"name": "name", "kind": "Text", "unique": true},
76+
{"name": "label", "kind": "Text", "optional": true},
77+
{"name": "description", "kind": "Text", "optional": true}
78+
],
79+
"relationships": [
80+
{
81+
"name": "tags",
82+
"peer": "BuiltinTag",
83+
"kind": "Attribute",
84+
"optional": true,
85+
"cardinality": "many"
86+
}
87+
]
88+
},
89+
{
90+
"name": "Site",
91+
"namespace": "Infra",
92+
"description": "A location represent a physical element site",
93+
"include_in_menu": true,
94+
"icon": "mdi:map-marker-radius-outline",
95+
"label": "Site",
96+
"default_filter": "name__value",
97+
"order_by": ["name__value"],
98+
"display_labels": ["name__value"],
99+
"attributes": [
100+
{"name": "name", "kind": "Text", "unique": true},
101+
{"name": "description", "kind": "Text", "optional": true},
102+
{"name": "type", "kind": "Text"}
103+
],
104+
"relationships": [
105+
{
106+
"name": "tags",
107+
"peer": "BuiltinTag",
108+
"kind": "Attribute",
109+
"optional": true,
110+
"cardinality": "many"
111+
}
61112
]
62113
}
63114
]

backend/tests/fixtures/schemas/infra_w_extensions_01.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
]
2626
},
2727
{
28-
"kind": "CoreOrganization",
28+
"kind": "TestingOrganization",
2929
"relationships": [
3030
{"name": "devices", "peer": "InfraDevice", "kind": "Generic", "cardinality": "many", "optional": true }
3131
]

backend/tests/functional/api/__init__.py

Whitespace-only changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
from infrahub_sdk.schema import GenericSchema as SDKGenericSchema
7+
8+
from infrahub.core.registry import registry
9+
from infrahub.core.schema import core_models
10+
from infrahub.core.utils import count_relationships
11+
from infrahub.database import InfrahubDatabase
12+
from tests.helpers.test_app import TestInfrahubApp
13+
14+
if TYPE_CHECKING:
15+
from infrahub_sdk import InfrahubClient
16+
17+
from infrahub.core.branch import Branch
18+
from infrahub.database import InfrahubDatabase
19+
from tests.adapters.message_bus import BusSimulator
20+
from tests.conftest import TestHelper
21+
22+
23+
class TestLoadSchemaAPI(TestInfrahubApp):
24+
@pytest.fixture(scope="class")
25+
async def initial_dataset(
26+
self,
27+
db: InfrahubDatabase,
28+
initialize_registry: None,
29+
client: InfrahubClient,
30+
bus_simulator: BusSimulator,
31+
prefect_test_fixture: None,
32+
) -> None:
33+
pass
34+
35+
async def test_schema_load_endpoint_idempotent_simple(
36+
self, initial_dataset: str, client: InfrahubClient, helper: TestHelper, db: InfrahubDatabase
37+
) -> None:
38+
creation = await client.schema.load(schemas=[helper.schema_file("infra_simple_01.json")])
39+
assert creation.schema_updated
40+
test_device = await client.schema.get(kind="TestDevice")
41+
attributes = {attrib.name: attrib.order_weight for attrib in test_device.attributes}
42+
relationships = {attrib.name: attrib.order_weight for attrib in test_device.relationships}
43+
assert attributes["name"] == 1000
44+
assert attributes["description"] == 900
45+
assert attributes["type"] == 3000
46+
assert relationships["interfaces"] == 450
47+
assert relationships["tags"] == 7000
48+
49+
first_relationship_count = await count_relationships(db=db)
50+
update = await client.schema.load(schemas=[helper.schema_file("infra_simple_01.json")])
51+
assert not update.schema_updated
52+
updated_relationship_count = await count_relationships(db=db)
53+
54+
assert first_relationship_count == updated_relationship_count
55+
56+
async def test_schema_load_endpoint_idempotent_with_generics(
57+
self, initial_dataset: str, client: InfrahubClient, helper: TestHelper, db: InfrahubDatabase
58+
) -> None:
59+
creation = await client.schema.load(schemas=[helper.schema_file("infra_w_generics_01.json")])
60+
assert creation.schema_updated
61+
assert creation.schema_updated
62+
first_relationship_count = await count_relationships(db=db)
63+
update = await client.schema.load(schemas=[helper.schema_file("infra_w_generics_01.json")])
64+
assert not update.schema_updated
65+
updated_relationship_count = await count_relationships(db=db)
66+
67+
assert first_relationship_count == updated_relationship_count
68+
69+
all_schemas = await client.schema.all(refresh=True)
70+
generic_schemas = [schema for schema in all_schemas.values() if isinstance(schema, SDKGenericSchema)]
71+
72+
assert len(generic_schemas) == len(core_models["generics"]) + 1
73+
74+
async def test_schema_load_endpoint_valid_with_extensions(
75+
self,
76+
initial_dataset: str,
77+
client: InfrahubClient,
78+
helper: TestHelper,
79+
db: InfrahubDatabase,
80+
default_branch: Branch,
81+
) -> None:
82+
schema = registry.schema.get_schema_branch(name=default_branch.name)
83+
await registry.schema.load_schema_to_db(schema=schema, branch=default_branch, db=db)
84+
simple = await client.schema.load(schemas=[helper.schema_file("infra_simple_01.json")])
85+
assert not simple.errors
86+
org_schema = registry.schema.get(name="TestingOrganization", branch=default_branch.name)
87+
initial_nbr_relationships = len(org_schema.relationships)
88+
89+
extended_schema = await client.schema.load(schemas=[helper.schema_file("infra_w_extensions_01.json")])
90+
assert not extended_schema.errors
91+
assert extended_schema.schema_updated
92+
93+
org_schema = registry.schema.get(name="TestingOrganization", branch=default_branch.name)
94+
assert len(org_schema.relationships) == initial_nbr_relationships + 1

backend/tests/functional/graphql/__init__.py

Whitespace-only changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
from infrahub.core.node import Node
8+
from infrahub.core.timestamp import Timestamp
9+
from infrahub.database import InfrahubDatabase
10+
from tests.helpers.schema import CAR_SCHEMA, load_schema
11+
from tests.helpers.test_app import TestInfrahubApp
12+
13+
if TYPE_CHECKING:
14+
from infrahub_sdk import InfrahubClient
15+
16+
from infrahub.database import InfrahubDatabase
17+
from tests.adapters.message_bus import BusSimulator
18+
19+
20+
class TestPreviousVersions(TestInfrahubApp):
21+
@pytest.fixture(scope="class")
22+
async def initial_dataset(
23+
self,
24+
db: InfrahubDatabase,
25+
initialize_registry: None,
26+
client: InfrahubClient,
27+
bus_simulator: BusSimulator,
28+
prefect_test_fixture: None,
29+
) -> None:
30+
await load_schema(db, schema=CAR_SCHEMA, update_db=True)
31+
32+
p1 = await Node.init(db=db, schema="TestingPerson")
33+
await p1.new(db=db, name="John", height=180)
34+
await p1.save(db=db)
35+
p2 = await Node.init(db=db, schema="TestingPerson")
36+
await p2.new(db=db, name="Jane", height=170)
37+
await p2.save(db=db)
38+
39+
async def test_query_at_previous_schema(self, initial_dataset: str, client: InfrahubClient) -> None:
40+
time_before = Timestamp()
41+
42+
query = """
43+
query {
44+
TestingPerson {
45+
edges {
46+
node {
47+
display_label
48+
}
49+
}
50+
}
51+
}
52+
"""
53+
54+
response = await client.execute_graphql(query=query)
55+
56+
assert response == {
57+
"TestingPerson": {
58+
"edges": [
59+
{"node": {"display_label": "John"}},
60+
{"node": {"display_label": "Jane"}},
61+
],
62+
},
63+
}
64+
65+
creation = await client.schema.load(
66+
schemas=[
67+
{
68+
"version": "1.0",
69+
"nodes": [
70+
{
71+
"name": "Person",
72+
"namespace": "Testing",
73+
"default_filter": "name__value",
74+
"display_labels": ["name__value", "height__value"],
75+
"attributes": [
76+
{"name": "name", "kind": "Text", "unique": True},
77+
{"name": "height", "kind": "Number", "optional": True},
78+
{"name": "description", "kind": "Text", "optional": True},
79+
{"name": "age", "kind": "Number", "optional": True},
80+
],
81+
"inherit_from": ["LineageOwner", "LineageSource"],
82+
},
83+
],
84+
}
85+
]
86+
)
87+
assert creation.schema_updated
88+
response = await client.execute_graphql(query=query)
89+
assert response == {
90+
"TestingPerson": {
91+
"edges": [
92+
{"node": {"display_label": "John 180"}},
93+
{"node": {"display_label": "Jane 170"}},
94+
],
95+
},
96+
}
97+
98+
# Query before we updated the schema to validate that we can pull the latest schema
99+
response = await client.execute_graphql(query=query, at=time_before.to_string())
100+
assert response == {
101+
"TestingPerson": {
102+
"edges": [
103+
{"node": {"display_label": "John"}},
104+
{"node": {"display_label": "Jane"}},
105+
],
106+
},
107+
}

backend/tests/helpers/schema/person.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
include_in_menu=True,
99
label="Person",
1010
default_filter="name__value",
11+
display_labels=["name__value"],
1112
attributes=[
1213
AttributeSchema(name="name", kind="Text"),
1314
AttributeSchema(name="description", kind="Text", optional=True),

backend/tests/integration/schema_lifecycle/test_schema_migration_main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ async def test_step03_load(self, db: InfrahubDatabase, client: InfrahubClient, i
210210
assert not hasattr(john, "height")
211211

212212
# Ensure that we can query the existing node with graphql endpoint
213+
await client.schema.all(refresh=True)
213214
api_persons = await client.filters(kind=PERSON_KIND, firstname__value="John")
214215
assert len(api_persons) == 1
215216
api_john = api_persons[0]

0 commit comments

Comments
 (0)