Added tests for full crud operations on psql
This commit is contained in:
parent
091b1579f0
commit
5a4c422b7a
@ -3,7 +3,7 @@ package postgres
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -20,7 +20,8 @@ type TestDatabase struct {
|
|||||||
container testcontainers.Container
|
container testcontainers.Container
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTestDatabase() *TestDatabase {
|
func NewTestDatabase(tb testing.TB) *TestDatabase {
|
||||||
|
tb.Helper()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
// create container
|
// create container
|
||||||
postgresContainer, err := postgres.Run(ctx,
|
postgresContainer, err := postgres.Run(ctx,
|
||||||
@ -35,18 +36,18 @@ func NewTestDatabase() *TestDatabase {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create postgres container, %v\n", err)
|
tb.Fatalf("Failed to create postgres container, %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionString, err := postgresContainer.ConnectionString(ctx)
|
connectionString, err := postgresContainer.ConnectionString(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to get connection string")
|
tb.Fatalf("Failed to get connection string: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create db pool
|
// create db pool
|
||||||
db, err := sql.Open("pgx", connectionString)
|
db, err := sql.Open("pgx", connectionString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to open db pool")
|
tb.Fatalf("Failed to open db pool: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
migrate.Migrate(db)
|
migrate.Migrate(db)
|
||||||
@ -62,20 +63,64 @@ func (tdb *TestDatabase) TearDown() {
|
|||||||
_ = tdb.container.Terminate(context.Background())
|
_ = tdb.container.Terminate(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreate(t *testing.T) {
|
func TestCRUD(t *testing.T) {
|
||||||
tdb := NewTestDatabase()
|
tdb := NewTestDatabase(t)
|
||||||
defer tdb.TearDown()
|
defer tdb.TearDown()
|
||||||
r := NewPostgresTodoRepository(tdb.Db)
|
r := NewPostgresTodoRepository(tdb.Db)
|
||||||
|
|
||||||
todo := repository.TodoRow{Name: "clean dishes", Done: false}
|
t.Run("creates new todo", func(t *testing.T) {
|
||||||
|
want := repository.TodoRow{Id: 1, Name: "clean dishes", Done: false}
|
||||||
|
newTodo := repository.TodoRow{Name: "clean dishes", Done: false}
|
||||||
|
got, err := r.Create(newTodo)
|
||||||
|
AssertNoError(t, err)
|
||||||
|
AssertTodoRows(t, got, want)
|
||||||
|
})
|
||||||
|
|
||||||
newTodo, err := r.Create(todo)
|
t.Run("gets todo", func(t *testing.T) {
|
||||||
|
want := repository.TodoRow{Id: 1, Name: "clean dishes", Done: false}
|
||||||
|
got, err := r.GetById(1)
|
||||||
|
AssertNoError(t, err)
|
||||||
|
AssertTodoRows(t, got, want)
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
t.Run("updates todo", func(t *testing.T) {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
want := repository.TodoRow{Id: 1, Name: "clean dishes", Done: true}
|
||||||
}
|
err := r.Update(want)
|
||||||
|
AssertNoError(t, err)
|
||||||
|
|
||||||
if newTodo.Id != 1 || newTodo.Name != todo.Name || newTodo.Done != todo.Done {
|
got, err := r.GetById(1)
|
||||||
t.Errorf("got %+v, want %+v", newTodo, todo)
|
AssertNoError(t, err)
|
||||||
|
AssertTodoRows(t, got, want)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("deletes todo", func(t *testing.T) {
|
||||||
|
err := r.Delete(1)
|
||||||
|
AssertNoError(t, err)
|
||||||
|
|
||||||
|
want := repository.ErrNotFound
|
||||||
|
_, got := r.GetById(1)
|
||||||
|
|
||||||
|
AssertErrors(t, got, want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertErrors(t testing.TB, got, want error) {
|
||||||
|
t.Helper()
|
||||||
|
if !errors.Is(got, want) {
|
||||||
|
t.Errorf("got error: %v, want error: %v", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertNoError(t testing.TB, err error) {
|
||||||
|
t.Helper()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected no error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertTodoRows(t testing.TB, got, want repository.TodoRow) {
|
||||||
|
t.Helper()
|
||||||
|
if !got.Equal(want) {
|
||||||
|
t.Errorf("got %+v want %+v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,3 +17,7 @@ type TodoRow struct {
|
|||||||
func NewTodoRow(name string, done bool) TodoRow {
|
func NewTodoRow(name string, done bool) TodoRow {
|
||||||
return TodoRow{Name: name, Done: done}
|
return TodoRow{Name: name, Done: done}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t TodoRow) Equal(todo TodoRow) bool {
|
||||||
|
return t.Id == todo.Id && t.Name == todo.Name && t.Done == todo.Done
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user