4
4
"""
5
5
6
6
import os
7
+ import shutil
8
+ from argparse import Namespace
7
9
from pathlib import Path
8
10
9
11
import pytest
12
+ import yaml
10
13
14
+ from tests .unit .study .status_test_files import status_test_variables
11
15
12
- @pytest .fixture (scope = "class" )
16
+
17
+ @pytest .fixture (scope = "session" )
13
18
def status_testing_dir (temp_output_dir : str ) -> str :
14
19
"""
15
20
A pytest fixture to set up a temporary directory to write files to for testing status.
16
21
17
22
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
23
+ :returns: The path to the temporary testing directory for status testing
18
24
"""
19
25
testing_dir = f"{ temp_output_dir } /status_testing/"
20
26
if not os .path .exists (testing_dir ):
@@ -23,15 +29,118 @@ def status_testing_dir(temp_output_dir: str) -> str:
23
29
return testing_dir
24
30
25
31
26
- @pytest .fixture (scope = "class " )
32
+ @pytest .fixture (scope = "session " )
27
33
def status_empty_file (status_testing_dir : str ) -> str : # pylint: disable=W0621
28
34
"""
29
35
A pytest fixture to create an empty status file.
30
36
31
- :param status_testing_dir: A pytest fixture that defines a path to the the output directory we'll write to
37
+ :param status_testing_dir: A pytest fixture that defines a path to the the output
38
+ directory we'll write to
39
+ :returns: The path to the empty status file
32
40
"""
33
41
empty_file = Path (f"{ status_testing_dir } /empty_status.json" )
34
42
if not empty_file .exists ():
35
43
empty_file .touch ()
36
44
37
45
return empty_file
46
+
47
+
48
+ @pytest .fixture (scope = "session" )
49
+ def status_spec_path (status_testing_dir : str ) -> str : # pylint: disable=W0621
50
+ """
51
+ Copy the test spec to the temp directory and modify the OUTPUT_PATH in the spec
52
+ to point to the temp location.
53
+
54
+ :param status_testing_dir: A pytest fixture that defines a path to the the output
55
+ directory we'll write to
56
+ :returns: The path to the spec file
57
+ """
58
+ test_spec = f"{ os .path .dirname (__file__ )} /../unit/study/status_test_files/status_test_spec.yaml"
59
+ spec_in_temp_dir = f"{ status_testing_dir } /status_test_spec.yaml"
60
+ shutil .copy (test_spec , spec_in_temp_dir ) # copy test spec to temp directory
61
+
62
+ # Modify the OUTPUT_PATH variable to point to the temp directory
63
+ with open (spec_in_temp_dir , "r" ) as spec_file :
64
+ spec_contents = yaml .load (spec_file , yaml .Loader )
65
+ spec_contents ["env" ]["variables" ]["OUTPUT_PATH" ] = status_testing_dir
66
+ with open (spec_in_temp_dir , "w" ) as spec_file :
67
+ yaml .dump (spec_contents , spec_file )
68
+
69
+ return spec_in_temp_dir
70
+
71
+
72
+ def set_sample_path (output_workspace : str ):
73
+ """
74
+ A pytest fixture to set the path to the samples file in the test spec.
75
+
76
+ :param output_workspace: The workspace that we'll pull the spec file to update from
77
+ """
78
+ temp_merlin_info_path = f"{ output_workspace } /merlin_info"
79
+ expanded_spec_path = f"{ temp_merlin_info_path } /status_test_spec.expanded.yaml"
80
+
81
+ # Read in the contents of the expanded spec
82
+ with open (expanded_spec_path , "r" ) as expanded_file :
83
+ expanded_contents = yaml .load (expanded_file , yaml .Loader )
84
+
85
+ # Modify the samples file path
86
+ expanded_contents ["merlin" ]["samples" ]["file" ] = f"{ temp_merlin_info_path } /samples.csv"
87
+
88
+ # Write the new contents to the expanded spec
89
+ with open (expanded_spec_path , "w" ) as expanded_file :
90
+ yaml .dump (expanded_contents , expanded_file )
91
+
92
+
93
+ @pytest .fixture (scope = "session" )
94
+ def status_output_workspace (status_testing_dir : str ) -> str : # pylint: disable=W0621
95
+ """
96
+ A pytest fixture to copy the test output workspace for status to the temporary
97
+ status testing directory.
98
+
99
+ :param status_testing_dir: A pytest fixture that defines a path to the the output
100
+ directory we'll write to
101
+ :returns: The path to the output workspace in the temp status testing directory
102
+ """
103
+ output_workspace = f"{ status_testing_dir } /{ status_test_variables .VALID_WORKSPACE } "
104
+ shutil .copytree (status_test_variables .VALID_WORKSPACE_PATH , output_workspace ) # copy over the files
105
+ set_sample_path (output_workspace ) # set the path to the samples file in the expanded yaml
106
+ return output_workspace
107
+
108
+
109
+ @pytest .fixture (scope = "function" )
110
+ def status_args ():
111
+ """
112
+ A pytest fixture to set up a namespace with all the arguments necessary for
113
+ the Status object.
114
+
115
+ :returns: The namespace with necessary arguments for the Status object
116
+ """
117
+ return Namespace (
118
+ subparsers = "status" ,
119
+ level = "INFO" ,
120
+ detailed = False ,
121
+ output_path = None ,
122
+ task_server = "celery" ,
123
+ cb_help = False ,
124
+ dump = None ,
125
+ no_prompts = True , # We'll set this to True here since it's easier to test this way
126
+ )
127
+
128
+
129
+ @pytest .fixture (scope = "session" )
130
+ def status_nested_workspace (status_testing_dir : str ) -> str : # pylint: disable=W0621
131
+ """
132
+ Create an output workspace that contains another output workspace within one of its
133
+ steps. In this case it will copy the status test workspace then within the 'just_samples'
134
+ step we'll copy the status test workspace again but with a different name.
135
+
136
+ :param status_testing_dir: A pytest fixture that defines a path to the the output
137
+ directory we'll write to
138
+ :returns: The path to the top level workspace
139
+ """
140
+ top_level_workspace = f"{ status_testing_dir } /status_test_study_nested_20240520-163524"
141
+ nested_workspace = f"{ top_level_workspace } /just_samples/nested_workspace_20240520-163524"
142
+ shutil .copytree (status_test_variables .VALID_WORKSPACE_PATH , top_level_workspace ) # copy over the top level workspace
143
+ shutil .copytree (status_test_variables .VALID_WORKSPACE_PATH , nested_workspace ) # copy over the nested workspace
144
+ set_sample_path (top_level_workspace ) # set the path to the samples file in the expanded yaml of the top level workspace
145
+ set_sample_path (nested_workspace ) # set the path to the samples file in the expanded yaml of the nested workspace
146
+ return top_level_workspace
0 commit comments