71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
//func newStorageFromTestdata(t *testing.T) {
|
|
|
|
func TestStorage(t *testing.T) {
|
|
ctx, can := context.WithTimeout(context.Background(), time.Second)
|
|
defer can()
|
|
|
|
t.Run("Threads", func(t *testing.T) {
|
|
s := NewStorage(NewRAM())
|
|
|
|
mX1 := Message{ID: "1", Thread: "X", TS: 1}
|
|
mX2 := Message{ID: "2", Thread: "X", TS: 2}
|
|
mY1 := Message{ID: "1", Thread: "Y", TS: 3}
|
|
|
|
for _, m := range []Message{mX1, mX2, mY1} {
|
|
if err := s.Upsert(ctx, m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if threads, err := s.Threads(ctx); err != nil {
|
|
t.Error(err)
|
|
} else if len(threads) != 2 {
|
|
t.Error(threads)
|
|
} else if !slices.Contains(threads, "X") {
|
|
t.Error(threads, "X")
|
|
} else if !slices.Contains(threads, "Y") {
|
|
t.Error(threads, "Y")
|
|
}
|
|
|
|
if threads, err := s.ThreadsSince(ctx, time.Unix(3, 0)); err != nil {
|
|
t.Error(err)
|
|
} else if len(threads) != 1 {
|
|
t.Error(threads)
|
|
} else if threads[0] != "Y" {
|
|
t.Error(threads[0])
|
|
}
|
|
})
|
|
|
|
t.Run("Get Upsert", func(t *testing.T) {
|
|
s := NewStorage(NewRAM())
|
|
|
|
if _, err := s.Get(ctx, "id"); err != ErrNotFound {
|
|
t.Error("failed to get 404", err)
|
|
}
|
|
|
|
m := Message{
|
|
ID: "id",
|
|
TS: 1,
|
|
}
|
|
|
|
if err := s.Upsert(ctx, m); err != nil {
|
|
t.Error("failed to upsert", err)
|
|
}
|
|
|
|
if m2, err := s.Get(ctx, "id"); err != nil {
|
|
t.Error("failed to get", err)
|
|
} else if m != m2 {
|
|
t.Error(m2)
|
|
}
|
|
})
|
|
}
|