-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathtest_recommender.py
More file actions
181 lines (142 loc) · 6.25 KB
/
Copy pathtest_recommender.py
File metadata and controls
181 lines (142 loc) · 6.25 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
# test_recommender.py
# Run from the repo root with: python test_recommender.py
import sys
import os
# Make sure imports resolve from the repo root regardless of where Python
# looks by default.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from utils.recommender import (
get_recommendations,
validate_recommendation_inputs,
_get_related,
_load_clusters,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def passed(label):
print(f" PASS {label}")
def failed(label, detail):
print(f" FAIL {label}")
print(f" {detail}")
def section(title):
print(f"\n{title}")
print("-" * len(title))
# ---------------------------------------------------------------------------
# Validation
# ---------------------------------------------------------------------------
section("Input validation")
errors = validate_recommendation_inputs("", "Beginner", "Data", "Low")
if errors:
passed("empty skills caught")
else:
failed("empty skills caught", "expected an error, got none")
errors = validate_recommendation_inputs("Python", "", "Data", "Low")
if errors:
passed("empty level caught")
else:
failed("empty level caught", "expected an error, got none")
errors = validate_recommendation_inputs("Python", "Beginner", "Data", "Low")
if not errors:
passed("valid inputs pass through cleanly")
else:
failed("valid inputs pass through cleanly", f"unexpected errors: {errors}")
# ---------------------------------------------------------------------------
# Return shape
# ---------------------------------------------------------------------------
section("Return shape")
result = get_recommendations("Python", "Beginner", "Data", "Low")
if isinstance(result, dict):
passed("get_recommendations returns a dict")
else:
failed("get_recommendations returns a dict", f"got {type(result)}")
if "recommendations" in result:
passed("dict has 'recommendations' key")
else:
failed("dict has 'recommendations' key", f"keys found: {list(result.keys())}")
if "related" in result:
passed("dict has 'related' key")
else:
failed("dict has 'related' key", f"keys found: {list(result.keys())}")
# ---------------------------------------------------------------------------
# Recommendations list
# ---------------------------------------------------------------------------
section("Recommendations")
recs = result["recommendations"]
if isinstance(recs, list):
passed(f"recommendations is a list ({len(recs)} result(s))")
else:
failed("recommendations is a list", f"got {type(recs)}")
if len(recs) <= 3:
passed(f"respects MAX_RESULTS cap (got {len(recs)})")
else:
failed("respects MAX_RESULTS cap", f"got {len(recs)} results")
required_fields = {"id", "title", "skills", "level", "interest", "time"}
all_valid = all(required_fields.issubset(p.keys()) for p in recs)
if all_valid:
passed("all results have required fields")
else:
failed("all results have required fields", "one or more fields missing")
# High time should return >= results as Low (it opens up more projects)
high_recs = get_recommendations("Python", "Beginner", "Data", "High")["recommendations"]
low_recs = get_recommendations("Python", "Beginner", "Data", "Low")["recommendations"]
if len(high_recs) >= len(low_recs):
passed("High time availability returns >= results than Low")
else:
failed("High time availability returns >= results than Low",
f"High={len(high_recs)}, Low={len(low_recs)}")
# Nonsense input should return empty recommendations, not crash
junk = get_recommendations("cobol_fortran_brainfuck", "Expert", "Knitting", "Low")["recommendations"]
if isinstance(junk, list) and len(junk) == 0:
passed("no-match input returns empty recommendations")
else:
failed("no-match input returns empty recommendations", f"got: {junk}")
# ---------------------------------------------------------------------------
# Skill alias normalisation
# ---------------------------------------------------------------------------
section("Skill alias normalisation")
js_results = get_recommendations("js", "Beginner", "Web", "Low")["recommendations"]
full_results = get_recommendations("javascript", "Beginner", "Web", "Low")["recommendations"]
if js_results == full_results:
passed("'js' alias resolves to 'javascript'")
else:
failed("'js' alias resolves to 'javascript'",
f"js={[p['title'] for p in js_results]}, "
f"javascript={[p['title'] for p in full_results]}")
# ---------------------------------------------------------------------------
# Related projects (soft — skipped if clusters.json missing)
# ---------------------------------------------------------------------------
section("Related projects (requires clusters.json)")
clusters_path = os.path.join("data", "clusters.json")
if not os.path.exists(clusters_path):
print(" SKIP clusters.json not found — run: python scripts/cluster_projects.py")
else:
cluster_data = _load_clusters()
all_projects = __import__(
"utils.data_loader", fromlist=["load_all_projects"]
).load_all_projects()
rec_result = get_recommendations("Python", "Beginner", "Data", "Low")
recs = rec_result["recommendations"]
related = rec_result["related"]
if isinstance(related, list):
passed(f"related is a list ({len(related)} result(s))")
else:
failed("related is a list", f"got {type(related)}")
if len(related) <= 3:
passed(f"related respects MAX_RELATED cap (got {len(related)})")
else:
failed("related respects MAX_RELATED cap", f"got {len(related)}")
if recs:
rec_ids = [p["id"] for p in recs]
overlap = [p for p in related if p["id"] in rec_ids]
if not overlap:
passed("related projects don't repeat recommended ones")
else:
failed("related projects don't repeat recommended ones",
f"overlap: {[p['title'] for p in overlap]}")
else:
print(" SKIP no recommendations returned, skipping overlap check")
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
print("\nDone.\n")