package feeds_test import ( "context" "show-rss/src/db" "show-rss/src/feeds" "testing" "time" ) func TestFeeds(t *testing.T) { ctx, can := context.WithTimeout(context.Background(), 5*time.Second) defer can() t.Run("crud", func(t *testing.T) { ctx := db.Test(t, ctx) id, err := feeds.Insert(ctx, "url", "cron") if err != nil { t.Fatal("cannot insert:", err) } got, err := feeds.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") } if err := got.Executed(ctx); err != nil { t.Fatal("cannot executed:", err) } got2, err := feeds.Get(ctx, id) if err != nil { t.Fatal("cannot get w executed:", err) } t.Logf("%+v", got2) if got2.Execution.Executed.IsZero() { t.Error("no execution.executed") } if got2.Execution.Version != got.Version.Created { t.Errorf("bad execution.version: expected %v but got %v (difference of %v)", got.Version.Created, got2.Execution.Version, got2.Execution.Version.Sub(got.Execution.Version)) } got2.Execution = got.Execution if got != got2 { t.Errorf("changes after execution: was \n\t%+v but now \n\t%+v", got, got2) } if err := got.Executed(ctx); err != nil { t.Fatal("cannot executed again:", err) } got3, err := feeds.Get(ctx, id) if err != nil { t.Fatal("cannot get w executed again:", err) } else if got2.Execution == got3.Execution { t.Errorf("getting after second execution returned first execution") } n := 0 if err := feeds.ForEach(ctx, func(feed feeds.Feed) error { n += 1 if feed != got3 { t.Errorf("for each yielded difference than last get") } return nil }); err != nil { t.Error(err) } else if n == 0 { t.Errorf("for each didnt hit known get") } }) }