Skip to content

Commit 30bb813

Browse files
committed
add pytest plugin python-transform-unit-process test
1 parent 1018811 commit 30bb813

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/unit/pytest_plugin/test_plugin.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,60 @@ def test_jinja2_transform_unexpected_output(pytester):
231231

232232
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
233233
result.assert_outcomes(failed=1)
234+
235+
236+
def test_python_transform(pytester):
237+
pass
238+
pytester.makefile(
239+
".yml",
240+
test_python_transform="""
241+
---
242+
version: "1.0"
243+
infrahub_tests:
244+
- resource: "PythonTransform"
245+
resource_name: "device_config"
246+
tests:
247+
- name: "base_config"
248+
expect: PASS
249+
spec:
250+
kind: "python-transform-unit-process"
251+
directory: device_config/base_config
252+
""",
253+
)
254+
pytester.makefile(
255+
".yml",
256+
infrahub_config="""
257+
---
258+
schemas:
259+
- schemas/dcim.yml
260+
261+
python_transforms:
262+
- name: device_config
263+
class_name: "DeviceConfig"
264+
file_path: "transforms/device_config.py"
265+
""",
266+
)
267+
test_input = pytester.makefile(".json", input='{"data": { "InfraDevice": { "edges": [ { "node": { "name": {"value": "atl1-edge1"} } } ] } } }')
268+
test_output = pytester.makefile(".json", output='{"hostname": "atl1-edge1"}')
269+
test_template = pytester.makefile(
270+
".py",
271+
device_config="""
272+
from infrahub_sdk.transforms import InfrahubTransform
273+
274+
class DeviceConfig(InfrahubTransform):
275+
query = "device_config"
276+
async def transform(self, data):
277+
return {"hostname": data["InfraDevice"]["edges"][0]["node"]["name"]["value"]}
278+
""",
279+
)
280+
281+
pytester.mkdir("device_config")
282+
test_dir = pytester.mkdir("device_config/base_config")
283+
pytester.run("mv", test_input, test_dir)
284+
pytester.run("mv", test_output, test_dir)
285+
286+
transform_dir = pytester.mkdir("transforms")
287+
pytester.run("mv", test_template, transform_dir)
288+
289+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
290+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)