-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
336 lines (274 loc) · 10.7 KB
/
validate.py
File metadata and controls
336 lines (274 loc) · 10.7 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
#!/usr/bin/env python3
"""
Comprehensive validation script for design patterns tutorial.
This script validates notebooks, source code, and tests.
"""
import json
import os
import sys
import subprocess
import importlib.util
from pathlib import Path
from typing import Dict, List, Tuple, Any
# Add learning-resources path for imports
sys.path.insert(0, str(Path(__file__).parent / "learning-resources" / "examples" / "implementations"))
def validate_notebooks() -> Tuple[bool, List[str]]:
"""Validate all Jupyter notebooks."""
print("🔍 Validating Jupyter notebooks...")
errors = []
notebooks_dir = Path("learning-resources/notebooks")
if not notebooks_dir.exists():
errors.append("notebooks directory does not exist")
return False, errors
expected_notebooks = [
"01_singleton_pattern.ipynb",
"02_factory_pattern.ipynb",
"03_observer_pattern.ipynb",
"04_strategy_pattern.ipynb",
"05_decorator_pattern.ipynb",
"06_command_pattern.ipynb",
"07_repository_pattern.ipynb",
"08_builder_pattern.ipynb",
"09_adapter_pattern.ipynb",
"10_state_pattern.ipynb"
]
for notebook_file in expected_notebooks:
notebook_path = notebooks_dir / notebook_file
if not notebook_path.exists():
errors.append(f"Missing notebook: {notebook_file}")
continue
try:
with open(notebook_path, 'r', encoding='utf-8') as f:
notebook = json.load(f)
# Check required fields
if 'cells' not in notebook:
errors.append(f"{notebook_file}: Missing 'cells' field")
continue
# Check for title and TOC
has_title = False
has_toc = False
for cell in notebook['cells']:
if cell['cell_type'] == 'markdown':
source = ''.join(cell['source']) if isinstance(cell['source'], list) else cell['source']
if source.startswith('# ') and 'Pattern Tutorial' in source:
has_title = True
if 'Table of Contents' in source:
has_toc = True
if not has_title:
errors.append(f"{notebook_file}: Missing proper title")
if not has_toc:
errors.append(f"{notebook_file}: Missing table of contents")
except json.JSONDecodeError as e:
errors.append(f"{notebook_file}: Invalid JSON - {e}")
except Exception as e:
errors.append(f"{notebook_file}: Error reading file - {e}")
success = len(errors) == 0
if success:
print(f"✅ All {len(expected_notebooks)} notebooks are valid")
else:
print(f"❌ Found {len(errors)} notebook validation errors")
return success, errors
def validate_source_code() -> Tuple[bool, List[str]]:
"""Validate source code implementations."""
print("🔍 Validating source code implementations...")
errors = []
src_dir = Path("learning-resources/examples/implementations")
patterns_dir = src_dir / "patterns"
if not patterns_dir.exists():
errors.append("learning-resources/examples/implementations/patterns directory does not exist")
return False, errors
expected_patterns = [
"singleton.py",
"factory.py",
"observer.py",
"strategy.py",
"decorator.py",
"command.py",
"repository.py",
"builder.py",
"adapter.py",
"state.py"
]
for pattern_file in expected_patterns:
pattern_path = patterns_dir / pattern_file
if not pattern_path.exists():
errors.append(f"Missing pattern implementation: {pattern_file}")
continue
try:
# Try to import the module
spec = importlib.util.spec_from_file_location(
f"patterns.{pattern_file[:-3]}",
pattern_path
)
if spec is None:
errors.append(f"{pattern_file}: Could not create module spec")
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception as e:
errors.append(f"{pattern_file}: Import error - {e}")
# Test basic functionality
try:
from patterns import Singleton, ComputerBuilder
# Test singleton
class TestSingleton(Singleton):
pass
s1 = TestSingleton()
s2 = TestSingleton()
if s1 is not s2:
errors.append("Singleton pattern not working correctly")
# Test builder
computer = (ComputerBuilder()
.set_cpu("Intel i7")
.set_memory("16GB")
.build())
if not hasattr(computer, 'cpu') or computer.cpu != "Intel i7":
errors.append("Builder pattern not working correctly")
except Exception as e:
errors.append(f"Pattern functionality test failed: {e}")
success = len(errors) == 0
if success:
print(f"✅ All {len(expected_patterns)} pattern implementations are valid")
else:
print(f"❌ Found {len(errors)} source code validation errors")
return success, errors
def validate_tests() -> Tuple[bool, List[str]]:
"""Validate test files."""
print("🔍 Validating test files...")
errors = []
tests_dir = Path("learning-resources/examples/tests") / "test_patterns"
if not tests_dir.exists():
errors.append("learning-resources/examples/tests/test_patterns directory does not exist")
return False, errors
expected_tests = [
"test_singleton.py",
"test_factory.py",
"test_observer.py",
"test_strategy.py",
"test_decorator.py",
"test_command.py",
"test_repository.py",
"test_builder.py",
"test_adapter.py",
"test_state.py"
]
for test_file in expected_tests:
test_path = tests_dir / test_file
if not test_path.exists():
errors.append(f"Missing test file: {test_file}")
continue
try:
# Check if file contains test functions
with open(test_path, 'r', encoding='utf-8') as f:
content = f.read()
if 'def test_' not in content:
errors.append(f"{test_file}: No test functions found")
except Exception as e:
errors.append(f"{test_file}: Error reading file - {e}")
success = len(errors) == 0
if success:
print(f"✅ All {len(expected_tests)} test files are present")
else:
print(f"❌ Found {len(errors)} test validation errors")
return success, errors
def validate_docker() -> Tuple[bool, List[str]]:
"""Validate Docker configuration."""
print("🔍 Validating Docker configuration...")
errors = []
# Check Dockerfile exists
dockerfile_path = Path("Dockerfile")
if not dockerfile_path.exists():
errors.append("Dockerfile does not exist")
# Check docker-compose.yml exists
compose_path = Path("docker-compose.yml")
if not compose_path.exists():
errors.append("docker-compose.yml does not exist")
# Check requirements files exist
req_path = Path("requirements.txt")
if not req_path.exists():
errors.append("requirements.txt does not exist")
req_dev_path = Path("requirements-dev.txt")
if not req_dev_path.exists():
errors.append("requirements-dev.txt does not exist")
success = len(errors) == 0
if success:
print("✅ Docker configuration is valid")
else:
print(f"❌ Found {len(errors)} Docker configuration errors")
return success, errors
def validate_project_structure() -> Tuple[bool, List[str]]:
"""Validate overall project structure."""
print("🔍 Validating project structure...")
errors = []
expected_dirs = [
"learning-resources/examples/implementations",
"learning-resources/examples/implementations/patterns",
"learning-resources/examples/tests",
"learning-resources/examples/tests/test_patterns",
"learning-resources/notebooks",
"learning-resources/guides",
"ai-engine",
"commands",
"data"
]
for dir_path in expected_dirs:
if not Path(dir_path).exists():
errors.append(f"Missing directory: {dir_path}")
expected_files = [
"README.md",
"pyproject.toml",
"requirements.txt",
"requirements-dev.txt",
"Dockerfile",
"docker-compose.yml",
"CLAUDE.md",
"learning-resources/examples/implementations/__init__.py",
"learning-resources/examples/implementations/patterns/__init__.py",
"learning-resources/examples/tests/__init__.py",
"learning-resources/examples/tests/conftest.py",
"QUICK_START.md"
]
for file_path in expected_files:
if not Path(file_path).exists():
errors.append(f"Missing file: {file_path}")
success = len(errors) == 0
if success:
print("✅ Project structure is valid")
else:
print(f"❌ Found {len(errors)} project structure errors")
return success, errors
def main():
"""Run comprehensive validation."""
print("🚀 Starting comprehensive validation of Design Patterns Tutorial")
print("=" * 60)
all_success = True
all_errors = []
# Run all validations
validations = [
("Project Structure", validate_project_structure),
("Notebooks", validate_notebooks),
("Source Code", validate_source_code),
("Tests", validate_tests),
("Docker", validate_docker)
]
for name, validation_func in validations:
success, errors = validation_func()
if not success:
all_success = False
all_errors.extend([f"{name}: {error}" for error in errors])
print()
# Summary
print("=" * 60)
if all_success:
print("🎉 All validations passed! The tutorial is ready to use.")
print("\nNext steps:")
print("1. Run tests: docker compose --profile test up test-runner")
print("2. Start Jupyter: docker compose up jupyter")
print("3. Access at: http://localhost:8888/tree?token=design-patterns-2025")
else:
print("❌ Validation failed with the following errors:")
for error in all_errors:
print(f" - {error}")
sys.exit(1)
if __name__ == "__main__":
main()