Skip to content

Commit 0ef61af

Browse files
committed
Added unit test, corrected formatting
1 parent 5f3237e commit 0ef61af

File tree

2 files changed

+96
-46
lines changed

2 files changed

+96
-46
lines changed

tests/test_filepattern.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,30 @@
55
import test_generate_filepattern_data
66
import test_filepattern_data as fp_data
77

8+
89
class TestFilePatternFunctions():
9-
10+
1011
def test_get_regex(self):
11-
12+
# Test with a pattern that includes a single variable
1213
pattern = 'img_{row:c}{col:dd}f{f:dd}d{channel:d}.tif'
13-
14-
regex_pattern = fp.get_regex(pattern)
15-
14+
regex_pattern = fp.get_regex(pattern)
1615
assert regex_pattern == 'img_([a-zA-Z])([0-9][0-9])f([0-9][0-9])d([0-9]).tif'
17-
18-
def test_get_variables(self):
1916

17+
def test_get_variables(self):
18+
# Test with a pattern that includes a single variable
2019
pattern = 'img_r{r:ddd}_c{c:ddd}.tif'
21-
2220
variables = fp.get_variables(pattern)
23-
2421
assert (variables == ['r', 'c'] or variables == ['c', 'r'])
2522

23+
2624
class TestArrayPattern():
2725

2826
test_generate_filepattern_data.generate_text_data()
29-
3027
root_directory = os.path.dirname(os.path.realpath(__file__))
31-
3228
filepath = root_directory + '/test_data/data100.txt'
33-
3429
old_pattern = 'img_r{rrr}_c{ccc}.tif'
35-
36-
patterns = ['img_r{r:ddd}_c{c:ddd}.tif', 'img_r{r:d+}_c{c:d+}.tif', old_pattern]
37-
30+
patterns = ['img_r{r:ddd}_c{c:ddd}.tif', 'img_r{r:d+}_c{c:d+}.tif',
31+
old_pattern]
3832
MAX_NUM = 9
3933

4034
with open(filepath, 'r') as file:
@@ -273,18 +267,14 @@ def test_no_sorting(self):
273267
for i in range(len(data)):
274268
assert str(results[i][1][0]) == data[i]
275269

270+
276271
class TestFilePattern():
277272

278273
root_directory = os.path.dirname(os.path.realpath(__file__))
279-
280274
path = root_directory + '/test_data/data100'
281-
282275
sorted_path = root_directory + '/test_data/sorted_data'
283-
284276
old_pattern = 'img_r{rrr}_c{ccc}.tif'
285-
286277
patterns = ['img_r00{r:d}_c{c:ddd}.tif', 'img_r{r:d+}_c{c:d+}.tif', old_pattern]
287-
288278
MAX_NUM = 9
289279

290280
test_generate_filepattern_data.generate_data()
@@ -293,6 +283,7 @@ class TestFilePattern():
293283
test_generate_filepattern_data.generate_bracket_data()
294284
test_generate_filepattern_data.generate_channel_data_sc()
295285
test_generate_filepattern_data.generate_channel_data_spaces()
286+
test_generate_filepattern_data.generate_recursive_no_capture_data() # Added new generator call
296287

297288
def test_file_pattern(self):
298289

@@ -501,11 +492,11 @@ def test_group_by_all_pydantic(self):
501492
assert fp_data.test_fp[i][0]["r"] == result[i].r
502493
assert fp_data.test_fp[i][0]["c"] == result[i].c
503494
assert os.path.basename(fp_data.test_fp[i][1][0]) == os.path.basename(result[i].path[0])
504-
495+
505496
def test_named_group_direcotry(self):
506-
497+
507498
path = self.root_directory + '/test_data/recursive_data'
508-
499+
509500
path += '/(?P<dir>[a-zA-Z]+)/img_r{r:ddd}_c{c:ddd}.tif'
510501

511502
for pattern in self.patterns:
@@ -532,13 +523,11 @@ def test_named_group_direcotry(self):
532523
basename = os.path.basename(mapping[1][0])
533524
for filepath in mapping[1]:
534525
assert basename == os.path.basename(filepath)
535-
526+
536527
def test_recursive_directory_fp(self):
537-
528+
538529
path = self.root_directory + '/test_data/recursive_data'
539-
540530
filepattern = '/{directory:c+}/img_r{r:ddd}_c{c:ddd}.tif'
541-
542531
files = fp.FilePattern(path, filepattern, recursive=True)
543532

