-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
496 lines (419 loc) · 12.5 KB
/
database_test.go
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
package main
import (
"log"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"encoding/gob"
"encoding/json"
)
func init() {
// Register the User type with gob
gob.Register(User{})
gob.Register(Announcement{})
}
var (
// testStore = sessions.NewCookieStore([]byte("a-very-secret-key"))
currentUser = User{
GoogleID: "admin-user",
Name: "Admin User",
Email: "[email protected]",
ContactNumber: "12345678",
Role: "Admin",
}
// Sample data for testing
students = []Student{
{
User: User{
GoogleID: "test-student",
Name: "John Doe",
Email: "[email protected]",
ContactNumber: "91234567",
Role: "Student",
},
Age: 12,
LessonCredits: 10.0,
ClassID: "TE-6-10",
ParentID: "test-parent",
},
}
instructor = Instructor{
User: User{
GoogleID: "test-instructor",
Name: "John Doe",
Email: "[email protected]",
ContactNumber: "91234567",
Role: "Student",
},
BasePay: 15,
NumberOfStudents: 24,
}
admin = Admin{
User: User{
GoogleID: "test-admin",
Name: "Awesomeness",
ContactNumber: "99999999",
Email: "[email protected]",
Role: "Admin",
},
BasePay: 15,
Incentive: 24,
}
parent = Parent{
User: User{
GoogleID: "test-parent",
Name: "Awesomeness",
ContactNumber: "99999999",
Email: "[email protected]",
Role: "Parent",
},
FolderID: "1F38DgyV0V5q2CWzoO3xkHTC4FY6cNrig",
}
classes = []Class{
{
ClassID: "te-6-10",
Name: "Test Class",
Instructor: "[email protected]",
FolderID: "1zrj8iUefB9D0u1RXT8Pi0-At8aQf9n2m",
},
}
announcement = Announcement{
AnnouncementID: "test-announcement",
Subject: "Test Announcement",
Content: "This is a test announcement.",
}
)
func mockRequest() *http.Request {
req, _ := http.NewRequest("GET", "/test", nil)
recorder := httptest.NewRecorder()
session, _ := store.Get(req, "auth-session")
userData, err := json.Marshal(currentUser)
if err != nil {
log.Println(err)
}
session.Values["user"] = userData
err = session.Save(req, recorder)
if err != nil {
log.Println(err)
}
req.Header.Set("Cookie", recorder.Header().Get("Set-Cookie"))
return req
}
func TestInitializeFirebase(t *testing.T) {
err := initializeFirebase()
if err != nil {
t.Fatalf("Error initializing Firebase: %v", err)
}
if firebaseClient == nil {
t.Fatal("FirebaseClient is not set")
}
}
// Testing for student CRUD operations
func TestCreateStudent(t *testing.T) {
err := initializeFirebase()
if err != nil {
t.Fatalf("Error initializing Firebase: %v", err)
}
err = createStudent(students[0], mockRequest())
if err != nil {
t.Fatalf("Error creating student: %v", err)
}
readStudent, err := readStudent(students[0].GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading student: %v", err)
}
if !reflect.DeepEqual(students[0], readStudent) {
t.Errorf("Created and read students do not match")
}
}
func TestReadStudent(t *testing.T) {
readStudent, err := readStudent(students[0].GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading student: %v", err)
}
if readStudent.GoogleID != "test-student" {
t.Errorf("Expected ID %v, got %v", "test-student", readStudent.GoogleID)
}
}
func TestUpdateStudent(t *testing.T) {
// Update the student's email
updates := map[string]interface{}{
"name": "John Doe",
"contact_number": "99999999",
}
err := updateStudent(students[0].GoogleID, updates, mockRequest())
if err != nil {
t.Fatalf("Error updating student: %v", err)
}
// Read the updated student
readStudent, err := readStudent(students[0].GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading student after updating: %v", err)
}
// Assert that the updated student's email is correct
if students[0].Name != updates["name"] {
t.Errorf("Updated student's name is incorrect. Expected: %v, Got: %v", updates["name"], readStudent.Name)
}
}
func TestDeleteStudent(t *testing.T) {
// Delete the student
err := deleteStudent(students[0], mockRequest())
if err != nil {
t.Fatalf("Error deleting student: %v", err)
}
}
// Testing for instructor CRUD operations
func TestCreateInstructor(t *testing.T) {
err := createInstructor(instructor, mockRequest())
if err != nil {
t.Fatalf("Error creating instructor: %v", err)
}
// Read the created instructor
readInstructor, err := readInstructor(instructor.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading instructor: %v", err)
}
// Assert that the created and read instructor are equal
if !reflect.DeepEqual(instructor, readInstructor) {
t.Error("Created and read instructors are not equal")
}
}
func TestReadInstructor(t *testing.T) {
instructor, err := readInstructor(instructor.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Failed to read instructor: %v", err)
}
if instructor.GoogleID != "test-instructor" {
t.Fatalf("Expected GoogleID %v, got %v", "test-instructor", instructor.GoogleID)
}
}
func TestUpdateInstructor(t *testing.T) {
// Update the instructor's email
updates := map[string]interface{}{
"email": "[email protected]",
}
err := updateInstructor(instructor.GoogleID, updates, mockRequest())
if err != nil {
t.Fatalf("Error updating instructor: %v", err)
}
// Read the updated instructor
readInstructor, err := readInstructor(instructor.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading instructor: %v", err)
}
// Assert that the updated instructor's email is correct
if readInstructor.Email != updates["email"] {
t.Errorf("Updated instructor's email is incorrect. Expected: %v, Got: %v", updates["email"], readInstructor.Email)
}
}
func TestDeleteInstructor(t *testing.T) {
// Delete the instructor
err := deleteInstructor(instructor, mockRequest())
if err != nil {
t.Fatalf("Error deleting instructor: %v", err)
}
}
// Testing for admin CRUD operations
func TestCreateAdmin(t *testing.T) {
err := createAdmin(admin, mockRequest())
if err != nil {
t.Fatalf("Error creating admin: %v", err)
}
// Read the created admin
readAdmin, err := readAdmin(admin.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading admin: %v", err)
}
// Assert that the created and read admin are equal
if !reflect.DeepEqual(admin, readAdmin) {
t.Error("Created and read admins are not equal")
}
}
func TestReadAdmin(t *testing.T) {
admin, err := readAdmin(admin.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Failed to read instructor: %v", err)
}
if admin.GoogleID != "test-admin" {
t.Fatalf("Expected GoogleID %v, got %v", "test-admin", admin.GoogleID)
}
}
func TestUpdateAdmin(t *testing.T) {
// Update the admin's email
updates := map[string]interface{}{
"email": "[email protected]",
}
err := updateAdmin(admin.GoogleID, updates, mockRequest())
if err != nil {
t.Fatalf("Error updating admin: %v", err)
}
// Read the updated admin
readAdmin, err := readAdmin(admin.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading admin: %v", err)
}
// Assert that the updated admin's email is correct
if readAdmin.Email != updates["email"] {
t.Errorf("Updated admin's email is incorrect. Expected: %v, Got: %v", updates["email"], readAdmin.Email)
}
}
func TestDeleteAdmin(t *testing.T) {
// Delete the admin
err := deleteAdmin(admin, mockRequest())
if err != nil {
t.Fatalf("Error deleting admin: %v", err)
}
}
// Testing for parent CRUD operations
func TestCreateParent(t *testing.T) {
err := createParent(parent, mockRequest())
if err != nil {
t.Fatalf("Error creating parent: %v", err)
}
// Read the created parent
readParent, err := readParent(parent.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading parent: %v", err)
}
// Assert that the created and read parent are equal
if !reflect.DeepEqual(parent, readParent) {
t.Error("Created and read parents are not equal")
}
}
func TestReadParent(t *testing.T) {
parent, err := readParent(parent.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Failed to read parent: %v", err)
}
if parent.GoogleID != "test-parent" {
t.Fatalf("Expected GoogleID %v, got %v", "test-parent", parent.GoogleID)
}
}
func TestUpdateParent(t *testing.T) {
// Update the parent's email
updates := map[string]interface{}{
"email": "[email protected]",
}
err := updateParent(parent.GoogleID, updates, mockRequest())
if err != nil {
t.Fatalf("Error updating parent: %v", err)
}
// Read the updated parent
readParent, err := readParent(parent.GoogleID, mockRequest())
if err != nil {
t.Fatalf("Error reading parent: %v", err)
}
// Assert that the updated parent's email is correct
if readParent.Email != updates["email"] {
t.Errorf("Updated parent's email is incorrect. Expected: %v, Got: %v", updates["email"], readParent.Email)
}
}
func TestDeleteParent(t *testing.T) {
// Delete the parent
err := deleteParent(parent, mockRequest())
if err != nil {
t.Fatalf("Error deleting parent: %v", err)
}
}
// Testing for class CRUD operations
func TestCreateClass(t *testing.T) {
err := createClass(classes[0], mockRequest())
if err != nil {
t.Fatalf("Error creating class: %v", err)
}
// Read the created class
readClass, err := readClass(students, classes[0].ClassID, mockRequest())
if err != nil {
t.Fatalf("Error reading class: %v", err)
}
// Assert that the created and read class are equal
if !reflect.DeepEqual(classes[0], readClass) {
t.Error("Created and read classes are not equal")
}
}
func TestReadClass(t *testing.T) {
class, err := readClass(students, classes[0].ClassID, mockRequest())
if err != nil {
t.Fatalf("Failed to read class: %v", err)
}
if class.ClassID != classes[0].ClassID {
t.Fatalf("Expected ID %v, got %v", classes[0].ClassID, class.ClassID)
}
}
func TestUpdateClass(t *testing.T) {
// Update the class's name
updates := map[string]interface{}{
"class_name": "DN",
}
err := updateClass(classes[0], updates, mockRequest())
if err != nil {
t.Fatalf("Error updating class: %v", err)
}
// Read the updated class
readClass, err := readClass(students, classes[0].ClassID, mockRequest())
if err != nil {
t.Fatalf("Error reading class: %v", err)
}
// Assert that the updated class's name is correct
if readClass.Name != updates["class_name"] {
t.Errorf("Updated class's name is incorrect. Expected: %v, Got: %v", updates["class_name"], readClass.Name)
}
}
func TestDeleteClass(t *testing.T) {
// Delete the class
err := deleteClass(classes[0], mockRequest())
if err != nil {
t.Fatalf("Error deleting class: %v", err)
}
}
func TestCreateAnnouncement(t *testing.T) {
err := createAnnouncement(announcement, mockRequest())
if err != nil {
t.Fatalf("Error creating announcement: %v", err)
}
// Read the announcement
readAnnouncement, err := readAnnouncement(announcement.AnnouncementID, mockRequest())
if err != nil {
t.Fatalf("Error reading announcement: %v", err)
}
// Assert that the created and read announcement are equal
if !reflect.DeepEqual(announcement, readAnnouncement) {
t.Errorf("Created and read announcements are not equal. Expected: %+v, Got: %+v", announcement, readAnnouncement)
}
}
func TestReadAnnouncement(t *testing.T) {
announcement, err := readAnnouncement(announcement.AnnouncementID, mockRequest())
if err != nil {
t.Fatalf("Failed to read announcement: %v", err)
}
if announcement.Subject != "Test Announcement" {
t.Fatalf("Expected Title %v, got %v", "Test Announcement", announcement.Subject)
}
}
func TestUpdateAnnouncement(t *testing.T) {
// Update the announcement content
updates := map[string]interface{}{
"content": "This is an updated announcement.",
}
err := updateAnnouncement(announcement.AnnouncementID, updates, mockRequest())
if err != nil {
t.Fatalf("Error updating announcement: %v", err)
}
// Read the updated announcement
readAnnouncement, err := readAnnouncement(announcement.AnnouncementID, mockRequest())
if err != nil {
t.Fatalf("Error reading announcement: %v", err)
}
// Assert that the updated announcement's content is correct
if readAnnouncement.Content != updates["content"] {
t.Errorf("Updated announcement's content is incorrect. Expected: %v, Got: %v", updates["content"], readAnnouncement.Content)
}
}
func TestDeleteAnnouncement(t *testing.T) {
// Delete the announcement
err := deleteAnnouncement(announcement.AnnouncementID, mockRequest())
if err != nil {
t.Fatalf("Error deleting announcement: %v", err)
}
}