From 9341e47cb9640048a1b98a15cf9054623baa21e8 Mon Sep 17 00:00:00 2001 From: mrjk Date: Sun, 19 Jun 2022 23:21:01 -0400 Subject: [PATCH] Add: Tests WIP --- conftest.py | 17 ++++++++++ run_jupyter.sh | 4 +++ run_tests.sh | 6 ++++ tests/test_app.py | 27 +++++++++++++++ tests/test_lib.py | 68 ++++++++++++++++++++++++++++++++++++++ tests/test_plugin_scope.py | 34 +++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 conftest.py create mode 100755 run_jupyter.sh create mode 100755 run_tests.sh create mode 100644 tests/test_app.py create mode 100644 tests/test_lib.py create mode 100644 tests/test_plugin_scope.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..7484ef9 --- /dev/null +++ b/conftest.py @@ -0,0 +1,17 @@ +import pytest + +from collections import namedtuple + + +TestCase = namedtuple("TestCase", ["args", "kwargs", "expected"]) + + +@pytest.fixture( + params = [ + TestCase('Simple string', ['bli-blah-bloh !?'], None), + TestCase('fqdn', ['this.is.my.domain'], None), + ] + ) +def test_case(request): + return request.param + diff --git a/run_jupyter.sh b/run_jupyter.sh new file mode 100755 index 0000000..51523e4 --- /dev/null +++ b/run_jupyter.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +export KHEOPS_EXAMPLES_DIR=$PWD +jupyter notebook --notebook-dir=docs/jupyter/ diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..b09f827 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,6 @@ + + +# Coverage: +pytest --cov=kheops tests/ + +pytest diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..1342a39 --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,27 @@ + +import kheops.app as Kheops + + + +def test_app_config(): + + # Testing missing namespace + for namespace in ['missing', '_weird_key' ]: + try: + config= 'examples/kheops.yml' + Kheops.Kheops(config=config, namespace=namespace) + assert False + except Exception: + assert True + + # Testing unexisting config files + namespace = 'default' + for config in ['toto.yaml', 'toto.noext']: + try: + Kheops.Kheops(config=config, namespace=namespace) + assert False + except Exception: + assert True + + + diff --git a/tests/test_lib.py b/tests/test_lib.py new file mode 100644 index 0000000..b34c878 --- /dev/null +++ b/tests/test_lib.py @@ -0,0 +1,68 @@ +import pytest + +from kheops.utils import * + + +@pytest.mark.parametrize( + "result,args,kwargs", + [ + ( + ['this', 'this/is', 'this/is/my', 'this/is/my/hierarchy'], + ['this/is/my/hierarchy'], + { + 'reverse': False, + 'sep': '/', + 'start_index': 0, + }, + ), + ( + ['this/is/my', 'this/is/my/hierarchy'], + ['this/is/my/hierarchy'], + { + 'reverse': False, + 'sep': '/', + 'start_index': 3, + }, + ), + ( + ['hierarchy', 'hierarchy/my', 'hierarchy/my/is', 'hierarchy/my/is/this'], + ['this/is/my/hierarchy'], + { + 'reverse': True, + 'sep': '/', + 'start_index': 0, + }, + ), + ( + ['this', 'this/is', 'this/is/my', 'this/is/my/server', 'this/is/my/server/domaine'], + ['this.is.my.server.domaine'], + { + 'reverse': False, + 'sep': '.', + 'start_index': 0, + }, + ), + ] + ) + +def test_path_assemble_hier2(result, args, kwargs): + + res = path_assemble_hier(*args, **kwargs) + assert result == res + + +@pytest.mark.parametrize( + "result,args", + [ + ( + 'hello super world', + ['hello {{ world }}', {'world': 'super world'}] + ) + ] + ) +def test_render_template(result, args): + + res = render_template(*args) + assert res == result + + diff --git a/tests/test_plugin_scope.py b/tests/test_plugin_scope.py new file mode 100644 index 0000000..5391105 --- /dev/null +++ b/tests/test_plugin_scope.py @@ -0,0 +1,34 @@ +import pytest + +from kheops.plugin.scope.loop import Plugin + + +lookups = [ + { + '_run':{ 'scope': { 'my': 'scope'}},}, + ] + +conf = { + 'var': 'item_loop', + 'data': { + "my": 'data', + }, + } + + +@pytest.mark.parametrize( + "" + [ + test_case("Validate simple loop", + [ + lookups, conf, + ], + None, + ), + ]) +def test_plugin(): + + plugin = Plugin() + ret = plugin.process_items() + +