Skip to content

Commit 46a1c09

Browse files
committed
test: run db-download and plass_class depth tests in temp dirs
Stop the suite mutating committed fixtures: - test_db: the download test installs into a TemporaryDirectory rather than removing/overwriting the committed Plassembler_Test_DB fixtures in place. - test_plass_class: the get_depth / get_depth_long / combine_tsv tests (Plass and Assembly) copy their fixture dir into a TemporaryDirectory and work there, so committed intermediates (e.g. assembly_class/depth/combined_short.sam) are no longer regenerated over the tracked copies. Validated with real tools: the 75MB DB download passes and the plass_class tests pass, both leaving the committed fixtures untouched.
1 parent 6c76cd3 commit 46a1c09

2 files changed

Lines changed: 40 additions & 44 deletions

File tree

tests/test_db.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import sys
9+
import tempfile
910

1011
# import
1112
import unittest
@@ -42,12 +43,13 @@ def test_check_db_installation_good(self):
4243
check_db_installation(db_path, force=False, install_flag=False)
4344

4445
# for plassembler download
45-
# NOTE: force=True + install_flag=True downloads the real (~75MB) database
46-
# over the network and overwrites the committed test fixtures in place, so
47-
# this is neither offline-safe nor idempotent. Kept out of the fast subset.
46+
# NOTE: downloads the real (~75MB) database over the network, so this is slow
47+
# and offline-unsafe. It installs into a throwaway tmp dir so the committed
48+
# fixture DB is never removed or overwritten.
4849
@pytest.mark.slow
4950
def test_check_db_installation_good_d(self):
50-
check_db_installation(db_path, force=True, install_flag=True)
51+
with tempfile.TemporaryDirectory() as tmp:
52+
check_db_installation(Path(tmp) / "db", force=True, install_flag=True)
5153

5254
def test_check_db_installation_bad(self):
5355
with self.assertRaises(SystemExit):

