From ec4419287c136fcf800d41fb41a4173093f27793 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 2 Dec 2024 20:24:01 +0100 Subject: [PATCH] fix: update tests --- src/rougail/output_json/__init__.py | 65 ++++++++++++------- .../01_8calculation_information_multi.json | 4 +- .../01_8calculation_information_multi.json | 9 ++- .../01_8calculation_information_multi.json | 4 +- .../01_8calculation_information_multi.json | 5 +- tests/test_load.py | 12 ++-- 6 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/rougail/output_json/__init__.py b/src/rougail/output_json/__init__.py index 69ac945..e361851 100644 --- a/src/rougail/output_json/__init__.py +++ b/src/rougail/output_json/__init__.py @@ -22,30 +22,63 @@ from json import dumps from tiramisu import undefined from tiramisu.error import PropertiesOptionError, ConfigError +from rougail.error import ExtentionError from .i18n import _ class RougailOutputJson: + output_name = 'json' + def __init__( self, config: "Config", + *, rougailconfig: "RougailConfig" = None, user_data_errors: Optional[list] = None, user_data_warnings: Optional[list] = None, ) -> None: if rougailconfig is None: from rougail import RougailConfig - rougailconfig = RougailConfig + rougailconfig["step.output"] = self.output_name + if rougailconfig["step.output"] != self.output_name: + raise ExtentionError(_('the "step.output" is not set to "{0}"').format(self.output_name)) self.rougailconfig = rougailconfig self.config = config - self.errors = [] - self.warnings = [] + if user_data_errors: + self.errors = user_data_errors + else: + self.errors = [] + if user_data_warnings: + self.warnings = user_data_warnings + else: + self.warnings = [] self.read_write = self.rougailconfig["json.read_write"] self.is_mandatory = self.rougailconfig["json.mandatory"] self.dico = {} + def run(self) -> None: + self.exporter() + return dumps(self.dico, ensure_ascii=False, indent=2) + '\n' + + def print(self) -> str: + print(self.run()) + + def exporter(self) -> None: + self.config.property.read_write() + self.mandatory() + self.manage_warnings() + self.config.property.read_only() + if self.manage_errors(): + return + if self.read_write: + self.config.property.read_write() + self.parse_family( + self.config, + self.dico, + ) + def mandatory(self): if not self.is_mandatory: return @@ -79,29 +112,15 @@ class RougailOutputJson: ) self.errors.append(f" - {option.description()}") - def exporter(self) -> None: - self.config.property.read_write() - self.mandatory() - if self.errors: - self.dico = {"_errors": self.errors} - return + def manage_warnings(self) -> None: if self.warnings: self.dico["_warnings"] = self.warnings - if self.read_write: - self.config.property.read_write() - else: - self.config.property.read_only() - self.parse_family( - self.config, - self.dico, - ) - def run(self) -> None: - self.exporter() - return dumps(self.dico, ensure_ascii=False, indent=2) + '\n' - - def print(self) -> str: - print(self.run()) + def manage_errors(self) -> bool: + if not self.errors: + return False + self.dico = {"_errors": self.errors} + return True def parse_family( self, diff --git a/tests/results/test_namespace_read_write/01_8calculation_information_multi.json b/tests/results/test_namespace_read_write/01_8calculation_information_multi.json index 68ee785..2942cca 100644 --- a/tests/results/test_namespace_read_write/01_8calculation_information_multi.json +++ b/tests/results/test_namespace_read_write/01_8calculation_information_multi.json @@ -1,7 +1,5 @@ { "rougail": { - "variable": [ - "[]" - ] + "variable": [] } } diff --git a/tests/results/test_namespace_read_write_mandatory_errors/01_8calculation_information_multi.json b/tests/results/test_namespace_read_write_mandatory_errors/01_8calculation_information_multi.json index 68ee785..3f6c63f 100644 --- a/tests/results/test_namespace_read_write_mandatory_errors/01_8calculation_information_multi.json +++ b/tests/results/test_namespace_read_write_mandatory_errors/01_8calculation_information_multi.json @@ -1,7 +1,6 @@ { - "rougail": { - "variable": [ - "[]" - ] - } + "_errors": [ + "The following variables are mandatory but have no value:", + " - rougail.variable (a variable)" + ] } diff --git a/tests/results/test_read_write/01_8calculation_information_multi.json b/tests/results/test_read_write/01_8calculation_information_multi.json index 53b9375..8fc7add 100644 --- a/tests/results/test_read_write/01_8calculation_information_multi.json +++ b/tests/results/test_read_write/01_8calculation_information_multi.json @@ -1,5 +1,3 @@ { - "variable": [ - "[]" - ] + "variable": [] } diff --git a/tests/results/test_read_write_mandatory_errors/01_8calculation_information_multi.json b/tests/results/test_read_write_mandatory_errors/01_8calculation_information_multi.json index 53b9375..f51dcb9 100644 --- a/tests/results/test_read_write_mandatory_errors/01_8calculation_information_multi.json +++ b/tests/results/test_read_write_mandatory_errors/01_8calculation_information_multi.json @@ -1,5 +1,6 @@ { - "variable": [ - "[]" + "_errors": [ + "The following variables are mandatory but have no value:", + " - variable (a variable)" ] } diff --git a/tests/test_load.py b/tests/test_load.py index 6441adf..c4df791 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -10,13 +10,13 @@ EXT = 'json' excludes = [] -#excludes = set([ +#excludes = [ # '60_5family_dynamic_unknown_suffix', # '60_5family_dynamic_variable_outside_sub_suffix', -#]) +#] test_ok = get_structures_list(excludes) -# test_ok = ['60_0family_dynamic_variable_suffix'] +#test_ok = [Path('../rougail-tests/structures/00_0version_underscore')] def idfn(fixture_value): @@ -42,18 +42,22 @@ def _test_dictionaries(test_dir, namespace, ext, *, read_write=True, mandatory=F dir_name += '_namespace' elif (test_dir / 'force_namespace').is_file(): return + ################################## if read_write: dir_name += '_read_write' if mandatory: dir_name += '_mandatory' if not do_calc: dir_name += '_errors' + ################################## rougail = Rougail(rougailconfig) config = rougail.run() + ################################## if do_calc and (mandatory or not read_write): get_values_for_config(config) + ################################## generated_output = RougailOutput(config, rougailconfig=rougailconfig).run() - output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + EXT) + output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + ext) if not output_file.is_file(): if not output_file.parent.is_dir(): output_file.parent.mkdir()