Skip to content

Commit 33d04a3

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

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/unit/pytest_plugin/test_plugin.py

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

0 commit comments

Comments
 (0)