tests/test_plass_class.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"""
77

88
# import
9+
import shutil
10+
import tempfile
911
import unittest
1012
from pathlib import Path
1113

1214
import pytest
1315

14-
from src.plassembler.utils.cleanup import remove_file
1516
from src.plassembler.utils.plass_class import Assembly, Plass
1617

1718
# import functions
@@ -65,11 +66,12 @@ def test_check_get_depth(self):
6566
plass = Plass()
6667
pacbio_model = "nothing"
6768
threads = 1
68-
# set to the depth dir for intermediate files
69-
plass.outdir = plass_class_depth_dir
70-
plass.get_depth(logdir, pacbio_model, threads)
71-
remove_file(Path(f"{plass_class_depth_dir}/combined_short.sam"))
72-
remove_file(Path(f"{plass_class_depth_dir}/combined_sorted_short.bam"))
69+
with tempfile.TemporaryDirectory() as tmp:
70+
# work in a copy so the committed fixtures are not mutated
71+
workdir = Path(tmp) / "depth"
72+
shutil.copytree(plass_class_depth_dir, workdir)
73+
plass.outdir = workdir
74+
plass.get_depth(logdir, pacbio_model, threads)
7375
self.assertEqual(expected, True)
7476

7577
@pytest.mark.slow
@@ -78,14 +80,12 @@ def test_check_get_depth_long(self):
7880
plass = Plass()
7981
pacbio_model = "nothing"
8082
threads = 1
81-
plasmids_for_sketching = Path(
82-
f"{plass_class_depth_dir}/plasmids_for_sketching.fasta"
83-
)
84-
# set to the depth dir for intermediate files
85-
plass.outdir = plass_class_depth_dir
86-
plass.get_depth_long(logdir, pacbio_model, threads, plasmids_for_sketching)
87-
remove_file(Path(f"{plass_class_depth_dir}/combined_long.sam"))
88-
remove_file(Path(f"{plass_class_depth_dir}/combined_sorted_long.bam"))
83+
with tempfile.TemporaryDirectory() as tmp:
84+
workdir = Path(tmp) / "depth"
85+
shutil.copytree(plass_class_depth_dir, workdir)
86+
plasmids_for_sketching = workdir / "plasmids_for_sketching.fasta"
87+
plass.outdir = workdir
88+
plass.get_depth_long(logdir, pacbio_model, threads, plasmids_for_sketching)
8989
self.assertEqual(expected, True)
9090

9191
def test_process_mash_tsv(self):
@@ -100,18 +100,16 @@ def test_process_mash_tsv(self):
100100
def test_combine_tsv(self):
101101
expected = True
102102
plass = Plass()
103-
plass.outdir = plass_class_depth_dir
104103
prefix = "plassembler"
105104
pacbio_model = "nothing"
106105
threads = 1
107-
plass.get_depth(logdir, pacbio_model, threads)
108-
plass.process_mash_tsv(plassembler_db_dir)
109-
plass.combine_depth_mash_tsvs(prefix, depth_filter=0.1, skip_mash=False)
110-
remove_file(Path(f"{plass_class_depth_dir}/combined_long.sam"))
111-
remove_file(Path(f"{plass_class_depth_dir}/combined_short.sam"))
112-
remove_file(Path(f"{plass_class_depth_dir}/combined_sorted_long.bam"))
113-
remove_file(Path(f"{plass_class_depth_dir}/combined_sorted_short.bam"))
114-
remove_file(Path(f"{plass_class_depth_dir}/{prefix}_summary.tsv"))
106+
with tempfile.TemporaryDirectory() as tmp:
107+
workdir = Path(tmp) / "depth"
108+
shutil.copytree(plass_class_depth_dir, workdir)
109+
plass.outdir = workdir
110+
plass.get_depth(logdir, pacbio_model, threads)
111+
plass.process_mash_tsv(plassembler_db_dir)
112+
plass.combine_depth_mash_tsvs(prefix, depth_filter=0.1, skip_mash=False)
115113
self.assertEqual(expected, True)
116114

117115

@@ -133,13 +131,11 @@ def test_check_get_depth(self):
133131
assembly = Assembly()
134132
pacbio_model = "nothing"
135133
threads = 1
136-
# set to the depth dir for intermediate files
137-
assembly.outdir = assembly_depth_dir
138-
assembly.get_depth(logdir, pacbio_model, threads)
139-
remove_file(Path(f"{assembly_depth_dir}/combined_long.sam"))
140-
remove_file(Path(f"{assembly_depth_dir}/combined_short.sam"))
141-
remove_file(Path(f"{assembly_depth_dir}/combined_sorted_long.bam"))
142-
remove_file(Path(f"{assembly_depth_dir}/combined_sorted_short.bam"))
134+
with tempfile.TemporaryDirectory() as tmp:
135+
workdir = Path(tmp) / "depth"
136+
shutil.copytree(assembly_depth_dir, workdir)
137+
assembly.outdir = workdir
138+
assembly.get_depth(logdir, pacbio_model, threads)
143139
self.assertEqual(expected, True)
144140

145141
def test_process_mash_tsv(self):
@@ -155,17 +151,15 @@ def test_process_mash_tsv(self):
155151
def test_combine_tsv(self):
156152
expected = True
157153
assembly = Assembly()
158-
assembly.outdir = assembly_depth_dir
159154
prefix = "plassembler"
160155
pacbio_model = "nothing"
161156
threads = 1
162-
plasmid_fasta = Path(f"{assembly_depth_dir}/plasmids.fasta")
163-
assembly.get_depth(logdir, pacbio_model, threads)
164-
assembly.process_mash_tsv(plassembler_db_dir, plasmid_fasta)
165-
assembly.combine_depth_mash_tsvs(prefix, False)
166-
remove_file(Path(f"{assembly_depth_dir}/combined_long.sam"))
167-
remove_file(Path(f"{assembly_depth_dir}/combined_short.bam"))
168-
remove_file(Path(f"{assembly_depth_dir}/combined_sorted_long.bam"))
169-
remove_file(Path(f"{assembly_depth_dir}/combined_sorted_short.bam"))
170-
remove_file(Path(f"{assembly_depth_dir}/{prefix}_summary.tsv"))
157+
with tempfile.TemporaryDirectory() as tmp:
158+
workdir = Path(tmp) / "depth"
159+
shutil.copytree(assembly_depth_dir, workdir)
160+
assembly.outdir = workdir
161+
plasmid_fasta = workdir / "plasmids.fasta"
162+
assembly.get_depth(logdir, pacbio_model, threads)
163+
assembly.process_mash_tsv(plassembler_db_dir, plasmid_fasta)
164+
assembly.combine_depth_mash_tsvs(prefix, False)
171165
self.assertEqual(expected, True)

0 commit comments

Comments
 (0)