|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | +from infrahub_sdk.exceptions import BranchNotFoundError |
| 7 | +from infrahub_sdk.graphql import Mutation |
| 8 | + |
| 9 | +from infrahub.core.constants import InfrahubKind |
| 10 | +from infrahub.core.node import Node |
| 11 | +from infrahub.services.adapters.cache.redis import RedisCache |
| 12 | +from tests.constants import TestKind |
| 13 | +from tests.helpers.file_repo import FileRepo |
| 14 | +from tests.helpers.schema import CAR_SCHEMA, load_schema |
| 15 | +from tests.helpers.test_app import TestInfrahubApp |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from pathlib import Path |
| 19 | + |
| 20 | + from infrahub_sdk import InfrahubClient |
| 21 | + |
| 22 | + from infrahub.database import InfrahubDatabase |
| 23 | + from tests.adapters.message_bus import BusSimulator |
| 24 | + |
| 25 | + |
| 26 | +class TestBranchMutations(TestInfrahubApp): |
| 27 | + @pytest.fixture(scope="class") |
| 28 | + async def initial_dataset( |
| 29 | + self, |
| 30 | + db: InfrahubDatabase, |
| 31 | + initialize_registry: None, |
| 32 | + git_repos_source_dir_module_scope: Path, |
| 33 | + client: InfrahubClient, |
| 34 | + bus_simulator: BusSimulator, |
| 35 | + prefect_test_fixture: None, |
| 36 | + ) -> str: |
| 37 | + await load_schema(db, schema=CAR_SCHEMA) |
| 38 | + |
| 39 | + john = await Node.init(schema=TestKind.PERSON, db=db) |
| 40 | + await john.new(db=db, name="John", height=175, description="The famous Joe Doe") |
| 41 | + await john.save(db=db) |
| 42 | + |
| 43 | + koenigsegg = await Node.init(schema=TestKind.MANUFACTURER, db=db) |
| 44 | + await koenigsegg.new(db=db, name="Koenigsegg") |
| 45 | + await koenigsegg.save(db=db) |
| 46 | + |
| 47 | + people = await Node.init(schema=InfrahubKind.STANDARDGROUP, db=db) |
| 48 | + await people.new(db=db, name="people", members=[john]) |
| 49 | + await people.save(db=db) |
| 50 | + |
| 51 | + jesko = await Node.init(schema=TestKind.CAR, db=db) |
| 52 | + await jesko.new( |
| 53 | + db=db, |
| 54 | + name="Jesko", |
| 55 | + color="Red", |
| 56 | + description="A limited production mid-engine sports car", |
| 57 | + owner=john, |
| 58 | + manufacturer=koenigsegg, |
| 59 | + ) |
| 60 | + await jesko.save(db=db) |
| 61 | + |
| 62 | + bus_simulator.service.cache = RedisCache() |
| 63 | + FileRepo(name="car-dealership", sources_directory=git_repos_source_dir_module_scope) |
| 64 | + client_repository = await client.create( |
| 65 | + kind=InfrahubKind.REPOSITORY, |
| 66 | + data={"name": "car-dealership", "location": f"{git_repos_source_dir_module_scope}/car-dealership"}, |
| 67 | + ) |
| 68 | + await client_repository.save() |
| 69 | + return client_repository.id |
| 70 | + |
| 71 | + async def test_branch_delete_async(self, initial_dataset: str, client: InfrahubClient) -> None: |
| 72 | + branch = await client.branch.create(branch_name="branch_to_delete") |
| 73 | + |
| 74 | + query = Mutation( |
| 75 | + mutation="BranchDelete", |
| 76 | + input_data={"data": {"name": branch.name}, "wait_until_completion": False}, |
| 77 | + query={"ok": None, "task": {"id": None}}, |
| 78 | + ) |
| 79 | + result = await client.execute_graphql(query=query.render()) |
| 80 | + assert result["BranchDelete"]["ok"] is True |
| 81 | + assert result["BranchDelete"]["task"]["id"] |
| 82 | + |
| 83 | + with pytest.raises(BranchNotFoundError): |
| 84 | + await client.branch.get(branch_name=branch.name) |
| 85 | + |
| 86 | + async def test_branch_delete(self, initial_dataset: str, client: InfrahubClient) -> None: |
| 87 | + branch = await client.branch.create(branch_name="branch_to_delete_sync") |
| 88 | + |
| 89 | + query = Mutation( |
| 90 | + mutation="BranchDelete", |
| 91 | + input_data={"data": {"name": branch.name}}, |
| 92 | + query={"ok": None, "task": {"id": None}}, |
| 93 | + ) |
| 94 | + result = await client.execute_graphql(query=query.render()) |
| 95 | + assert result["BranchDelete"]["ok"] is True |
| 96 | + assert result["BranchDelete"]["task"] is None |
| 97 | + |
| 98 | + with pytest.raises(BranchNotFoundError): |
| 99 | + await client.branch.get(branch_name=branch.name) |
| 100 | + |
| 101 | + async def test_branch_rebase_async(self, initial_dataset: str, client: InfrahubClient) -> None: |
| 102 | + branch = await client.branch.create(branch_name="branch_to_rebase") |
| 103 | + |
| 104 | + query = Mutation( |
| 105 | + mutation="BranchRebase", |
| 106 | + input_data={"data": {"name": branch.name}, "wait_until_completion": False}, |
| 107 | + query={"ok": None, "task": {"id": None}, "object": {"id": None}}, |
| 108 | + ) |
| 109 | + result = await client.execute_graphql(query=query.render()) |
| 110 | + assert result["BranchRebase"]["ok"] is True |
| 111 | + assert result["BranchRebase"]["object"]["id"] == branch.id |
| 112 | + assert result["BranchRebase"]["task"]["id"] |
| 113 | + |
| 114 | + branch_after = await client.branch.get(branch_name=branch.name) |
| 115 | + assert branch.branched_from != branch_after.branched_from |
| 116 | + |
| 117 | + async def test_branch_rebase(self, initial_dataset: str, client: InfrahubClient) -> None: |
| 118 | + branch = await client.branch.create(branch_name="branch_to_rebase_sync") |
| 119 | + |
| 120 | + query = Mutation( |
| 121 | + mutation="BranchRebase", |
| 122 | + input_data={"data": {"name": branch.name}}, |
| 123 | + query={"ok": None, "task": {"id": None}, "object": {"id": None}}, |
| 124 | + ) |
| 125 | + result = await client.execute_graphql(query=query.render()) |
| 126 | + assert result["BranchRebase"]["ok"] is True |
| 127 | + assert result["BranchRebase"]["object"]["id"] == branch.id |
| 128 | + assert result["BranchRebase"]["task"] is None |
| 129 | + |
| 130 | + branch_after = await client.branch.get(branch_name=branch.name) |
| 131 | + assert branch.branched_from != branch_after.branched_from |
0 commit comments