-
Notifications
You must be signed in to change notification settings - Fork 6
Hoist by_name() matching helpers out of template into importable code
#106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apdavison
wants to merge
1
commit into
openMetadataInitiative:pipeline
Choose a base branch
from
apdavison:by-name-helpers
base: pipeline
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−53
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """ | ||
| Tests for the name-matching helpers in `openminds.base`, | ||
| and for the parts of the `by_name()` contract that depend on them. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| import openminds.latest | ||
| import openminds.v4 | ||
| from openminds.base import ( | ||
| MATCH_TYPES, | ||
| NAMELIKE_PROPERTIES, | ||
| matches_name, | ||
| normalize_name, | ||
| remove_accents, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text,expected", | ||
| [ | ||
| ("Müller", "Muller"), | ||
| ("République française", "Republique francaise"), | ||
| ("Azərbaycan Respublikası", "Azerbaycan Respublikasi"), | ||
| ("Straße", "Strasse"), | ||
| ("Œuvre", "OEuvre"), | ||
| ("Ångström", "Angstrom"), | ||
| ("plain text", "plain text"), | ||
| ], | ||
| ) | ||
| def test_remove_accents(text, expected): | ||
| assert remove_accents(text) == expected | ||
|
|
||
|
|
||
| def test_normalize_name(): | ||
| assert normalize_name("Raphé") == "Raphé" | ||
| assert normalize_name("Raphé", case_sensitive=False) == "raphé" | ||
| assert normalize_name("Raphé", ignore_accents=True) == "Raphe" | ||
| assert normalize_name("Raphé", case_sensitive=False, ignore_accents=True) == "raphe" | ||
| assert normalize_name("CLARITY/TDE") == "CLARITY/TDE" | ||
| assert normalize_name("CLARITY/TDE", ignore_separators=True) == "CLARITY TDE" | ||
| assert normalize_name("two-photon imaging", ignore_separators=True) == "two photon imaging" | ||
|
|
||
|
|
||
| class TestMatchesName: | ||
| def test_equals_is_case_sensitive_by_default(self): | ||
| assert matches_name("Mus musculus", "Mus musculus") | ||
| assert not matches_name("Mus musculus", "mus musculus") | ||
| assert matches_name("Mus musculus", "mus musculus", case_sensitive=False) | ||
|
|
||
| def test_accents(self): | ||
| assert not matches_name("République française", "Republique francaise") | ||
| assert matches_name("République française", "Republique francaise", ignore_accents=True) | ||
| assert matches_name("Republique francaise", "République française", ignore_accents=True) | ||
|
|
||
| def test_contains(self): | ||
| assert matches_name("Mus musculus", "musculus", match="contains") | ||
| assert not matches_name("musculus", "Mus musculus", match="contains") | ||
|
|
||
| def test_within(self): | ||
| assert matches_name("Mus musculus", "Mus musculus - House mouse", match="within") | ||
| assert not matches_name("Mus musculus - House mouse", "Mus musculus", match="within") | ||
|
|
||
| def test_separators(self): | ||
| assert not matches_name("CLARITY/TDE", "CLARITY-TDE") | ||
| assert matches_name("CLARITY/TDE", "CLARITY-TDE", ignore_separators=True) | ||
| assert matches_name("CLARITY/TDE", "CLARITY TDE", ignore_separators=True) | ||
| assert matches_name("two-photon fluorescence microscopy", "two photon fluorescence microscopy", | ||
| ignore_separators=True) | ||
|
|
||
| def test_invalid_match(self): | ||
| with pytest.raises(ValueError): | ||
| matches_name("a", "a", match="approximately") | ||
|
|
||
| def test_match_types(self): | ||
| assert MATCH_TYPES == ("equals", "contains", "within") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("om", [openminds.latest]) | ||
| class TestByNameUsesMatchesName: | ||
| """`by_name()` must apply exactly the rules `matches_name()` describes.""" | ||
|
|
||
| @pytest.mark.parametrize("match", MATCH_TYPES) | ||
| @pytest.mark.parametrize("case_sensitive", [True, False]) | ||
| @pytest.mark.parametrize("ignore_accents", [True, False]) | ||
| @pytest.mark.parametrize("ignore_separators", [True, False]) | ||
| def test_agreement(self, om, match, case_sensitive, ignore_accents, ignore_separators): | ||
| SovereignState = om.controlled_terms.SovereignState | ||
| query = "Republique francaise" | ||
| found = SovereignState.by_name( | ||
| query, | ||
| match=match, | ||
| all=True, | ||
| case_sensitive=case_sensitive, | ||
| ignore_accents=ignore_accents, | ||
| ignore_separators=ignore_separators, | ||
| ) | ||
| for state in found or []: | ||
| names = [getattr(state, prop_name, None) for prop_name in NAMELIKE_PROPERTIES] | ||
| names += list(state.synonyms or []) if hasattr(state, "synonyms") else [] | ||
| assert any( | ||
| matches_name(name, query, match, case_sensitive, ignore_accents, ignore_separators) | ||
| for name in names | ||
| if name is not None | ||
| ) | ||
|
|
||
| def test_invalid_match_is_rejected(self, om): | ||
| with pytest.raises(ValueError): | ||
| om.controlled_terms.Species.by_name("Mus musculus", match="approximately") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_agreementasserts nothing whenfoundisNone: should it also check that no instance's name-like properties/synonyms satisfymatches_name()for the query in that case?