Skip to content

Commit

Permalink
Merge pull request #4684 from opsmill/dga-20241022-fix-test-merged
Browse files Browse the repository at this point in the history
Fix service fixture initialization for  test_merged
  • Loading branch information
LucasG0 authored Oct 23, 2024
2 parents e97e8f7 + 1d86a08 commit 29e35aa
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions backend/tests/unit/message_bus/operations/event/test_branch.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
from unittest.mock import AsyncMock, MagicMock, Mock, patch
from uuid import uuid4

import pytest

from infrahub.core.branch import Branch
from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRoot
from infrahub.core.diff.repository.repository import DiffRepository
from infrahub.core.timestamp import Timestamp
from infrahub.dependencies.component.registry import ComponentDependencyRegistry
from infrahub.message_bus import messages
from infrahub.message_bus.operations.event.branch import delete, merge, rebased
from infrahub.services import InfrahubServices
from infrahub.services import InfrahubServices, services
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
from tests.adapters.message_bus import BusRecorder


@pytest.fixture
def init_service():
original = services.service
recorder = BusRecorder()
database = MagicMock()
workflow = WorkflowLocalExecution()
service = InfrahubServices(message_bus=recorder, database=database, workflow=workflow)
services.service = service
yield service
services.service = original


async def test_delete(prefect_test_fixture):
"""Validate that a deleted branch triggers a registry refresh and cancels open proposed changes"""

Expand All @@ -32,18 +46,15 @@ async def test_delete(prefect_test_fixture):
assert trigger_cancel.branch == "cr1234"


async def test_merged(default_branch: Branch, prefect_test_fixture):
async def test_merged(default_branch: Branch, init_service: InfrahubServices, prefect_test_fixture):
source_branch_name = "cr1234"
target_branch_name = "main"
right_now = Timestamp()
message = messages.EventBranchMerge(
source_branch=source_branch_name, target_branch=target_branch_name, ipam_node_details=[]
)
service = init_service

recorder = BusRecorder()
database = MagicMock()
workflow = WorkflowLocalExecution()
service = InfrahubServices(message_bus=recorder, database=database, workflow=workflow)
tracked_diff_roots = [
EnrichedDiffRoot(
base_branch_name=target_branch_name,
Expand Down Expand Up @@ -76,14 +87,20 @@ async def test_merged(default_branch: Branch, prefect_test_fixture):
with patch("infrahub.message_bus.operations.event.branch.get_component_registry", new=mock_get_component_registry):
await merge(message=message, service=service)

mock_component_registry.get_component.assert_awaited_once_with(DiffRepository, db=database, branch=default_branch)
mock_component_registry.get_component.assert_awaited_once_with(
DiffRepository, db=service.database, branch=default_branch
)
diff_repo.get_empty_roots.assert_awaited_once_with(base_branch_names=[target_branch_name])
assert len(recorder.messages) == 5
assert recorder.messages[0] == messages.RefreshRegistryBranches()
assert recorder.messages[1] == messages.TriggerArtifactDefinitionGenerate(branch=target_branch_name)
assert recorder.messages[2] == messages.TriggerGeneratorDefinitionRun(branch=target_branch_name)
assert recorder.messages[3] == messages.RequestDiffUpdate(branch_name=tracked_diff_roots[0].diff_branch_name)
assert recorder.messages[4] == messages.RequestDiffUpdate(branch_name=tracked_diff_roots[1].diff_branch_name)
assert len(service.message_bus.messages) == 5
assert service.message_bus.messages[0] == messages.RefreshRegistryBranches()
assert service.message_bus.messages[1] == messages.TriggerArtifactDefinitionGenerate(branch=target_branch_name)
assert service.message_bus.messages[2] == messages.TriggerGeneratorDefinitionRun(branch=target_branch_name)
assert service.message_bus.messages[3] == messages.RequestDiffUpdate(
branch_name=tracked_diff_roots[0].diff_branch_name
)
assert service.message_bus.messages[4] == messages.RequestDiffUpdate(
branch_name=tracked_diff_roots[1].diff_branch_name
)


async def test_rebased(default_branch: Branch, prefect_test_fixture):
Expand Down

0 comments on commit 29e35aa

Please sign in to comment.