-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_core_modules.py
More file actions
443 lines (401 loc) · 23.2 KB
/
test_core_modules.py
File metadata and controls
443 lines (401 loc) · 23.2 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
Comprehensive Core Module Tests
This module provides thorough testing for all core GNN processing modules
to ensure 100% functionality and coverage. Each test validates:
1. Module import capabilities and dependency resolution
2. Core functionality and data processing
3. Error handling and edge cases
4. Integration with other modules
5. Performance characteristics
6. Documentation and API consistency
All tests execute real methods and file operations without mocking; tests may skip if optional backends are unavailable.
"""
import logging
from pathlib import Path
import pytest
pytestmark = [pytest.mark.core, pytest.mark.fast]
class TestGNNModuleComprehensive:
"""Comprehensive tests for the GNN processing module."""
@pytest.mark.unit
def test_gnn_module_imports(self):
"""Test that GNN module can be imported and has expected structure."""
from src.gnn import discover_gnn_files, generate_gnn_report, parse_gnn_file, process_gnn_directory, validate_gnn_structure
assert callable(discover_gnn_files), 'discover_gnn_files should be callable'
assert callable(parse_gnn_file), 'parse_gnn_file should be callable'
assert callable(validate_gnn_structure), 'validate_gnn_structure should be callable'
assert callable(process_gnn_directory), 'process_gnn_directory should be callable'
assert callable(generate_gnn_report), 'generate_gnn_report should be callable'
logging.info('GNN module imports validated')
@pytest.mark.unit
def test_gnn_file_discovery(self, sample_gnn_files):
"""Test GNN file discovery functionality."""
from src.gnn import discover_gnn_files
gnn_dir = list(sample_gnn_files.values())[0].parent
discovered_files = discover_gnn_files(gnn_dir)
assert isinstance(discovered_files, list), 'discover_gnn_files should return a list'
assert len(discovered_files) > 0, 'Should discover GNN files'
for file_path in discovered_files:
assert isinstance(file_path, Path), 'Discovered files should be Path objects'
assert file_path.exists(), 'Discovered files should exist'
logging.info(f'GNN file discovery validated: {len(discovered_files)} files found')
@pytest.mark.unit
def test_gnn_file_parsing(self, sample_gnn_files):
"""Test GNN file parsing functionality."""
from src.gnn import parse_gnn_file
for file_path in sample_gnn_files.values():
try:
parsed_data = parse_gnn_file(file_path)
assert isinstance(parsed_data, dict), 'Parsed data should be a dictionary'
assert 'ModelName' in parsed_data, 'Parsed data should contain ModelName'
assert isinstance(parsed_data.get('StateSpaceBlock', {}), dict), 'StateSpaceBlock should be a dictionary'
assert isinstance(parsed_data.get('Connections', []), list), 'Connections should be a list'
logging.info(f'Successfully parsed {file_path.name}')
except Exception as e:
logging.warning(f'Failed to parse {file_path.name}: {e}')
@pytest.mark.unit
def test_gnn_validation(self, sample_gnn_files):
"""Test GNN structure validation (simplified for speed)."""
for file_path in sample_gnn_files.values():
try:
with open(file_path, 'r') as f:
content = f.read()
has_model_name = '## ModelName' in content
has_gnn_version = '## GNNVersionAndFlags' in content
has_structure = has_model_name or has_gnn_version
assert isinstance(has_structure, bool), 'Validation should return boolean'
if file_path.name != 'invalid.md':
assert has_structure, f'Valid GNN file should have basic structure: {file_path.name}'
logging.info(f'Validation result for {file_path.name}: {has_structure}')
except Exception as e:
logging.warning(f'Validation check failed for {file_path.name}: {e}')
pytest.skip(f'Validation test skipped due to: {e}')
class TestRenderModuleComprehensive:
"""Comprehensive tests for the render module."""
@pytest.mark.unit
def test_render_module_imports(self):
"""Test that render module can be imported and has expected structure."""
from src.render import process_render, render_gnn_to_activeinference_jl, render_gnn_to_discopy, render_gnn_to_pymdp, render_gnn_to_rxinfer
assert callable(render_gnn_to_pymdp), 'render_gnn_to_pymdp should be callable'
assert callable(render_gnn_to_rxinfer), 'render_gnn_to_rxinfer should be callable'
assert callable(render_gnn_to_discopy), 'render_gnn_to_discopy should be callable'
assert callable(render_gnn_to_activeinference_jl), 'render_gnn_to_activeinference_jl should be callable'
assert callable(process_render), 'process_render should be callable'
logging.info('Render module imports validated')
@pytest.mark.unit
def test_pymdp_rendering(self, sample_gnn_files, isolated_temp_dir):
"""Test PyMDP code rendering."""
from src.render import render_gnn_to_pymdp
output_path = isolated_temp_dir / 'test_pymdp.py'
try:
render_gnn_to_pymdp(sample_gnn_files, output_path)
assert output_path.exists(), 'PyMDP output file should be created'
content = output_path.read_text()
assert len(content) > 0, 'PyMDP output should not be empty'
assert 'import' in content, 'PyMDP output should contain imports'
logging.info('PyMDP rendering validated')
except Exception as e:
logging.warning(f'PyMDP rendering failed: {e}')
@pytest.mark.unit
def test_rxinfer_rendering(self, sample_gnn_files, isolated_temp_dir):
"""Test RxInfer code rendering with POMDP structure validation."""
from src.render import render_gnn_to_rxinfer
output_path = isolated_temp_dir / 'test_rxinfer.jl'
try:
render_gnn_to_rxinfer(sample_gnn_files, output_path)
assert output_path.exists(), 'RxInfer output file should be created'
content = output_path.read_text()
assert len(content) > 0, 'RxInfer output should not be empty'
assert 'NUM_STATES' in content, 'RxInfer should define NUM_STATES'
assert 'NUM_OBSERVATIONS' in content, 'RxInfer should define NUM_OBSERVATIONS'
assert 'NUM_ACTIONS' in content, 'RxInfer should define NUM_ACTIONS for POMDP'
import re
actions_match = re.search('NUM_ACTIONS\\s*=\\s*(\\d+)', content)
if actions_match:
num_actions = int(actions_match.group(1))
assert num_actions >= 1, f'NUM_ACTIONS should be >= 1, got {num_actions}'
logging.info(f'RxInfer POMDP validated: {num_actions} actions')
assert 'B_matrix' in content or 'B' in content, 'RxInfer should define B matrix'
logging.info('RxInfer POMDP rendering validated')
except Exception as e:
logging.warning(f'RxInfer rendering failed: {e}')
@pytest.mark.unit
def test_discopy_rendering(self, sample_gnn_files):
"""Test DisCoPy rendering functionality."""
from src.render import render_gnn_to_discopy
sample_spec = {'model_name': 'TestModel', 'variables': [{'name': 'A', 'dimensions': [2, 2]}], 'model_parameters': {}, 'initial_parameterization': {}, 'connections': []}
import tempfile
with tempfile.TemporaryDirectory() as td:
output_path = Path(td) / 'discopy_diagram.py'
result = render_gnn_to_discopy(sample_spec, output_path)
assert isinstance(result, tuple), 'render_gnn_to_discopy should return a tuple'
assert len(result) == 3, 'render_gnn_to_discopy should return (success, message, warnings)'
assert result[0] is True, 'render_gnn_to_discopy should succeed'
assert output_path.exists(), 'Output file should be created'
class TestExecuteModuleComprehensive:
"""Comprehensive tests for the execute module."""
@pytest.mark.unit
def test_execute_module_imports(self):
"""Test that execute module can be imported and has expected functions."""
from src.execute import GNNExecutor, PyMDPSimulation, process_execute, validate_execution_environment
assert GNNExecutor is not None, 'GNNExecutor should be available'
assert PyMDPSimulation is not None, 'PyMDPSimulation should be available'
assert callable(process_execute), 'process_execute should be callable'
assert callable(validate_execution_environment), 'validate_execution_environment should be callable'
@pytest.mark.unit
def test_execution_environment_validation(self):
"""Test execution environment validation."""
from src.execute import validate_execution_environment
try:
env_status = validate_execution_environment()
assert isinstance(env_status, dict), 'Environment status should be a dictionary'
assert 'python_version' in env_status, 'Status should contain python_version'
assert 'dependencies' in env_status, 'Status should contain dependencies'
logging.info('Execution environment validation completed')
except Exception as e:
logging.warning(f'Execution environment validation failed: {e}')
@pytest.mark.unit
def test_safe_script_execution(self, isolated_temp_dir):
"""Test safe script execution functionality."""
from src.execute import GNNExecutor
test_script = isolated_temp_dir / 'test_script.py'
test_script.write_text("print('Hello from test script!')")
engine = GNNExecutor()
assert engine is not None, 'GNNExecutor should be instantiable'
class TestLLMModuleComprehensive:
"""Comprehensive tests for the LLM module."""
@pytest.mark.unit
def test_llm_module_imports(self):
"""Test that LLM module can be imported and has expected functions."""
from src.llm import analyze_gnn_file_with_llm, generate_code_suggestions, generate_model_insights, process_llm
assert callable(process_llm), 'process_llm should be callable'
assert callable(analyze_gnn_file_with_llm), 'analyze_gnn_file_with_llm should be callable'
assert callable(generate_model_insights), 'generate_model_insights should be callable'
assert callable(generate_code_suggestions), 'generate_code_suggestions should be callable'
@pytest.mark.unit
@pytest.mark.slow
def test_llm_model_analysis(self, sample_gnn_files):
"""Test LLM-based model analysis functionality."""
from src.llm import analyze_gnn_file_with_llm
for file_path in sample_gnn_files.values():
analysis = analyze_gnn_file_with_llm(file_path, verbose=False)
assert isinstance(analysis, dict), 'Analysis should return a dict'
assert 'file_path' in analysis, 'Analysis should contain file_path'
break
@pytest.mark.unit
def test_llm_description_generation(self, sample_gnn_files):
"""Test LLM description generation functionality."""
from src.llm import generate_documentation
sample_analysis = {'file_path': 'test.md', 'file_name': 'test.md', 'semantic_analysis': {'model_type': 'POMDP', 'complexity_level': 'simple'}, 'complexity_metrics': {'variable_count': 3, 'connection_count': 2}, 'variables': [{'name': 'X', 'line': 1}, {'name': 'Y', 'line': 2}]}
docs = generate_documentation(sample_analysis)
assert isinstance(docs, dict), 'Documentation should return a dict'
assert 'file_path' in docs, 'Documentation should contain file_path'
class TestMCPModuleComprehensive:
"""Comprehensive tests for the MCP module."""
@pytest.mark.unit
def test_mcp_module_imports(self):
"""Test that MCP module can be imported and has expected structure."""
from src.mcp import generate_mcp_report, get_available_tools, handle_mcp_request
from src.mcp import register_module_tools as register_tools
assert callable(register_tools), 'register_tools should be callable'
assert callable(get_available_tools), 'get_available_tools should be callable'
assert callable(handle_mcp_request), 'handle_mcp_request should be callable'
assert callable(generate_mcp_report), 'generate_mcp_report should be callable'
logging.info('MCP module imports validated')
@pytest.mark.unit
def test_mcp_tool_registration(self):
"""Test MCP tool registration."""
from src.mcp import get_available_tools
from src.mcp import register_module_tools as register_tools
try:
tools = register_tools()
assert isinstance(tools, list), 'Tools should be a list'
assert len(tools) > 0, 'Should register at least one tool'
available_tools = get_available_tools()
assert isinstance(available_tools, list), 'Available tools should be a list'
logging.info('MCP tool registration validated')
except Exception as e:
logging.warning(f'MCP tool registration failed: {e}')
@pytest.mark.unit
def test_mcp_request_handling(self):
"""Test MCP request handling."""
from src.mcp import handle_mcp_request
sample_request = {'method': 'tools/list', 'params': {}, 'id': 1}
try:
response = handle_mcp_request(sample_request)
assert isinstance(response, dict), 'Response should be a dictionary'
assert 'id' in response, 'Response should contain id'
logging.info('MCP request handling validated')
except Exception as e:
logging.warning(f'MCP request handling failed: {e}')
class TestOntologyModuleComprehensive:
"""Comprehensive tests for the ontology module."""
@pytest.mark.unit
def test_ontology_module_imports(self):
"""Test that ontology module can be imported and has expected functions."""
from src.ontology import FEATURES, process_ontology
assert callable(process_ontology), 'process_ontology should be callable'
assert isinstance(FEATURES, dict), 'FEATURES should be a dict'
assert FEATURES.get('basic_processing', False), 'Basic processing should be available'
@pytest.mark.unit
def test_ontology_term_validation(self, isolated_temp_dir):
"""Test ontology processing functionality."""
from src.ontology import process_ontology
input_dir = isolated_temp_dir / 'input'
input_dir.mkdir()
sample_file = input_dir / 'test_model.md'
sample_file.write_text('## GNNVersionAndFlags\nVersion: 1.0\n\n## ModelName\nTestModel\n\n## Variables\n- X: [2]\n')
output_dir = isolated_temp_dir / 'output'
result = process_ontology(input_dir, output_dir, verbose=False)
assert isinstance(result, bool), 'process_ontology should return a boolean'
assert (output_dir / 'ontology_results.json').exists(), 'Results file should be created'
class TestWebsiteModuleComprehensive:
"""Comprehensive tests for the website module."""
@pytest.mark.unit
def test_website_module_imports(self):
"""Test that website module can be imported and has expected functions."""
from src.website import FEATURES, process_website
assert callable(process_website), 'process_website should be callable'
assert isinstance(FEATURES, dict), 'FEATURES should be a dict'
assert FEATURES.get('basic_processing', False), 'Basic processing should be available'
@pytest.mark.unit
def test_website_generation(self, isolated_temp_dir):
"""Test website generation functionality."""
from src.website import process_website
input_dir = isolated_temp_dir / 'input'
input_dir.mkdir()
sample_file = input_dir / 'test_model.md'
sample_file.write_text('## GNNVersionAndFlags\nVersion: 1.0\n\n## ModelName\nTestModel\n\n## Variables\n- X: [2]\n')
output_dir = isolated_temp_dir / 'output'
result = process_website(input_dir, output_dir, verbose=False)
assert isinstance(result, bool), 'process_website should return a boolean'
assert (output_dir / 'index.html').exists(), 'Index file should be created'
@pytest.mark.unit
def test_html_report_creation(self, isolated_temp_dir):
"""Test HTML report creation functionality."""
from src.website import process_website
input_dir = isolated_temp_dir / 'input'
input_dir.mkdir()
for i in range(3):
sample_file = input_dir / f'test_model_{i}.md'
sample_file.write_text(f'## GNNVersionAndFlags\nVersion: 1.0\n\n## ModelName\nTestModel{i}\n\n## Variables\n- X: [{i + 1}]\n')
output_dir = isolated_temp_dir / 'output'
result = process_website(input_dir, output_dir, verbose=False)
assert isinstance(result, bool), 'process_website should return a boolean'
results_dir = output_dir
assert results_dir.exists(), 'Results directory should be created'
results_file = results_dir / 'website_results.json'
assert results_file.exists(), 'Results file should be created'
class TestSAPFModuleComprehensive:
"""Comprehensive tests for the SAPF module."""
@pytest.mark.unit
def test_sapf_module_imports(self):
"""Test that SAPF module can be imported and has expected structure."""
from src.audio.sapf import convert_gnn_to_sapf, create_sapf_visualization, generate_sapf_audio, generate_sapf_report, validate_sapf_code
assert callable(convert_gnn_to_sapf), 'convert_gnn_to_sapf should be callable'
assert callable(generate_sapf_audio), 'generate_sapf_audio should be callable'
assert callable(validate_sapf_code), 'validate_sapf_code should be callable'
logging.info('SAPF module imports validated successfully')
@pytest.mark.unit
def test_gnn_to_sapf_conversion(self):
"""Test GNN to SAPF conversion functionality."""
sample_gnn_files = '\n## ModelName\nTestActiveInferenceModel\n\n## StateSpaceBlock\ns1: State\ns2: State\ns3: State\n\n## Connections\ns1 -> s2: Transition\ns2 -> s3: Transition\ns3 -> s1: Transition\n\n## InitialParameterization\nA: [0.8, 0.2; 0.3, 0.7]\nB: [0.9, 0.1; 0.2, 0.8]\nC: [0.7, 0.3; 0.4, 0.6]\n'
try:
from src.audio.sapf import convert_gnn_to_sapf
except ImportError:
pytest.skip('SAPF module not available')
try:
sapf_code = convert_gnn_to_sapf(sample_gnn_files, model_name='TestActiveInferenceModel')
assert isinstance(sapf_code, str), 'SAPF code should be a string'
assert len(sapf_code) > 0, 'SAPF code should not be empty'
logging.info('GNN to SAPF conversion validated')
except Exception as e:
logging.warning(f'GNN to SAPF conversion failed: {e}')
pytest.skip(f'SAPF conversion not available: {e}')
@pytest.mark.unit
def test_sapf_validation(self):
"""Test SAPF code validation functionality."""
sample_sapf_code = '\n; Test SAPF code\n261.63 = base_freq\nbase_freq 0 sinosc 0.3 * = osc1\n10 sec 0.1 1 0.8 0.2 env = envelope\nosc1 envelope * = final_audio\nfinal_audio play\n'
try:
from src.audio.sapf import validate_sapf_code
except ImportError:
pytest.skip('SAPF validation not available')
try:
is_valid, issues = validate_sapf_code(sample_sapf_code)
assert isinstance(is_valid, bool), 'Validation should return boolean'
assert isinstance(issues, list), 'Issues should be a list'
logging.info('SAPF validation functionality confirmed')
except Exception as e:
logging.warning(f'SAPF validation failed: {e}')
pytest.skip(f'SAPF validation not available: {e}')
@pytest.mark.unit
def test_sapf_audio_generation(self):
"""Test SAPF audio generation functionality."""
try:
from src.audio.sapf import generate_sapf_audio
except ImportError:
pytest.skip('SAPF audio generation not available')
try:
assert callable(generate_sapf_audio), 'generate_sapf_audio should be callable'
logging.info('SAPF audio generation functionality confirmed')
except Exception as e:
logging.warning(f'SAPF audio generation test failed: {e}')
pytest.skip(f'SAPF audio generation not available: {e}')
class TestCoreModuleIntegration:
"""Integration tests for core module coordination."""
@pytest.mark.integration
def test_module_coordination(self, sample_gnn_files, isolated_temp_dir):
"""Test coordination between core modules."""
try:
from src.execute import execute_gnn_model
from src.gnn import parse_gnn_file
from src.render import render_gnn_to_pymdp
gnn_data = parse_gnn_file(list(sample_gnn_files.values())[0])
pymdp_path = isolated_temp_dir / 'test_pymdp.py'
render_gnn_to_pymdp({list(sample_gnn_files.values())[0]: gnn_data}, pymdp_path)
result = execute_gnn_model(f'python {pymdp_path}', timeout=10)
assert isinstance(result, dict), 'Execution result should be a dictionary'
logging.info('Core module coordination validated')
except Exception as e:
logging.warning(f'Core module coordination failed: {e}')
@pytest.mark.integration
def test_module_data_flow(self, sample_gnn_files, isolated_temp_dir):
"""Test data flow between modules."""
try:
from src.gnn import parse_gnn_file
from src.llm import analyze_gnn_model
from src.website import generate_html_report
gnn_data = parse_gnn_file(list(sample_gnn_files.values())[0])
analysis = analyze_gnn_model(gnn_data)
report_path = isolated_temp_dir / 'test_report.html'
generate_html_report(analysis, report_path)
assert report_path.exists(), 'Report should be created'
logging.info('Module data flow validated')
except Exception as e:
logging.warning(f'Module data flow failed: {e}')
def test_core_module_completeness():
"""Test that all core modules are complete and functional."""
core_modules = ['gnn', 'render', 'execute', 'validation', 'visualization']
imported = []
for module_name in core_modules:
try:
module = __import__(module_name)
imported.append(module_name)
assert hasattr(module, '__version__') or hasattr(module, 'FEATURES'), f'Module {module_name} missing __version__ or FEATURES'
except ImportError:
pass
assert len(imported) >= 3, f'Expected at least 3 core modules, got {len(imported)}: {imported}'
logging.info(f'Core module completeness: {len(imported)}/{len(core_modules)} modules available')
@pytest.mark.slow
def test_core_module_performance():
"""Test performance characteristics of core modules."""
import time
modules_to_time = ['gnn', 'render', 'validation']
for module_name in modules_to_time:
start = time.time()
try:
__import__(module_name)
elapsed = time.time() - start
assert elapsed < 2.0, f'Module {module_name} import took {elapsed:.2f}s'
except ImportError:
pass
logging.info('Core module performance test completed')