|
18 | 18 | SchemaPathType, |
19 | 19 | ) |
20 | 20 | from infrahub.core.schema import ( |
| 21 | + AttributeSchema, |
21 | 22 | GenericSchema, |
22 | 23 | NodeSchema, |
| 24 | + RelationshipSchema, |
23 | 25 | SchemaRoot, |
24 | 26 | core_models, |
25 | 27 | internal_schema, |
26 | 28 | ) |
| 29 | +from infrahub.core.schema.computed_attribute import ComputedAttribute |
27 | 30 | from infrahub.core.schema.manager import SchemaManager |
28 | 31 | from infrahub.core.schema.schema_branch import SchemaBranch |
29 | 32 | from infrahub.database import InfrahubDatabase |
30 | 33 | from infrahub.exceptions import SchemaNotFoundError, ValidationError |
| 34 | +from tests.helpers.schema import CHILD, THING |
31 | 35 |
|
32 | 36 | from .conftest import _get_schema_by_kind |
33 | 37 |
|
@@ -1416,10 +1420,11 @@ async def test_schema_branch_validate_count_against_cardinality_invalid(relation |
1416 | 1420 |
|
1417 | 1421 |
|
1418 | 1422 | async def test_schema_branch_from_dict_schema_object(): |
1419 | | - schema = SchemaRoot(**core_models) |
1420 | | - |
1421 | 1423 | schema_branch = SchemaBranch(cache={}, name="test") |
1422 | | - schema_branch.load_schema(schema=schema) |
| 1424 | + |
| 1425 | + # Load the core models and a model with a computed_attribute |
| 1426 | + schema_branch.load_schema(schema=SchemaRoot(**core_models)) |
| 1427 | + schema_branch.load_schema(schema=SchemaRoot(nodes=[CHILD, THING])) |
1423 | 1428 |
|
1424 | 1429 | exported = schema_branch.to_dict_schema_object() |
1425 | 1430 |
|
@@ -2073,31 +2078,32 @@ async def test_load_node_to_db_node_schema(db: InfrahubDatabase, default_branch: |
2073 | 2078 | registry.schema = SchemaManager() |
2074 | 2079 | registry.schema.register_schema(schema=SchemaRoot(**internal_schema), branch=default_branch.name) |
2075 | 2080 |
|
2076 | | - SCHEMA = { |
2077 | | - "name": "Criticality", |
2078 | | - "namespace": "Builtin", |
2079 | | - "default_filter": "name__value", |
2080 | | - "attributes": [ |
2081 | | - {"name": "name", "kind": "Text", "unique": True}, |
2082 | | - {"name": "level", "kind": "Number"}, |
2083 | | - {"name": "color", "kind": "Text", "default_value": "#444444"}, |
2084 | | - {"name": "description", "kind": "Text", "optional": True}, |
2085 | | - ], |
2086 | | - "relationships": [ |
2087 | | - {"name": "others", "peer": "BuiltinCriticality", "optional": True, "cardinality": "many"}, |
| 2081 | + node = NodeSchema( |
| 2082 | + name="Criticality", |
| 2083 | + namespace="Builtin", |
| 2084 | + default_filter="name__value", |
| 2085 | + attributes=[ |
| 2086 | + AttributeSchema(name="name", kind="Text", unique=True), |
| 2087 | + AttributeSchema(name="level", kind="Number"), |
| 2088 | + AttributeSchema(name="color", kind="Text", default_value="default_value"), |
| 2089 | + AttributeSchema( |
| 2090 | + name="description", |
| 2091 | + kind="Text", |
| 2092 | + optional=True, |
| 2093 | + computed_attribute=ComputedAttribute(kind="Jinja2", jinja2_template="{{ name__value }}"), |
| 2094 | + ), |
2088 | 2095 | ], |
2089 | | - } |
2090 | | - node = NodeSchema(**SCHEMA) |
2091 | | - |
| 2096 | + relationships=[RelationshipSchema(name="others", peer="BuiltinCriticality", optional=True, cardinality="many")], |
| 2097 | + ) |
2092 | 2098 | await registry.schema.load_node_to_db(node=node, db=db, branch=default_branch) |
2093 | 2099 |
|
2094 | 2100 | node2 = registry.schema.get(name=node.kind, branch=default_branch) |
2095 | 2101 | assert node2.id |
2096 | 2102 | assert node2.relationships[0].id |
2097 | 2103 | assert node2.attributes[0].id |
2098 | 2104 |
|
2099 | | - results = await SchemaManager.query(schema="SchemaNode", branch=default_branch, db=db) |
2100 | | - assert len(results) == 1 |
| 2105 | + node_from_db = await SchemaManager.get_one(db=db, id=node2.id, branch=default_branch) |
| 2106 | + assert node_from_db |
2101 | 2107 |
|
2102 | 2108 |
|
2103 | 2109 | async def test_load_node_to_db_generic_schema(db: InfrahubDatabase, default_branch): |
@@ -2249,7 +2255,14 @@ async def test_load_schema_from_db( |
2249 | 2255 | {"name": "name", "kind": "Text", "label": "Name", "unique": True}, |
2250 | 2256 | {"name": "level", "kind": "Number", "label": "Level"}, |
2251 | 2257 | {"name": "color", "kind": "Text", "label": "Color", "default_value": "#444444"}, |
2252 | | - {"name": "description", "kind": "Text", "label": "Description", "optional": True}, |
| 2258 | + { |
| 2259 | + "name": "description", |
| 2260 | + "kind": "Text", |
| 2261 | + "label": "Description", |
| 2262 | + "optional": True, |
| 2263 | + "read_only": True, |
| 2264 | + "computed_attribute": {"kind": "Jinja2", "jinja2_template": "{{ name__value }}"}, |
| 2265 | + }, |
2253 | 2266 | ], |
2254 | 2267 | "relationships": [ |
2255 | 2268 | { |
@@ -2320,6 +2333,10 @@ async def test_load_schema_from_db( |
2320 | 2333 | assert schema11.get(name=InfrahubKind.TAG).get_hash() == schema2.get(name="BuiltinTag").get_hash() |
2321 | 2334 | assert schema11.get(name="TestGenericInterface").get_hash() == schema2.get(name="TestGenericInterface").get_hash() |
2322 | 2335 |
|
| 2336 | + description_schema = crit_schema.get_attribute("description") |
| 2337 | + assert description_schema.computed_attribute is not None |
| 2338 | + assert description_schema.computed_attribute.jinja2_template == "{{ name__value }}" |
| 2339 | + |
2323 | 2340 |
|
2324 | 2341 | async def test_load_schema( |
2325 | 2342 | db: InfrahubDatabase, reset_registry, default_branch: Branch, register_internal_models_schema |
|
0 commit comments