feat(m1): backend scaffold - entries CRUD, start/stop, auth, migrations
This commit is contained in:
136
internal/service/entry_service_test.go
Normal file
136
internal/service/entry_service_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/wotra/wotra/internal/service"
|
||||
"github.com/wotra/wotra/internal/store"
|
||||
)
|
||||
|
||||
func newTestServices(t *testing.T) *service.EntryService {
|
||||
t.Helper()
|
||||
db, err := store.Open(":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
entryStore := store.NewEntryStore(db)
|
||||
closedDayStore := store.NewClosedDayStore(db)
|
||||
settingsStore := store.NewSettingsStore(db)
|
||||
|
||||
tz, _ := time.LoadLocation("UTC")
|
||||
return service.NewEntryService(entryStore, closedDayStore, settingsStore, tz)
|
||||
}
|
||||
|
||||
func TestStartStop(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
entry, err := svc.Start(ctx, "test entry")
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
if entry.ID == "" {
|
||||
t.Fatal("expected non-empty ID")
|
||||
}
|
||||
if entry.EndTime != nil {
|
||||
t.Fatal("expected running entry (nil EndTime)")
|
||||
}
|
||||
|
||||
stopped, err := svc.Stop(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Stop: %v", err)
|
||||
}
|
||||
if stopped.EndTime == nil {
|
||||
t.Fatal("expected entry to be stopped")
|
||||
}
|
||||
if *stopped.EndTime < stopped.StartTime {
|
||||
t.Fatal("end_time before start_time")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTwiceFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
if _, err := svc.Start(ctx, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := svc.Start(ctx, "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error starting second entry")
|
||||
}
|
||||
if err != service.ErrEntryRunning {
|
||||
t.Fatalf("expected ErrEntryRunning, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopWithoutStart(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
_, err := svc.Stop(ctx)
|
||||
if err != service.ErrEntryNotRunning {
|
||||
t.Fatalf("expected ErrEntryNotRunning, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateEntry(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
entry, _ := svc.Start(ctx, "initial note")
|
||||
stopped, _ := svc.Stop(ctx)
|
||||
|
||||
note := "updated note"
|
||||
updated, err := svc.Update(ctx, stopped.ID, service.UpdateEntryInput{Note: ¬e})
|
||||
if err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
if updated.Note != "updated note" {
|
||||
t.Errorf("expected 'updated note', got %q", updated.Note)
|
||||
}
|
||||
_ = entry
|
||||
}
|
||||
|
||||
func TestDeleteEntry(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
entry, _ := svc.Start(ctx, "")
|
||||
svc.Stop(ctx)
|
||||
|
||||
if err := svc.Delete(ctx, entry.ID); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
|
||||
entries, err := svc.List(ctx, "0000-01-01", "9999-12-31")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("expected 0 entries after delete, got %d", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListEntries(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newTestServices(t)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
svc.Start(ctx, "")
|
||||
svc.Stop(ctx)
|
||||
}
|
||||
|
||||
today := time.Now().UTC().Format("2006-01-02")
|
||||
entries, err := svc.List(ctx, today, today)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 3 {
|
||||
t.Fatalf("expected 3 entries, got %d", len(entries))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user