Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Busch committed Oct 22, 2024
1 parent d048020 commit 709b2ce
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 deletions.
32 changes: 29 additions & 3 deletions climate_categories/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import pyparsing
import strictyaml as sy

# import climate_categories

if TYPE_CHECKING:
from ._categories import Categorization, Category, HierarchicalCategory

Expand Down Expand Up @@ -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."""
Expand Down
35 changes: 35 additions & 0 deletions climate_categories/tests/data/simple_categorisation_a.yaml
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions climate_categories/tests/data/simple_categorisation_b.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions climate_categories/tests/data/simple_conversion.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# references: test
# last_update: 2024-10-14
A,B,comment
1,1, no comment
2+3,2
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
38 changes: 35 additions & 3 deletions climate_categories/tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 709b2ce

Please sign in to comment.