17
17
Author: Jackson Antonio do Prado Lima
18
18
Email: jacksonpradolima@gmail.com
19
19
"""
20
+ import re
20
21
21
22
import pytest
23
+
22
24
from gsppy .gsp import GSP
23
25
26
+
24
27
@pytest .fixture
25
28
def supermarket_transactions ():
26
29
"""
@@ -74,6 +77,67 @@ def test_single_transaction():
74
77
GSP (transactions )
75
78
76
79
80
+ def test_invalid_transaction_format ():
81
+ """
82
+ Test the GSP algorithm with invalid transaction formats.
83
+
84
+ Asserts:
85
+ - A ValueError is raised indicating that the transactions must be lists of lists.
86
+ """
87
+ invalid_data = ["A" , "B" ] # Invalid format: not a list of lists
88
+ with pytest .raises (ValueError , match = "The dataset must be a list of transactions." ):
89
+ GSP (invalid_data )
90
+
91
+
92
+ @pytest .mark .parametrize (
93
+ "min_support, expected_error" ,
94
+ [
95
+ (- 0.1 , re .escape ("Minimum support must be in the range (0.0, 1.0]" )),
96
+ (0.0 , re .escape ("Minimum support must be in the range (0.0, 1.0]" )),
97
+ (1.1 , re .escape ("Minimum support must be in the range (0.0, 1.0]" )),
98
+ ]
99
+ )
100
+ def test_invalid_min_support (supermarket_transactions , min_support , expected_error ):
101
+ """
102
+ Test the GSP algorithm with invalid minimum support values.
103
+
104
+ Asserts:
105
+ - A ValueError is raised if the min_support is outside the valid range.
106
+ """
107
+ gsp = GSP (supermarket_transactions )
108
+ with pytest .raises (ValueError , match = expected_error ):
109
+ gsp .search (min_support = min_support )
110
+
111
+
112
+ def test_valid_min_support_edge (supermarket_transactions ):
113
+ """
114
+ Test the GSP algorithm with a valid edge value for min_support.
115
+
116
+ Asserts:
117
+ - The algorithm runs successfully when min_support is set to 1.0.
118
+ """
119
+ gsp = GSP (supermarket_transactions )
120
+ result = gsp .search (min_support = 1.0 ) # Only patterns supported by ALL transactions should remain
121
+ assert not result , "Expected no frequent patterns with min_support = 1.0"
122
+
123
+
124
+ def test_min_support_valid (supermarket_transactions ):
125
+ """
126
+ Test the GSP algorithm with a minimum support set just above 0.0.
127
+
128
+ Asserts:
129
+ - Frequent patterns are generated correctly for a low min_support threshold.
130
+ """
131
+ gsp = GSP (supermarket_transactions )
132
+ result = gsp .search (min_support = 0.2 ) # At least 1 transaction should support the pattern
133
+
134
+ # All items should appear as 1-item patterns
135
+ level_1_patterns = {('Bread' ,), ('Milk' ,), ('Diaper' ,), ('Beer' ,), ('Coke' ,), ('Eggs' ,)}
136
+ result_level_1 = set (result [0 ].keys ()) # Extract patterns from Level 1
137
+
138
+ assert result_level_1 == level_1_patterns , f"Level 1 patterns mismatch. Got { result_level_1 } "
139
+
140
+
77
141
def test_no_frequent_items (supermarket_transactions ):
78
142
"""
79
143
Test the GSP algorithm with a high minimum support value.
@@ -86,6 +150,24 @@ def test_no_frequent_items(supermarket_transactions):
86
150
assert not result , "High minimum support should filter out all items."
87
151
88
152
153
+ def test_worker_batch_static_method (supermarket_transactions ):
154
+ """
155
+ Test the _worker_batch method directly for checkpoint validation.
156
+
157
+ Asserts:
158
+ - Candidates below the minimum support are filtered out.
159
+ - Candidates meeting the minimum support are returned with correct counts.
160
+ """
161
+ batch = [('Bread' ,), ('Milk' ,), ('Diaper' ,), ('Eggs' ,)] # 1-sequence candidates
162
+ transactions = [tuple (t ) for t in supermarket_transactions ]
163
+ min_support = 3 # Absolute support count
164
+ expected = [(('Bread' ,), 4 ), (('Milk' ,), 4 ), (('Diaper' ,), 4 )]
165
+
166
+ # Call the '_worker_batch' method
167
+ results = GSP ._worker_batch (batch , transactions , min_support )
168
+ assert results == expected , f"Expected results { expected } , but got { results } "
169
+
170
+
89
171
def test_frequent_patterns (supermarket_transactions ):
90
172
"""
91
173
Test the GSP algorithm with supermarket transactions and a realistic minimum support.
0 commit comments