package db import ( "context" "os" "strconv" "testing" "time" ) func TestStoreLifecycle(t *testing.T) { databaseURL := os.Getenv("TEST_DATABASE_URL") if databaseURL == "" { t.Skip("TEST_DATABASE_URL is not set") } store, err := Open(databaseURL) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = store.Close() }) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() title := "store-test-" + strconv.FormatInt(time.Now().UnixNano(), 10) if err := store.Create(ctx, title); err != nil { t.Fatal(err) } todos, err := store.List(ctx) if err != nil { t.Fatal(err) } var id int64 for _, todo := range todos { if todo.Title == title { id = todo.ID break } } if id == 0 { t.Fatalf("created todo %q not found", title) } t.Cleanup(func() { _, _ = store.Delete(context.Background(), id) }) found, err := store.Toggle(ctx, id) if err != nil || !found { t.Fatalf("toggle found = %v, error = %v", found, err) } found, err = store.Delete(ctx, id) if err != nil || !found { t.Fatalf("delete found = %v, error = %v", found, err) } found, err = store.Delete(ctx, id) if err != nil || found { t.Fatalf("second delete found = %v, error = %v", found, err) } }