diff --git a/cmd/actionExecute.go b/cmd/actionExecute.go index d2623da..6037d59 100644 --- a/cmd/actionExecute.go +++ b/cmd/actionExecute.go @@ -108,7 +108,7 @@ func actionExecute() { if 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") } } diff --git a/core/item.go b/core/item.go index f805004..87d79d8 100644 --- a/core/item.go +++ b/core/item.go @@ -23,6 +23,11 @@ func (item Item) Deletable() bool { 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 { title := item.Title if title == "" { diff --git a/core/item_test.go b/core/item_test.go new file mode 100644 index 0000000..2a5557f --- /dev/null +++ b/core/item_test.go @@ -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) + } +} diff --git a/core/source_test.go b/core/source_test.go index 838d178..f8bc0cd 100644 --- a/core/source_test.go +++ b/core/source_test.go @@ -205,7 +205,7 @@ func TestOnCreateAction(t *testing.T) { } updated := getItem("one") 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]) }