11from __future__ import annotations
22
33from pathlib import Path
4+ from unittest .mock import mock_open
45
56import pytest
67from pytest_mock import MockerFixture
78
89from murfey .client .analyser import Analyser
9- from murfey .client .contexts .spa import SPAContext
10- from murfey .client .contexts .tomo import TomographyContext
1110from murfey .util .models import ProcessingParametersSPA , ProcessingParametersTomo
1211
1312example_files = {
7675 file
7776 for file_list in example_files .values ()
7877 for file in file_list
79- for suffix in (".mrc" , ".tiff" , ".tif" , ".eer" , ".lif" , ".txrm" , ".xrm " )
78+ for suffix in (".mrc" , ".tiff" , ".tif" , ".eer" , ".mdoc " )
8079 if file .endswith (suffix )
8180 ],
8281)
83- def test_find_extension (test_file : str , tmp_path : Path ):
84- analyser = Analyser (basepath_local = tmp_path , token = "" )
82+ def test_find_extension (
83+ mocker : MockerFixture ,
84+ test_file : str ,
85+ tmp_path : Path ,
86+ ):
87+ # Mock the functions used to open a .mdoc file to return a dummy file path
88+ m = mock_open (read_data = "dummy data" )
89+ mocker .patch ("murfey.client.analyser.open" , m )
90+ mocker .patch (
91+ "murfey.client.analyser.get_block" ,
92+ return_value = {"SubFramePath" : "/path/to/test_file.tiff" },
93+ )
94+
8595 # Pass the file to the function, and check the outputs are as expected
96+ analyser = Analyser (basepath_local = tmp_path , token = "" )
8697 assert analyser ._find_extension (tmp_path / test_file )
87- assert test_file .endswith (analyser ._extension )
98+ if not test_file .endswith (".mdoc" ):
99+ assert test_file .endswith (analyser ._extension )
100+ else :
101+ assert analyser ._extension == ".tiff"
88102
89103
90104@pytest .mark .parametrize (
@@ -107,9 +121,9 @@ def test_find_context(file_and_context, tmp_path):
107121 assert analyser ._context is not None and context in str (analyser ._context )
108122
109123 # Additional checks for specific contexts
110- if isinstance ( analyser ._context , TomographyContext ) :
124+ if analyser ._context is not None and analyser . _context . name == " TomographyContext" :
111125 assert analyser .parameters_model == ProcessingParametersTomo
112- if isinstance ( analyser ._context , SPAContext ) :
126+ if analyser ._context is not None and analyser . _context . name == " SPAContext" :
113127 assert analyser .parameters_model == ProcessingParametersSPA
114128
115129
@@ -279,24 +293,23 @@ def test_analyse_sxt(
279293
280294
281295@pytest .mark .parametrize (
282- "test_params " ,
296+ "context_to_test " ,
283297 [
284- [ "SPAMetadataContext" , [ "AtlasContext" , "SPAContext" ]] ,
285- [ "TomographyMetadataContext" , [ "AtlasContext" , "SPAContext" ]] ,
298+ "SPAMetadataContext" ,
299+ "TomographyMetadataContext" ,
286300 ],
287301)
288302def test_analyse_limited (
289- mocker : MockerFixture , tmp_path : Path , test_params : tuple [str , list [str ]]
303+ mocker : MockerFixture ,
304+ context_to_test : str ,
305+ tmp_path : Path ,
290306):
291- # Unpack test params
292- expected_context , other_contexts = test_params
293-
294307 # Load example files related to the CLEM
295308 test_files = [
296309 file
297310 for context , file_list in example_files .items ()
298311 for file in file_list
299- if context in ( expected_context , * other_contexts )
312+ if context in context_to_test
300313 ]
301314
302315 # Mock the 'post_transfer' class function
@@ -308,21 +321,36 @@ def test_analyse_limited(
308321 analyser ._analyse (tmp_path / file )
309322
310323 # "_find_context" should be called only once
311- assert analyser ._context is not None and expected_context in str ( analyser ._context )
324+ assert analyser ._context is not None and analyser ._context . name == context_to_test
312325
313326 # "_post_transfer" should be called on every one of these files
314327 assert mock_post_transfer .call_count == len (test_files )
315328
316329
317- def test_analyse_atlas (
330+ @pytest .mark .parametrize (
331+ "context_to_test" ,
332+ [
333+ "AtlasContext" ,
334+ "CLEMContext" ,
335+ "FIBContext" ,
336+ "SPAMetadataContext" ,
337+ "SXTContext" ,
338+ "TomographyMetadataContext" ,
339+ ],
340+ )
341+ def test_analyse_generic (
318342 mocker : MockerFixture ,
343+ context_to_test : str ,
319344 tmp_path : Path ,
320345):
346+ """
347+ Tests the Contexts which has straightforward processing logic.
348+ """
321349 test_files = [
322350 file
323351 for context , file_list in example_files .items ()
324352 for file in file_list
325- if context == "AtlasContext"
353+ if context == context_to_test
326354 ]
327355
328356 # Set up mocks and spies
@@ -334,30 +362,25 @@ def test_analyse_atlas(
334362 for file in test_files :
335363 analyser ._analyse (tmp_path / file )
336364
337- # Context should be set
338- assert analyser ._context is not None and "AtlasContext" in str (analyser ._context )
339-
340365 # "_find_context" should be called once
341366 assert spy_find_context .call_count == 1
342367
368+ # Context should be set
369+ assert analyser ._context is not None and analyser ._context .name == context_to_test
370+
343371 # "post_transfer" should be called on all files
344372 assert mock_post_transfer .call_count == len (test_files )
345373
346374
347- @pytest .mark .parametrize (
348- "context_to_test" ,
349- ("SPAContext" , "SPAMetadataContext" ),
350- )
351375def test_analyse_spa (
352376 mocker : MockerFixture ,
353- context_to_test : str ,
354377 tmp_path : Path ,
355378):
356379 test_files = [
357380 file
358381 for context , file_list in example_files .items ()
359382 for file in file_list
360- if context == context_to_test
383+ if context == "SPAContext"
361384 ]
362385
363386 # Set up mocks and spies
@@ -371,41 +394,29 @@ def test_analyse_spa(
371394 analyser ._analyse (tmp_path / file )
372395
373396 assert spy_find_context .call_count == 1
374- assert analyser ._context is not None and context_to_test in str (analyser ._context )
375- if context_to_test == "SPAContext" :
376- assert spy_find_extension .call_count > 0
377- assert analyser ._extension in (".eer" , ".tiff" )
378- mock_post_transfer .assert_called ()
397+ assert analyser ._context is not None and analyser ._context .name == "SPAContext"
398+ assert spy_find_extension .call_count > 0
399+ assert analyser ._extension in (".eer" , ".tiff" )
400+ mock_post_transfer .assert_called ()
379401
380402
381- @pytest .mark .parametrize (
382- "context_to_test" ,
383- ("TomographyContext" , "TomographyMetadataContext" ),
384- )
385403def test_analyse_tomo (
386404 mocker : MockerFixture ,
387- context_to_test : str ,
388405 tmp_path : Path ,
389406):
390407 test_files = [
391408 file
392409 for context , file_list in example_files .items ()
393410 for file in file_list
394- if context == context_to_test
411+ if context == "TomographyContext"
395412 ]
396413
397414 # Set up mocks and spies
398415 mock_post_transfer = mocker .patch .object (Analyser , "post_transfer" )
399- if context_to_test == "TomographyContext" :
400- mock_gather_metadata = mocker .patch (
401- "murfey.client.contexts.tomo.TomographyContext.gather_metadata"
402- )
403- else :
404- mock_gather_metadata = mocker .patch (
405- "murfey.client.contexts.tomo_metadata.TomographyMetadataContext.gather_metadata"
406- )
416+ mock_gather_metadata = mocker .patch (
417+ "murfey.client.contexts.tomo.TomographyContext.gather_metadata"
418+ )
407419 mock_gather_metadata .return_value = {"dummy" : "dummy" }
408-
409420 spy_find_context = mocker .spy (Analyser , "_find_context" )
410421 spy_find_extension = mocker .spy (Analyser , "_find_extension" )
411422
0 commit comments