Skip to content

Commit 615a2b7

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

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/unit/pytest_plugin/test_plugin.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,61 @@ 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(
267+
".json", input='{"data": { "InfraDevice": { "edges": [ { "node": { "name": {"value": "atl1-edge1"} } } ] } } }'
268+
)
269+
test_output = pytester.makefile(".json", output='{"hostname": "atl1-edge1"}')
270+
test_template = pytester.makefile(
271+
".py",
272+
device_config="""
273+
from infrahub_sdk.transforms import InfrahubTransform
274+
275+
class DeviceConfig(InfrahubTransform):
276+
query = "device_config"
277+
async def transform(self, data):
278+
return {"hostname": data["InfraDevice"]["edges"][0]["node"]["name"]["value"]}
279+
""",
280+
)
281+
282+
pytester.mkdir("device_config")
283+
test_dir = pytester.mkdir("device_config/base_config")
284+
pytester.run("mv", test_input, test_dir)
285+
pytester.run("mv", test_output, test_dir)
286+
287+
transform_dir = pytester.mkdir("transforms")
288+
pytester.run("mv", test_template, transform_dir)
289+
290+
result = pytester.runpytest("--infrahub-repo-config=infrahub_config.yml")
291+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)