From 8c4f39ac04b99276f6f1064d3e5b45bfc0329d5b Mon Sep 17 00:00:00 2001 From: Ahmad Nawab Date: Mon, 6 May 2024 13:34:45 +0200 Subject: [PATCH] Add tests for fckit_yaml_reader --- .gitignore | 1 + cmake/fckit_install_venv.cmake | 7 +++- src/tests/CMakeLists.txt | 6 ++- src/tests/fckit_run_pytest.sh | 19 +++++++++ src/tests/test_config.yml | 12 ++++++ src/tests/test_yaml_reader.py | 72 ++++++++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 2 deletions(-) create mode 100755 src/tests/fckit_run_pytest.sh create mode 100644 src/tests/test_config.yml create mode 100644 src/tests/test_yaml_reader.py diff --git a/.gitignore b/.gitignore index 59c8784..c912c8c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build install/* CMakeLists.txt.user **/*.egg-info/ +__pycache__ diff --git a/cmake/fckit_install_venv.cmake b/cmake/fckit_install_venv.cmake index 0f13128..4364636 100644 --- a/cmake/fckit_install_venv.cmake +++ b/cmake/fckit_install_venv.cmake @@ -38,8 +38,13 @@ macro( fckit_install_venv ) # Find newly created python venv find_package( Python3 COMPONENTS Interpreter REQUIRED ) + set( _pkg_name "fckit_yaml_reader") + if( HAVE_TESTS ) + set( _pkg_name "fckit_yaml_reader/[tests]") + endif() + message( STATUS "Install fckit_yaml_reader in virtual environment ${VENV_PATH}" ) - execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install ${CMAKE_SOURCE_DIR}/fckit_yaml_reader OUTPUT_QUIET ) + execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install ${CMAKE_CURRENT_SOURCE_DIR}/src/fckit/${_pkg_name} OUTPUT_QUIET ) # install ruamel message( STATUS "Install ruamel.yaml in virtual environment ${VENV_PATH}" ) diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 0e4abbb..bd82fe9 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -147,10 +147,14 @@ endif() foreach( _test ${FCKIT_QUARANTAINED_TESTS} ) if( TEST ${_test} ) set_tests_properties( ${_test} PROPERTIES DISABLED 1 ) - ecbuild_warn("Test ${_test} is quarantained (shows as not run)") + ecbuild_warn("Test ${_test} is quarantined (shows as not run)") endif() endforeach() ### Test downstream find_package(fckit) projects add_subdirectory( test_downstream_fypp ) add_subdirectory( test_downstream_fctest ) + +### Test fckit_yaml_reader +ecbuild_add_test( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/fckit_run_pytest.sh + ARGS ${CMAKE_CURRENT_BINARY_DIR}/../.. ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/tests/fckit_run_pytest.sh b/src/tests/fckit_run_pytest.sh new file mode 100755 index 0000000..1d2eede --- /dev/null +++ b/src/tests/fckit_run_pytest.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# (C) Copyright 2021- ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation nor +# does it submit to any jurisdiction. + +source $1/venv/bin/activate + +pytest $2/test_yaml_reader.py +retval=$? + +deactivate + +exit $retval \ No newline at end of file diff --git a/src/tests/test_config.yml b/src/tests/test_config.yml new file mode 100644 index 0000000..96dab1e --- /dev/null +++ b/src/tests/test_config.yml @@ -0,0 +1,12 @@ +### Test config + +name: type config + +types: + - typeA: + - [memberA, real, 3] + - [memberB, real, 3] + - typeB: + - [memberA, real, 3] + - [memberB, int, 2] + diff --git a/src/tests/test_yaml_reader.py b/src/tests/test_yaml_reader.py new file mode 100644 index 0000000..06eea6f --- /dev/null +++ b/src/tests/test_yaml_reader.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +# (C) Copyright 2013 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation nor +# does it submit to any jurisdiction. + +from pathlib import Path +import pytest + +from fckit_yaml_reader import YAML +yaml = YAML() + + +@pytest.fixture(scope='module', name='here') +def fixture_here(): + return Path(__file__).parent + + +@pytest.fixture(scope='function', name='tmpfile') +def fixture_tmpfile(here): + + tmpfile = here / 'tmp.yml' + + yield tmpfile + + if tmpfile.exists(): + tmpfile.unlink() + + +@pytest.fixture(scope='module', name='refdict') +def fxiture_refdict(): + + return { + 'name': 'type config', + 'types': [ + {'typeA': [ + ['memberA', 'real', 3], + ['memberB', 'real', 3] + ] + }, + {'typeB': [ + ['memberA', 'real', 3], + ['memberB', 'int', 2] + ] + } + ] + } + + +def test_yaml_parse(here, refdict): + """Test parsing capability of fckit_yaml_reader.""" + + with open(here / 'test_config.yml', 'r') as stream: + _dict = yaml.safe_load(stream) + + assert _dict == refdict + + +def test_yaml_dump(here, refdict, tmpfile): + """Test writing capability of fckit_yaml_reader.""" + + with open(here / tmpfile, 'w') as stream: + yaml.dump(refdict, stream) + + with open(here / tmpfile, 'r') as stream: + _dict = yaml.safe_load(stream) + + assert _dict == refdict