Skip to content

Commit 6c54c0f

Browse files
Adopt fx_ convention for cached fixtures
1 parent 4a35bec commit 6c54c0f

File tree

5 files changed

+103
-83
lines changed

5 files changed

+103
-83
lines changed

tests/conftest.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import pathlib
22
import shutil
33
import gzip
4+
import tskit
45

56
import pytest
67

78
import sc2ts
89

910

1011
@pytest.fixture
11-
def data_cache():
12+
def fx_data_cache():
1213
cache_path = pathlib.Path("tests/data/cache")
1314
if not cache_path.exists():
1415
cache_path.mkdir()
1516
return cache_path
1617

1718

1819
@pytest.fixture
19-
def alignments_fasta(data_cache):
20-
cache_path = data_cache / "alignments.fasta"
20+
def fx_alignments_fasta(fx_data_cache):
21+
cache_path = fx_data_cache / "alignments.fasta"
2122
if not cache_path.exists():
2223
with gzip.open("tests/data/alignments.fasta.gz") as src:
2324
with open(cache_path, "wb") as dest:
@@ -26,18 +27,43 @@ def alignments_fasta(data_cache):
2627

2728

2829
@pytest.fixture
29-
def alignments_store(data_cache, alignments_fasta):
30-
cache_path = data_cache / "alignments.db"
30+
def fx_alignment_store(fx_data_cache, fx_alignments_fasta):
31+
cache_path = fx_data_cache / "alignments.db"
3132
if not cache_path.exists():
3233
with sc2ts.AlignmentStore(cache_path, "a") as a:
33-
fasta = sc2ts.core.FastaReader(alignments_fasta)
34+
fasta = sc2ts.core.FastaReader(fx_alignments_fasta)
3435
a.append(fasta, show_progress=False)
3536
return sc2ts.AlignmentStore(cache_path)
3637

3738
@pytest.fixture
38-
def metadata_db(data_cache):
39-
cache_path = data_cache / "metadata.db"
39+
def fx_metadata_db(fx_data_cache):
40+
cache_path = fx_data_cache / "metadata.db"
4041
tsv_path = "tests/data/metadata.tsv"
4142
if not cache_path.exists():
4243
sc2ts.MetadataDb.import_csv(tsv_path, cache_path)
4344
return sc2ts.MetadataDb(cache_path)
45+
46+
47+
@pytest.fixture
48+
def fx_ts_2020_02_10(tmp_path, fx_data_cache, fx_metadata_db, fx_alignment_store):
49+
target_date = "2020-02-10"
50+
cache_path = fx_data_cache / f"{target_date}.ts"
51+
if not cache_path.exists():
52+
last_ts = sc2ts.initial_ts()
53+
match_db = sc2ts.MatchDb.initialise(tmp_path / "match.db")
54+
for date in fx_metadata_db.date_sample_counts():
55+
print("INFERRING", date)
56+
last_ts = sc2ts.extend(
57+
alignment_store=fx_alignment_store,
58+
metadata_db=fx_metadata_db,
59+
base_ts=last_ts,
60+
date=date,
61+
match_db=match_db,
62+
min_group_size=2,
63+
)
64+
if date == target_date:
65+
break
66+
last_ts.dump(cache_path)
67+
return tskit.load(cache_path)
68+
69+

tests/test_alignments.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77

88

99
class TestAlignmentsStore:
10-
def test_info(self, alignments_store):
11-
assert "contains" in str(alignments_store)
10+
def test_info(self, fx_alignment_store):
11+
assert "contains" in str(fx_alignment_store)
1212

13-
def test_len(self, alignments_store):
14-
assert len(alignments_store) == 55
13+
def test_len(self, fx_alignment_store):
14+
assert len(fx_alignment_store) == 55
1515

16-
def test_fetch_known(self, alignments_store):
17-
a = alignments_store["SRR11772659"]
16+
def test_fetch_known(self, fx_alignment_store):
17+
a = fx_alignment_store["SRR11772659"]
1818
assert a.shape == (core.REFERENCE_SEQUENCE_LENGTH,)
1919
assert a[0] == "X"
2020
assert a[1] == "N"
2121
assert a[-1] == "N"
2222

