|
3 | 3 | from infrahub import config |
4 | 4 | from infrahub.core import registry |
5 | 5 | from infrahub.core.branch.models import Branch |
6 | | -from infrahub.core.constants import InfrahubKind, SchemaPathType |
| 6 | +from infrahub.core.constants import InfrahubKind, RelationshipKind, SchemaPathType |
7 | 7 | from infrahub.core.initialization import create_branch |
8 | 8 | from infrahub.core.manager import NodeManager |
9 | 9 | from infrahub.core.migrations.schema.node_kind_update import NodeKindUpdateMigration |
|
17 | 17 | from infrahub.graphql.initialization import prepare_graphql_params |
18 | 18 | from tests.constants import TestKind |
19 | 19 | from tests.helpers.graphql import graphql |
20 | | -from tests.helpers.schema import DEVICE_SCHEMA |
| 20 | +from tests.helpers.schema import CAR_SCHEMA, DEVICE_SCHEMA |
21 | 21 |
|
22 | 22 |
|
23 | 23 | async def test_create_simple_object(db: InfrahubDatabase, default_branch: Branch, car_person_schema: None) -> None: |
@@ -1523,6 +1523,100 @@ async def test_create_with_object_template( |
1523 | 1523 | assert sfp.part_number.source_id is None |
1524 | 1524 |
|
1525 | 1525 |
|
| 1526 | +async def test_create_with_object_template_and_real_object( |
| 1527 | + db: InfrahubDatabase, default_branch: Branch, register_core_models_schema: SchemaBranch, branch: Branch |
| 1528 | +): |
| 1529 | + """ |
| 1530 | + Test that relationships on sub-templates will correctly link the created sub-object to an existing object on non-component relationships |
| 1531 | + """ |
| 1532 | + updated_car_schema = CAR_SCHEMA.duplicate() |
| 1533 | + manufacturer_schema = updated_car_schema.get(name=TestKind.MANUFACTURER) |
| 1534 | + manufacturer_schema.generate_template = True |
| 1535 | + cars_rel = manufacturer_schema.get_relationship(name="cars") |
| 1536 | + cars_rel.kind = RelationshipKind.COMPONENT |
| 1537 | + person_schema = updated_car_schema.get(name=TestKind.PERSON) |
| 1538 | + person_schema.generate_template = True |
| 1539 | + car_schema = updated_car_schema.get(name=TestKind.CAR) |
| 1540 | + car_schema.generate_template = True |
| 1541 | + manufacturer_rel = car_schema.get_relationship(name="manufacturer") |
| 1542 | + manufacturer_rel.kind = RelationshipKind.PARENT |
| 1543 | + registry.schema.register_schema(schema=updated_car_schema, branch=branch.name) |
| 1544 | + |
| 1545 | + manufacturer_object = await Node.init(schema=TestKind.MANUFACTURER, db=db, branch=branch) |
| 1546 | + await manufacturer_object.new(db=db, name="Hark Motors") |
| 1547 | + await manufacturer_object.save(db=db) |
| 1548 | + |
| 1549 | + person_object = await Node.init(schema=TestKind.PERSON, db=db, branch=branch) |
| 1550 | + await person_object.new(db=db, name="John", height=180) |
| 1551 | + await person_object.save(db=db) |
| 1552 | + |
| 1553 | + car_object = await Node.init(schema=TestKind.CAR, db=db, branch=branch) |
| 1554 | + await car_object.new(db=db, name="Accord", manufacturer=manufacturer_object, owner=person_object, color="blurple") |
| 1555 | + await car_object.save(db=db) |
| 1556 | + |
| 1557 | + manufacturer_template: Node = await Node.init(schema=f"Template{TestKind.MANUFACTURER}", db=db, branch=branch) |
| 1558 | + await manufacturer_template.new(db=db, template_name="m_template", customers=[person_object]) |
| 1559 | + await manufacturer_template.save(db=db) |
| 1560 | + |
| 1561 | + car_template_with_person_object = await Node.init(schema=f"Template{TestKind.CAR}", db=db, branch=branch) |
| 1562 | + await car_template_with_person_object.new( |
| 1563 | + db=db, |
| 1564 | + template_name="c_template", |
| 1565 | + name="Civic", |
| 1566 | + color="blurple", |
| 1567 | + manufacturer=manufacturer_template, |
| 1568 | + owner=person_object, |
| 1569 | + ) |
| 1570 | + await car_template_with_person_object.save(db=db) |
| 1571 | + |
| 1572 | + create_manufacturer_with_template_query = """ |
| 1573 | + mutation CreateManufacturerWithTemplate($manufacturer_name: String!, $template_id: String!) { |
| 1574 | + TestingManufacturerCreate(data: { |
| 1575 | + name: {value: $manufacturer_name} |
| 1576 | + object_template: {id: $template_id} |
| 1577 | + }) { |
| 1578 | + ok |
| 1579 | + object { |
| 1580 | + id |
| 1581 | + } |
| 1582 | + } |
| 1583 | + } |
| 1584 | + """ |
| 1585 | + gql_params = await prepare_graphql_params(db=db, branch=branch) |
| 1586 | + result = await graphql( |
| 1587 | + schema=gql_params.schema, |
| 1588 | + source=create_manufacturer_with_template_query, |
| 1589 | + context_value=gql_params.context, |
| 1590 | + variable_values={"manufacturer_name": "Fresh Motors", "template_id": manufacturer_template.id}, |
| 1591 | + ) |
| 1592 | + assert not result.errors |
| 1593 | + new_manufacturer = await NodeManager.get_one( |
| 1594 | + db=db, |
| 1595 | + kind=TestKind.MANUFACTURER, |
| 1596 | + branch=branch, |
| 1597 | + id=result.data[f"{TestKind.MANUFACTURER}Create"]["object"]["id"], |
| 1598 | + ) |
| 1599 | + assert new_manufacturer |
| 1600 | + assert new_manufacturer.name.value == "Fresh Motors" |
| 1601 | + customers_peers = await new_manufacturer.customers.get_peers(db=db) |
| 1602 | + assert len(customers_peers) == 1 |
| 1603 | + customers_by_name = {person.name.value: person for person in customers_peers.values()} |
| 1604 | + # check non-template person |
| 1605 | + non_template_person = customers_by_name["John"] |
| 1606 | + assert non_template_person.id == person_object.id |
| 1607 | + |
| 1608 | + cars_peers = await new_manufacturer.cars.get_peers(db=db) |
| 1609 | + assert len(cars_peers) == 1 |
| 1610 | + cars_by_name = {car.name.value: car for car in cars_peers.values()} |
| 1611 | + # check car template with person object |
| 1612 | + car_template_with_person_object = cars_by_name["Civic"] |
| 1613 | + assert car_template_with_person_object.color.value == "blurple" |
| 1614 | + car_manufacturer = await car_template_with_person_object.manufacturer.get_peer(db=db) |
| 1615 | + assert car_manufacturer.id == new_manufacturer.id |
| 1616 | + car_owner = await car_template_with_person_object.owner.get_peer(db=db) |
| 1617 | + assert car_owner.id == person_object.id |
| 1618 | + |
| 1619 | + |
1526 | 1620 | async def test_create_without_object_template( |
1527 | 1621 | db: InfrahubDatabase, default_branch: Branch, register_core_models_schema: SchemaBranch, branch: Branch |
1528 | 1622 | ): |
|
0 commit comments