544533
result = []
@@ -556,13 +545,11 @@ def test_recursive_directory_fp(self):
556545
assert fp_data.test_recursive_directory_fp[i][0]["c"] == result[i][0]["c"]
557546
assert fp_data.test_recursive_directory_fp[i][0]["directory"] == result[i][0]["directory"]
558547
assert str(os.path.basename(fp_data.test_recursive_directory_fp[i][1][0])) == os.path.basename(result[i][1][0])
559-
548+
560549
def test_recursive_directory_regex_fp(self):
561-
550+
# Test that recursive matching with a regex pattern
562551
path = self.root_directory + '/test_data/recursive_data'
563-
564552
filepattern = '/(?P<directory>[a-zA-Z]+)/img_r{r:ddd}_c{c:ddd}.tif'
565-
566553
files = fp.FilePattern(path, filepattern, recursive=True)
567554

568555
result = []
@@ -580,13 +567,11 @@ def test_recursive_directory_regex_fp(self):
580567
assert fp_data.test_recursive_directory_fp[i][0]["c"] == result[i][0]["c"]
581568
assert fp_data.test_recursive_directory_fp[i][0]["directory"] == result[i][0]["directory"]
582569
assert str(os.path.basename(fp_data.test_recursive_directory_fp[i][1][0])) == os.path.basename(result[i][1][0])
583-
570+
584571
def test_recursive_directory_regex_special_character_fp(self):
585-
572+
# Test that recursive matching with a regex pattern
586573
path = self.root_directory + '/test_data/recursive_data_sc'
587-
588574
filepattern = '/(?P<directory>.*)/img_r{r:ddd}_c{c:ddd}.tif'
589-
590575
files = fp.FilePattern(path, filepattern, recursive=True)
591576

592577
result = []
@@ -606,11 +591,9 @@ def test_recursive_directory_regex_special_character_fp(self):
606591
assert str(os.path.basename(fp_data.test_recursive_directory_fp[i][1][0])) == os.path.basename(result[i][1][0])
607592

608593
def test_recursive_directory_spaces_fp(self):
609-
594+
610595
path = self.root_directory + '/test_data/recursive_data_spaces/'
611-
612596
filepattern = 'img_r{r:ddd}_c{c:ddd}.tif'
613-
614597
files = fp.FilePattern(path, filepattern, recursive=True)
615598

616599
result = []
@@ -631,11 +614,10 @@ def test_recursive_directory_spaces_fp(self):
631614
assert str(os.path.basename(fp_data.test_recursive_space[i][1][0])) == os.path.basename(result[i][1][0])
632615

633616
def test_recursive_multi_directory_regex_fp(self):
634-
617+
# Test that recursive matching with a regex pattern
618+
635619
path = self.root_directory + '/test_data'
636-
637620
filepattern = '/.*/{directory:c+}/img_r{r:ddd}_c{c:ddd}.tif'
638-
639621
files = fp.FilePattern(path, filepattern, recursive=True)
640622

641623
result = []
@@ -654,6 +636,22 @@ def test_recursive_multi_directory_regex_fp(self):
654636
assert fp_data.test_recursive_directory_fp[i][0]["directory"] == result[i][0]["directory"]
655637
assert str(os.path.basename(fp_data.test_recursive_directory_fp[i][1][0])) == os.path.basename(result[i][1][0])
656638

639+
def test_recursive_no_capture_group_returns_all_files(self):
640+
# Test that recursive matching with a non-capturing pattern
641+
# returns all matching files across subdirectories.
642+
path_to_test_dir = os.path.join(self.root_directory, 'test_data', 'recursive_no_capture_data')
643+
pattern_no_capture = '.*.tmp' # Match all .tmp files
644+
expected_file_count = 12 # 5 in subdir1 + 7 in subdir2
645+
646+
files_fp = fp.FilePattern(path_to_test_dir, pattern_no_capture, recursive=True)
647+
648+
# Using len() as it seems to be the standard way to get count in existing tests
649+
actual_file_count = len(files_fp)
650+
651+
assert actual_file_count == expected_file_count, \
652+
f"Expected {expected_file_count} '.tmp' files, but found {actual_file_count} " \
653+
f"using recursive non-capturing pattern in {path_to_test_dir}."
654+
657655
def test_file_pattern_iter(self):
658656

659657
for pattern in self.patterns:
@@ -674,9 +672,8 @@ def test_file_pattern_iter(self):
674672
assert fp_data.test_fp[i][0]["c"] == result[i][0]["c"]
675673
assert os.path.basename(fp_data.test_fp[i][1][0]) == os.path.basename(result[i][1][0])
676674

677-
# test that numeric only, double digit numbers are sorted properly
678675
def test_file_pattern_sorting(self):
679-
676+
# test that numeric only, double digit numbers are sorted properly
680677
sorted_pattern = '{index:d+}.tif'
681678
files = fp.FilePattern(self.sorted_path, sorted_pattern)
682679

@@ -687,6 +684,7 @@ def test_file_pattern_sorting(self):
687684
assert sorted(indices) == indices
688685