23-
def test_keys(self, alignments_store):
24-
keys = list(alignments_store.keys())
25-
assert len(keys) == len(alignments_store)
23+
def test_keys(self, fx_alignment_store):
24+
keys = list(fx_alignment_store.keys())
25+
assert len(keys) == len(fx_alignment_store)
2626
assert "SRR11772659" in keys
2727

28-
def test_in(self, alignments_store):
29-
assert "SRR11772659" in alignments_store
30-
assert "NOT_IN_STORE" not in alignments_store
28+
def test_in(self, fx_alignment_store):
29+
assert "SRR11772659" in fx_alignment_store
30+
assert "NOT_IN_STORE" not in fx_alignment_store
3131

3232

3333
def test_get_gene_coordinates():
@@ -89,8 +89,8 @@ def test_error__examples(self, a):
8989
with pytest.raises(ValueError):
9090
sa.decode_alignment(np.array(a))
9191

92-
def test_encode_real(self, alignments_store):
93-
h = alignments_store["SRR11772659"]
92+
def test_encode_real(self, fx_alignment_store):
93+
h = fx_alignment_store["SRR11772659"]
9494
a = sa.encode_alignment(h)
9595
assert a[0] == -1
9696
assert a[-1] == -1
@@ -145,8 +145,8 @@ def test_bad_window_size(self, w):
145145

146146

147147
class TestEncodeAndMask:
148-
def test_known(self, alignments_store):
149-
a = alignments_store["SRR11772659"]
148+
def test_known(self, fx_alignment_store):
149+
a = fx_alignment_store["SRR11772659"]
150150
ma = sa.encode_and_mask(a)
151151
assert ma.original_base_composition == {
152152
"T": 9566,

tests/test_cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ def test_additional_problematic_sites(self, tmp_path, additional):
4747

4848

4949
class TestListDates:
50-
def test_defaults(self, metadata_db):
50+
def test_defaults(self, fx_metadata_db):
5151
runner = ct.CliRunner(mix_stderr=False)
5252
result = runner.invoke(
5353
cli.cli,
54-
f"list-dates {metadata_db.path}",
54+
f"list-dates {fx_metadata_db.path}",
5555
catch_exceptions=False,
5656
)
5757
assert result.exit_code == 0
@@ -78,11 +78,11 @@ def test_defaults(self, metadata_db):
7878
"2020-02-13",
7979
]
8080

81-
def test_counts(self, metadata_db):
81+
def test_counts(self, fx_metadata_db):
8282
runner = ct.CliRunner(mix_stderr=False)
8383
result = runner.invoke(
8484
cli.cli,
85-
f"list-dates {metadata_db.path} --counts",
85+
f"list-dates {fx_metadata_db.path} --counts",
8686
catch_exceptions=False,
8787
)
8888
assert result.exit_code == 0

tests/test_inference.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,10 @@ def test_high_recomb_mutation(self):
486486

487487

488488
class TestRealData:
489-
# def test_first_day(self, tmp_path, alignments_store, metadata_db):
490-
def test_first_day(self, tmp_path, alignments_store, metadata_db):
489+
def test_first_day(self, tmp_path, fx_alignment_store, fx_metadata_db):
491490
ts = sc2ts.extend(
492-
alignment_store=alignments_store,
493-
metadata_db=metadata_db,
491+
alignment_store=fx_alignment_store,
492+
metadata_db=fx_metadata_db,
494493
base_ts=sc2ts.initial_ts(),
495494
date="2020-01-19",
496495
match_db=sc2ts.MatchDb.initialise(tmp_path / "match.db"),
@@ -536,3 +535,32 @@ def test_first_day(self, tmp_path, alignments_store, metadata_db):
536535
},
537536
"original_md5": "e96feaa72c4f4baba73c2e147ede7502",
538537
}
538+
539+
def test_2020_02_10_metadata(self, fx_ts_2020_02_10):
540+
ts = fx_ts_2020_02_10
541+
assert ts.metadata["sc2ts"]["date"] == "2020-02-10"
542+
samples_strain = [ts.node(u).metadata["strain"] for u in ts.samples()]
543+
assert ts.metadata["sc2ts"]["samples_strain"] == samples_strain
544+
# print(ts.tables.mutations)
545+
# print(ts.draw_text())
546+
547+
548+
class TestMatchingDetails:
549+
550+
def test_exact_matches(self, fx_ts_2020_02_10, fx_alignment_store, fx_metadata_db):
551+
print("HERE")
552+
553+
def test_other_exact_matches(self, tmp_path, fx_ts_2020_02_10, fx_alignment_store, fx_metadata_db):
554+
print("HERE")
555+
match_db = sc2ts.MatchDb.initialise(tmp_path / "match.db")
556+
ts = sc2ts.extend(
557+
alignment_store=fx_alignment_store,
558+
metadata_db=fx_metadata_db,
559+
base_ts=fx_ts_2020_02_10,
560+
date="2020-02-11",
561+
match_db=match_db,
562+
min_group_size=2,
563+
)
564+
565+
566+

