Skip to content

Commit c1a4bb6

Browse files
committed
Minor changes and fix test_graphql_group_update test
1 parent b0ec093 commit c1a4bb6

File tree

10 files changed

+86
-64
lines changed

10 files changed

+86
-64
lines changed

backend/infrahub/api/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
GRAPHQL_RESPONSE_SIZE_METRICS,
2424
GRAPHQL_TOP_LEVEL_QUERIES_METRICS,
2525
)
26-
from infrahub.graphql.models import RequestGraphQLQueryGroupUpdate
2726
from infrahub.graphql.utils import extract_data
27+
from infrahub.groups.models import RequestGraphQLQueryGroupUpdate
2828
from infrahub.log import get_logger
2929
from infrahub.workflows.catalogue import REQUEST_GRAPHQL_QUERY_GROUP_UPDATE
3030

backend/infrahub/graphql/tasks.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
from infrahub_sdk import InfrahubClient
44
from infrahub_sdk.node import InfrahubNode
5-
from infrahub_sdk.utils import dict_hash
6-
from prefect import flow
7-
8-
from infrahub.core.constants import InfrahubKind
9-
from infrahub.graphql.models import RequestGraphQLQueryGroupUpdate
10-
from infrahub.services import services
11-
from infrahub.workflows.utils import add_branch_tag
125

136

147
async def _group_add_subscriber(
@@ -33,31 +26,3 @@ async def _group_add_subscriber(
3326
)
3427

3528
return await client.execute_graphql(query=query, branch_name=branch, tracker="mutation-relationshipadd")
36-
37-
38-
@flow(name="request_graphql_query_group_update")
39-
async def request_graphql_query_group_update(model: RequestGraphQLQueryGroupUpdate) -> None:
40-
"""Create or Update a GraphQLQueryGroup."""
41-
42-
await add_branch_tag(branch_name=model.branch)
43-
service = services.service
44-
45-
params_hash = dict_hash(model.params)
46-
group_name = f"{model.query_name}__{params_hash}"
47-
group_label = f"Query {model.query_name} Hash({params_hash[:8]})"
48-
group = await service.client.create(
49-
kind=InfrahubKind.GRAPHQLQUERYGROUP,
50-
branch=model.branch,
51-
name=group_name,
52-
label=group_label,
53-
group_type="internal",
54-
query=model.query_id,
55-
parameters=model.params,
56-
members=model.related_node_ids,
57-
)
58-
await group.save(allow_upsert=True)
59-
60-
if model.subscribers:
61-
await _group_add_subscriber(
62-
client=service.client, group=group, subscribers=model.subscribers, branch=model.branch
63-
)

backend/infrahub/groups/__init__.py

Whitespace-only changes.
File renamed without changes.

backend/infrahub/groups/tasks.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from infrahub_sdk.utils import dict_hash
2+
3+
from infrahub.core.constants import InfrahubKind
4+
from infrahub.graphql.tasks import _group_add_subscriber
5+
from infrahub.groups.models import RequestGraphQLQueryGroupUpdate
6+
from infrahub.services import services
7+
from infrahub.workflows.utils import add_branch_tag
8+
9+
10+
# @flow(name="update_graphql_query_group")
11+
async def update_graphql_query_group(model: RequestGraphQLQueryGroupUpdate) -> None:
12+
"""Create or Update a GraphQLQueryGroup."""
13+
14+
await add_branch_tag(branch_name=model.branch)
15+
service = services.service
16+
17+
params_hash = dict_hash(model.params)
18+
group_name = f"{model.query_name}__{params_hash}"
19+
group_label = f"Query {model.query_name} Hash({params_hash[:8]})"
20+
group = await service.client.create(
21+
kind=InfrahubKind.GRAPHQLQUERYGROUP,
22+
branch=model.branch,
23+
name=group_name,
24+
label=group_label,
25+
group_type="internal",
26+
query=model.query_id,
27+
parameters=model.params,
28+
members=model.related_node_ids,
29+
)
30+
await group.save(allow_upsert=True)
31+
32+
if model.subscribers:
33+
await _group_add_subscriber(
34+
client=service.client, group=group, subscribers=model.subscribers, branch=model.branch
35+
)

backend/infrahub/workflows/catalogue.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,11 @@
188188
)
189189

190190
REQUEST_GRAPHQL_QUERY_GROUP_UPDATE = WorkflowDefinition(
191-
name="request_graphql_query_group_update",
191+
name="update_graphql_query_group",
192192
type=WorkflowType.INTERNAL,
193193
module="infrahub.graphql.tasks",
194-
function="request_graphql_query_group_update",
194+
function="update_graphql_query_group",
195195
branch_support=BranchSupportType.AWARE,
196-
tags=[WorkflowTag.DATABASE_CHANGE],
197196
)
198197

199198
worker_pools = [INFRAHUB_WORKER_POOL]