689686
def test_file_pattern_brackets(self):
687+
# test that numeric only, double digit numbers are sorted properly
690688

691689
bracket_path = self.root_directory + '/test_data/bracket_data/'
692690

@@ -703,7 +701,8 @@ def test_file_pattern_brackets(self):
703701

704702
for i in range(len(result)):
705703
result[i][0]['c'] == i
706-
os.path.basename(result[i][1][0]) == f'x(0-31)_y(01-48)_c{i}.ome.tif'
704+
os.path.basename(
705+
result[i][1][0]) == f'x(0-31)_y(01-48)_c{i}.ome.tif'
707706

708707

709708
# Todo: These tests need new data to be added after replacing the old version of filepattern.

tests/test_generate_filepattern_data.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import math
33
import os
44

5+
56
directory = 'test_data'
67
root_directory = os.path.dirname(os.path.realpath(__file__))
78
path = os.path.join(root_directory, directory)
89

10+
911
def generate_data():
1012
MAX = 100
1113
length = 0
@@ -100,6 +102,7 @@ def generate_channel_data():
100102

101103
print("Files generated.")
102104

105+
103106
def generate_channel_data_sc():
104107

105108
MAX = 3
@@ -153,6 +156,7 @@ def generate_channel_data_sc():
153156

154157
print("Files generated.")
155158

159+
156160
def generate_channel_data_spaces():
157161

158162
MAX = 3
@@ -206,6 +210,7 @@ def generate_channel_data_spaces():
206210

207211
print("Files generated.")
208212

213+
209214
def generate_sorted_data():
210215
MAX = 30
211216
length = 0
@@ -235,6 +240,7 @@ def generate_sorted_data():
235240

236241
print(str(length) + " files generated.")
237242

243+
238244
def generate_text_data():
239245
output_file = path + '/data100.txt'
240246
print(output_file)
@@ -244,6 +250,7 @@ def generate_text_data():
244250
filename = f"img_r{r:03}_c{c:03}.tif"
245251
file.write(filename + "\n")
246252

253+
247254
def generate_bracket_data():
248255
directory = 'test_data'
249256
root_directory = os.path.dirname(os.path.realpath(__file__))
@@ -271,14 +278,58 @@ def generate_bracket_data():
271278
f2.close()
272279

273280

281+
def generate_recursive_no_capture_data():
282+
base_data_dir_name = 'recursive_no_capture_data'
283+
subdir1_name = 'subdir1'
284+
subdir2_name = 'subdir2'
285+
num_files_subdir1 = 5
286+
num_files_subdir2 = 7
287+
file_extension = '.tmp'
288+
289+
recursive_base_path = os.path.join(path, base_data_dir_name)
290+
subdir1_path = os.path.join(recursive_base_path, subdir1_name)
291+
subdir2_path = os.path.join(recursive_base_path, subdir2_name)
292+
293+
try:
294+
os.makedirs(recursive_base_path, exist_ok=True)
295+
os.makedirs(subdir1_path, exist_ok=True)
296+
os.makedirs(subdir2_path, exist_ok=True)
297+
print(f"Created directory structure at {recursive_base_path}")
298+
except OSError as e:
299+
print(f"Error creating directories: {e}")
300+
return # Stop if directories can't be created
301+
302+
# Create files in subdir1
303+
for i in range(num_files_subdir1):
304+
file_name = f"file{i+1}{file_extension}"
305+
try:
306+
with open(os.path.join(subdir1_path, file_name), 'w'):
307+
pass # Create empty file
308+
except IOError as e:
309+
print(f"Error creating file {os.path.join(subdir1_path, file_name)}: {e}")
310+
311+
# Create files in subdir2
312+
for i in range(num_files_subdir2):
313+
file_name = f"item{chr(ord('A')+i)}{file_extension}" # e.g., itemA.tmp, itemB.tmp
314+
try:
315+
with open(os.path.join(subdir2_path, file_name), 'w'):
316+
pass # Create empty file
317+
except IOError as e:
318+
print(f"Error creating file {os.path.join(subdir2_path, file_name)}: {e}")
319+
320+
total_files = num_files_subdir1 + num_files_subdir2
321+
print(f"{total_files} files generated in {recursive_base_path}")
322+
323+
274324
if __name__ == '__main__':
275325
generate_data()
276326
generate_channel_data()
277327
generate_sorted_data()
278328
generate_text_data()
279329
generate_bracket_data()
280-
generate_channel_data_sc
281-
generate_channel_data_spaces
330+
generate_channel_data_sc() # Corrected call
331+
generate_channel_data_spaces() # Corrected call
332+
generate_recursive_no_capture_data() # Added new generator call
282333

283334

284335
MAX = 3

0 commit comments

Comments
 (0)