tests/test_metadata.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,39 @@
33

44

55
class TestMetadataDb:
6-
def test_known(self, metadata_db):
7-
record = metadata_db["SRR11772659"]
6+
def test_known(self, fx_metadata_db):
7+
record = fx_metadata_db["SRR11772659"]
88
assert record["strain"] == "SRR11772659"
99
assert record["date"] == "2020-01-19"
1010
assert record["Viridian_pangolin"] == "A"
1111

12-
def test_missing_sequence(self, metadata_db):
12+
def test_missing_sequence(self, fx_metadata_db):
1313
# We include sequence that's not in the alignments DB
14-
assert "ERR_MISSING" in metadata_db
14+
assert "ERR_MISSING" in fx_metadata_db
1515

16-
def test_keys(self, metadata_db):
17-
keys = list(metadata_db.keys())
16+
def test_keys(self, fx_metadata_db):
17+
keys = list(fx_metadata_db.keys())
1818
assert "SRR11772659" in keys
1919
assert len(set(keys)) == len(keys)
2020
df = pd.read_csv("tests/data/metadata.tsv", sep="\t")
2121
assert set(keys) == set(df["strain"])
2222

23-
def test_in(self, metadata_db):
24-
assert "SRR11772659" in metadata_db
25-
assert "DEFO_NOT_IN_DB" not in metadata_db
23+
def test_in(self, fx_metadata_db):
24+
assert "SRR11772659" in fx_metadata_db
25+
assert "DEFO_NOT_IN_DB" not in fx_metadata_db
2626

27-
def test_get_all_days(self, metadata_db):
28-
results = metadata_db.get_days()
29-
assert results == [
30-
"2020-01-01",
31-
"2020-01-19",
32-
"2020-01-24",
33-
"2020-01-25",
34-
"2020-01-28",
35-
"2020-01-29",
36-
"2020-01-30",
37-
"2020-01-31",
38-
"2020-02-01",
39-
"2020-02-02",
40-
"2020-02-03",
41-
"2020-02-04",
42-
"2020-02-05",
43-
"2020-02-06",
44-
"2020-02-07",
45-
"2020-02-08",
46-
"2020-02-09",
47-
"2020-02-10",
48-
"2020-02-11",
49-
"2020-02-13",
50-
]
27+
# TODO test count_days. See test_cli.
5128

52-
def test_get_days_greater(self, metadata_db):
53-
results = metadata_db.get_days("2020-02-06")
54-
assert results == [
55-
"2020-02-07",
56-
"2020-02-08",
57-
"2020-02-09",
58-
"2020-02-10",
59-
"2020-02-11",
60-
"2020-02-13",
61-
]
29+
def test_get_days_none(self, fx_metadata_db):
30+
assert fx_metadata_db.get_days("2022-02-06") == []
6231

63-
def test_get_days_none(self, metadata_db):
64-
assert metadata_db.get_days("2022-02-06") == []
65-
66-
def test_get_first(self, metadata_db):
67-
results = list(metadata_db.get("2020-01-01"))
32+
def test_get_first(self, fx_metadata_db):
33+
results = list(fx_metadata_db.get("2020-01-01"))
6834
assert len(results) == 1
69-
assert results[0] == metadata_db["SRR14631544"]
35+
assert results[0] == fx_metadata_db["SRR14631544"]
7036

71-
def test_get_multi(self, metadata_db):
72-
results = list(metadata_db.get("2020-02-11"))
37+
def test_get_multi(self, fx_metadata_db):
38+
results = list(fx_metadata_db.get("2020-02-11"))
7339
assert len(results) == 2
7440
for result in results:
7541
assert result["date"] == "2020-02-11"

0 commit comments

Comments
 (0)