Skip to content
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

ingest: Add tests #54

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ jobs:
with:
env: |
NEXTSTRAIN_DOCKER_IMAGE: nextstrain/base
test-ingest-scripts:
name: Test scripts under ingest/bin/
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: pip install pytest
- run: pytest ingest/bin/tests/
Empty file added ingest/bin/__init__.py
Empty file.
Empty file added ingest/bin/tests/__init__.py
Empty file.
144 changes: 144 additions & 0 deletions ingest/bin/tests/test_apply_geolocation_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import pytest
from ..apply_geolocation_rules import load_geolocation_rules, get_annotated_geolocation


def write_file_with_lines(tmpdir, lines):
"""Write a file `test_file.txt` with the provided lines."""
geolocation_rules_file = tmpdir.join("test_file.txt")
with open(geolocation_rules_file, 'w') as f:
f.write('\n'.join(lines))
return geolocation_rules_file


def get_geolocation_rules(tmpdir, lines):
"""Get geolocation rules from the provided lines."""
geolocation_rules_file = write_file_with_lines(tmpdir, lines)
return load_geolocation_rules(geolocation_rules_file)


class TestLoadGeolocationRules:
"Tests for loading geolocation rules files."

def test_load_with_comment(self, tmpdir):
"""Test that a line starting with `#` is skipped."""
lines = (
"# This is a comment",
)
geolocation_rules_file = write_file_with_lines(tmpdir, lines)
geolocation_rules = load_geolocation_rules(geolocation_rules_file)
assert geolocation_rules == {}

def test_load_with_inline_comment(self, tmpdir):
"""Test that `#` and any following characters are treated as an inline comment."""
lines = (
"a/b/c/d\t1/2/3/4 # this is a comment",
"a/b/c/e\t1/2/3/# 4 this is a comment",
)
geolocation_rules_file = write_file_with_lines(tmpdir, lines)
geolocation_rules = load_geolocation_rules(geolocation_rules_file)
assert geolocation_rules["a"]["b"]["c"]["d"] == ("1", "2", "3", "4")
assert geolocation_rules["a"]["b"]["c"]["e"] == ("1", "2", "3", "")

def test_load_with_wildcard_character(self, tmpdir):
"""Test that `*` characters can be loaded."""
lines = (
"a/b/*/*\t1/2/*/*",
"a/b/c/*\t1/2/3/4",
)
geolocation_rules_file = write_file_with_lines(tmpdir, lines)
geolocation_rules = load_geolocation_rules(geolocation_rules_file)
assert geolocation_rules["a"]["b"]["*"]["*"] == ("1", "2", "*", "*")
assert geolocation_rules["a"]["b"]["c"]["*"] == ("1", "2", "3", "4")

def test_load_with_empty(self, tmpdir):
"""Test that an empty field can be loaded in the raw and/or annotated column."""
lines = (
"a/b//\ta/b/c/d",
"a/b/c/d\ta/b//",
"a/c//\ta/b//",
)
geolocation_rules_file = write_file_with_lines(tmpdir, lines)
geolocation_rules = load_geolocation_rules(geolocation_rules_file)
assert geolocation_rules["a"]["b"][""][""] == ("a", "b", "c", "d")
assert geolocation_rules["a"]["b"]["c"]["d"] == ("a", "b", "", "")
assert geolocation_rules["a"]["c"][""][""] == ("a", "b", "", "")


class TestGetAnnotatedGeolocation:
"Tests for getting an annotated geolocation."

def test_simple_rule(self, tmpdir):
"""Test that a simple rule works."""
lines = (
"a/b/c/d\t1/2/3/4",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, ("a", "b", "c", "d")) == ("1", "2", "3", "4")

def test_none_if_no_matching_rule(self, tmpdir):
"""Test that `None` is returned when there is no matching rule."""
lines = (
"a/b/c/d\t1/2/3/4",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, ("a", "a", "a", "a")) is None

@pytest.mark.parametrize(
"raw_location, matching_annotation",
[
(("a", "b", "c", "d"), ("*", "2", "3", "4")),
(("e", "f", "g", "h"), ("5", "*", "7", "8")),
(("i", "j", "k", "l"), ("9", "10", "*", "12")),
(("m", "n", "o", "p"), ("13", "14", "15", "*")),
(("q", "r", "s", "t"), ("17", "*", "*", "20")),
(("u", "v", "w", "x"), ("*", "*", "*", "24")),
]
)
def test_wildcards_work_in_all_fields(self, tmpdir, raw_location, matching_annotation):
"""Test that wildcards work in any field."""
lines = (
"*/b/c/d\t*/2/3/4",
"e/*/g/h\t5/*/7/8",
"i/j/*/l\t9/10/*/12",
"m/n/o/*\t13/14/15/*",
"q/*/*/t\t17/*/*/20",
"*/*/*/x\t*/*/*/24",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, raw_location) == matching_annotation

@pytest.mark.parametrize(
"raw_location, matching_annotation",
[
(("a", "b", "c", "x"), ("1", "2", "3", "*")),
(("a", "b", "x", "x"), ("1", "2", "*", "*")),
(("a", "x", "x", "x"), ("1", "*", "*", "*")),
]
)
def test_wildcards_precedence(self, tmpdir, raw_location, matching_annotation):
"""Test that closer wildcard matches (with respect to hierarchy) are applied before more generic wildcard matches."""
lines = (
"a/b/c/*\t1/2/3/*",
"a/b/*/*\t1/2/*/*",
"a/*/*/*\t1/*/*/*",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, raw_location) == matching_annotation

def test_exact_match_over_wildcard(self, tmpdir):
"""Test that an exact match rules is used over a wildcard match."""
lines = (
"a/b/c/d\t1/2/3/4",
"a/*/*/*\t1/*/*/*",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, ("a", "b", "c", "d")) == ("1", "2", "3", "4")

def test_wildcards_work_if_partial_match_exists(self, tmpdir):
"""Test wildcards still work even when partial matches exist."""
lines = (
"a/b/c/d\t1/2/3/4",
"a/*/*/z\t1/*/*/26",
)
geolocation_rules = get_geolocation_rules(tmpdir, lines)
assert get_annotated_geolocation(geolocation_rules, ("a", "b", "c", "z")) == ("1", "*", "*", "26")
Comment on lines +137 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails on master, but passes if rebased onto #55

2 changes: 1 addition & 1 deletion ingest/workflow/snakemake_rules/transform.smk
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ rule transform:
--authors-field {params.authors_field} \
--default-value {params.authors_default_value} \
--abbr-authors-field {params.abbr_authors_field} \
| ./bin/apply-geolocation-rules \
| ./bin/apply_geolocation_rules.py \
--geolocation-rules {input.all_geolocation_rules} \
| ./bin/merge-user-metadata \
--annotations {params.annotations} \
Expand Down