Add tests for user deletion and improve error messages

This commit is contained in:
Miguel de la Cruz 2021-09-13 23:16:13 +02:00
parent 50a43b86c0
commit e0da55c63d
2 changed files with 28 additions and 4 deletions

View file

@ -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))

View file

@ -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)
*/
}