From 51492a8120440a35e010004630288f9d32df82ec Mon Sep 17 00:00:00 2001 From: YooSunyoung Date: Tue, 13 Aug 2024 13:11:30 +0200 Subject: [PATCH] Add configuration synchronizing helper. --- README.md | 6 ++---- pyproject.toml | 1 + tests/invalid_config.json | 3 ++- tests/test_scicat_config.py | 27 +++++++++++++++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2378c3d..840922f 100644 --- a/README.md +++ b/README.md @@ -46,11 +46,9 @@ background_ingestor \\ You can use a json file to configure options. There is a template, ``resources/config.sample.json`` you can copy/paste to make your own configuration file. -```bash -cp resources/config.sample.json config.20240405.json -``` +In order to update the configurations, you should update it the ``scicat_configuration`` module. -Then ``scicat_ingestor`` will automatically use the configuration file. +The template file can be synchronized automatically by ``synchronize_config`` command. ### Configuration Validator diff --git a/pyproject.toml b/pyproject.toml index 7714e7f..63ac86a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ dynamic = ["version"] scicat_ingestor = "scicat_online_ingestor:main" background_ingestor = "scicat_offline_ingestor:main" validate_ingestor_config = "scicat_configuration:validate_config_file" +synchronize_config = "scicat_configuration:synchronize_config_file" [project.entry-points."scicat_ingestor.metadata_extractor"] max = "numpy:max" diff --git a/tests/invalid_config.json b/tests/invalid_config.json index 3677c46..57da192 100644 --- a/tests/invalid_config.json +++ b/tests/invalid_config.json @@ -11,7 +11,8 @@ "hash_file_extension": "b2b", "ingestor_files_directory": "../ingestor", "message_to_file": true, - "message_file_extension": "message.json" + "message_file_extension": "message.json", + "some_random_field": 0 } } } \ No newline at end of file diff --git a/tests/test_scicat_config.py b/tests/test_scicat_config.py index 9980051..0c25979 100644 --- a/tests/test_scicat_config.py +++ b/tests/test_scicat_config.py @@ -22,16 +22,27 @@ def template_config_file() -> Path: return Path(__file__).parent / "../resources/config.sample.json" -def test_config_validator(template_config_file: Path) -> None: - _validate_config_file(OnlineIngestorConfig, template_config_file) +def test_template_config_file_synchronized(template_config_file: Path) -> None: + """Template config file should have all the fields in the OnlineIngestorConfig + + If this test fails, it means that the template config file is out of sync with the + OnlineIngestorConfig class. + In that case, use the command ``synchronize_config`` to update the template file. + + ```bash + synchronize_config + ``` + """ + import json + + assert ( + json.loads(template_config_file.read_text()) + == OnlineIngestorConfig(config_file="").to_dict() + ) -def test_config_validator_invalid_config_raises() -> None: - with pytest.raises(TypeError, match="(OnlineIngestorConfig.__init__)*(missing)"): - _validate_config_file( - OnlineIngestorConfig, - Path(__file__).parent / "invalid_config.json", - ) +def test_config_validator(template_config_file: Path) -> None: + _validate_config_file(OnlineIngestorConfig, template_config_file) def test_config_validator_unused_args_raises() -> None: