Coverage for tests / tests_io / test_configuration.py: 100%
54 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
1# SPDX-FileCopyrightText: Copyright INRIA
2#
3# SPDX-License-Identifier: LGPL-3.0-only
4#
5# Copyright INRIA
6#
7# This file is part of PhysioBlocks, a library mostly developed by the
8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA.
9#
10# Authors:
11# - Colin Drieu
12# - Dominique Chapelle
13# - François Kimmig
14# - Philippe Moireau
15#
16# PhysioBlocks is free software: you can redistribute it and/or modify it under the
17# terms of the GNU Lesser General Public License as published by the Free Software
18# Foundation, version 3 of the License.
19#
20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY
21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
23#
24# You should have received a copy of the GNU Lesser General Public License along with
25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>.
27import json
28from dataclasses import dataclass
29from pathlib import Path
31import pytest
33from physioblocks.configuration.base import Configuration
34from physioblocks.io.configuration import read_json, write_json
35from tests.helpers.file_helpers import clean_files
37write_file_path = "./tests/tests_io/references/test_write.json"
38ref_file_path = "./tests/tests_io/references/configuration.json"
39alternative_config_file_path = "./tests/tests_io/references/alt_config_file_path.json"
40commented_config_file_path = (
41 "./tests/tests_io/references/commented_config_file_path.jsonc"
42)
44ROOT_ID = "root_item"
45FLOAT_VAL_ID = "float_value"
46INT_VAL_ID = "int_value"
47STR_VAL_ID = "str_value"
48CHILD_ID = "child"
49CHILD_ITEM_ID = "child_item"
52@dataclass
53class NonSerializable:
54 val_1: float
55 val_2: int
58@pytest.fixture
59def ref_config():
60 root = Configuration(ROOT_ID)
61 root.configuration_items[FLOAT_VAL_ID] = 1.0
62 root.configuration_items[INT_VAL_ID] = 1
63 root.configuration_items[STR_VAL_ID] = "one"
65 child = Configuration(CHILD_ITEM_ID)
66 child.configuration_items[FLOAT_VAL_ID] = 2.0
67 child.configuration_items[INT_VAL_ID] = 2
68 child.configuration_items[STR_VAL_ID] = "two"
70 root[CHILD_ID] = child
72 return root
75@pytest.fixture
76def ref_alt_config(ref_config: Configuration) -> dict:
77 return ref_config.configuration_items.copy()
80def test_read_json(ref_config: Configuration, ref_alt_config: Configuration):
81 config = read_json(ref_file_path)
82 assert config == ref_config
84 commented_config = read_json(commented_config_file_path)
85 assert commented_config == ref_config
87 alt_config = read_json(alternative_config_file_path)
88 assert alt_config == ref_alt_config
91@clean_files(write_file_path)
92def test_write_json(ref_config: Configuration):
93 write_json(write_file_path, ref_config)
95 write_json_txt = Path(write_file_path).read_text()
96 write_json_obj = json.loads(write_json_txt)
98 ref_json_txt = Path(ref_file_path).read_text()
99 ref_json_obj = json.loads(ref_json_txt)
101 assert write_json_obj == ref_json_obj
103 obj = NonSerializable(1.0, 2)
104 with pytest.raises(TypeError):
105 write_json(write_file_path, obj)