From e0da55c63d78111cef724d4693317dab72978242 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 23:16:13 +0200 Subject: [PATCH] Add tests for user deletion and improve error messages --- server/services/store/user.go | 9 +++++---- server/services/store/user_test.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/server/services/store/user.go b/server/services/store/user.go index e17a3b4..0ccea42 100644 --- a/server/services/store/user.go +++ b/server/services/store/user.go @@ -2,6 +2,7 @@ package store import ( "database/sql" + "fmt" sq "github.com/Masterminds/squirrel" @@ -69,13 +70,13 @@ func (us *UserStore) getUsersByCondition(condition sq.Eq) ([]*model.User, error) Where(condition). Query() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get users: %w", err) } defer rows.Close() users, err := us.usersFromRows(rows) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get users from rows: %w", err) } if len(users) == 0 { @@ -92,12 +93,12 @@ func (us *UserStore) Create(user *model.User) (*model.User, error) { res, err := query.Exec() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot insert user: %w", err) } id, err := res.LastInsertId() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get last insert id for created user: %w", err) } return us.GetByID(int(id)) diff --git a/server/services/store/user_test.go b/server/services/store/user_test.go index bb1b864..9d13a74 100644 --- a/server/services/store/user_test.go +++ b/server/services/store/user_test.go @@ -42,3 +42,26 @@ func TestGetByID(t *testing.T) { require.Equal(t, user.Username, user2.Username) require.Equal(t, user.Password, user2.Password) } + +func TestDeleteUsername(t *testing.T) { + th, teardown := NewTestHelper(t) + defer teardown() + + user := th.NewUser() + gameName := "awesome game" + game, err := th.store.Game().Create(gameName, user.ID) + require.NoError(t, err) + + gameMember, err := th.store.Game().GetMember(game.ID, user.ID) + require.NoError(t, err) + require.NotZero(t, gameMember) + + // ToDo: implement member deletion + /* + err = th.store.User().DeleteByUsername(user.Username) + require.NoError(t, err) + + _, err = th.store.Game().GetMember(game.ID, user.ID) + require.Error(t, err) + */ +}