2020-07-28 16:38:30 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-07-30 13:52:21 +00:00
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
2020-07-28 16:38:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
|
|
|
func MustNotError(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("operation failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyChanges(t *testing.T) {
|
|
|
|
db, err := NewDatabase("file::memory:", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to NewDatabase: %s", err)
|
|
|
|
}
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 0, "@alice:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 1, "@bob:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 2, "@charlie:localhost"))
|
2020-07-30 13:52:21 +00:00
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, 0, 1, sarama.OffsetNewest)
|
2020-07-28 16:38:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
|
|
|
if latest != 2 {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want 2", latest)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@charlie:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyChangesNoDupes(t *testing.T) {
|
|
|
|
db, err := NewDatabase("file::memory:", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to NewDatabase: %s", err)
|
|
|
|
}
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 0, "@alice:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 1, "@alice:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 2, "@alice:localhost"))
|
2020-07-30 13:52:21 +00:00
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, 0, 0, sarama.OffsetNewest)
|
2020-07-28 16:38:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
|
|
|
if latest != 2 {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want 2", latest)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@alice:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 13:52:21 +00:00
|
|
|
|
|
|
|
func TestKeyChangesUpperLimit(t *testing.T) {
|
|
|
|
db, err := NewDatabase("file::memory:", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to NewDatabase: %s", err)
|
|
|
|
}
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 0, "@alice:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 1, "@bob:localhost"))
|
|
|
|
MustNotError(t, db.StoreKeyChange(ctx, 0, 2, "@charlie:localhost"))
|
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, 0, 0, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
|
|
|
if latest != 1 {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want 1", latest)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@bob:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|