-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user table and unit test for it
- Loading branch information
1 parent
bad3565
commit 3e0ff9d
Showing
7 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
-- name: CreateUser :one | ||
INSERT INTO users ( | ||
"username", | ||
"hashed_password", | ||
"full_name", | ||
"email" | ||
) VALUES ( | ||
$1, $2, $3, $4 | ||
) RETURNING *; | ||
|
||
-- name: GetUser :one | ||
SELECT * FROM users | ||
WHERE username = $1 LIMIT 1; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 db | ||
|
||
import ( | ||
"context" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func createRandomUser(t *testing.T) User { | ||
arg := CreateUserParams{ | ||
Username: utils.RandomOwner(), | ||
HashedPassword: "secret", | ||
FullName: utils.RandomOwner(), | ||
Email: utils.RandomEmail(), | ||
} | ||
|
||
user, err := testQuery.CreateUser(context.Background(), arg) | ||
|
||
require.Nil(t, err) | ||
require.NotEmpty(t, user) | ||
|
||
require.Equal(t, arg.Username, user.Username) | ||
require.Equal(t, arg.FullName, user.FullName) | ||
require.Equal(t, arg.Email, user.Email) | ||
require.Equal(t, arg.HashedPassword, user.HashedPassword) | ||
|
||
require.True(t, user.PasswordChangedAt.IsZero()) | ||
require.NotZero(t, user.CreatedAt) | ||
|
||
return user | ||
} | ||
|
||
func TestQueries_CreateUser(t *testing.T) { | ||
createRandomUser(t) | ||
} | ||
|
||
func TestQueries_GetUser(t *testing.T) { | ||
want := createRandomUser(t) | ||
|
||
have, err := testQuery.GetUser(context.Background(), want.Username) | ||
|
||
require.Nil(t, err) | ||
require.Equal(t, want, have) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
@@ -17,7 +18,7 @@ func RandomInt(min, max int64) int64 { | |
return min + rand.Int63n(max-min+1) | ||
} | ||
|
||
// RandomString | ||
// RandomString return a random string with input length | ||
func RandomString(n int) string { | ||
var sb strings.Builder | ||
|
||
|
@@ -30,16 +31,24 @@ func RandomString(n int) string { | |
return sb.String() | ||
} | ||
|
||
// RandomOwner return a string length 6 | ||
func RandomOwner() string { | ||
return RandomString(6) | ||
} | ||
|
||
// RandomMoney return an integer between 0-10000 | ||
func RandomMoney() int64 { | ||
return RandomInt(0, 10000) | ||
} | ||
|
||
// RandomCurrency return supported currency | ||
func RandomCurrency() string { | ||
currencies := []string{"USD", "VND", "EUR"} | ||
currencies := []string{USD, VND, EUR} | ||
n := len(currencies) | ||
return currencies[rand.Intn(n)] | ||
} | ||
|
||
// RandomEmail return an email length 6 | ||
func RandomEmail() string { | ||
return fmt.Sprintf("%[email protected]", RandomOwner()) | ||
} |