Skip to content

Commit

Permalink
Add date offset tests
Browse files Browse the repository at this point in the history
- Add several date offset tests in test_filter.py.
- Add a new file test_dates.py which patches datetime.date.today() to a fixed date for reproducible date offset testing.
    - inspirations: https://stackoverflow.com/a/20503374 and https://stackoverflow.com/a/4482067
  • Loading branch information
victorlin committed Mar 29, 2022
1 parent e303c25 commit 428fdd6
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import datetime
import pytest

# used for reproducible date offset testing
FIXED_DATE = datetime.date(2020, 3, 25)

@pytest.fixture
def patch_datetime_today(monkeypatch):
class FixedDate(datetime.date):
@classmethod
def today(cls):
return FIXED_DATE
monkeypatch.setattr(datetime, 'date', FixedDate)


def test_patch_datetime(patch_datetime_today):
assert datetime.date.today() == FIXED_DATE
144 changes: 144 additions & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import augur.filter
from augur.utils import read_metadata

from test_dates import patch_datetime_today

@pytest.fixture
def argparser():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -265,3 +267,145 @@ def test_filter_date_formats(self, tmpdir, fasta_fn, argparser):
augur.filter.run(args)
output = SeqIO.to_dict(SeqIO.parse(out_fn, "fasta"))
assert list(output.keys()) == ["SEQ_1", "SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_1m(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset 1M works"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "1M"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_p1m(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset P1M works"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "P1M"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_2y(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset 2Y works"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "2Y"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2017-03-25"),
("SEQ_2","2018-03-25"),
("SEQ_3","2019-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_4w(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset 4W works"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "4W"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-02-25"),
("SEQ_2","2020-02-26"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_1y2w5d(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset 1Y2W5D works"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "1Y2W5D"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2019-03-05"),
("SEQ_2","2019-03-06"),
("SEQ_3","2019-03-07")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_2", "SEQ_3"]

def test_filter_min_date_offset_ignore(self, tmpdir, argparser, patch_datetime_today):
"""Test that --min-date-offset gets ignored when min_date is present"""
out_fn = str(tmpdir / "filtered.txt")
min_date_offset = "1M"
min_date = "2020"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --min-date-offset {min_date_offset} --min-date {min_date}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_1", "SEQ_2", "SEQ_3"]

def test_filter_max_date_offset_1m(self, tmpdir, argparser, patch_datetime_today):
"""Test that --max-date-offset 1M works"""
out_fn = str(tmpdir / "filtered.txt")
max_date_offset = "1M"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --max-date-offset {max_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_1", "SEQ_2"]

def test_filter_max_date_offset_p1m(self, tmpdir, argparser, patch_datetime_today):
"""Test that --max-date-offset P1M works"""
out_fn = str(tmpdir / "filtered.txt")
max_date_offset = "P1M"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --max-date-offset {max_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_1", "SEQ_2"]

def test_filter_max_date_offset_ignore(self, tmpdir, argparser, patch_datetime_today):
"""Test that --max-date-offset gets ignored when max_date is present"""
out_fn = str(tmpdir / "filtered.txt")
max_date_offset = "1M"
max_date = "2021"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-01-25"),
("SEQ_2","2020-02-25"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --max-date-offset {max_date_offset} --max-date {max_date}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_1", "SEQ_2", "SEQ_3"]

def test_filter_max_date_offset_1d(self, tmpdir, argparser, patch_datetime_today):
"""Test that --max-date-offset 1D works"""
out_fn = str(tmpdir / "filtered.txt")
max_date_offset = "1D"
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-03-23"),
("SEQ_2","2020-03-24"),
("SEQ_3","2020-03-25")))
args = argparser(f'--metadata {meta_fn} --output-strains {out_fn} --max-date-offset {max_date_offset}')
augur.filter.run(args)
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == ["SEQ_1", "SEQ_2"]

0 comments on commit 428fdd6

Please sign in to comment.