79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
package core
|
|
|
|
import "testing"
|
|
|
|
func TestChannel(t *testing.T) {
|
|
db := EphemeralDb(t)
|
|
if err := AddSource(db, "one"); err != nil {
|
|
t.Fatalf("failed to add source: %v", err)
|
|
}
|
|
if err := AddSource(db, "two"); err != nil {
|
|
t.Fatalf("failed to add source: %v", err)
|
|
}
|
|
|
|
// Add sources to channel
|
|
if err := AddSourceToChannel(db, "channel", "one"); err != nil {
|
|
t.Fatalf("failed to add source to channel: %v", err)
|
|
}
|
|
if err := AddSourceToChannel(db, "channel", "two"); err != nil {
|
|
t.Fatalf("failed to add source to channel: %v", err)
|
|
}
|
|
|
|
// Both sources are in the channel
|
|
sources, err := GetSourcesInChannel(db)
|
|
if err != nil {
|
|
t.Fatalf("failed to get sources in channel: %v", err)
|
|
}
|
|
if len(sources["channel"]) != 2 || sources["channel"][0] != "one" || sources["channel"][1] != "two" {
|
|
t.Fatalf("expected two sources, got %d: %v", len(sources), sources)
|
|
}
|
|
|
|
// Get sources in channel after deletion
|
|
if err := DeleteSourceFromChannel(db, "channel", "one"); err != nil {
|
|
t.Fatalf("failed to delete source from channel: %v", err)
|
|
}
|
|
sources, err = GetSourcesInChannel(db)
|
|
if err != nil {
|
|
t.Fatalf("failed to get sources in channel: %v", err)
|
|
}
|
|
if len(sources) != 1 || sources["channel"][0] != "two" {
|
|
t.Fatalf("unexpected sources in channel after deletion: %v", sources)
|
|
}
|
|
if err := AddSourceToChannel(db, "channel", "one"); err != nil {
|
|
t.Fatalf("failed to add source to channel: %v", err)
|
|
}
|
|
|
|
// Items on both sources appear in the channel
|
|
if err := AddItems(db, []Item{
|
|
{Source: "one", Id: "a"},
|
|
{Source: "two", Id: "b"},
|
|
}); err != nil {
|
|
t.Fatalf("failed to add items to one: %v", err)
|
|
}
|
|
if counts, err := GetChannelsAndActiveCounts(db); counts["channel"] != 2 || err != nil {
|
|
t.Fatalf("expected 2 active items in channel, got %d: %v", counts["channel"], err)
|
|
}
|
|
|
|
if _, err := DeactivateItem(db, "one", "a"); err != nil {
|
|
t.Fatalf("failed to deactivate item: %v", err)
|
|
}
|
|
if counts, err := GetChannelsAndActiveCounts(db); counts["channel"] != 1 || err != nil {
|
|
t.Fatalf("expected 1 active items in channel, got %d: %v", counts["channel"], err)
|
|
}
|
|
|
|
items, err := GetAllItemsForChannel(db, "channel", 0, -1)
|
|
if err != nil {
|
|
t.Fatalf("failed to get all items in channel: %v", err)
|
|
}
|
|
if len(items) != 2 || items[0].Id != "a" || items[1].Id != "b" {
|
|
t.Fatalf("expected two items, got %d: %v", len(items), items)
|
|
}
|
|
items, err = GetActiveItemsForChannel(db, "channel", 0, -1)
|
|
if err != nil {
|
|
t.Fatalf("failed to get all items in channel: %v", err)
|
|
}
|
|
if len(items) != 1 || items[0].Id != "b" {
|
|
t.Fatalf("expected one item, got %d: %v", len(items), items)
|
|
}
|
|
}
|