-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.js
More file actions
60 lines (50 loc) · 2.1 KB
/
test_api.js
File metadata and controls
60 lines (50 loc) · 2.1 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
const axios = require('axios');
const BASE_URL = 'http://localhost:3000';
// Test data
const testSchool = {
name: "Test School API",
address: "Test Address, Test City, India",
latitude: 28.7041,
longitude: 77.1025
};
async function testAPI() {
console.log('🧪 Testing School Management API...\n');
try {
// Test 1: Health Check
console.log('1. Testing Health Check...');
const healthResponse = await axios.get(`${BASE_URL}/health`);
console.log('✅ Health Check:', healthResponse.data.message);
console.log('');
// Test 2: Add School
console.log('2. Testing Add School...');
const addResponse = await axios.post(`${BASE_URL}/addSchool`, testSchool);
console.log('✅ School Added:', addResponse.data.message);
console.log(' School ID:', addResponse.data.data.id);
console.log('');
// Test 3: List Schools
console.log('3. Testing List Schools...');
const listResponse = await axios.get(`${BASE_URL}/listSchools?latitude=28.7041&longitude=77.1025`);
console.log('✅ Schools Retrieved:', listResponse.data.message);
console.log(' Total Schools:', listResponse.data.total);
console.log(' Closest School:', listResponse.data.data[0]?.name);
console.log(' Distance to closest:', listResponse.data.data[0]?.distance, 'km');
console.log('');
// Test 4: Get School by ID
console.log('4. Testing Get School by ID...');
const schoolId = addResponse.data.data.id;
const getResponse = await axios.get(`${BASE_URL}/school/${schoolId}`);
console.log('✅ School Retrieved:', getResponse.data.message);
console.log(' School Name:', getResponse.data.data.name);
console.log('');
console.log('🎉 All tests passed successfully!');
console.log('📚 API is working correctly.');
} catch (error) {
console.error('❌ Test failed:', error.response?.data || error.message);
console.log('\n💡 Make sure:');
console.log(' 1. The server is running (npm start)');
console.log(' 2. MySQL database is connected');
console.log(' 3. Environment variables are set correctly');
}
}
// Run tests
testAPI();