forked from techschool/simplebank
-
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
cd6c45f
commit e6c8e85
Showing
12 changed files
with
209 additions
and
2 deletions.
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
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,5 @@ | ||
ALTER TABLE IF EXISTS "accounts" DROP CONSTRAINT IF EXISTS "owner_currency_key"; | ||
|
||
ALTER TABLE IF EXISTS "accounts" DROP CONSTRAINT IF EXISTS "accounts_owner_fkey"; | ||
|
||
DROP TABLE IF EXISTS "users"; |
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,12 @@ | ||
CREATE TABLE "users" ( | ||
"username" varchar PRIMARY KEY, | ||
"hashed_password" varchar NOT NULL, | ||
"full_name" varchar NOT NULL, | ||
"email" varchar UNIQUE NOT NULL, | ||
"password_changed_at" timestamptz NOT NULL DEFAULT('0001-01-01 00:00:00+00'), | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
ALTER TABLE "accounts" ADD FOREIGN KEY ("owner") REFERENCES "users" ("username"); | ||
|
||
ALTER TABLE "accounts" ADD CONSTRAINT "owner_currency_key" UNIQUE ("owner", "currency"); |
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, | ||
) VALUES ( | ||
$1, $2, $3, $4 | ||
) RETURNING *; | ||
|
||
-- name: GetUser :one | ||
SELECT * FROM users | ||
WHERE username = $1 LIMIT 1; |
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
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,50 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/techschool/simplebank/util" | ||
) | ||
|
||
func createRandomUser(t *testing.T) User { | ||
arg := CreateUserParams{ | ||
Username: util.RandomOwner(), | ||
HashedPassword: "secret", | ||
FullName: util.RandomOwner(), | ||
Email: util.RandomEmail(), | ||
} | ||
|
||
user, err := testQueries.CreateUser(context.Background(), arg) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, user) | ||
|
||
require.Equal(t, arg.Username, user.Username) | ||
require.Equal(t, arg.HashedPassword, user.HashedPassword) | ||
require.Equal(t, arg.FullName, user.FullName) | ||
require.Equal(t, arg.Email, user.Email) | ||
require.Zero(t, user.PasswordChangedAt) | ||
require.NotZero(t, user.CreatedAt) | ||
|
||
return user | ||
} | ||
|
||
func TestCreateUser(t *testing.T) { | ||
createRandomUser(t) | ||
} | ||
|
||
func TestGetUser(t *testing.T) { | ||
user1 := createRandomUser(t) | ||
user2, err := testQueries.GetUser(context.Background(), user1.Username) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, user2) | ||
|
||
require.Equal(t, user1.Username, user2.Username) | ||
require.Equal(t, user1.HashedPassword, user2.HashedPassword) | ||
require.Equal(t, user1.FullName, user2.FullName) | ||
require.Equal(t, user1.Email, user2.Email) | ||
require.WithinDuration(t, user1.PasswordChangedAt, user2.PasswordChangedAt, time.Second) | ||
require.WithinDuration(t, user1.CreatedAt, user2.CreatedAt, time.Second) | ||
} |
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 util | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
@@ -46,3 +47,8 @@ func RandomCurrency() string { | |
n := len(currencies) | ||
return currencies[rand.Intn(n)] | ||
} | ||
|
||
// RandomEmail generates a random email | ||
func RandomEmail() string { | ||
return fmt.Sprintf("%[email protected]", RandomString(6)) | ||
} |