Replace AddItem with AddItems

This commit is contained in:
Tim Van Baak 2025-01-29 07:43:06 -08:00
parent 421271e2c3
commit 453bc9d601
4 changed files with 26 additions and 37 deletions

View File

@ -59,8 +59,15 @@ func itemAdd() {
db := openAndMigrateDb() db := openAndMigrateDb()
err := core.AddItem(db, addSource, addId, addTitle, addAuthor, addBody, addLink, addTime) if err := core.AddItems(db, []core.Item{{
if err != nil { Source: addSource,
Id: addId,
Title: addTitle,
Author: addAuthor,
Body: addBody,
Link: addLink,
Time: addTime,
}}); err != nil {
log.Fatalf("Failed to add item: %s", err) log.Fatalf("Failed to add item: %s", err)
} }

View File

@ -11,21 +11,21 @@ func TestDeleteSourceCascade(t *testing.T) {
db := EphemeralDb(t) db := EphemeralDb(t)
if err := AddSource(db, "source1"); err != nil { if err := AddSource(db, "source1"); err != nil {
t.Fatal(err) t.Fatalf("failed to add source1: %v", err)
} }
if err := AddSource(db, "source2"); err != nil { if err := AddSource(db, "source2"); err != nil {
t.Fatal(err) t.Fatalf("failed to add source2: %v", err)
} }
if err := AddItem(db, "source1", "item1", "", "", "", "", 0); err != nil { if err := AddItems(db, []Item{
t.Fatal(err) {"source1", "item1", 0, true, "", "", "", "", 0},
} {"source2", "item2", 0, true, "", "", "", "", 0},
if err := AddItem(db, "source2", "item2", "", "", "", "", 0); err != nil { }); err != nil {
t.Fatal(err) t.Fatalf("failed to add items: %v", err)
} }
items, err := GetAllActiveItems(db) items, err := GetAllActiveItems(db)
if err != nil { if err != nil {
t.Fatal(err) t.Fatalf("failed to get active items: %v", err)
} }
if len(items) != 2 { if len(items) != 2 {
t.Fatal("Expected 2 items") t.Fatal("Expected 2 items")
@ -39,10 +39,10 @@ func TestDeleteSourceCascade(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if len(items) != 1 { if len(items) != 1 {
t.Fatal("Expected only 1 item after source delete") t.Fatalf("Expected only 1 item after source delete, got %d", len(items))
} }
err = AddItem(db, "source1", "item3", "", "", "", "", 0) err = AddItems(db, []Item{{"source1", "item3", 0, true, "", "", "", "", 0}})
if err == nil { if err == nil {
t.Fatal("Unexpected success adding item for nonexistent source") t.Fatal("Unexpected success adding item for nonexistent source")
} }

View File

@ -48,24 +48,6 @@ func DeleteSource(db *DB, name string) error {
return err return err
} }
func AddItem(
db *DB,
source string,
id string,
title string,
author string,
body string,
link string,
time int,
) error {
_, err := db.Exec(`
insert into items (source, id, active, title, author, body, link, time)
values (?, ?, ?, ?, ?, ?, ?, ?)
`, source, id, true, title, author, body, link, time)
return err
}
func AddItems(db *DB, items []Item) error { func AddItems(db *DB, items []Item) error {
return db.Transact(func(tx *sql.Tx) error { return db.Transact(func(tx *sql.Tx) error {
stmt, err := tx.Prepare(` stmt, err := tx.Prepare(`

View File

@ -57,18 +57,18 @@ func AssertItemIs(t *testing.T, item Item, expected string) {
func TestAddItem(t *testing.T) { func TestAddItem(t *testing.T) {
db := EphemeralDb(t) db := EphemeralDb(t)
if err := AddSource(db, "test"); err != nil { if err := AddSource(db, "test"); err != nil {
t.Fatal(err) t.Fatalf("failed to add source: %v", err)
} }
if err := AddItem(db, "test", "one", "", "", "", "", 0); err != nil { if err := AddItems(db, []Item{
t.Fatal(err) {"test", "one", 0, true, "", "", "", "", 0},
} {"test", "two", 0, true, "title", "author", "body", "link", 123456},
if err := AddItem(db, "test", "two", "title", "author", "body", "link", 123456); err != nil { }); err != nil {
t.Fatal(err) t.Fatalf("failed to add items: %v", err)
} }
items, err := GetActiveItemsForSource(db, "test") items, err := GetActiveItemsForSource(db, "test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatalf("failed to get active items: %v", err)
} }
if len(items) != 2 { if len(items) != 2 {
t.Fatal("should get two items") t.Fatal("should get two items")