Skip to content

Commit

Permalink
⚡️ Added users.UserExists function
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Jan 10, 2025
1 parent b00e181 commit 403754b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/api/users/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package users
import "errors"

var (
FailedToInsertUser = errors.New("failed to insert user")
FailedToInsertUser = errors.New("failed to insert user")
FailedToRetrieveIfUserAlreadyExists = errors.New("failed to retrieve if user already exists")
)
37 changes: 37 additions & 0 deletions cmd/api/users/select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package users

import (
"context"
"errors"

"github.com/jackc/pgx/v5"

"ahbcc/internal/database"
"ahbcc/internal/log"
)

// UserExists validates if the given username was already registered in the database
type UserExists func(ctx context.Context, username string) (bool, error)

// MakeUserExists creates a new UserExists
func MakeUserExists(db database.Connection) UserExists {
const query string = `
SELECT EXISTS (
SELECT 1
FROM users
WHERE username = $1
)
`

return func(ctx context.Context, username string) (bool, error) {
var applied bool

err := db.QueryRow(ctx, query, username).Scan(&applied)
if errors.Is(err, pgx.ErrNoRows) {
log.Error(ctx, err.Error())
return false, FailedToRetrieveIfUserAlreadyExists
}

return applied, nil
}
}
45 changes: 45 additions & 0 deletions cmd/api/users/select_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package users_test

import (
"context"
"testing"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"ahbcc/cmd/api/users"
"ahbcc/internal/database"
)

func TestUserExists_success(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRow := new(database.MockPgxRow)
database.MockScan(mockPgxRow, []any{true}, t)
mockPostgresConnection.On("QueryRow", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRow)

userExists := users.MakeUserExists(mockPostgresConnection)

got, err := userExists(context.Background(), "user")

assert.Nil(t, err)
assert.True(t, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRow.AssertExpectations(t)
}

func TestUserExists_failsWhenSelectOperationFails(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRow := new(database.MockPgxRow)
mockPgxRow.On("Scan", mock.Anything).Return(pgx.ErrNoRows)
mockPostgresConnection.On("QueryRow", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRow)

userExists := users.MakeUserExists(mockPostgresConnection)

want := users.FailedToRetrieveIfUserAlreadyExists
_, got := userExists(context.Background(), "user")

assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRow.AssertExpectations(t)
}

0 comments on commit 403754b

Please sign in to comment.