askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
test_tracker.py · 3.3 KB · 107 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Test Tracker using dbbasic-tsv |
| 4 | Tracks which components have been tested and when, to avoid re-testing unchanged files |
| 5 | """ |
| 6 | |
| 7 | import hashlib |
| 8 | from pathlib import Path |
| 9 | from datetime import datetime |
| 10 | from dbbasic.tsv import TSV |
| 11 | |
| 12 | # Initialize database |
| 13 | DB_PATH = Path("test-results") |
| 14 | DB_PATH.mkdir(exist_ok=True) |
| 15 | |
| 16 | # Initialize TSV table |
| 17 | db = TSV("test-tracker", ["component", "file_hash", "last_tested", "passed", "issues_count"], data_dir=DB_PATH) |
| 18 | |
| 19 | def get_file_hash(file_path): |
| 20 | """Calculate SHA256 hash of a file""" |
| 21 | sha256 = hashlib.sha256() |
| 22 | with open(file_path, "rb") as f: |
| 23 | for chunk in iter(lambda: f.read(4096), b""): |
| 24 | sha256.update(chunk) |
| 25 | return sha256.hexdigest() |
| 26 | |
| 27 | def needs_testing(component_name): |
| 28 | """Check if a component needs testing (file changed or never tested)""" |
| 29 | file_path = Path(f"docs/{component_name}.html") |
| 30 | |
| 31 | if not file_path.exists(): |
| 32 | return False, "file not found" |
| 33 | |
| 34 | current_hash = get_file_hash(file_path) |
| 35 | |
| 36 | # Query database for this component |
| 37 | result = db.query_one(component=component_name) |
| 38 | |
| 39 | if not result: |
| 40 | return True, "never tested" |
| 41 | |
| 42 | stored_hash = result["file_hash"] |
| 43 | |
| 44 | if current_hash != stored_hash: |
| 45 | return True, f"file changed (was {stored_hash[:8]}..., now {current_hash[:8]}...)" |
| 46 | |
| 47 | return False, f"unchanged since {result['last_tested']}" |
| 48 | |
| 49 | def record_test_result(component_name, passed, issues_count): |
| 50 | """Record test result in database""" |
| 51 | file_path = Path(f"docs/{component_name}.html") |
| 52 | file_hash = get_file_hash(file_path) |
| 53 | timestamp = datetime.now().isoformat() |
| 54 | |
| 55 | # Delete old record if exists |
| 56 | existing = db.query_one(component=component_name) |
| 57 | if existing: |
| 58 | db.delete(component=component_name) |
| 59 | |
| 60 | # Insert new record |
| 61 | db.insert({ |
| 62 | "component": component_name, |
| 63 | "file_hash": file_hash, |
| 64 | "last_tested": timestamp, |
| 65 | "passed": "true" if passed else "false", |
| 66 | "issues_count": str(issues_count) |
| 67 | }) |
| 68 | |
| 69 | def get_test_status(component_name): |
| 70 | """Get last test status for a component""" |
| 71 | return db.query_one(component=component_name) |
| 72 | |
| 73 | def list_all_status(): |
| 74 | """List test status for all components""" |
| 75 | try: |
| 76 | return list(db.all()) |
| 77 | except: |
| 78 | return [] |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | import sys |
| 82 | |
| 83 | if len(sys.argv) < 2: |
| 84 | print("Usage: python3 test_tracker.py <component-name>") |
| 85 | print(" python3 test_tracker.py --list") |
| 86 | sys.exit(1) |
| 87 | |
| 88 | if sys.argv[1] == "--list": |
| 89 | print("\n📊 Test Status for All Components:\n") |
| 90 | statuses = list_all_status() |
| 91 | if not statuses: |
| 92 | print("No components tested yet.") |
| 93 | else: |
| 94 | for status in statuses: |
| 95 | passed = "✅" if status["passed"] == "true" else "❌" |
| 96 | print(f"{passed} {status['component']:25s} | Issues: {status['issues_count']:2s} | Tested: {status['last_tested']}") |
| 97 | else: |
| 98 | component = sys.argv[1] |
| 99 | needs_test, reason = needs_testing(component) |
| 100 | |
| 101 | if needs_test: |
| 102 | print(f"🔄 {component} needs testing: {reason}") |
| 103 | else: |
| 104 | print(f"✓ {component} does not need testing: {reason}") |
| 105 | status = get_test_status(component) |
| 106 | if status: |
| 107 | print(f" Last result: {'PASSED' if status['passed'] == 'true' else 'FAILED'} with {status['issues_count']} issues") |