Check Item equality with a function

Using == won't work when the Action field is a map[string]RawMessage
This commit is contained in:
Tim Van Baak 2025-01-29 08:14:49 -08:00
parent 453bc9d601
commit d23efdf00b
4 changed files with 52 additions and 2 deletions

View File

@ -108,7 +108,7 @@ func actionExecute() {
if item.Time != newItem.Time { if item.Time != newItem.Time {
log.Printf("time: %d => %d", item.Time, newItem.Time) log.Printf("time: %d => %d", item.Time, newItem.Time)
} }
if item == newItem { if core.ItemsAreEqual(item, newItem) {
log.Printf("no changes\n") log.Printf("no changes\n")
} }
} }

View File

@ -23,6 +23,11 @@ func (item Item) Deletable() bool {
return !item.Active return !item.Active
} }
func ItemsAreEqual(first Item, second Item) bool {
// Hacky but easy to use
return fmt.Sprintf("%#v", first) == fmt.Sprintf("%#v", second)
}
func FormatAsHeadline(item Item) string { func FormatAsHeadline(item Item) string {
title := item.Title title := item.Title
if title == "" { if title == "" {

45
core/item_test.go Normal file
View File

@ -0,0 +1,45 @@
package core
import "testing"
func TestItemFormatsExist(t *testing.T) {
for name := range AvailableFormats {
formatter, err := FormatAs(name)
if err != nil {
t.Fatalf("error getting formatter for available format %s: %v", name, err)
}
if formatter == nil {
t.Fatalf("formatter %s is nil", name)
}
}
}
func TestItemRoundTrip(t *testing.T) {
db := EphemeralDb(t)
if err := AddSource(db, "_"); err != nil {
t.Fatalf("failed to create source: %v", err)
}
item1 := Item{
Source: "_",
Id: "a",
Created: 0,
Active: true,
Title: "title",
Author: "author",
Body: "body",
Link: "link",
Time: 123456,
}
if err := AddItems(db, []Item{item1}); err != nil {
t.Fatalf("update failed: %v", err)
}
item2, err := GetItem(db, item1.Source, item1.Id)
if err != nil {
t.Fatalf("could not get item: %v", err)
}
item2.Created = 0 // automatically set by db
if !ItemsAreEqual(item1, item2) {
t.Fatalf("items are not equal, err %v", err)
}
}

View File

@ -205,7 +205,7 @@ func TestOnCreateAction(t *testing.T) {
} }
updated := getItem("one") updated := getItem("one")
updated.Created = 0 // zero out for comparison with pre-insert item updated.Created = 0 // zero out for comparison with pre-insert item
if updated != items[0] { if !ItemsAreEqual(updated, items[0]) {
t.Fatalf("expected no change: %#v != %#v", updated, items[0]) t.Fatalf("expected no change: %#v != %#v", updated, items[0])
} }