-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debugging usercredit, userprofile, balance display issues, Kenyon #346
base: main
Are you sure you want to change the base?
Changes from all commits
b99985b
a1e2cac
6f172fa
c0387c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package usercredit_test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be a separate package. We're not exporting test functionality here. |
||
|
||
import ( | ||
usercredit "socialpredict/handlers/users/credit" | ||
"socialpredict/models" | ||
"socialpredict/models/modelstesting" | ||
"testing" | ||
) | ||
|
||
type UserPublicInfo struct { | ||
AccountBalance int | ||
} | ||
|
||
func TestCalculateUserCredit(t *testing.T) { | ||
|
||
db := modelstesting.NewFakeDB(t) | ||
if err := db.AutoMigrate(&models.User{}); err != nil { | ||
t.Fatalf("Failed to migrate models: %v", err) | ||
} | ||
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. db migration happens when creating the fake. |
||
|
||
tests := []struct { | ||
username string | ||
email string | ||
apiKey string | ||
accountBalance int64 | ||
maxDebt int64 | ||
expectedCredit int64 | ||
}{ | ||
// Test with maximum debt of 500 | ||
{"testuser1", "testuser1@example.com", "api_key_testuser1", -100, 500, 400}, | ||
{"testuser2", "testuser2@example.com", "api_key_testuser2", 0, 500, 500}, | ||
{"testuser3", "testuser3@example.com", "api_key_testuser3", 100, 500, 500 + 100}, | ||
|
||
// Test with maximum debt of 5000 | ||
{"testuser4", "testuser4@example.com", "api_key_testuser4", -100, 5000, 4900}, | ||
{"testuser5", "testuser5@example.com", "api_key_testuser5", 0, 5000, 5000}, | ||
{"testuser6", "testuser6@example.com", "api_key_testuser6", 100, 5000, 5100}, | ||
} | ||
|
||
for _, test := range tests { | ||
// Clear the users table before each test run | ||
if err := db.Exec("DELETE FROM users").Error; err != nil { | ||
t.Fatalf("Failed to clear users table: %v", err) | ||
} | ||
|
||
user := &models.User{ | ||
Username: test.username, | ||
Email: test.email, | ||
AccountBalance: test.accountBalance, | ||
ApiKey: test.apiKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this field necessary to create a user? It doesn't look like it, but I may misunderstand the gorm struct tagging. If it's not necessary, I don't think we're using this field in the tests at all and can safely remove it. |
||
Password: "testpassword", | ||
} | ||
|
||
if err := user.HashPassword(user.Password); err != nil { | ||
t.Fatalf("Failed to hash password for user %s: %v", user.Username, err) | ||
} | ||
|
||
if err := db.Create(user).Error; err != nil { | ||
t.Fatalf("Failed to save user to database: %v", err) | ||
} | ||
|
||
userCredit := usercredit.CalculateUserCredit(db, test.username, test.maxDebt) | ||
|
||
if userCredit != test.expectedCredit { | ||
t.Errorf("For %s, with max debt %d, expected user credit to be %d, got %d", test.username, test.maxDebt, test.expectedCredit, userCredit) | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as publicuser_test- this isn't necessary unless you're adding tests. Since there is no new privateuser code here, I think it makes sense to add this when we create tests for the privateuser package. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package privateuser_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package usershandlers | ||
package publicuser | ||
|
||
import ( | ||
"encoding/json" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package publicuser_test | ||
|
||
import ( | ||
"socialpredict/handlers/users/publicuser" | ||
"socialpredict/models" | ||
"socialpredict/models/modelstesting" | ||
|
||
"testing" | ||
) | ||
|
||
// Test for GetPublicUserInfo using an in-memory SQLite database | ||
func TestGetPublicUserInfo(t *testing.T) { | ||
// Set up the in-memory SQLite database using modelstesting.NewFakeDB | ||
db := modelstesting.NewFakeDB(t) | ||
|
||
// Migrate the User model | ||
if err := db.AutoMigrate(&models.User{}); err != nil { | ||
t.Fatalf("Failed to migrate user model: %v", err) | ||
} | ||
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Create a test user in the database | ||
user := &models.User{ | ||
Username: "testuser", | ||
DisplayName: "Test User", | ||
UserType: "regular", | ||
InitialAccountBalance: 1000, | ||
AccountBalance: 500, | ||
PersonalEmoji: "😊", | ||
Description: "Test description", | ||
PersonalLink1: "http://link1.com", | ||
PersonalLink2: "http://link2.com", | ||
PersonalLink3: "http://link3.com", | ||
PersonalLink4: "http://link4.com", | ||
} | ||
if err := db.Create(user).Error; err != nil { | ||
t.Fatalf("Failed to save user to database: %v", err) | ||
} | ||
|
||
// Call GetPublicUserInfo to retrieve the user | ||
retrievedUser := publicuser.GetPublicUserInfo(db, "testuser") | ||
|
||
// Expected result | ||
expectedUser := publicuser.PublicUserType{ | ||
Username: "testuser", | ||
DisplayName: "Test User", | ||
UserType: "regular", | ||
InitialAccountBalance: 1000, | ||
AccountBalance: 500, | ||
PersonalEmoji: "😊", | ||
Description: "Test description", | ||
PersonalLink1: "http://link1.com", | ||
PersonalLink2: "http://link2.com", | ||
PersonalLink3: "http://link3.com", | ||
PersonalLink4: "http://link4.com", | ||
} | ||
|
||
// Compare the retrieved user with the expected result | ||
if retrievedUser != expectedUser { | ||
t.Errorf("GetPublicUserInfo(db, 'testuser') = %+v, want %+v", retrievedUser, expectedUser) | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider renaming this file to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package usershandlers | ||
package publicuser | ||
|
||
import ( | ||
"encoding/json" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to add an empty test file. If you're planning to add tests, I would add this file as part of the PR that adds the tests. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package publicuser |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can remain private when the testing is done in the same package.