-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b00e181
commit 403754b
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |