File size: 6,582 Bytes
9df97a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Quick API test examples for semantic matching
Run this after starting the FastAPI server
"""

import requests
import json
from typing import Dict, Any

BASE_URL = "http://localhost:8000/api/matching"

def print_response(title: str, response: requests.Response):
    """Pretty print API response"""
    print(f"\n{'='*60}")
    print(f"πŸ“ {title}")
    print(f"{'='*60}")
    print(f"Status: {response.status_code}")
    try:
        data = response.json()
        print(json.dumps(data, indent=2, ensure_ascii=False))
    except:
        print(response.text)


def test_create_criteria():
    """Test: Create job criteria"""
    print("\nπŸ§ͺ TEST 1: Create Job Criteria")
    
    payload = {
        "title": "Senior Python Developer",
        "description": "We need an experienced Python developer with Django and PostgreSQL expertise",
        "mode": "search",
        "required_skills": [
            {"name": "Python", "weight": 100},
            {"name": "Django", "weight": 80},
            {"name": "PostgreSQL", "weight": 70},
            {"name": "REST API", "weight": 60},
            {"name": "Docker", "weight": 50}
        ]
    }
    
    response = requests.post(f"{BASE_URL}/criteria", json=payload)
    print_response("Create Job Criteria", response)
    
    if response.status_code == 200:
        criteria_id = response.json().get("id")
        return criteria_id
    return None


def test_search_candidates(criteria_id: int):
    """Test: Search candidates matching criteria"""
    print("\nπŸ§ͺ TEST 2: Search Candidates (Semantic Matching)")
    
    if not criteria_id:
        print("⚠️  Skip: No criteria_id from previous test")
        return
    
    response = requests.post(f"{BASE_URL}/search/{criteria_id}")
    print_response("Search Candidates", response)


def test_generate_profile():
    """Test: Generate ideal profile from job description"""
    print("\nπŸ§ͺ TEST 3: Generate Ideal Profile")
    
    payload = {
        "job_title": "Full Stack Engineer",
        "description": """
        Looking for a Full Stack Engineer who can:
        - Build scalable Python backends with Django/FastAPI
        - Create responsive frontend with React or Vue
        - Manage PostgreSQL databases
        - Deploy on AWS/Docker
        - Work with microservices architecture
        Required: 3+ years experience, Agile methodology
        """
    }
    
    response = requests.post(f"{BASE_URL}/generate-profile", json=payload)
    print_response("Generate Profile", response)


def test_generate_and_match():
    """Test: Generate profile and match against candidates"""
    print("\nπŸ§ͺ TEST 4: Generate & Match (Complete Workflow)")
    
    payload = {
        "job_title": "Full Stack Engineer",
        "description": """
        We need a Full Stack Engineer who is strong in:
        - Python/Django backend development
        - React frontend
        - PostgreSQL and Redis
        - Cloud deployment (AWS preferred)
        """
    }
    
    response = requests.post(f"{BASE_URL}/generate-and-match", json=payload)
    print_response("Generate & Match", response)


def test_semantic_similarity():
    """Test semantic similarity endpoint (if available)"""
    print("\nπŸ§ͺ TEST 5: Semantic Similarity (if endpoint exists)")
    
    payload = {
        "skill1": "Python",
        "skill2": "Django",
        "threshold": 0.6
    }
    
    # This endpoint might not exist yet - just try
    response = requests.post(f"{BASE_URL}/similarity", json=payload)
    if response.status_code != 404:
        print_response("Semantic Similarity", response)
    else:
        print("⚠️  Endpoint not available (can be created later)")


def main():
    """Run all test scenarios"""
    print("\n")
    print("β•”" + "═" * 58 + "β•—")
    print("β•‘" + " " * 58 + "β•‘")
    print("β•‘" + "  πŸš€ API Tests - Semantic Matching".center(58) + "β•‘")
    print("β•‘" + "  all-MiniLM-L6-v2 Integration".center(58) + "β•‘")
    print("β•‘" + " " * 58 + "β•‘")
    print("β•š" + "═" * 58 + "╝")
    print()
    
    print("βœ… Before running these tests:")
    print("   1. pip install -r requirements.txt")
    print("   2. python -m uvicorn app.main:app --reload")
    print("   3. Wait for server to start on http://localhost:8000")
    print()
    
    try:
        # Test API connectivity
        print("πŸ” Checking API connectivity...")
        response = requests.get("http://localhost:8000", timeout=2)
        print("   βœ“ API is reachable")
    except Exception as e:
        print(f"   βœ— API is not reachable: {e}")
        print("\n   Make sure the FastAPI server is running:")
        print("   python -m uvicorn app.main:app --reload")
        return
    
    try:
        # Run tests
        criteria_id = test_create_criteria()
        test_search_candidates(criteria_id)
        test_generate_profile()
        test_generate_and_match()
        test_semantic_similarity()
        
        print("\n")
        print("=" * 60)
        print("βœ… Test Suite Completed!")
        print("=" * 60)
        print("\nπŸ“Š Next Steps:")
        print("   1. Check the responses above for correctness")
        print("   2. Create test data with actual candidates")
        print("   3. Compare scoring before/after semantic matching")
        print("   4. Adjust threshold if needed: backend/app/api/matching.py")
        
    except Exception as e:
        print(f"\n❌ Error: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()


# ============================================================================
# CURL Examples - Use if you prefer command line
# ============================================================================
"""
# 1. Create criteria
curl -X POST http://localhost:8000/api/matching/criteria \\
  -H "Content-Type: application/json" \\
  -d '{
    "title": "Senior Python Developer",
    "description": "Expert needed in Python, Django, PostgreSQL",
    "mode": "search",
    "required_skills": [
      {"name": "Python", "weight": 100},
      {"name": "Django", "weight": 80},
      {"name": "PostgreSQL", "weight": 70}
    ]
  }'

# 2. Search candidates (replace 1 with criteria_id)
curl -X POST http://localhost:8000/api/matching/search/1

# 3. Generate and match
curl -X POST http://localhost:8000/api/matching/generate-and-match \\
  -H "Content-Type: application/json" \\
  -d '{
    "job_title": "Senior Python Developer",
    "description": "Besoin expert Python, Django, PostgreSQL, Docker"
  }'
"""