Skip to content

Commit

Permalink
Add tests for fckit_yaml_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
awnawab committed May 6, 2024
1 parent 89dc4cb commit 8c4f39a
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build
install/*
CMakeLists.txt.user
**/*.egg-info/
__pycache__

7 changes: 6 additions & 1 deletion cmake/fckit_install_venv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}" )
Expand Down
6 changes: 5 additions & 1 deletion src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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} )
19 changes: 19 additions & 0 deletions src/tests/fckit_run_pytest.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/tests/test_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Test config

name: type config

types:
- typeA:
- [memberA, real, 3]
- [memberB, real, 3]
- typeB:
- [memberA, real, 3]
- [memberB, int, 2]

72 changes: 72 additions & 0 deletions src/tests/test_yaml_reader.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c4f39a

Please sign in to comment.