Skip to content

Commit

Permalink
feat: add user table and unit test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
cukhoaimon committed Feb 1, 2024
1 parent bad3565 commit 3e0ff9d
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
1 change: 1 addition & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions db/query/user.sql
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;
9 changes: 9 additions & 0 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions db/sqlc/user.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions db/sqlc/user.sql_test.go
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)
}
13 changes: 11 additions & 2 deletions utils/random.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"fmt"
"math/rand"
"strings"
"time"
Expand All @@ -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

Expand All @@ -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())
}

0 comments on commit 3e0ff9d

Please sign in to comment.