Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/handlers/markets/listmarkets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
marketmath "socialpredict/handlers/math/market"
"socialpredict/handlers/math/probabilities/wpam"
"socialpredict/handlers/tradingdata"
usersHandlers "socialpredict/handlers/users"
"socialpredict/handlers/users/publicuser"
"socialpredict/models"
"socialpredict/util"
"strconv"
Expand All @@ -23,7 +23,7 @@ type ListMarketsResponse struct {

type MarketOverview struct {
Market marketpublicresponse.PublicResponseMarket `json:"market"`
Creator usersHandlers.PublicUserType `json:"creator"`
Creator publicuser.PublicUserType `json:"creator"`
LastProbability float64 `json:"lastProbability"`
NumUsers int `json:"numUsers"`
TotalVolume int64 `json:"totalVolume"`
Expand Down Expand Up @@ -52,7 +52,7 @@ func ListMarketsHandler(w http.ResponseWriter, r *http.Request) {
marketVolume := marketmath.GetMarketVolume(bets)
lastProbability := probabilityChanges[len(probabilityChanges)-1].Probability

creatorInfo := usersHandlers.GetPublicUserInfo(db, market.CreatorUsername)
creatorInfo := publicuser.GetPublicUserInfo(db, market.CreatorUsername)

// return the PublicResponse type with information about the market
marketIDStr := strconv.FormatUint(uint64(market.ID), 10)
Expand Down
6 changes: 3 additions & 3 deletions backend/handlers/markets/marketdetailshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
marketmath "socialpredict/handlers/math/market"
"socialpredict/handlers/math/probabilities/wpam"
"socialpredict/handlers/tradingdata"
usersHandlers "socialpredict/handlers/users"
"socialpredict/handlers/users/publicuser"
"socialpredict/models"
"socialpredict/util"
"strconv"
Expand All @@ -18,7 +18,7 @@ import (
// MarketDetailResponse defines the structure for the market detail response
type MarketDetailHandlerResponse struct {
Market marketpublicresponse.PublicResponseMarket `json:"market"`
Creator usersHandlers.PublicUserType `json:"creator"`
Creator publicuser.PublicUserType `json:"creator"`
ProbabilityChanges []wpam.ProbabilityChange `json:"probabilityChanges"`
NumUsers int `json:"numUsers"`
TotalVolume int64 `json:"totalVolume"`
Expand Down Expand Up @@ -68,7 +68,7 @@ func MarketDetailsHandler(w http.ResponseWriter, r *http.Request) {

// get market creator
// Fetch the Creator's public information using utility function
publicCreator := usersHandlers.GetPublicUserInfo(db, publicResponseMarket.CreatorUsername)
publicCreator := publicuser.GetPublicUserInfo(db, publicResponseMarket.CreatorUsername)

// Manually construct the response
response := MarketDetailHandlerResponse{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package usershandlers
package usercredit

import (
"encoding/json"
"log"
"net/http"
"socialpredict/handlers/users/publicuser"
"socialpredict/setup"
"socialpredict/util"

Expand All @@ -23,26 +24,27 @@ func init() {
}

type UserCredit struct {
Credit int `json:"credit"`
Credit int64 `json:"credit"`
}

// gets the user's available credits for display
func GetUserCreditHandler(w http.ResponseWriter, r *http.Request) {

// accept get requests
if r.Method != http.MethodGet {
http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)
return
}

// Extract username from the URL path
vars := mux.Vars(r)
username := vars["username"]

// Use database connection
db := util.GetDB()

userCredit := calculateUserCredit(db, username)
userCredit := CalculateUserCredit(
db,
username,
appConfig.Economics.User.MaximumDebtAllowed,
)

response := UserCredit{
Credit: userCredit,
Expand All @@ -53,12 +55,11 @@ func GetUserCreditHandler(w http.ResponseWriter, r *http.Request) {

}

func calculateUserCredit(db *gorm.DB, username string) int {
func CalculateUserCredit(db *gorm.DB, username string, maximumdebt int64) int64 {

Copy link
Collaborator

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.

userPublicInfo := GetPublicUserInfo(db, username)
userPublicInfo := publicuser.GetPublicUserInfo(db, username)

// add the maximum debt from the setup file and he account balance, which may be negative
userCredit := appConfig.Economics.User.MaximumDebtAllowed + userPublicInfo.AccountBalance
userCredit := maximumdebt + userPublicInfo.AccountBalance

return int(userCredit)
return int64(userCredit)
}
68 changes: 68 additions & 0 deletions backend/handlers/users/credit/usercredit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package usercredit_test
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package usershandlers
package privateuser

import (
"encoding/json"
"net/http"
"socialpredict/handlers/users/publicuser"
"socialpredict/middleware"
"socialpredict/models"
"socialpredict/util"
Expand Down Expand Up @@ -54,7 +55,7 @@ func GetPrivateProfileUserResponse(w http.ResponseWriter, r *http.Request) {
// The username is extracted from the token
username := user.Username

publicInfo := GetPublicUserInfo(db, username)
publicInfo := publicuser.GetPublicUserInfo(db, username)
privateInfo := GetPrivateUserInfo(db, username)

response := CombinedUserResponse{
Expand Down
1 change: 1 addition & 0 deletions backend/handlers/users/privateuserinfo/privateuser_test.go
Copy link
Collaborator

Choose a reason for hiding this comment

The 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"
Expand Down
61 changes: 61 additions & 0 deletions backend/handlers/users/publicuser/publicuser_test.go
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewFakeDB(t) migrates the models on creation, so this is redundant.


// 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)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider renaming this file to portfolio.go since it is a member of the publicuser package.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package usershandlers
package publicuser

import (
"encoding/json"
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
41 changes: 0 additions & 41 deletions backend/handlers/users/updateprofile.draft.go

This file was deleted.

Loading
Loading