Skip to content

Commit 4513b43

Browse files
committed
Add tests for coverage
1 parent 4c69fd6 commit 4513b43

1 file changed

Lines changed: 289 additions & 0 deletions

File tree

tests/unit/pytest_plugin/test_plugin.py

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
import pytest
26

7+
if TYPE_CHECKING:
8+
from pytest_httpx import HTTPXMock
9+
310

411
def test_help_message(pytester: pytest.Pytester) -> None:
512
"""Make sure that the plugin is loaded by capturing an option it adds in the help message."""
@@ -292,3 +299,285 @@ async def transform(self, data):
292299

293300
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
294301
result.assert_outcomes(passed=1)
302+
303+
304+
def test_graphql_query_smoke(pytester: pytest.Pytester) -> None:
305+
"""Smoke item should pass when the query file exists and parses successfully."""
306+
pytester.makefile(
307+
".yml",
308+
test_graphql_query="""
309+
---
310+
version: "1.0"
311+
infrahub_tests:
312+
- resource: "GraphQLQuery"
313+
resource_name: "device_query"
314+
tests:
315+
- name: "smoke"
316+
expect: PASS
317+
spec:
318+
kind: "graphql-query-smoke"
319+
path: "queries/device.gql"
320+
""",
321+
)
322+
pytester.makefile(
323+
".yml",
324+
infrahub_config="""
325+
---
326+
schemas: []
327+
""",
328+
)
329+
queries_dir = pytester.mkdir("queries")
330+
query_file = pytester.makefile(
331+
".gql",
332+
device="""
333+
query DeviceQuery {
334+
InfraDevice {
335+
edges {
336+
node {
337+
name { value }
338+
}
339+
}
340+
}
341+
}
342+
""",
343+
)
344+
pytester.run("mv", query_file, queries_dir)
345+
346+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
347+
result.assert_outcomes(passed=1)
348+
349+
350+
def test_graphql_query_smoke_invalid(pytester: pytest.Pytester) -> None:
351+
"""Smoke item should fail when the query file contains invalid GraphQL."""
352+
pytester.makefile(
353+
".yml",
354+
test_graphql_query="""
355+
---
356+
version: "1.0"
357+
infrahub_tests:
358+
- resource: "GraphQLQuery"
359+
resource_name: "device_query"
360+
tests:
361+
- name: "smoke"
362+
expect: PASS
363+
spec:
364+
kind: "graphql-query-smoke"
365+
path: "queries/broken.gql"
366+
""",
367+
)
368+
pytester.makefile(
369+
".yml",
370+
infrahub_config="""
371+
---
372+
schemas: []
373+
""",
374+
)
375+
queries_dir = pytester.mkdir("queries")
376+
query_file = pytester.makefile(
377+
".gql",
378+
broken="this is not a valid graphql query {",
379+
)
380+
pytester.run("mv", query_file, queries_dir)
381+
382+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
383+
result.assert_outcomes(failed=1)
384+
385+
386+
def test_jinja2_transform_smoke(pytester: pytest.Pytester) -> None:
387+
"""Smoke item should pass when the template parses successfully."""
388+
pytester.makefile(
389+
".yml",
390+
test_jinja2_transform="""
391+
---
392+
version: "1.0"
393+
infrahub_tests:
394+
- resource: "Jinja2Transform"
395+
resource_name: "bgp_config"
396+
tests:
397+
- name: "smoke"
398+
expect: PASS
399+
spec:
400+
kind: "jinja2-transform-smoke"
401+
""",
402+
)
403+
pytester.makefile(
404+
".yml",
405+
infrahub_config="""
406+
---
407+
schemas: []
408+
409+
jinja2_transforms:
410+
- name: bgp_config
411+
description: "Template for BGP config base"
412+
query: "bgp_sessions"
413+
template_path: "templates/bgp_config.j2"
414+
""",
415+
)
416+
template_dir = pytester.mkdir("templates")
417+
template = pytester.makefile(
418+
".j2",
419+
bgp_config="""
420+
protocols {
421+
bgp {
422+
log-up-down;
423+
bgp-error-tolerance;
424+
}
425+
}
426+
""",
427+
)
428+
pytester.run("mv", template, template_dir)
429+
430+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
431+
result.assert_outcomes(passed=1)
432+
433+
434+
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
435+
def test_graphql_query_integration(pytester: pytest.Pytester, httpx_mock: HTTPXMock) -> None:
436+
"""Integration item should pass when the mocked GraphQL response matches the expected output."""
437+
httpx_mock.add_response(
438+
method="POST",
439+
url="http://localhost:8000/api/query/device_query?branch=main&update_group=false&",
440+
json={"data": {"InfraDevice": {"edges": [{"node": {"name": {"value": "atl1-edge1"}}}]}}},
441+
is_reusable=True,
442+
)
443+
444+
pytester.makefile(
445+
".yml",
446+
test_graphql_query="""
447+
---
448+
version: "1.0"
449+
infrahub_tests:
450+
- resource: "GraphQLQuery"
451+
resource_name: "device_query"
452+
tests:
453+
- name: "integration"
454+
expect: PASS
455+
spec:
456+
kind: "graphql-query-integration"
457+
query: "device_query"
458+
variables: {}
459+
""",
460+
)
461+
pytester.makefile(
462+
".yml",
463+
infrahub_config="""
464+
---
465+
schemas: []
466+
""",
467+
)
468+
469+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
470+
result.assert_outcomes(passed=1)
471+
472+
473+
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
474+
def test_jinja2_transform_integration(pytester: pytest.Pytester, httpx_mock: HTTPXMock) -> None:
475+
"""Integration item should render the template using data from a mocked GraphQL response."""
476+
httpx_mock.add_response(
477+
method="POST",
478+
url="http://localhost:8000/api/query/bgp_sessions?branch=main&update_group=false&",
479+
json={"data": {"BgpSession": {"edges": []}}},
480+
is_reusable=True,
481+
)
482+
483+
pytester.makefile(
484+
".yml",
485+
test_jinja2_transform="""
486+
---
487+
version: "1.0"
488+
infrahub_tests:
489+
- resource: "Jinja2Transform"
490+
resource_name: "bgp_config"
491+
tests:
492+
- name: "integration"
493+
expect: PASS
494+
spec:
495+
kind: "jinja2-transform-integration"
496+
variables: {}
497+
""",
498+
)
499+
pytester.makefile(
500+
".yml",
501+
infrahub_config="""
502+
---
503+
schemas: []
504+
505+
jinja2_transforms:
506+
- name: bgp_config
507+
description: "Template for BGP config base"
508+
query: "bgp_sessions"
509+
template_path: "templates/bgp_config.j2"
510+
""",
511+
)
512+
template_dir = pytester.mkdir("templates")
513+
template = pytester.makefile(
514+
".j2",
515+
bgp_config="""
516+
protocols {
517+
bgp {
518+
log-up-down;
519+
bgp-error-tolerance;
520+
}
521+
}
522+
""",
523+
)
524+
pytester.run("mv", template, template_dir)
525+
526+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
527+
result.assert_outcomes(passed=1)
528+
529+
530+
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
531+
def test_python_transform_integration(pytester: pytest.Pytester, httpx_mock: HTTPXMock) -> None:
532+
"""Integration item should run the transform using data from a mocked GraphQL response."""
533+
httpx_mock.add_response(
534+
method="POST",
535+
url="http://localhost:8000/api/query/device_config?branch=main&update_group=false&",
536+
json={"data": {"InfraDevice": {"edges": [{"node": {"name": {"value": "atl1-edge1"}}}]}}},
537+
is_reusable=True,
538+
)
539+
540+
pytester.makefile(
541+
".yml",
542+
test_python_transform="""
543+
---
544+
version: "1.0"
545+
infrahub_tests:
546+
- resource: "PythonTransform"
547+
resource_name: "device_config"
548+
tests:
549+
- name: "integration"
550+
expect: PASS
551+
spec:
552+
kind: "python-transform-integration"
553+
variables: {}
554+
""",
555+
)
556+
pytester.makefile(
557+
".yml",
558+
infrahub_config="""
559+
---
560+
schemas: []
561+
562+
python_transforms:
563+
- name: device_config
564+
class_name: "DeviceConfig"
565+
file_path: "transforms/device_config.py"
566+
""",
567+
)
568+
test_template = pytester.makefile(
569+
".py",
570+
device_config="""
571+
from infrahub_sdk.transforms import InfrahubTransform
572+
573+
class DeviceConfig(InfrahubTransform):
574+
query = "device_config"
575+
async def transform(self, data):
576+
return {"hostname": data["InfraDevice"]["edges"][0]["node"]["name"]["value"]}
577+
""",
578+
)
579+
transform_dir = pytester.mkdir("transforms")
580+
pytester.run("mv", test_template, transform_dir)
581+
582+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
583+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)