backend/tests/helpers/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
from contextlib import contextmanager
2+
from typing import Generator
3+
4+
from infrahub_sdk import InfrahubClient
15
from testcontainers.core.container import DockerContainer
26
from testcontainers.core.waiting_utils import wait_for_logs
37

8+
from infrahub.services import InfrahubServices, services
9+
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
410
from tests.helpers.constants import PORT_BOLT_NEO4J, PORT_HTTP_NEO4J
511

612

@@ -29,3 +35,19 @@ def start_neo4j_container(neo4j_image: str) -> DockerContainer:
2935
container.start()
3036
wait_for_logs(container, "Started.") # wait_container_is_ready does not seem to be enough
3137
return container
38+
39+
40+
@contextmanager
41+
def init_service_with_client(client: InfrahubClient) -> Generator:
42+
"""
43+
This helper is needed for tests defining a specific client while `service` still needs to be accessed
44+
through a global variable within prefect tasks.
45+
"""
46+
47+
original = services.service
48+
service = InfrahubServices(client=client, workflow=WorkflowLocalExecution())
49+
services.service = service
50+
try:
51+
yield service
52+
finally:
53+
services.service = original

backend/tests/unit/api/test_05_query_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
from infrahub.core.initialization import create_branch
9-
from infrahub.graphql.models import RequestGraphQLQueryGroupUpdate
9+
from infrahub.groups.models import RequestGraphQLQueryGroupUpdate
1010
from infrahub.workflows.catalogue import REQUEST_GRAPHQL_QUERY_GROUP_UPDATE
1111

1212
if TYPE_CHECKING:

backend/tests/unit/conftest.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,9 +2943,6 @@ def workflow_local():
29432943
@pytest.fixture
29442944
def init_service(db: InfrahubDatabase):
29452945
original = services.service
2946-
database = db
2947-
workflow = WorkflowLocalExecution()
2948-
service = InfrahubServices(database=database, workflow=workflow)
2949-
services.service = service
2950-
yield service
2946+
services.service = InfrahubServices(database=db, workflow=WorkflowLocalExecution())
2947+
yield services.service
29512948
services.service = original

backend/tests/unit/message_bus/operations/requests/test_graphql_query_group.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import os
22
import uuid
33
from pathlib import Path
4+
from unittest.mock import patch
45

56
import pytest
67
import ujson
78
from infrahub_sdk import Config, InfrahubClient
89
from pytest_httpx import HTTPXMock
910

1011
from infrahub.database import InfrahubDatabase
11-
from infrahub.graphql.models import RequestGraphQLQueryGroupUpdate
12-
from infrahub.graphql.tasks import request_graphql_query_group_update
13-
from infrahub.services import InfrahubServices
12+
from infrahub.groups.models import RequestGraphQLQueryGroupUpdate
13+
from infrahub.groups.tasks import update_graphql_query_group
14+
from tests.helpers.utils import init_service_with_client
1415

1516

1617
@pytest.fixture
@@ -44,23 +45,26 @@ async def test_graphql_group_update(db: InfrahubDatabase, httpx_mock: HTTPXMock,
4445
client = InfrahubClient(
4546
config=config,
4647
)
47-
service = InfrahubServices(client=client)
4848

49-
response1 = {
50-
"data": {"CoreGraphQLQueryGroupUpsert": {"ok": True, "object": {"id": "957aea37-4510-4386-916f-3febd6665ae6"}}}
51-
}
49+
with init_service_with_client(client=client), patch("infrahub.groups.tasks.add_branch_tag"):
50+
# add_branch_tag requires a prefect client, ie it does not work with WorkflowLocal
51+
response1 = {
52+
"data": {
53+
"CoreGraphQLQueryGroupUpsert": {"ok": True, "object": {"id": "957aea37-4510-4386-916f-3febd6665ae6"}}
54+
}
55+
}
5256

53-
httpx_mock.add_response(
54-
method="POST",
55-
json=response1,
56-
match_headers={"X-Infrahub-Tracker": "mutation-coregraphqlquerygroup-upsert"},
57-
)
57+
httpx_mock.add_response(
58+
method="POST",
59+
json=response1,
60+
match_headers={"X-Infrahub-Tracker": "mutation-coregraphqlquerygroup-upsert"},
61+
)
5862

59-
response2 = {"data": {"RelationshipAdd": {"ok": True}}}
60-
httpx_mock.add_response(
61-
method="POST",
62-
json=response2,
63-
match_headers={"X-Infrahub-Tracker": "mutation-relationshipadd"},
64-
)
63+
response2 = {"data": {"RelationshipAdd": {"ok": True}}}
64+
httpx_mock.add_response(
65+
method="POST",
66+
json=response2,
67+
match_headers={"X-Infrahub-Tracker": "mutation-relationshipadd"},
68+
)
6569

66-
await request_graphql_query_group_update.fn(message=model, service=service)
70+
await update_graphql_query_group(model=model)

0 commit comments

Comments
 (0)