diff --git a/pipeline/src/additional_methods/by_name.py.txt b/pipeline/src/additional_methods/by_name.py.txt index 13491111..1dcd7cd7 100644 --- a/pipeline/src/additional_methods/by_name.py.txt +++ b/pipeline/src/additional_methods/by_name.py.txt @@ -38,7 +38,7 @@ else: cls._instance_lookup[key] = [instance] if match == "equals": - matches = cls._instance_lookup.get(name, None) + matches = cls._instance_lookup.get(name, []) elif match == "contains": matches = [] for key, instances in cls._instance_lookup.items(): @@ -46,9 +46,9 @@ matches.extend(instances) else: raise ValueError("'match' must be either 'equals' or 'contains'") - if all: + if not matches: + return None + elif all: return matches - elif len(matches) > 0: - return matches[0] else: - return None + return matches[0] \ No newline at end of file diff --git a/pipeline/tests/test_regressions.py b/pipeline/tests/test_regressions.py index 359df163..962cba39 100644 --- a/pipeline/tests/test_regressions.py +++ b/pipeline/tests/test_regressions.py @@ -331,3 +331,17 @@ def test_issue0069(om): results = om.core.License.by_name("Creative Commons", all=True, match="contains") assert len(results) == 7 assert all("CC" in r.short_name for r in results) + +@pytest.mark.parametrize("om", [openminds.latest]) +def test_pr0083(om): + # https://github.com/openMetadataInitiative/openMINDS_Python/pull/83 + # by_name() should return None consistently + # when no matches are found, regardless of the 'all' parameter + + # all=False (default) should return None when no match is found + result = om.controlled_terms.BiologicalOrder.by_name("nonexistent_order_xyz") + assert result is None + + # all=True should also return None when no match is found + results = om.controlled_terms.BiologicalOrder.by_name("nonexistent_order_xyz", all=True) + assert results is None \ No newline at end of file