From 5dcf9556d0b41ede4d0683f472a26adfa55e245e Mon Sep 17 00:00:00 2001 From: eugene huang Date: Thu, 29 Jun 2023 01:11:37 -0700 Subject: [PATCH] fix tests. use calculated field in /bettor --- internal/app/bettor/discord/bettor.go | 17 +----- internal/app/bettor/repo/mem/repo.go | 2 + internal/app/bettor/repo/repo.go | 7 +-- internal/app/bettor/server/users_test.go | 70 ++++++++++++++++++------ 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/internal/app/bettor/discord/bettor.go b/internal/app/bettor/discord/bettor.go index 5eb5ec8..afbefb7 100644 --- a/internal/app/bettor/discord/bettor.go +++ b/internal/app/bettor/discord/bettor.go @@ -3,9 +3,7 @@ package discord import ( "context" - "github.com/bufbuild/connect-go" "github.com/bwmarrin/discordgo" - api "github.com/elh/bettor/api/bettor/v1alpha" ) var getBettorCommand = &discordgo.ApplicationCommand{ @@ -26,20 +24,7 @@ func GetBettor(ctx context.Context, client bettorClient) Handler { return nil, CErr("Failed to get or create new user", err) } - resp, err := client.ListBets(ctx, &connect.Request[api.ListBetsRequest]{Msg: &api.ListBetsRequest{ - Book: guildBookName(guildID), - User: bettorUser.GetName(), - ExcludeSettled: true, - }}) - if err != nil { - return nil, CErr("Failed to list bets", err) - } - var unsettledCentipoints uint64 - for _, b := range resp.Msg.GetBets() { - unsettledCentipoints += b.GetCentipoints() - } - - msgformat, margs := formatUser(bettorUser, unsettledCentipoints) + msgformat, margs := formatUser(bettorUser, bettorUser.UnsettledCentipoints) msgformat = "🎲 👤\n\n" + msgformat return &discordgo.InteractionResponseData{Content: localized.Sprintf(msgformat, margs...)}, nil } diff --git a/internal/app/bettor/repo/mem/repo.go b/internal/app/bettor/repo/mem/repo.go index e2baa90..2ebc52b 100644 --- a/internal/app/bettor/repo/mem/repo.go +++ b/internal/app/bettor/repo/mem/repo.go @@ -51,6 +51,8 @@ func (r *Repo) CreateUser(_ context.Context, user *api.User) error { r.userMtx.Lock() defer r.userMtx.Unlock() + user.UnsettledCentipoints = 0 // defensive + bookID, _ := entity.UserIDs(user.GetName()) for _, u := range r.Users { if u.GetName() == user.GetName() { diff --git a/internal/app/bettor/repo/repo.go b/internal/app/bettor/repo/repo.go index 876cbad..dc16f13 100644 --- a/internal/app/bettor/repo/repo.go +++ b/internal/app/bettor/repo/repo.go @@ -6,6 +6,8 @@ import ( api "github.com/elh/bettor/api/bettor/v1alpha" ) +// NOTE: same models E2E from API to repo out of laziness + // Repo is a persistence repository. type Repo interface { CreateUser(ctx context.Context, user *api.User) error @@ -26,12 +28,9 @@ type Repo interface { // ListUsersArgs are the arguments for listing users. type ListUsersArgs struct { Book string - GreaterThanName string // only supported if OrderBy=name + GreaterThanName string Users []string Limit int - // TODO: implement - // "name" (asc) or "total_centipoints" (desc). defaults to "name" - OrderBy string } // ListMarketsArgs are the arguments for listing markets. diff --git a/internal/app/bettor/server/users_test.go b/internal/app/bettor/server/users_test.go index 8ae07c8..954c144 100644 --- a/internal/app/bettor/server/users_test.go +++ b/internal/app/bettor/server/users_test.go @@ -116,18 +116,24 @@ func TestGetUser(t *testing.T) { Username: "rusty", Centipoints: 100, } - userWithUnsettled := &api.User{ + userHydrated := &api.User{ Name: entity.UserN("guild:1", uuid.NewString()), Username: "linus", Centipoints: 100, } unsettledBet := &api.Bet{ - Name: entity.BetN("guild:1", "b"), - User: userWithUnsettled.Name, + Name: entity.BetN("guild:1", "a"), + User: userHydrated.Name, Centipoints: 200, } - hydratedUserWithUnsettled := proto.Clone(userWithUnsettled).(*api.User) - hydratedUserWithUnsettled.UnsettledCentipoints = unsettledBet.Centipoints + unsettledBet2 := &api.Bet{ + Name: entity.BetN("guild:1", "b"), + User: userHydrated.Name, + Centipoints: 50, + } + hydratedUserHydrated := proto.Clone(userHydrated).(*api.User) + hydratedUserHydrated.UnsettledCentipoints += unsettledBet.Centipoints + hydratedUserHydrated.UnsettledCentipoints += unsettledBet2.Centipoints testCases := []struct { desc string user string @@ -141,8 +147,8 @@ func TestGetUser(t *testing.T) { }, { desc: "hydrate unsettled points", - user: userWithUnsettled.GetName(), - expected: hydratedUserWithUnsettled, + user: userHydrated.GetName(), + expected: hydratedUserHydrated, }, { desc: "fails if user does not exist", @@ -158,7 +164,7 @@ func TestGetUser(t *testing.T) { for _, tC := range testCases { tC := tC t.Run(tC.desc, func(t *testing.T) { - s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user, userWithUnsettled}, Bets: []*api.Bet{unsettledBet}})) + s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user, userHydrated}, Bets: []*api.Bet{unsettledBet, unsettledBet2}})) require.Nil(t, err) out, err := s.GetUser(context.Background(), connect.NewRequest(&api.GetUserRequest{Name: tC.user})) if tC.expectErr { @@ -171,13 +177,30 @@ func TestGetUser(t *testing.T) { } } -// TODO: add test for unsettled func TestGetUserByUsername(t *testing.T) { user := &api.User{ Name: entity.UserN("guild:1", uuid.NewString()), Username: "rusty", Centipoints: 100, } + userHydrated := &api.User{ + Name: entity.UserN("guild:1", uuid.NewString()), + Username: "linus", + Centipoints: 100, + } + unsettledBet := &api.Bet{ + Name: entity.BetN("guild:1", "a"), + User: userHydrated.Name, + Centipoints: 200, + } + unsettledBet2 := &api.Bet{ + Name: entity.BetN("guild:1", "b"), + User: userHydrated.Name, + Centipoints: 50, + } + hydratedUserHydrated := proto.Clone(userHydrated).(*api.User) + hydratedUserHydrated.UnsettledCentipoints += unsettledBet.Centipoints + hydratedUserHydrated.UnsettledCentipoints += unsettledBet2.Centipoints testCases := []struct { desc string book string @@ -191,6 +214,12 @@ func TestGetUserByUsername(t *testing.T) { username: "rusty", expected: user, }, + { + desc: "hydrate unsettled points", + book: entity.BookN("guild:1"), + username: "linus", + expected: hydratedUserHydrated, + }, { desc: "fails if user does not exist", book: entity.BookN("guild:1"), @@ -207,7 +236,7 @@ func TestGetUserByUsername(t *testing.T) { for _, tC := range testCases { tC := tC t.Run(tC.desc, func(t *testing.T) { - s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user}})) + s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user, userHydrated}, Bets: []*api.Bet{unsettledBet, unsettledBet2}})) require.Nil(t, err) out, err := s.GetUserByUsername(context.Background(), connect.NewRequest(&api.GetUserByUsernameRequest{Book: tC.book, Username: tC.username})) if tC.expectErr { @@ -220,7 +249,6 @@ func TestGetUserByUsername(t *testing.T) { } } -// TODO: add test for unsettled func TestListUsers(t *testing.T) { // tests pagination until all users are returned // alphabetically ordered ids @@ -235,11 +263,19 @@ func TestListUsers(t *testing.T) { Username: "danny", Centipoints: 200, } + // has an unsettled bet user3 := &api.User{ Name: entity.UserN(bookID, "c"), Username: "linus", Centipoints: 300, } + unsettledBet := &api.Bet{ + Name: entity.BetN(bookID, "a"), + User: user3.Name, + Centipoints: 200, + } + user3Hydrated := proto.Clone(user3).(*api.User) + user3Hydrated.UnsettledCentipoints += unsettledBet.Centipoints testCases := []struct { desc string req *api.ListUsersRequest @@ -250,7 +286,7 @@ func TestListUsers(t *testing.T) { { desc: "basic case", req: &api.ListUsersRequest{Book: entity.BookN(bookID)}, - expected: []*api.User{user1, user2, user3}, + expected: []*api.User{user1, user2, user3Hydrated}, expectedCalls: 1, }, { @@ -262,25 +298,25 @@ func TestListUsers(t *testing.T) { { desc: "page size 1", req: &api.ListUsersRequest{Book: entity.BookN(bookID), PageSize: 1}, - expected: []*api.User{user1, user2, user3}, + expected: []*api.User{user1, user2, user3Hydrated}, expectedCalls: 3, }, { desc: "page size 2", req: &api.ListUsersRequest{Book: entity.BookN(bookID), PageSize: 2}, - expected: []*api.User{user1, user2, user3}, + expected: []*api.User{user1, user2, user3Hydrated}, expectedCalls: 2, }, { desc: "page size 3", req: &api.ListUsersRequest{Book: entity.BookN(bookID), PageSize: 3}, - expected: []*api.User{user1, user2, user3}, + expected: []*api.User{user1, user2, user3Hydrated}, expectedCalls: 1, }, { desc: "page size 4", req: &api.ListUsersRequest{Book: entity.BookN(bookID), PageSize: 4}, - expected: []*api.User{user1, user2, user3}, + expected: []*api.User{user1, user2, user3Hydrated}, expectedCalls: 1, }, { @@ -293,7 +329,7 @@ func TestListUsers(t *testing.T) { for _, tC := range testCases { tC := tC t.Run(tC.desc, func(t *testing.T) { - s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user1, user2, user3}})) + s, err := server.New(server.WithRepo(&mem.Repo{Users: []*api.User{user1, user2, user3}, Bets: []*api.Bet{unsettledBet}})) require.Nil(t, err) var all []*api.User var calls int