Skip to content

Commit

Permalink
test account CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
techschool committed Jul 16, 2020
1 parent 7d5ebb9 commit 5772711
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ migratedown:
sqlc:
sqlc generate

.PHONY: postgres createdb dropdb migrateup migratedown sqlc
test:
go test -v -cover ./...

.PHONY: postgres createdb dropdb migrateup migratedown sqlc test
98 changes: 98 additions & 0 deletions db/sqlc/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package db

import (
"context"
"database/sql"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/techschool/simplebank/util"
)

func createRandomAccount(t *testing.T) Account {
arg := CreateAccountParams{
Owner: util.RandomOwner(),
Balance: util.RandomMoney(),
Currency: util.RandomCurrency(),
}

account, err := testQueries.CreateAccount(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, account)

require.Equal(t, arg.Owner, account.Owner)
require.Equal(t, arg.Balance, account.Balance)
require.Equal(t, arg.Currency, account.Currency)

require.NotZero(t, account.ID)
require.NotZero(t, account.CreatedAt)

return account
}

func TestCreateAccount(t *testing.T) {
createRandomAccount(t)
}

func TestGetAccount(t *testing.T) {
account1 := createRandomAccount(t)
account2, err := testQueries.GetAccount(context.Background(), account1.ID)
require.NoError(t, err)
require.NotEmpty(t, account2)

require.Equal(t, account1.ID, account2.ID)
require.Equal(t, account1.Owner, account2.Owner)
require.Equal(t, account1.Balance, account2.Balance)
require.Equal(t, account1.Currency, account2.Currency)
require.WithinDuration(t, account1.CreatedAt, account2.CreatedAt, time.Second)
}

func TestUpdateAccount(t *testing.T) {
account1 := createRandomAccount(t)

arg := UpdateAccountParams{
ID: account1.ID,
Balance: util.RandomMoney(),
}

account2, err := testQueries.UpdateAccount(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, account2)

require.Equal(t, account1.ID, account2.ID)
require.Equal(t, account1.Owner, account2.Owner)
require.Equal(t, arg.Balance, account2.Balance)
require.Equal(t, account1.Currency, account2.Currency)
require.WithinDuration(t, account1.CreatedAt, account2.CreatedAt, time.Second)
}

func TestDeleteAccount(t *testing.T) {
account1 := createRandomAccount(t)
err := testQueries.DeleteAccount(context.Background(), account1.ID)
require.NoError(t, err)

account2, err := testQueries.GetAccount(context.Background(), account1.ID)
require.Error(t, err)
require.EqualError(t, err, sql.ErrNoRows.Error())
require.Empty(t, account2)
}

func TestListAccounts(t *testing.T) {
for i := 0; i < 10; i++ {
createRandomAccount(t)
}

arg := ListAccountsParams{
Limit: 5,
Offset: 5,
}

accounts, err := testQueries.ListAccounts(context.Background(), arg)
require.NoError(t, err)
require.Len(t, accounts, 5)

for _, account := range accounts {
require.NotEmpty(t, account)
}
}
28 changes: 28 additions & 0 deletions db/sqlc/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package db

import (
"database/sql"
"log"
"os"
"testing"

_ "github.com/lib/pq"
)

const (
dbDriver = "postgres"
dbSource = "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable"
)

var testQueries *Queries

func TestMain(m *testing.M) {
conn, err := sql.Open(dbDriver, dbSource)
if err != nil {
log.Fatal("cannot connect to db:", err)
}

testQueries = New(conn)

os.Exit(m.Run())
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/techschool/simplebank

go 1.14

require (
github.com/lib/pq v1.7.0
github.com/stretchr/testify v1.6.1
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY=
github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
48 changes: 48 additions & 0 deletions util/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"math/rand"
"strings"
"time"
)

const alphabet = "abcdefghijklmnopqrstuvwxyz"

func init() {
rand.Seed(time.Now().UnixNano())
}

// RandomInt generates a random integer between min and max
func RandomInt(min, max int64) int64 {
return min + rand.Int63n(max-min+1)
}

// RandomString generates a random string of length n
func RandomString(n int) string {
var sb strings.Builder
k := len(alphabet)

for i := 0; i < n; i++ {
c := alphabet[rand.Intn(k)]
sb.WriteByte(c)
}

return sb.String()
}

// RandomOwner generates a random owner name
func RandomOwner() string {
return RandomString(6)
}

// RandomMoney generates a random amount of money
func RandomMoney() int64 {
return RandomInt(0, 1000)
}

// RandomCurrency generates a random currency code
func RandomCurrency() string {
currencies := []string{"EUR", "USD", "CAD"}
n := len(currencies)
return currencies[rand.Intn(n)]
}

0 comments on commit 5772711

Please sign in to comment.