76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package repository_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"gitea.michaelthomson.dev/mthomson/habits/internal/test"
|
|
"gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository"
|
|
)
|
|
|
|
func TestCRUD(t *testing.T) {
|
|
ctx := context.Background()
|
|
logger := slog.Default()
|
|
tdb := test.NewTestDatabase(t)
|
|
defer tdb.TearDown()
|
|
r := repository.NewTodoRepository(logger, tdb.Db)
|
|
|
|
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(ctx, newTodo)
|
|
AssertNoError(t, err)
|
|
AssertTodoRows(t, got, want)
|
|
})
|
|
|
|
t.Run("gets todo", func(t *testing.T) {
|
|
want := repository.TodoRow{Id: 1, Name: "clean dishes", Done: false}
|
|
got, err := r.GetById(ctx, 1)
|
|
AssertNoError(t, err)
|
|
AssertTodoRows(t, got, want)
|
|
})
|
|
|
|
t.Run("updates todo", func(t *testing.T) {
|
|
want := repository.TodoRow{Id: 1, Name: "clean dishes", Done: true}
|
|
err := r.Update(ctx, want)
|
|
AssertNoError(t, err)
|
|
|
|
got, err := r.GetById(ctx, 1)
|
|
AssertNoError(t, err)
|
|
AssertTodoRows(t, got, want)
|
|
})
|
|
|
|
t.Run("deletes todo", func(t *testing.T) {
|
|
err := r.Delete(ctx, 1)
|
|
AssertNoError(t, err)
|
|
|
|
want := repository.ErrNotFound
|
|
_, got := r.GetById(ctx, 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)
|
|
}
|
|
}
|