2022-04-08 17:53:24 +01:00
|
|
|
package tables_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/postgres"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/sqlite3"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2023-04-04 18:16:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2022-04-08 17:53:24 +01:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
2022-04-13 12:16:02 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-04-08 17:53:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func newOutputRoomEventsTable(t *testing.T, dbType test.DBType) (tables.Events, *sql.DB, func()) {
|
|
|
|
t.Helper()
|
|
|
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
|
|
db, err := sqlutil.Open(&config.DatabaseOptions{
|
|
|
|
ConnectionString: config.DataSource(connStr),
|
2022-05-03 16:35:06 +01:00
|
|
|
}, sqlutil.NewExclusiveWriter())
|
2022-04-08 17:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open db: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tab tables.Events
|
|
|
|
switch dbType {
|
|
|
|
case test.DBTypePostgres:
|
|
|
|
tab, err = postgres.NewPostgresEventsTable(db)
|
|
|
|
case test.DBTypeSQLite:
|
|
|
|
var stream sqlite3.StreamIDStatements
|
|
|
|
if err = stream.Prepare(db); err != nil {
|
|
|
|
t.Fatalf("failed to prepare stream stmts: %s", err)
|
|
|
|
}
|
|
|
|
tab, err = sqlite3.NewSqliteEventsTable(db, &stream)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to make new table: %s", err)
|
|
|
|
}
|
|
|
|
return tab, db, close
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutputRoomEventsTable(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-05-17 13:23:35 +01:00
|
|
|
alice := test.NewUser(t)
|
2022-04-08 17:53:24 +01:00
|
|
|
room := test.NewRoom(t, alice)
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
tab, db, close := newOutputRoomEventsTable(t, dbType)
|
|
|
|
defer close()
|
|
|
|
events := room.Events()
|
|
|
|
err := sqlutil.WithTransaction(db, func(txn *sql.Tx) error {
|
|
|
|
for _, ev := range events {
|
2022-07-18 13:46:15 +01:00
|
|
|
_, err := tab.InsertEvent(ctx, txn, ev, nil, nil, nil, false, gomatrixserverlib.HistoryVisibilityShared)
|
2022-04-08 17:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to InsertEvent: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// order = 2,0,3,1
|
|
|
|
wantEventIDs := []string{
|
|
|
|
events[2].EventID(), events[0].EventID(), events[3].EventID(), events[1].EventID(),
|
|
|
|
}
|
2022-04-13 12:16:02 +01:00
|
|
|
gotEvents, err := tab.SelectEvents(ctx, txn, wantEventIDs, nil, true)
|
2022-04-08 17:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to SelectEvents: %s", err)
|
|
|
|
}
|
|
|
|
gotEventIDs := make([]string, len(gotEvents))
|
|
|
|
for i := range gotEvents {
|
|
|
|
gotEventIDs[i] = gotEvents[i].EventID()
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotEventIDs, wantEventIDs) {
|
|
|
|
return fmt.Errorf("SelectEvents\ngot %v\n want %v", gotEventIDs, wantEventIDs)
|
|
|
|
}
|
|
|
|
|
2022-04-13 12:16:02 +01:00
|
|
|
// Test that contains_url is correctly populated
|
|
|
|
urlEv := room.CreateEvent(t, alice, "m.text", map[string]interface{}{
|
|
|
|
"body": "test.txt",
|
|
|
|
"url": "mxc://test.txt",
|
|
|
|
})
|
2022-07-18 13:46:15 +01:00
|
|
|
if _, err = tab.InsertEvent(ctx, txn, urlEv, nil, nil, nil, false, gomatrixserverlib.HistoryVisibilityShared); err != nil {
|
2022-04-13 12:16:02 +01:00
|
|
|
return fmt.Errorf("failed to InsertEvent: %s", err)
|
|
|
|
}
|
|
|
|
wantEventID := []string{urlEv.EventID()}
|
|
|
|
t := true
|
2023-04-04 18:16:53 +01:00
|
|
|
gotEvents, err = tab.SelectEvents(ctx, txn, wantEventID, &synctypes.RoomEventFilter{Limit: 1, ContainsURL: &t}, true)
|
2022-04-13 12:16:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to SelectEvents: %s", err)
|
|
|
|
}
|
|
|
|
gotEventIDs = make([]string, len(gotEvents))
|
|
|
|
for i := range gotEvents {
|
|
|
|
gotEventIDs[i] = gotEvents[i].EventID()
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotEventIDs, wantEventID) {
|
|
|
|
return fmt.Errorf("SelectEvents\ngot %v\n want %v", gotEventIDs, wantEventID)
|
|
|
|
}
|
|
|
|
|
2022-04-08 17:53:24 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-04-17 10:25:33 +01:00
|
|
|
|
|
|
|
func TestReindex(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
room := test.NewRoom(t, alice)
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
room.CreateAndInsert(t, alice, spec.MRoomName, map[string]interface{}{
|
2023-04-17 10:25:33 +01:00
|
|
|
"name": "my new room name",
|
|
|
|
}, test.WithStateKey(""))
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
room.CreateAndInsert(t, alice, spec.MRoomTopic, map[string]interface{}{
|
2023-04-17 10:25:33 +01:00
|
|
|
"topic": "my new room topic",
|
|
|
|
}, test.WithStateKey(""))
|
|
|
|
|
|
|
|
room.CreateAndInsert(t, alice, "m.room.message", map[string]interface{}{
|
|
|
|
"msgbody": "my room message",
|
|
|
|
"type": "m.text",
|
|
|
|
})
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
tab, db, close := newOutputRoomEventsTable(t, dbType)
|
|
|
|
defer close()
|
|
|
|
err := sqlutil.WithTransaction(db, func(txn *sql.Tx) error {
|
|
|
|
for _, ev := range room.Events() {
|
|
|
|
_, err := tab.InsertEvent(ctx, txn, ev, nil, nil, nil, false, gomatrixserverlib.HistoryVisibilityShared)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to InsertEvent: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := tab.ReIndex(ctx, nil, 10, 0, []string{
|
2023-04-19 15:50:33 +01:00
|
|
|
spec.MRoomName,
|
|
|
|
spec.MRoomTopic,
|
2023-04-17 10:25:33 +01:00
|
|
|
"m.room.message"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wantEventCount := 3
|
|
|
|
if len(events) != wantEventCount {
|
|
|
|
t.Fatalf("expected %d events, got %d", wantEventCount, len(events))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|