Skip to content

Commit 61c515a

Browse files
committed
[tests/doc] Refactor tests
Methods for recurring patterns, refactor test image handling.
1 parent f57b194 commit 61c515a

File tree

1 file changed

+50
-86
lines changed

1 file changed

+50
-86
lines changed

nixio/test/test_doc_examples.py

Lines changed: 50 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import importlib.util as getmod
2-
import matplotlib.pyplot as plt
32
import os
43
import runpy
54
import unittest
65

76
from pathlib import Path
87
from shutil import copyfile
98

9+
import matplotlib.pyplot as plt
10+
11+
12+
TEST_IMAGE = "lenna.png"
13+
1014

1115
class TestDocumentationExamples(unittest.TestCase):
1216

17+
def run_script(self, script_name):
18+
file_path = Path.joinpath(self.examples_path, script_name)
19+
runpy.run_path(path_name=str(file_path), run_name="__main__")
20+
21+
def handle_lif(self):
22+
lif_path = Path.joinpath(self.examples_path, "lif.py")
23+
spec = getmod.spec_from_file_location("lif", str(lif_path))
24+
spec.loader.load_module("lif")
25+
26+
def handle_image(self):
27+
image_path = Path.joinpath(self.examples_path, TEST_IMAGE)
28+
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), TEST_IMAGE)))
29+
1330
def setUp(self):
1431
curr_path = os.getcwd()
1532
if os.path.basename(curr_path) == "nixpy":
@@ -28,184 +45,131 @@ def setUp(self):
2845
def tearDown(self):
2946
plt.close("all")
3047
plt.ioff()
48+
if os.path.exists(TEST_IMAGE):
49+
os.remove(TEST_IMAGE)
3150

3251
def test_annotations(self):
33-
file_path = Path.joinpath(self.examples_path, "annotations.py")
34-
runpy.run_path(path_name=str(file_path), run_name="__main__")
52+
self.run_script("annotations.py")
3553
# cleanup
3654
os.remove("annotations.nix")
3755

3856
def test_category_data(self):
39-
file_path = Path.joinpath(self.examples_path, "categoryData.py")
40-
runpy.run_path(path_name=str(file_path), run_name="__main__")
57+
self.run_script("categoryData.py")
4158
# cleanup
4259
os.remove("categoryData.nix")
4360

4461
def test_continuous_recording(self):
45-
file_path = Path.joinpath(self.examples_path, "continuousRecording.py")
46-
runpy.run_path(path_name=str(file_path), run_name="__main__")
62+
self.run_script("continuousRecording.py")
4763
# cleanup
4864
os.remove("continuous_recording.nix")
4965

5066
def test_file_create(self):
51-
file_path = Path.joinpath(self.examples_path, "fileCreate.py")
52-
runpy.run_path(path_name=str(file_path), run_name="__main__")
67+
self.run_script("fileCreate.py")
5368
# cleanup
5469
os.remove("file_create_example.nix")
5570

5671
def test_image_data(self):
5772
# Requires PIL package and the "Lenna" image.
58-
image_path = Path.joinpath(self.examples_path, "lenna.png")
59-
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), "lenna.png")))
60-
61-
file_path = Path.joinpath(self.examples_path, "imageData.py")
62-
runpy.run_path(path_name=str(file_path), run_name="__main__")
63-
73+
self.handle_image()
74+
self.run_script("imageData.py")
6475
# cleanup
6576
os.remove("image_example.nix")
66-
os.remove("lenna.png")
6777

6878
def test_image_with_metadata(self):
6979
# Requires PIL package and the "Lenna" image.
70-
image_path = Path.joinpath(self.examples_path, "lenna.png")
71-
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), "lenna.png")))
72-
73-
file_path = Path.joinpath(self.examples_path, "imageWithMetadata.py")
74-
runpy.run_path(path_name=str(file_path), run_name="__main__")
75-
80+
self.handle_image()
81+
self.run_script("imageWithMetadata.py")
7682
# cleanup
7783
os.remove("image_with_source_example.h5")
7884
os.remove("image_with_metadata.png")
79-
os.remove("lenna.png")
8085

8186
def test_irregularly_sampled_data(self):
82-
file_path = Path.joinpath(self.examples_path, "irregularlySampledData.py")
83-
runpy.run_path(path_name=str(file_path), run_name="__main__")
87+
self.run_script("irregularlySampledData.py")
8488
# cleanup
8589
os.remove("irregular_data_example.nix")
8690

8791
def test_lif(self):
88-
file_path = Path.joinpath(self.examples_path, "lif.py")
89-
runpy.run_path(path_name=str(file_path), run_name="__main__")
92+
self.run_script("lif.py")
9093

9194
def test_multiple_points(self):
92-
file_path = Path.joinpath(self.examples_path, "multiple_points.py")
93-
runpy.run_path(path_name=str(file_path), run_name="__main__")
95+
self.run_script("multiple_points.py")
9496
# cleanup
9597
os.remove("multiple_points.nix")
9698

9799
def test_multiple_regions(self):
98-
file_path = Path.joinpath(self.examples_path, "multiple_regions.py")
99-
runpy.run_path(path_name=str(file_path), run_name="__main__")
100+
self.run_script("multiple_regions.py")
100101
# cleanup
101102
os.remove("multiple_regions.nix")
102103

