11import gzip
2+ import importlib
3+ from urllib .error import URLError
4+
25import pytest
3- from pathlib import Path
6+
47from seqBackupLib .backup import DEFAULT_MIN_FILE_SIZE
5- from seqBackupLib .illumina import IlluminaDir , IlluminaFastq , MACHINE_TYPES
68
79
810machine_fixtures = {
1618}
1719
1820
19- @pytest .mark .parametrize ("machine_type" , MACHINE_TYPES .keys ())
20- def test_illumina_fastq (machine_type , request ):
21+ @pytest .fixture
22+ def illumina_module (monkeypatch ):
23+ tsv_rows = ["instrument_code\t machine_type" ]
24+ fallback = {
25+ "VH" : "Illumina-NextSeq" ,
26+ "D" : "Illumina-HiSeq" ,
27+ "M" : "Illumina-MiSeq" ,
28+ "A" : "Illumina-NovaSeq" ,
29+ "NB" : "Illumina-MiniSeq" ,
30+ "LH" : "Illumina-NovaSeqX" ,
31+ "SH" : "Illumina-MiSeq" ,
32+ }
33+ tsv_rows .extend (f"{ code } \t { machine } " for code , machine in fallback .items ())
34+ tsv = "\n " .join (tsv_rows ) + "\n "
35+
36+ class FakeResponse :
37+ def __init__ (self , data : str ):
38+ self ._data = data
39+
40+ def read (self ):
41+ return self ._data .encode ("utf-8" )
42+
43+ def __enter__ (self ):
44+ return self
45+
46+ def __exit__ (self , exc_type , exc , tb ):
47+ return False
48+
49+ monkeypatch .setattr (
50+ "urllib.request.urlopen" , lambda * args , ** kwargs : FakeResponse (tsv )
51+ )
52+ import seqBackupLib .illumina as illumina
53+
54+ return importlib .reload (illumina )
55+
56+
57+ @pytest .mark .parametrize ("machine_type" , machine_fixtures .keys ())
58+ def test_illumina_fastq (machine_type , request , illumina_module ):
2159 fixture_name = machine_fixtures .get (machine_type )
2260 if not fixture_name :
2361 raise ValueError (
@@ -27,18 +65,18 @@ def test_illumina_fastq(machine_type, request):
2765 fp = request .getfixturevalue (fixture_name )
2866
2967 with gzip .open (fp / "Undetermined_S0_L001_R1_001.fastq.gz" , "rt" ) as f :
30- r1 = IlluminaFastq (f )
68+ r1 = illumina_module . IlluminaFastq (f )
3169
3270 print ("FASTQ info: " , r1 .fastq_info , "\n Folder info: " , r1 .folder_info )
33- assert r1 .machine_type == MACHINE_TYPES [machine_type ]
71+ assert r1 .machine_type == illumina_module . MACHINE_TYPES [machine_type ]
3472 assert r1 .check_fp_vs_content ()[0 ], r1 .check_fp_vs_content ()
3573 assert not r1 .check_file_size (DEFAULT_MIN_FILE_SIZE )
3674 assert r1 .check_file_size (100 )
3775 assert r1 .check_index_read_exists ()
3876
3977
40- @pytest .mark .parametrize ("machine_type" , MACHINE_TYPES .keys ())
41- def test_illumina_dir (machine_type , request ):
78+ @pytest .mark .parametrize ("machine_type" , machine_fixtures .keys ())
79+ def test_illumina_dir (machine_type , request , illumina_module ):
4280 fixture_name = machine_fixtures .get (machine_type )
4381 if not fixture_name :
4482 raise ValueError (
@@ -47,14 +85,51 @@ def test_illumina_dir(machine_type, request):
4785
4886 fp = request .getfixturevalue (fixture_name )
4987
50- d = IlluminaDir (fp .name )
88+ d = illumina_module . IlluminaDir (fp .name )
5189
5290
53- def test_illumina_fastq_without_lane (novaseq_dir ):
91+ def test_illumina_fastq_without_lane (novaseq_dir , illumina_module ):
5492 original = novaseq_dir / "Undetermined_S0_L001_R1_001.fastq.gz"
5593 renamed = novaseq_dir / "Undetermined_S0_R1_001.fastq.gz"
5694 original .rename (renamed )
5795 with gzip .open (renamed , "rt" ) as f :
58- r1 = IlluminaFastq (f )
96+ r1 = illumina_module . IlluminaFastq (f )
5997 assert r1 .check_fp_vs_content ()[0 ]
6098 assert r1 .build_archive_dir ().endswith ("L001" )
99+
100+
101+ def test_load_machine_types_from_tsv (monkeypatch ):
102+ tsv = "instrument_code\t machine_type\n ZZ\t Illumina-Test\n "
103+
104+ class FakeResponse :
105+ def __init__ (self , data : str ):
106+ self ._data = data
107+
108+ def read (self ):
109+ return self ._data .encode ("utf-8" )
110+
111+ def __enter__ (self ):
112+ return self
113+
114+ def __exit__ (self , exc_type , exc , tb ):
115+ return False
116+
117+ monkeypatch .setattr (
118+ "urllib.request.urlopen" , lambda * args , ** kwargs : FakeResponse (tsv )
119+ )
120+ import seqBackupLib .illumina as illumina
121+
122+ illumina = importlib .reload (illumina )
123+ assert illumina .MACHINE_TYPES ["ZZ" ] == "Illumina-Test"
124+
125+
126+ def test_load_machine_types_fallback_warning (monkeypatch ):
127+ def raise_url_error (* args , ** kwargs ):
128+ raise URLError ("network down" )
129+
130+ monkeypatch .setattr ("urllib.request.urlopen" , raise_url_error )
131+ import seqBackupLib .illumina as illumina
132+
133+ with pytest .warns (RuntimeWarning , match = "Falling back to bundled machine types" ):
134+ illumina = importlib .reload (illumina )
135+ assert illumina .MACHINE_TYPES == illumina .MACHINE_TYPES_FALLBACK
0 commit comments