-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
630 lines (583 loc) · 18.8 KB
/
test.js
File metadata and controls
630 lines (583 loc) · 18.8 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
const axios = require('axios')
const API_BASE = 'http://localhost:3000/api'
// Test data
const testData = {
circular: {
url: 'https://example.com/circular1.pdf',
className: 'JUNIOR',
subject: 'Maths',
},
timetable: {
url: 'https://example.com/timetable1.pdf',
className: 'PRIMARY',
section: 'A',
},
homework: {
className: 'SENIOR',
section: 'B',
subject: 'Science',
teacher: 'Mr. Sharma',
},
}
// Helper function to log test results
function logTest(testName, result, error = null) {
console.log(`\n${'='.repeat(50)}`)
console.log(`TEST: ${testName}`)
console.log(`${'='.repeat(50)}`)
if (error) {
console.log('❌ FAILED:', error.message)
if (error.response) {
console.log('Status:', error.response.status)
console.log('Response:', error.response.data)
}
} else {
console.log('✅ PASSED')
console.log('Response:', JSON.stringify(result, null, 2))
}
}
// Helper function to delay execution
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function runAllTests() {
console.log('\n🚀 STARTING COMPREHENSIVE API TESTS')
console.log('=====================================')
const testResults = {
passed: 0,
failed: 0,
tests: [],
}
try {
// Test 1: Health Check
console.log('\n📋 Testing Health Check...')
try {
const healthCheck = await axios.get('http://localhost:3000/')
logTest('Health Check', healthCheck.data)
testResults.passed++
testResults.tests.push({ name: 'Health Check', status: 'PASSED' })
} catch (error) {
logTest('Health Check', null, error)
testResults.failed++
testResults.tests.push({ name: 'Health Check', status: 'FAILED' })
}
// Test 2: Circulars API Tests
console.log('\n📄 TESTING CIRCULARS API')
console.log('========================')
let createdCircularId = null
// Create Circular
try {
const circular = await axios.post(
`${API_BASE}/circular`,
testData.circular
)
logTest('Create Circular', circular.data)
createdCircularId = circular.data.circular.id
testResults.passed++
testResults.tests.push({ name: 'Create Circular', status: 'PASSED' })
} catch (error) {
logTest('Create Circular', null, error)
testResults.failed++
testResults.tests.push({ name: 'Create Circular', status: 'FAILED' })
}
// Get Circulars by Class
try {
const circulars = await axios.get(`${API_BASE}/circular/JUNIOR`)
logTest('Get Circulars by Class', circulars.data)
testResults.passed++
testResults.tests.push({
name: 'Get Circulars by Class',
status: 'PASSED',
})
} catch (error) {
logTest('Get Circulars by Class', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Circulars by Class',
status: 'FAILED',
})
}
// Test invalid class name
try {
const circulars = await axios.get(`${API_BASE}/circular/INVALID`)
logTest('Get Circulars with Invalid Class', circulars.data)
testResults.passed++
testResults.tests.push({
name: 'Get Circulars with Invalid Class',
status: 'PASSED',
})
} catch (error) {
logTest('Get Circulars with Invalid Class', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Circulars with Invalid Class',
status: 'FAILED',
})
}
// Test missing fields in create circular
try {
const circular = await axios.post(`${API_BASE}/circular`, {
url: 'https://example.com/test.pdf',
// Missing className and subject
})
logTest('Create Circular with Missing Fields', circular.data)
testResults.failed++
testResults.tests.push({
name: 'Create Circular with Missing Fields',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 400) {
logTest(
'Create Circular with Missing Fields (Expected Error)',
error.response.data
)
testResults.passed++
testResults.tests.push({
name: 'Create Circular with Missing Fields',
status: 'PASSED',
})
} else {
logTest('Create Circular with Missing Fields', null, error)
testResults.failed++
testResults.tests.push({
name: 'Create Circular with Missing Fields',
status: 'FAILED',
})
}
}
// Delete Circular
if (createdCircularId) {
try {
await axios.delete(`${API_BASE}/circular/${createdCircularId}`)
logTest('Delete Circular', { success: true, message: 'Deleted' })
testResults.passed++
testResults.tests.push({ name: 'Delete Circular', status: 'PASSED' })
} catch (error) {
logTest('Delete Circular', null, error)
testResults.failed++
testResults.tests.push({ name: 'Delete Circular', status: 'FAILED' })
}
}
// Test 3: Timetable API Tests
console.log('\n📅 TESTING TIMETABLE API')
console.log('========================')
let createdTimetableId = null
// Create Timetable
try {
const timetable = await axios.post(
`${API_BASE}/timetable`,
testData.timetable
)
logTest('Create Timetable', timetable.data)
createdTimetableId = timetable.data.timetable.id
testResults.passed++
testResults.tests.push({ name: 'Create Timetable', status: 'PASSED' })
} catch (error) {
logTest('Create Timetable', null, error)
testResults.failed++
testResults.tests.push({ name: 'Create Timetable', status: 'FAILED' })
}
// Get Timetable by Class and Section
try {
const timetables = await axios.get(`${API_BASE}/timetable/PRIMARY/A`)
logTest('Get Timetable by Class and Section', timetables.data)
testResults.passed++
testResults.tests.push({
name: 'Get Timetable by Class and Section',
status: 'PASSED',
})
} catch (error) {
logTest('Get Timetable by Class and Section', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Timetable by Class and Section',
status: 'FAILED',
})
}
// Test invalid timetable query
try {
const timetables = await axios.get(`${API_BASE}/timetable/INVALID/Z`)
logTest('Get Timetable with Invalid Class/Section', timetables.data)
testResults.passed++
testResults.tests.push({
name: 'Get Timetable with Invalid Class/Section',
status: 'PASSED',
})
} catch (error) {
logTest('Get Timetable with Invalid Class/Section', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Timetable with Invalid Class/Section',
status: 'FAILED',
})
}
// Test missing fields in create timetable
try {
const timetable = await axios.post(`${API_BASE}/timetable`, {
url: 'https://example.com/test.pdf',
// Missing className and section
})
logTest('Create Timetable with Missing Fields', timetable.data)
testResults.failed++
testResults.tests.push({
name: 'Create Timetable with Missing Fields',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 400) {
logTest(
'Create Timetable with Missing Fields (Expected Error)',
error.response.data
)
testResults.passed++
testResults.tests.push({
name: 'Create Timetable with Missing Fields',
status: 'PASSED',
})
} else {
logTest('Create Timetable with Missing Fields', null, error)
testResults.failed++
testResults.tests.push({
name: 'Create Timetable with Missing Fields',
status: 'FAILED',
})
}
}
// Delete Timetable
if (createdTimetableId) {
try {
await axios.delete(`${API_BASE}/timetable/${createdTimetableId}`)
logTest('Delete Timetable', { success: true, message: 'Deleted' })
testResults.passed++
testResults.tests.push({ name: 'Delete Timetable', status: 'PASSED' })
} catch (error) {
logTest('Delete Timetable', null, error)
testResults.failed++
testResults.tests.push({ name: 'Delete Timetable', status: 'FAILED' })
}
}
// Test 4: Homework API Tests
console.log('\n📚 TESTING HOMEWORK API')
console.log('=======================')
let createdHomeworkId = null
// Create Homework
try {
const homework = await axios.post(
`${API_BASE}/homework`,
testData.homework
)
logTest('Create Homework', homework.data)
createdHomeworkId = homework.data.homework.id
testResults.passed++
testResults.tests.push({ name: 'Create Homework', status: 'PASSED' })
} catch (error) {
logTest('Create Homework', null, error)
testResults.failed++
testResults.tests.push({ name: 'Create Homework', status: 'FAILED' })
}
// Get Homework by Teacher
try {
const teacherHW = await axios.get(
`${API_BASE}/homework/teacher/Mr. Sharma`
)
logTest('Get Homework by Teacher', teacherHW.data)
testResults.passed++
testResults.tests.push({
name: 'Get Homework by Teacher',
status: 'PASSED',
})
} catch (error) {
logTest('Get Homework by Teacher', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Homework by Teacher',
status: 'FAILED',
})
}
// Get Homework for Students
try {
const studentHW = await axios.get(`${API_BASE}/homework/student/SENIOR/B`)
logTest('Get Homework for Students', studentHW.data)
testResults.passed++
testResults.tests.push({
name: 'Get Homework for Students',
status: 'PASSED',
})
} catch (error) {
logTest('Get Homework for Students', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Homework for Students',
status: 'FAILED',
})
}
// Test homework query with invalid teacher
try {
const teacherHW = await axios.get(
`${API_BASE}/homework/teacher/NonExistentTeacher`
)
logTest('Get Homework by Non-existent Teacher', teacherHW.data)
testResults.passed++
testResults.tests.push({
name: 'Get Homework by Non-existent Teacher',
status: 'PASSED',
})
} catch (error) {
logTest('Get Homework by Non-existent Teacher', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Homework by Non-existent Teacher',
status: 'FAILED',
})
}
// Test homework query with invalid class/section
try {
const studentHW = await axios.get(
`${API_BASE}/homework/student/INVALID/Z`
)
logTest('Get Homework for Invalid Class/Section', studentHW.data)
testResults.passed++
testResults.tests.push({
name: 'Get Homework for Invalid Class/Section',
status: 'PASSED',
})
} catch (error) {
logTest('Get Homework for Invalid Class/Section', null, error)
testResults.failed++
testResults.tests.push({
name: 'Get Homework for Invalid Class/Section',
status: 'FAILED',
})
}
// Test missing fields in create homework
try {
const homework = await axios.post(`${API_BASE}/homework`, {
className: 'SENIOR',
// Missing section, subject, and teacher
})
logTest('Create Homework with Missing Fields', homework.data)
testResults.failed++
testResults.tests.push({
name: 'Create Homework with Missing Fields',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 400) {
logTest(
'Create Homework with Missing Fields (Expected Error)',
error.response.data
)
testResults.passed++
testResults.tests.push({
name: 'Create Homework with Missing Fields',
status: 'PASSED',
})
} else {
logTest('Create Homework with Missing Fields', null, error)
testResults.failed++
testResults.tests.push({
name: 'Create Homework with Missing Fields',
status: 'FAILED',
})
}
}
// Delete Homework
if (createdHomeworkId) {
try {
await axios.delete(`${API_BASE}/homework/${createdHomeworkId}`)
logTest('Delete Homework', { success: true, message: 'Deleted' })
testResults.passed++
testResults.tests.push({ name: 'Delete Homework', status: 'PASSED' })
} catch (error) {
logTest('Delete Homework', null, error)
testResults.failed++
testResults.tests.push({ name: 'Delete Homework', status: 'FAILED' })
}
}
// Test 5: Edge Cases and Error Handling
console.log('\n🔍 TESTING EDGE CASES AND ERROR HANDLING')
console.log('=========================================')
// Test invalid HTTP methods
try {
await axios.put(`${API_BASE}/circular/1`, testData.circular)
logTest(
'PUT Method on Circular (Should Fail)',
null,
new Error('PUT method should not be allowed')
)
testResults.failed++
testResults.tests.push({
name: 'PUT Method on Circular',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 404) {
logTest('PUT Method on Circular (Expected 404)', error.response.data)
testResults.passed++
testResults.tests.push({
name: 'PUT Method on Circular',
status: 'PASSED',
})
} else {
logTest('PUT Method on Circular', null, error)
testResults.failed++
testResults.tests.push({
name: 'PUT Method on Circular',
status: 'FAILED',
})
}
}
// Test non-existent resource deletion
try {
await axios.delete(`${API_BASE}/circular/99999`)
logTest(
'Delete Non-existent Circular',
null,
new Error('Should not be able to delete non-existent resource')
)
testResults.failed++
testResults.tests.push({
name: 'Delete Non-existent Circular',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 500) {
logTest(
'Delete Non-existent Circular (Expected Error)',
error.response.data
)
testResults.passed++
testResults.tests.push({
name: 'Delete Non-existent Circular',
status: 'PASSED',
})
} else {
logTest('Delete Non-existent Circular', null, error)
testResults.failed++
testResults.tests.push({
name: 'Delete Non-existent Circular',
status: 'FAILED',
})
}
}
// Test invalid ID format
try {
await axios.delete(`${API_BASE}/circular/invalid-id`)
logTest(
'Delete with Invalid ID Format',
null,
new Error('Should handle invalid ID format')
)
testResults.failed++
testResults.tests.push({
name: 'Delete with Invalid ID Format',
status: 'FAILED',
})
} catch (error) {
if (error.response && error.response.status === 500) {
logTest(
'Delete with Invalid ID Format (Expected Error)',
error.response.data
)
testResults.passed++
testResults.tests.push({
name: 'Delete with Invalid ID Format',
status: 'PASSED',
})
} else {
logTest('Delete with Invalid ID Format', null, error)
testResults.failed++
testResults.tests.push({
name: 'Delete with Invalid ID Format',
status: 'FAILED',
})
}
}
// Test 6: Data Validation Tests
console.log('\n✅ TESTING DATA VALIDATION')
console.log('==========================')
// Test all ClassType enum values
const classTypes = ['JUNIOR', 'PRIMARY', 'SENIOR']
for (const classType of classTypes) {
try {
const circular = await axios.post(`${API_BASE}/circular`, {
url: `https://example.com/circular-${classType.toLowerCase()}.pdf`,
className: classType,
subject: 'Test Subject',
})
logTest(`Create Circular with ${classType} Class`, circular.data)
// Clean up
await axios.delete(`${API_BASE}/circular/${circular.data.circular.id}`)
testResults.passed++
testResults.tests.push({
name: `Create Circular with ${classType} Class`,
status: 'PASSED',
})
} catch (error) {
logTest(`Create Circular with ${classType} Class`, null, error)
testResults.failed++
testResults.tests.push({
name: `Create Circular with ${classType} Class`,
status: 'FAILED',
})
}
}
// Test case sensitivity handling
try {
const circular = await axios.post(`${API_BASE}/circular`, {
url: 'https://example.com/circular-case-test.pdf',
className: 'junior', // lowercase
subject: 'Case Test',
})
logTest('Create Circular with Lowercase Class Name', circular.data)
// Clean up
await axios.delete(`${API_BASE}/circular/${circular.data.circular.id}`)
testResults.passed++
testResults.tests.push({
name: 'Create Circular with Lowercase Class Name',
status: 'PASSED',
})
} catch (error) {
logTest('Create Circular with Lowercase Class Name', null, error)
testResults.failed++
testResults.tests.push({
name: 'Create Circular with Lowercase Class Name',
status: 'FAILED',
})
}
} catch (error) {
console.error('\n💥 CRITICAL ERROR:', error.message)
testResults.failed++
testResults.tests.push({ name: 'Critical Error', status: 'FAILED' })
}
// Final Results Summary
console.log('\n' + '='.repeat(60))
console.log('📊 FINAL TEST RESULTS SUMMARY')
console.log('='.repeat(60))
console.log(`✅ Tests Passed: ${testResults.passed}`)
console.log(`❌ Tests Failed: ${testResults.failed}`)
console.log(
`📈 Success Rate: ${(
(testResults.passed / (testResults.passed + testResults.failed)) *
100
).toFixed(2)}%`
)
console.log('\n📋 Detailed Test Results:')
testResults.tests.forEach((test) => {
const status = test.status === 'PASSED' ? '✅' : '❌'
console.log(` ${status} ${test.name}`)
})
if (testResults.failed === 0) {
console.log('\n🎉 ALL TESTS PASSED! API is working correctly.')
} else {
console.log(
`\n⚠️ ${testResults.failed} test(s) failed. Please review the errors above.`
)
}
console.log('\n🏁 Test execution completed.')
}
// Run the tests
runAllTests().catch((err) => {
console.error('\n💥 Test execution failed:', err.message)
process.exit(1)
})