From 709b2cedfa7e2210e02daf0b08acb1593f635a12 Mon Sep 17 00:00:00 2001 From: Daniel Busch Date: Tue, 22 Oct 2024 15:16:48 +0200 Subject: [PATCH] more tests --- climate_categories/_conversions.py | 32 ++++++++++++++-- .../tests/data/simple_categorisation_a.yaml | 35 +++++++++++++++++ .../tests/data/simple_categorisation_b.yaml | 27 +++++++++++++ .../tests/data/simple_conversion.csv | 5 +++ ...nversion_with_existing_categorizations.csv | 6 +++ ...with_existing_categorizations_aux_dims.csv | 6 +++ climate_categories/tests/test_conversions.py | 38 +++++++++++++++++-- 7 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 climate_categories/tests/data/simple_categorisation_a.yaml create mode 100644 climate_categories/tests/data/simple_categorisation_b.yaml create mode 100644 climate_categories/tests/data/simple_conversion.csv create mode 100644 climate_categories/tests/data/test_conversion_with_existing_categorizations.csv create mode 100644 climate_categories/tests/data/test_conversion_with_existing_categorizations_aux_dims.csv diff --git a/climate_categories/_conversions.py b/climate_categories/_conversions.py index de63d3d1..983711d7 100644 --- a/climate_categories/_conversions.py +++ b/climate_categories/_conversions.py @@ -11,6 +11,8 @@ import pyparsing import strictyaml as sy +# import climate_categories + if TYPE_CHECKING: from ._categories import Categorization, Category, HierarchicalCategory @@ -899,9 +901,33 @@ def __init__( self.rules = rules self.auxiliary_categorizations = auxiliary_categorizations - @classmethod - def from_csv(cls, filepath): - return ConversionSpec.from_csv(filepath) + @staticmethod + def from_csv( + filepath: typing.Union[str, pathlib.Path, typing.TextIO], + # cats : typing.Union[dict[str, Categorization], None] = None, + cats = None, + ): + + conv = ConversionSpec.from_csv(filepath) + def get_cats(cat_names): + import climate_categories + + return { + cat_name: climate_categories.cats[cat_name] for cat_name in cat_names + } + + if not cats: + cat_names = [ + conv.categorization_a_name, + conv.categorization_b_name, + ] + + if conv.auxiliary_categorizations_names: + cat_names = cat_names + conv.auxiliary_categorizations_names + + cats = get_cats(cat_names) + + return conv.hydrate(cats=cats) def reversed(self) -> "Conversion": """Returns the Conversion with categorization_a and categorization_b swapped.""" diff --git a/climate_categories/tests/data/simple_categorisation_a.yaml b/climate_categories/tests/data/simple_categorisation_a.yaml new file mode 100644 index 00000000..1656c97b --- /dev/null +++ b/climate_categories/tests/data/simple_categorisation_a.yaml @@ -0,0 +1,35 @@ +name: A +title: Simple Categorization +comment: A simple example categorization without relationships between categories +references: doi:00000/00000 +institution: PIK +last_update: 2021-02-23 +hierarchical: no +version: 1 +categories: + 1: + title: Category 1 + comment: The first category + alternative_codes: + - A + - CatA + info: + important_data: + - A + - B + - C + other_important_thing: ABC + 2: + title: Category 2 + comment: The second category + alternative_codes: + - B + - CatB + 3: + title: Category 3 + comment: The third category + alternative_codes: + - C + - CatC + unnumbered: + title: The unnumbered category \ No newline at end of file diff --git a/climate_categories/tests/data/simple_categorisation_b.yaml b/climate_categories/tests/data/simple_categorisation_b.yaml new file mode 100644 index 00000000..35751f9b --- /dev/null +++ b/climate_categories/tests/data/simple_categorisation_b.yaml @@ -0,0 +1,27 @@ +name: B +title: Simple Categorization +comment: A simple example categorization without relationships between categories +references: doi:00000/00000 +institution: PIK +last_update: 2021-02-23 +hierarchical: no +version: 1 +categories: + 1: + title: Category 1 + comment: The first category + alternative_codes: + - A + - CatA + info: + important_data: + - A + - B + - C + other_important_thing: ABC + 2: + title: Category 2 + comment: The second category + alternative_codes: + - B + - CatB \ No newline at end of file diff --git a/climate_categories/tests/data/simple_conversion.csv b/climate_categories/tests/data/simple_conversion.csv new file mode 100644 index 00000000..724f62d9 --- /dev/null +++ b/climate_categories/tests/data/simple_conversion.csv @@ -0,0 +1,5 @@ +# references: test +# last_update: 2024-10-14 +A,B,comment +1,1, no comment +2+3,2 diff --git a/climate_categories/tests/data/test_conversion_with_existing_categorizations.csv b/climate_categories/tests/data/test_conversion_with_existing_categorizations.csv new file mode 100644 index 00000000..3df5c6d8 --- /dev/null +++ b/climate_categories/tests/data/test_conversion_with_existing_categorizations.csv @@ -0,0 +1,6 @@ +# references: a reference to a www.website.com +# institution: organisation +# last_update: 2021-07-09 +BURDI,IPCC2006_PRIMAP,comment +14638,1, +2,2,a comment diff --git a/climate_categories/tests/data/test_conversion_with_existing_categorizations_aux_dims.csv b/climate_categories/tests/data/test_conversion_with_existing_categorizations_aux_dims.csv new file mode 100644 index 00000000..33dc1873 --- /dev/null +++ b/climate_categories/tests/data/test_conversion_with_existing_categorizations_aux_dims.csv @@ -0,0 +1,6 @@ +# references: a reference to a www.website.com +# institution: organisation +# last_update: 2021-07-09 +BURDI,gas,IPCC2006_PRIMAP,comment +14638,,1, +2,,2,a comment diff --git a/climate_categories/tests/test_conversions.py b/climate_categories/tests/test_conversions.py index 9ee59c2e..78d19d20 100644 --- a/climate_categories/tests/test_conversions.py +++ b/climate_categories/tests/test_conversions.py @@ -429,9 +429,41 @@ def test_relevant_rules(): def test_read_csv_in_conversion_class(): + # generate a conversion from method in Conversion object + conv_from_conversion = conversions.Conversion.from_csv("../data/conversion.IPCC1996.IPCC2006.csv") - conv_from_conversion = conversions.Conversion.from_csv("data/good_conversion.csv") - - conv_from_conversion_spec = conversions.ConversionSpec.from_csv("data/good_conversion.csv") + # generate a conversion from method in ConversionSpec object + conv_from_conversion_spec = conversions.ConversionSpec.from_csv("../data/conversion.IPCC1996.IPCC2006.csv") + conv_from_conversion_spec = conv_from_conversion_spec.hydrate(cats=climate_categories.cats) assert vars(conv_from_conversion_spec) == vars(conv_from_conversion) + + +def test_read_conversion_from_csv_with_custom_categorizations(): + categorisation_a = climate_categories.from_yaml("data/simple_categorisation_a.yaml") + + # make categorisation B from yaml + categorisation_b = climate_categories.from_yaml("data/simple_categorisation_b.yaml") + + cats = {"A" : categorisation_a, "B" : categorisation_b} + + # make conversion from csv + conv = climate_categories.Conversion.from_csv("data/simple_conversion.csv", cats=cats) + + assert conv.categorization_a_name == "A" + assert conv.categorization_b_name == "B" + +def test_read_conversion_from_csv_with_existing_categorizations(): + + # make conversion from csv + conv = climate_categories.Conversion.from_csv("data/test_conversion_with_existing_categorizations.csv") + + assert conv.categorization_a_name == "BURDI" + assert conv.categorization_b_name == "IPCC2006_PRIMAP" + +def test_read_conversion_from_csv_with_existing_categorizations_aux_dims(): + + conv = climate_categories.Conversion.from_csv("data/test_conversion_with_existing_categorizations_aux_dims.csv") + + assert conv.categorization_a_name == "BURDI" + assert conv.categorization_b_name == "IPCC2006_PRIMAP" \ No newline at end of file