Skip to content

Commit c305ba2

Browse files
Add new tests for GSP algorithm error handling and edge cases
Enhanced test coverage by adding cases for invalid transaction formats, edge values for `min_support`, and direct `_worker_batch` method validation. These tests ensure proper handling of errors and edge conditions in the GSP algorithm.
1 parent 8be2acb commit c305ba2

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

gsppy/tests/test_gsp.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
Author: Jackson Antonio do Prado Lima
1818
Email: jacksonpradolima@gmail.com
1919
"""
20+
import re
2021

2122
import pytest
23+
2224
from gsppy.gsp import GSP
2325

26+
2427
@pytest.fixture
2528
def supermarket_transactions():
2629
"""
@@ -74,6 +77,67 @@ def test_single_transaction():
7477
GSP(transactions)
7578

7679

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+
77141
def test_no_frequent_items(supermarket_transactions):
78142
"""
79143
Test the GSP algorithm with a high minimum support value.
@@ -86,6 +150,24 @@ def test_no_frequent_items(supermarket_transactions):
86150
assert not result, "High minimum support should filter out all items."
87151

88152

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+
89171
def test_frequent_patterns(supermarket_transactions):
90172
"""
91173
Test the GSP algorithm with supermarket transactions and a realistic minimum support.

0 commit comments

Comments
 (0)