Skip to content

Commit ad9e05b

Browse files
committed
Add is_resource to ContextField
1 parent 380344d commit ad9e05b

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

src/flowmapper/context.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
"unknown",
99
"unspecified",
1010
}
11+
RESOURCE_CATEGORY = {
12+
"natural resources",
13+
"natural resource",
14+
"resources",
15+
"resource",
16+
"land use",
17+
"economic",
18+
"social",
19+
"raw materials",
20+
"raw",
21+
}
1122

1223

1324
class ContextField:
@@ -34,6 +45,13 @@ def normalize(self, obj: Any | None = None, mapping: dict | None = None) -> Self
3445

3546
return type(self)(value=tuple(intermediate))
3647

48+
def is_resource(self) -> bool:
49+
if isinstance(self.value, str):
50+
return any(cat in self.value.lower() for cat in RESOURCE_CATEGORY)
51+
else:
52+
lowered = [elem.lower() for elem in self.value]
53+
return any(cat in lowered for cat in RESOURCE_CATEGORY)
54+
3755
def as_tuple(self) -> tuple | str:
3856
if isinstance(self.value, str):
3957
return self.value

tests/unit/test_context.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,89 @@ def test_normalize_with_mapping_parameter(self):
555555
"a",
556556
"b",
557557
), f"Expected normalized.value to be ('a', 'b'), but got {normalized.value!r}"
558+
559+
560+
class TestContextFieldIsResource:
561+
"""Test ContextField is_resource method."""
562+
563+
@pytest.mark.parametrize(
564+
"value,expected",
565+
[
566+
# String values that should return True (resource categories)
567+
("resource", True),
568+
("resources", True),
569+
("natural resource", True),
570+
("natural resources", True),
571+
("land use", True),
572+
("economic", True),
573+
("social", True),
574+
("raw materials", True),
575+
("raw", True),
576+
# Case insensitivity
577+
("RESOURCE", True),
578+
("Natural Resource", True),
579+
# Substring matches
580+
("water resource extraction", True),
581+
("natural resource extraction", True),
582+
("economic activity", True),
583+
("social aspect", True),
584+
# Slash-separated strings with resource
585+
("resource/air", True),
586+
# String values that should return False
587+
("emission", False),
588+
("air", False),
589+
("water", False),
590+
("", False),
591+
("emission/air", False),
592+
],
593+
)
594+
def test_is_resource_with_string(self, value, expected):
595+
"""Test is_resource with string values."""
596+
c = ContextField(value)
597+
assert (
598+
c.is_resource() is expected
599+
), f"Expected is_resource() to be {expected} for {value!r}, but got {c.is_resource()}"
600+
601+
@pytest.mark.parametrize(
602+
"value,expected",
603+
[
604+
# List values that should return True
605+
(["resource"], True),
606+
(["resources"], True),
607+
(["raw"], True),
608+
(["land use"], True),
609+
(["economic"], True),
610+
(["social"], True),
611+
(["raw materials"], True),
612+
(["RESOURCE"], True), # Case insensitive
613+
(["emission", "resource", "air"], True), # Multiple elements, one resource
614+
# List values that should return False
615+
(["emission", "air", "water"], False),
616+
([], False),
617+
],
618+
)
619+
def test_is_resource_with_list(self, value, expected):
620+
"""Test is_resource with list values."""
621+
c = ContextField(value)
622+
assert (
623+
c.is_resource() is expected
624+
), f"Expected is_resource() to be {expected} for {value!r}, but got {c.is_resource()}"
625+
626+
@pytest.mark.parametrize(
627+
"value,expected",
628+
[
629+
# Tuple values that should return True
630+
(("resource",), True),
631+
(("raw",), True),
632+
(("emission", "resource", "air"), True), # Multiple elements, one resource
633+
# Tuple values that should return False
634+
(("emission", "air"), False),
635+
((), False),
636+
],
637+
)
638+
def test_is_resource_with_tuple(self, value, expected):
639+
"""Test is_resource with tuple values."""
640+
c = ContextField(value)
641+
assert (
642+
c.is_resource() is expected
643+
), f"Expected is_resource() to be {expected} for {value!r}, but got {c.is_resource()}"

0 commit comments

Comments
 (0)