70 lines
970 B
Go
70 lines
970 B
Go
package outbox_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"code.uint32.ru/tiny/outbox"
|
|
)
|
|
|
|
type My struct {
|
|
A string
|
|
B int
|
|
}
|
|
|
|
func TestOutboxMethods(t *testing.T) {
|
|
db, err := sql.Open("sqlite3", "test.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer os.Remove("test.db")
|
|
defer db.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
box, err := outbox.OpenOrCreate[My](ctx, db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
one := My{
|
|
A: "hello",
|
|
B: 42,
|
|
}
|
|
|
|
if err := box.Save(ctx, "test", &one); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
lst, err := box.GetPending(ctx, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(lst) != 1 {
|
|
t.Fatal("incorrect pending len")
|
|
}
|
|
|
|
if err := box.MarkProcessed(ctx, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
lst, err = box.GetPending(ctx, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(lst) != 0 {
|
|
t.Fatal("should be zero pending")
|
|
}
|
|
|
|
if err := box.Cleanup(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|