103104
def test_multiple_rois(self):
104105
# Requires PIL package and the "Lenna" image.
105-
image_path = Path.joinpath(self.examples_path, "lenna.png")
106-
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), "lenna.png")))
107-
108-
file_path = Path.joinpath(self.examples_path, "multipleROIs.py")
109-
runpy.run_path(path_name=str(file_path), run_name="__main__")
110-
106+
self.handle_image()
107+
self.run_script("multipleROIs.py")
111108
# cleanup
112-
os.remove("lenna.png")
113109
os.remove("multiple_roi.nix")
114110

115111
def test_range_dimension_link(self):
116-
file_path = Path.joinpath(self.examples_path, "rangeDimensionLink.py")
117-
runpy.run_path(path_name=str(file_path), run_name="__main__")
112+
self.run_script("rangeDimensionLink.py")
118113
# cleanup
119114
os.remove("range_link.nix")
120115

121116
def test_regularly_sampled_data(self):
122-
file_path = Path.joinpath(self.examples_path, "regularlySampledData.py")
123-
runpy.run_path(path_name=str(file_path), run_name="__main__")
117+
self.run_script("regularlySampledData.py")
124118
# cleanup
125119
os.remove("regular_data_example.nix")
126120

127121
def test_single_roi(self):
128122
# Requires PIL package and the "Lenna" image.
129-
image_path = Path.joinpath(self.examples_path, "lenna.png")
130-
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), "lenna.png")))
131-
132-
file_path = Path.joinpath(self.examples_path, "singleROI.py")
133-
runpy.run_path(path_name=str(file_path), run_name="__main__")
134-
123+
self.handle_image()
124+
self.run_script("singleROI.py")
135125
# cleanup
136-
os.remove("lenna.png")
137126
os.remove("single_roi.nix")
138127

139128
def test_sources(self):
140-
file_path = Path.joinpath(self.examples_path, "sources.py")
141-
runpy.run_path(path_name=str(file_path), run_name="__main__")
129+
self.run_script("sources.py")
142130
# cleanup
143131
os.remove("sources.nix")
144132

145133
def test_spike_features(self):
146134
# Requires scipy package and "lif.py"
147-
lif_path = Path.joinpath(self.examples_path, "lif.py")
148-
spec = getmod.spec_from_file_location("lif", str(lif_path))
149-
spec.loader.load_module("lif")
150-
151-
file_path = Path.joinpath(self.examples_path, "spikeFeatures.py")
152-
runpy.run_path(path_name=str(file_path), run_name="__main__")
153-
135+
self.handle_lif()
136+
self.run_script("spikeFeatures.py")
154137
# cleanup
155138
os.remove("spike_features.h5")
156139

157140
def test_spike_tagging(self):
158141
# Requires "lif.py"
159-
lif_path = Path.joinpath(self.examples_path, "lif.py")
160-
spec = getmod.spec_from_file_location("lif", str(lif_path))
161-
spec.loader.load_module("lif")
162-
163-
file_path = Path.joinpath(self.examples_path, "spikeTagging.py")
164-
runpy.run_path(path_name=str(file_path), run_name="__main__")
165-
142+
self.handle_lif()
143+
self.run_script("spikeTagging.py")
166144
# cleanup
167145
os.remove("spike_tagging.nix")
168146

169147
def test_tabular_data(self):
170-
file_path = Path.joinpath(self.examples_path, "tabulardata.py")
171-
runpy.run_path(path_name=str(file_path), run_name="__main__")
148+
self.run_script("tabulardata.py")
172149
# cleanup
173150
os.remove("dataframe.nix")
174151

175152
def test_tagged_feature(self):
176153
# Requires scipy package and "lif.py"
177-
lif_path = Path.joinpath(self.examples_path, "lif.py")
178-
spec = getmod.spec_from_file_location("lif", str(lif_path))
179-
spec.loader.load_module("lif")
180-
181-
file_path = Path.joinpath(self.examples_path, "taggedFeature.py")
182-
runpy.run_path(path_name=str(file_path), run_name="__main__")
183-
154+
self.handle_lif()
155+
self.run_script("taggedFeature.py")
184156
# cleanup
185157
os.remove("spike_features.nix")
186158

187159
def test_tagging_example(self):
188160
# Requires scipy package
189-
file_path = Path.joinpath(self.examples_path, "tagging_example.py")
190-
runpy.run_path(path_name=str(file_path), run_name="__main__")
191-
161+
self.run_script("tagging_example.py")
192162
# cleanup
193163
os.remove("tagging1.nix")
194164

195165
def test_tagging_nd(self):
196166
# not testing any nix feature
197-
# file_path = Path.joinpath(self.examples_path, "tagging_nd.py")
198-
# runpy.run_path(path_name=str(file_path), run_name="__main__")
167+
# self.run_script("tagging_nd.py")
199168
pass
200169

201170
def test_untagged_feature(self):
202171
# Requires scipy package and "lif.py"
203-
lif_path = Path.joinpath(self.examples_path, "lif.py")
204-
spec = getmod.spec_from_file_location("lif", str(lif_path))
205-
spec.loader.load_module("lif")
206-
207-
file_path = Path.joinpath(self.examples_path, "untaggedFeature.py")
208-
runpy.run_path(path_name=str(file_path), run_name="__main__")
209-
172+
self.handle_lif()
173+
self.run_script("untaggedFeature.py")
210174
# cleanup
211175
os.remove("untagged_feature.h5")

0 commit comments

Comments
 (0)