show-rss/src/feeds/db_test.go

87 lines
1.7 KiB
Go

package feeds_test
import (
"context"
"show-rss/src/db"
"show-rss/src/feeds"
"strconv"
"testing"
"time"
)
func TestFeeds(t *testing.T) {
ctx, can := context.WithTimeout(context.Background(), 5*time.Second)
defer can()
t.Run("same ctx", func(t *testing.T) {
ctx := db.Test(t, ctx)
for i := 0; i < 2; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if _, err := feeds.New(ctx); err != nil && ctx.Err() == nil {
t.Fatalf("failed %d: %v", i, err)
}
})
}
})
t.Run("new ctx", func(t *testing.T) {
for i := 0; i < 2; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if _, err := feeds.New(db.Test(t, ctx)); err != nil && ctx.Err() == nil {
t.Fatalf("failed %d: %v", i, err)
}
})
}
})
t.Run("crud", func(t *testing.T) {
ctx := db.Test(t, ctx)
f, err := feeds.New(ctx)
if err != nil && ctx.Err() == nil {
t.Fatalf("failed: %v", err)
}
id, err := f.Insert(ctx, "url", "cron")
if err != nil {
t.Fatal("cannot insert:", err)
}
got, err := f.Get(ctx, id)
if err != nil {
t.Fatal("cannot get:", err)
}
t.Logf("%+v", got)
if got.Entry.ID == "" {
t.Error("no entry.id")
}
if got.Entry.Created.IsZero() {
t.Error("no entry.created")
}
if got.Entry.Updated.IsZero() {
t.Error("no entry.updated")
}
if !got.Entry.Deleted.IsZero() {
t.Error("entry.deleted")
}
if got.Version.Created.IsZero() {
t.Error("no version.created")
}
if got.Version.URL != "url" {
t.Error("no version.url")
}
if got.Version.Cron != "cron" {
t.Error("no version.cron")
}
if !got.Execution.Executed.IsZero() {
t.Error("execution.executed")
}
if !got.Execution.Version.IsZero() {
t.Error("execution.version")
}
})
}