1
+ from nbiatoolkit .dicomsort .helper_functions import parseDICOMKeysFromFormat ,sanitizeFileName ,truncateUID
2
+ import pytest
3
+ ###############################################################################
4
+ # parseDICOMKeysFromFormat
5
+
6
+ def test_parseDICOMKeysFromFormat ():
7
+ targetPattern = "%PatientID-%StudyDate"
8
+ expected_format = "%(PatientID)s-%(StudyDate)s"
9
+ expected_keys = ['PatientID' , 'StudyDate' ]
10
+
11
+ format_string , keys = parseDICOMKeysFromFormat (targetPattern )
12
+
13
+ assert format_string == expected_format
14
+ assert keys == expected_keys
15
+
16
+ def test_parseDICOMKeysFromFormat_no_keys ():
17
+ targetPattern = "some string without keys"
18
+ expected_format = "some string without keys"
19
+ expected_keys = []
20
+
21
+ format_string , keys = parseDICOMKeysFromFormat (targetPattern )
22
+
23
+ assert format_string == expected_format
24
+ assert keys == expected_keys
25
+
26
+ def test_parseDICOMKeysFromFormat_multiple_keys ():
27
+ targetPattern = "%PatientID-%StudyDate-%SeriesNumber"
28
+ expected_format = "%(PatientID)s-%(StudyDate)s-%(SeriesNumber)s"
29
+ expected_keys = ['PatientID' , 'StudyDate' , 'SeriesNumber' ]
30
+
31
+ format_string , keys = parseDICOMKeysFromFormat (targetPattern )
32
+
33
+ assert format_string == expected_format
34
+ assert keys == expected_keys
35
+
36
+ def test_parseDICOMKeysFromFormat_duplicate_keys ():
37
+ targetPattern = "%PatientID-%StudyDate-%PatientID"
38
+ expected_format = "%(PatientID)s-%(StudyDate)s-%(PatientID)s"
39
+ expected_keys = ['PatientID' , 'StudyDate' , 'PatientID' ]
40
+
41
+ format_string , keys = parseDICOMKeysFromFormat (targetPattern )
42
+
43
+ assert format_string == expected_format
44
+ assert keys == expected_keys
45
+
46
+ ###############################################################################
47
+ # sanitizeFileName
48
+
49
+ def test_sanitizeFileName_no_special_characters ():
50
+ fileName = "test_file_name"
51
+ sanitized_name = sanitizeFileName (fileName )
52
+ assert sanitized_name == fileName
53
+
54
+ def test_sanitizeFileName_with_special_characters ():
55
+ fileName = "file<name>:with?special*characters"
56
+ sanitized_name = sanitizeFileName (fileName )
57
+ assert sanitized_name == "file_name_with_special_characters"
58
+
59
+ def test_sanitizeFileName_with_spaces ():
60
+ fileName = "file name with spaces"
61
+ sanitized_name = sanitizeFileName (fileName )
62
+ assert sanitized_name == "file_name_with_spaces"
63
+
64
+ def test_sanitizeFileName_empty_string ():
65
+ fileName = ""
66
+ sanitized_name = sanitizeFileName (fileName )
67
+ assert sanitized_name == ""
68
+
69
+ def test_sanitizeFileName_assertions ():
70
+ with pytest .raises (AssertionError ):
71
+ sanitizeFileName (None )
72
+ with pytest .raises (AssertionError ):
73
+ sanitizeFileName (123 )
74
+
75
+ ###############################################################################
76
+ # truncateUID
77
+
78
+ @pytest .fixture (scope = "session" )
79
+ def uid ():
80
+ uid = "1.3.6.1.4.1.14519.5.2.1.215314536760363548451614931725770729635"
81
+ return uid
82
+
83
+
84
+ def test_truncateUID_with_valid_inputs (uid ):
85
+ lastDigits = 5
86
+ expected_output = "29635"
87
+ assert truncateUID (uid , lastDigits ) == expected_output
88
+
89
+ def test_truncateUID_with_lastDigits_greater_than_length_of_UID (uid ):
90
+ lastDigits = 100
91
+ expected_output = uid
92
+ assert truncateUID (uid , lastDigits ) == expected_output
93
+
94
+ def test_truncateUID_with_invalid_input_types (uid ):
95
+ lastDigits = "5"
96
+ with pytest .raises (AssertionError ):
97
+ truncateUID (uid , lastDigits )
98
+
99
+ def test_truncateUID_with_None_input (uid ):
100
+ lastDigits = None
101
+ with pytest .raises (AssertionError ):
102
+ truncateUID (uid , lastDigits )
103
+
0 commit comments