-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_analysis.py
More file actions
254 lines (202 loc) · 8.99 KB
/
test_analysis.py
File metadata and controls
254 lines (202 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
"""
Test Script for Big Data Text Analysis Pipeline
Validates the functionality of all components
"""
import sys
import os
import tempfile
import subprocess
import logging
from typing import List, Dict, Any
from utils import setup_logging, preprocess_text
from config import Config
# Setup logging
setup_logging()
logger = logging.getLogger(__name__)
class AnalysisTester:
"""Test class for the analysis pipeline"""
def __init__(self):
self.test_text = """
This is a test document for the EKU student handbook analysis.
The university provides various academic services to students.
Students must follow the academic policies and conduct guidelines.
The campus offers many resources for student success.
"""
self.temp_dir = tempfile.mkdtemp()
def test_preprocessing(self) -> bool:
"""Test text preprocessing functionality"""
try:
logger.info("Testing text preprocessing...")
tokens = preprocess_text(self.test_text, remove_stops=True)
if not tokens:
logger.error("Preprocessing returned no tokens")
return False
# Check that stopwords are removed
stopwords = Config.get_stopwords()
for token in tokens:
if token in stopwords:
logger.error(f"Stopword '{token}' not removed")
return False
logger.info(f"Preprocessing successful: {len(tokens)} tokens")
return True
except Exception as e:
logger.error(f"Preprocessing test failed: {e}")
return False
def test_word_count_pipeline(self) -> bool:
"""Test word count MapReduce pipeline"""
try:
logger.info("Testing word count pipeline...")
# Create temporary input file
input_file = os.path.join(self.temp_dir, "test_input.txt")
with open(input_file, 'w') as f:
f.write(self.test_text)
# Run preprocessing - use appropriate command for OS
if os.name == 'nt': # Windows
preprocess_cmd = f"type {input_file} | python preprocess.py"
else: # Unix/Linux/Mac
preprocess_cmd = f"cat {input_file} | python preprocess.py"
preprocess_result = subprocess.run(
preprocess_cmd, shell=True, capture_output=True, text=True
)
if preprocess_result.returncode != 0:
logger.error(f"Preprocessing failed: {preprocess_result.stderr}")
return False
# Run mapper
mapper_cmd = f"echo '{preprocess_result.stdout}' | python mapper.py"
mapper_result = subprocess.run(
mapper_cmd, shell=True, capture_output=True, text=True
)
if mapper_result.returncode != 0:
logger.error(f"Mapper failed: {mapper_result.stderr}")
return False
# Run reducer
reducer_cmd = f"echo '{mapper_result.stdout}' | python reducer.py"
reducer_result = subprocess.run(
reducer_cmd, shell=True, capture_output=True, text=True
)
if reducer_result.returncode != 0:
logger.error(f"Reducer failed: {reducer_result.stderr}")
return False
# Check output
output_lines = reducer_result.stdout.strip().split('\n')
if len(output_lines) == 0:
logger.error("No word count output")
return False
logger.info("Word count pipeline test successful")
return True
except Exception as e:
logger.error(f"Word count pipeline test failed: {e}")
return False
def test_tfidf_pipeline(self) -> bool:
"""Test TF-IDF pipeline"""
try:
logger.info("Testing TF-IDF pipeline...")
# Create temporary input file
input_file = os.path.join(self.temp_dir, "test_input.txt")
with open(input_file, 'w') as f:
f.write(self.test_text)
# Run TF-IDF preprocessing - use appropriate command for OS
if os.name == 'nt': # Windows
preprocess_cmd = f"type {input_file} | python preprocess_tfidf.py"
else: # Unix/Linux/Mac
preprocess_cmd = f"cat {input_file} | python preprocess_tfidf.py"
preprocess_result = subprocess.run(
preprocess_cmd, shell=True, capture_output=True, text=True
)
if preprocess_result.returncode != 0:
logger.error(f"TF-IDF preprocessing failed: {preprocess_result.stderr}")
return False
# Run TF-IDF mapper
mapper_cmd = f"echo '{preprocess_result.stdout}' | python mapper_tfidf.py test_doc"
mapper_result = subprocess.run(
mapper_cmd, shell=True, capture_output=True, text=True
)
if mapper_result.returncode != 0:
logger.error(f"TF-IDF mapper failed: {mapper_result.stderr}")
return False
# Run TF-IDF reducer
reducer_cmd = f"echo '{mapper_result.stdout}' | python reducer_tfidf.py"
reducer_result = subprocess.run(
reducer_cmd, shell=True, capture_output=True, text=True
)
if reducer_result.returncode != 0:
logger.error(f"TF-IDF reducer failed: {reducer_result.stderr}")
return False
logger.info("TF-IDF pipeline test successful")
return True
except Exception as e:
logger.error(f"TF-IDF pipeline test failed: {e}")
return False
def test_lda_pipeline(self) -> bool:
"""Test LDA pipeline"""
try:
logger.info("Testing LDA pipeline...")
# Create temporary input file
input_file = os.path.join(self.temp_dir, "test_input.txt")
with open(input_file, 'w') as f:
f.write(self.test_text)
# Run LDA mapper - use appropriate command for OS
if os.name == 'nt': # Windows
mapper_cmd = f"type {input_file} | python mapper_lda.py"
else: # Unix/Linux/Mac
mapper_cmd = f"cat {input_file} | python mapper_lda.py"
mapper_result = subprocess.run(
mapper_cmd, shell=True, capture_output=True, text=True
)
if mapper_result.returncode != 0:
logger.error(f"LDA mapper failed: {mapper_result.stderr}")
return False
# Run LDA reducer
reducer_cmd = f"echo '{mapper_result.stdout}' | python reducer_lda.py"
reducer_result = subprocess.run(
reducer_cmd, shell=True, capture_output=True, text=True
)
if reducer_result.returncode != 0:
logger.error(f"LDA reducer failed: {reducer_result.stderr}")
return False
logger.info("LDA pipeline test successful")
return True
except Exception as e:
logger.error(f"LDA pipeline test failed: {e}")
return False
def run_all_tests(self) -> Dict[str, bool]:
"""Run all tests and return results"""
logger.info("Starting analysis pipeline tests...")
results = {
'preprocessing': self.test_preprocessing(),
'word_count': self.test_word_count_pipeline(),
'tfidf': self.test_tfidf_pipeline(),
'lda': self.test_lda_pipeline()
}
# Print summary
logger.info("\n" + "="*50)
logger.info("TEST RESULTS SUMMARY")
logger.info("="*50)
all_passed = True
for test_name, passed in results.items():
status = "PASSED" if passed else "FAILED"
logger.info(f"{test_name:15} : {status}")
if not passed:
all_passed = False
logger.info("="*50)
if all_passed:
logger.info("ALL TESTS PASSED! ✅")
else:
logger.error("SOME TESTS FAILED! ❌")
return results
def main():
"""Main test function"""
try:
tester = AnalysisTester()
results = tester.run_all_tests()
# Exit with appropriate code
if all(results.values()):
sys.exit(0)
else:
sys.exit(1)
except Exception as e:
logger.error(f"Test execution failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()