Skip to content

Commit

Permalink
Merge pull request #50 from TheWisePigeon/26-users-insert-test
Browse files Browse the repository at this point in the history
Add Users.Insert integration test
  • Loading branch information
joseph0x45 authored Jan 8, 2024
2 parents 29883c8 + f37e3c5 commit d3365d2
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module visio
go 1.21.5

require (
github.com/DATA-DOG/go-sqlmock v1.5.1
github.com/gofiber/fiber/v2 v2.51.0
github.com/gofiber/template/html/v2 v2.0.5
github.com/jmoiron/sqlx v1.3.5
Expand All @@ -12,6 +11,7 @@ require (
github.com/oklog/ulid/v2 v2.1.0
github.com/ory/dockertest/v3 v3.10.0
github.com/redis/go-redis/v9 v9.3.1
github.com/stretchr/testify v1.8.0
golang.org/x/crypto v0.17.0
)

Expand All @@ -23,6 +23,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v24.0.7+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
Expand All @@ -44,6 +45,7 @@ require (
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand All @@ -56,4 +58,5 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.16.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
3 changes: 2 additions & 1 deletion internal/store/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package store
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"visio/internal/types"

"github.com/jmoiron/sqlx"
)

type Users struct {
Expand Down
147 changes: 147 additions & 0 deletions internal/store/users_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package store

import (
"errors"
"fmt"
"log"
"os"
"testing"
"time"
"visio/internal/types"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
)

const (
testDBUser = "testuser"
testDBPassword = "testpassword"
testDBName = "testdb"
)

var testDB *sqlx.DB

func TestMain(m *testing.M) {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not construct pool: %s", err)
}

err = pool.Client.Ping()
if err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
}

resource, err := pool.RunWithOptions(
&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
Env: []string{
"POSTGRES_PASSWORD=" + testDBPassword,
"POSTGRES_USER=" + testDBUser,
"POSTGRES_DB=" + testDBName,
"listen_addresses = '*'",
},
},
)
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}

dbURL := fmt.Sprintf(
"postgres://%s:%s@%s/%s?sslmode=disable",
testDBUser,
testDBPassword,
resource.GetHostPort("5432/tcp"),
testDBName,
)

if err = pool.Retry(func() error {
testDB, err = sqlx.Connect("postgres", dbURL)
if err != nil {
return err
}
return testDB.Ping()
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}

if err := migrateTestDB(testDB); err != nil {
log.Fatalf("Fail to migrate test DB: %v", err)
}

code := m.Run()

if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}

os.Exit(code)
}

// TODO: As an improvement, we might consider to use a migration folder
//
// github.com/golang-migrate/migrate
func migrateTestDB(db *sqlx.DB) error {
q := `
create table if not exists users (
id text not null primary key,
email text not null unique,
password text not null,
signup_date timestamp not null
);`
_, err := db.Exec(q)
return err
}

func TestUsers_Insert(t *testing.T) {
s := NewUsersStore(testDB)

t.Run("duplicate email", func(t *testing.T) {
err := s.Insert(&types.User{
Id: "1",
Email: "[email protected]",
Password: "password1",
SignupDate: time.Now(),
})
require.NoError(t, err)

err = s.Insert(&types.User{
Id: "2",
Email: "[email protected]",
Password: "password2",
SignupDate: time.Now(),
})
require.Error(t, err)
var pqErr *pq.Error
require.True(t, errors.As(err, &pqErr))
// 23505 - unique_violation
// https://www.postgresql.org/docs/current/errcodes-appendix.html
require.Equal(t, "unique_violation", pqErr.Code.Name())

testDB.Exec("TRUNCATE users;")
})

t.Run("success", func(t *testing.T) {
err := s.Insert(&types.User{
Id: "1",
Email: "[email protected]",
Password: "password1",
SignupDate: time.Now(),
})
require.NoError(t, err)

err = s.Insert(&types.User{
Id: "2",
Email: "[email protected]",
Password: "password2",
SignupDate: time.Now(),
})
require.NoError(t, err)

testDB.Exec("TRUNCATE users;")
})
}

0 comments on commit d3365d2

Please sign in to comment.