|
12 | 12 | from tests.adapters.event import MemoryInfrahubEvent |
13 | 13 | from tests.constants import TestKind |
14 | 14 | from tests.helpers.graphql import graphql |
15 | | -from tests.helpers.schema import TICKET |
| 15 | +from tests.helpers.schema import COLOR, TICKET, TSHIRT |
16 | 16 | from tests.node_creation import create_and_save |
17 | 17 |
|
18 | 18 |
|
@@ -673,3 +673,82 @@ async def test_upsert_node_on_branch_with_hfid_on_default(db: InfrahubDatabase, |
673 | 673 | in result.errors[0].message |
674 | 674 | ) |
675 | 675 | assert f"Please rebase this branch to access {person.id} / TestPerson" in result.errors[0].message |
| 676 | + |
| 677 | + |
| 678 | +async def test_upsert_with_required_relationship_from_template( |
| 679 | + db: InfrahubDatabase, default_branch: Branch, register_core_models_schema: None |
| 680 | +) -> None: |
| 681 | + """Validate that we can use a template to populate required relationships in upsert mutations. |
| 682 | +
|
| 683 | + Steps: |
| 684 | + - Create a color node and a Tshirt template node. |
| 685 | + - Try to upsert a Tshirt without specifying color or template (should fail). |
| 686 | + - Upsert a Tshirt specifying the template (should succeed and apply the color from the template). |
| 687 | + """ |
| 688 | + registry.schema.register_schema(schema=SchemaRoot(nodes=[TSHIRT, COLOR]), branch=default_branch.name) |
| 689 | + |
| 690 | + # Create a color node |
| 691 | + color_node = await Node.init(db=db, schema="TestingColor", branch=default_branch) |
| 692 | + await color_node.new(db=db, name="Red", description="Bright Red Color") |
| 693 | + await color_node.save(db=db) |
| 694 | + |
| 695 | + # Create a Tshirt template node with the color relationship set |
| 696 | + template_node = await Node.init(db=db, schema="TemplateTestingTShirt", branch=default_branch) |
| 697 | + await template_node.new(db=db, template_name="Basic Red Tshirt", color=color_node) |
| 698 | + await template_node.save(db=db) |
| 699 | + |
| 700 | + # Try to upsert a TShirt without specifying color or template (should fail) |
| 701 | + query_missing_required = """ |
| 702 | + mutation { |
| 703 | + TestingTShirtUpsert(data: {name: {value: "My Shirt"} }) { |
| 704 | + ok |
| 705 | + object { |
| 706 | + id |
| 707 | + name { value } |
| 708 | + color { node { id name { value } } } |
| 709 | + } |
| 710 | + } |
| 711 | + } |
| 712 | + """ |
| 713 | + gql_params = await prepare_graphql_params(db=db, include_subscription=False, branch=default_branch) |
| 714 | + result_missing = await graphql( |
| 715 | + schema=gql_params.schema, |
| 716 | + source=query_missing_required, |
| 717 | + context_value=gql_params.context, |
| 718 | + root_value=None, |
| 719 | + variable_values={}, |
| 720 | + ) |
| 721 | + assert result_missing.errors |
| 722 | + assert "color is mandatory for TestingTShirt at color" in str(result_missing.errors) |
| 723 | + |
| 724 | + # Upsert a Tshirt specifying the template (should succeed and apply the color from the template) |
| 725 | + query_with_template = """ |
| 726 | + mutation UpsertTShirt($template_id: String!) { |
| 727 | + TestingTShirtUpsert(data: { |
| 728 | + name: {value: "My Tshirt"}, |
| 729 | + object_template: {id: $template_id} |
| 730 | + }) { |
| 731 | + ok |
| 732 | + object { |
| 733 | + id |
| 734 | + name { value } |
| 735 | + color { node { id name { value } } } |
| 736 | + } |
| 737 | + } |
| 738 | + } |
| 739 | + """ |
| 740 | + |
| 741 | + result_with_template = await graphql( |
| 742 | + schema=gql_params.schema, |
| 743 | + source=query_with_template, |
| 744 | + context_value=gql_params.context, |
| 745 | + root_value=None, |
| 746 | + variable_values={"template_id": template_node.id}, |
| 747 | + ) |
| 748 | + assert result_with_template.errors is None |
| 749 | + assert result_with_template.data |
| 750 | + assert result_with_template.data["TestingTShirtUpsert"]["ok"] is True |
| 751 | + tshirt_obj = result_with_template.data["TestingTShirtUpsert"]["object"] |
| 752 | + assert tshirt_obj["name"]["value"] == "My Tshirt" |
| 753 | + assert tshirt_obj["color"]["node"]["id"] == color_node.id |
| 754 | + assert tshirt_obj["color"]["node"]["name"]["value"] == "Red" |
0 commit comments