-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/COS301-SE-2024/occupi in…
…to develop
- Loading branch information
Showing
5 changed files
with
1,408 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -915,3 +915,177 @@ func TestCheckIfUserIsLoggingInFromKnownLocation_withCache(t *testing.T) { | |
assert.Nil(t, err) | ||
assert.NotNil(t, cachedUser) | ||
} | ||
|
||
func TestGetUserDetailsPerformance(t *testing.T) { | ||
userStruct := models.User{ | ||
OccupiID: "123456", | ||
Password: "hashedpassword123", | ||
Email: "[email protected]", | ||
Role: constants.Admin, | ||
OnSite: true, | ||
IsVerified: true, | ||
NextVerificationDate: time.Now(), // this will be updated once the email is verified | ||
TwoFAEnabled: false, | ||
KnownLocations: []models.Location{ | ||
{ | ||
City: "Cape Town", | ||
Region: "Western Cape", | ||
Country: "South Africa", | ||
}, | ||
}, | ||
Details: models.Details{ | ||
ImageID: "", | ||
Name: "Michael", | ||
DOB: time.Now(), | ||
Gender: "Male", | ||
Pronouns: "He/Him", | ||
}, | ||
Notifications: models.Notifications{ | ||
Invites: true, | ||
BookingReminder: true, | ||
}, | ||
Security: models.Security{ | ||
MFA: false, | ||
Biometrics: false, | ||
ForceLogout: false, | ||
}, | ||
Status: "5", | ||
Position: "Chief Executive Engineer", | ||
DepartmentNo: "01", | ||
ExpoPushToken: "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", | ||
} | ||
|
||
// Create database connection and Cache | ||
db := configs.ConnectToDatabase(constants.AdminDBAccessOption) | ||
Cache := configs.CreateCache() | ||
|
||
// Create a new ResponseRecorder (which satisfies http.ResponseWriter) to record the response. | ||
w := httptest.NewRecorder() | ||
|
||
// Create a response writer and context | ||
ctx, _ := gin.CreateTestContext(w) | ||
|
||
// Create a new AppSession with the Cache | ||
appsessionWithCache := &models.AppSession{ | ||
DB: db, | ||
Cache: Cache, | ||
} | ||
// Create a new AppSession without the Cache | ||
appsessionWithoutCache := &models.AppSession{ | ||
DB: db, | ||
Cache: nil, | ||
} | ||
|
||
// Mock the DB response | ||
collection := db.Database(configs.GetMongoDBName()).Collection("Users") | ||
|
||
_, err := collection.InsertOne(ctx, userStruct) | ||
if err != nil { | ||
t.Fatalf("Failed to insert test user into database: %v", err) | ||
} | ||
|
||
// Test performance with Cache | ||
startTime := time.Now() | ||
for i := 0; i < 1000; i++ { | ||
database.GetUserDetails(ctx, appsessionWithCache, userStruct.Email) | ||
} | ||
durationWithCache := time.Since(startTime) | ||
|
||
// Test performance without Cache | ||
startTime = time.Now() | ||
for i := 0; i < 1000; i++ { | ||
database.GetUserDetails(ctx, appsessionWithoutCache, userStruct.Email) | ||
} | ||
durationWithoutCache := time.Since(startTime) | ||
|
||
// Assert that the Cache improves the speed | ||
if durationWithoutCache <= durationWithCache { | ||
t.Errorf("Cache did not improve performance: duration with Cache %v, duration without Cache %v", durationWithCache, durationWithoutCache) | ||
} | ||
} | ||
|
||
func TestGetUserDetails_withCache(t *testing.T) { | ||
userStruct := models.User{ | ||
OccupiID: "123456", | ||
Password: "hashedpassword123", | ||
Email: "[email protected]", | ||
Role: constants.Admin, | ||
OnSite: true, | ||
IsVerified: true, | ||
NextVerificationDate: time.Now(), // this will be updated once the email is verified | ||
TwoFAEnabled: false, | ||
KnownLocations: []models.Location{ | ||
{ | ||
City: "Cape Town", | ||
Region: "Western Cape", | ||
Country: "South Africa", | ||
}, | ||
}, | ||
Details: models.Details{ | ||
ImageID: "", | ||
Name: "Michael", | ||
DOB: time.Now(), | ||
Gender: "Male", | ||
Pronouns: "He/Him", | ||
}, | ||
Notifications: models.Notifications{ | ||
Invites: true, | ||
BookingReminder: true, | ||
}, | ||
Security: models.Security{ | ||
MFA: false, | ||
Biometrics: false, | ||
ForceLogout: false, | ||
}, | ||
Status: "5", | ||
Position: "Chief Executive Engineer", | ||
DepartmentNo: "01", | ||
ExpoPushToken: "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", | ||
} | ||
// Create database connection and Cache | ||
db := configs.ConnectToDatabase(constants.AdminDBAccessOption) | ||
Cache := configs.CreateCache() | ||
|
||
// Create a new ResponseRecorder (which satisfies http.ResponseWriter) to record the response. | ||
w := httptest.NewRecorder() | ||
|
||
// Create a response writer and context | ||
ctx, _ := gin.CreateTestContext(w) | ||
|
||
// Create a new AppSession with the Cache | ||
appSession := &models.AppSession{ | ||
DB: db, | ||
Cache: Cache, | ||
} | ||
|
||
// Mock the DB response | ||
collection := db.Database(configs.GetMongoDBName()).Collection("Users") | ||
|
||
_, err := collection.InsertOne(ctx, userStruct) | ||
if err != nil { | ||
t.Fatalf("Failed to insert test user into database: %v", err) | ||
} | ||
|
||
// Verify the user is not in the Cache before calling the function | ||
nocachedUser, err := Cache.Get(cache.UserKey(userStruct.Email)) | ||
|
||
assert.NotNil(t, err) | ||
assert.Nil(t, nocachedUser) | ||
|
||
// call the function to test | ||
user, err := database.GetUserDetails(ctx, appSession, userStruct.Email) | ||
|
||
// Verify the response | ||
assert.Equal(t, userStruct.Email, user.Email) | ||
assert.Equal(t, userStruct.Details.Name, user.Name) | ||
assert.Equal(t, userStruct.Details.Gender, user.Gender) | ||
assert.Equal(t, userStruct.Details.Pronouns, user.Pronouns) | ||
assert.Equal(t, userStruct.Details.ContactNo, user.Number) | ||
assert.Nil(t, err) | ||
|
||
// Verify the user is in the Cache | ||
cachedUser, err := Cache.Get(cache.UserKey(userStruct.Email)) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, cachedUser) | ||
} |
Oops, something went wrong.