intake/core/source_test.go

121 lines
2.3 KiB
Go

package core
import (
"fmt"
"slices"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func TestCreateSource(t *testing.T) {
db := EphemeralDb(t)
if err := AddSource(db, "one"); err != nil {
t.Fatal(err)
}
if err := AddSource(db, "two"); err != nil {
t.Fatal(err)
}
if err := AddSource(db, "three"); err != nil {
t.Fatal(err)
}
if err := DeleteSource(db, "two"); err != nil {
t.Fatal(err)
}
names, err := GetSources(db)
if err != nil {
t.Fatal(err)
}
expected := []string{"one", "three"}
for i := 0; i < len(expected); i += 1 {
if !slices.Contains(names, expected[i]) {
t.Fatalf("missing %s, have: %v", expected[i], names)
}
}
}
func AssertItemIs(t *testing.T, item Item, expected string) {
actual := fmt.Sprintf(
"%s/%s/%t/%s/%s/%s/%s/%d",
item.Source,
item.Id,
item.Active,
item.Title,
item.Author,
item.Body,
item.Link,
item.Time,
)
if actual != expected {
t.Fatalf("expected %s, got %s", expected, actual)
}
}
func TestAddItem(t *testing.T) {
db := EphemeralDb(t)
if err := AddSource(db, "test"); err != nil {
t.Fatal(err)
}
if err := AddItem(db, "test", "one", "", "", "", "", 0); err != nil {
t.Fatal(err)
}
if err := AddItem(db, "test", "two", "title", "author", "body", "link", 123456); err != nil {
t.Fatal(err)
}
items, err := GetActiveItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 2 {
t.Fatal("should get two items")
}
AssertItemIs(t, items[0], "test/one/true/////0")
AssertItemIs(t, items[1], "test/two/true/title/author/body/link/123456")
if _, err = DeactivateItem(db, "test", "one"); err != nil {
t.Fatal(err)
}
items, err = GetActiveItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 1 {
t.Fatal("should get one item")
}
items, err = GetAllItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 2 {
t.Fatal("should get two items")
}
deleted, err := DeleteItem(db, "test", "one")
if err != nil {
t.Fatal(err)
}
if deleted != 1 {
t.Fatal("expected one deletion")
}
deleted, err = DeleteItem(db, "test", "one")
if err != nil {
t.Fatal(err)
}
if deleted != 0 {
t.Fatal("expected no deletion")
}
items, err = GetAllItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 1 {
t.Fatal("should get one item")
}
}