From 41ac01242b41614d8fdbb5f4469d7a8771a9450e Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Fri, 6 May 2022 11:32:58 +0200 Subject: [PATCH] Add eventJSON tests --- .../storage/postgres/event_json_table.go | 4 +- roomserver/storage/postgres/storage.go | 4 +- .../storage/sqlite3/event_json_table.go | 4 +- roomserver/storage/sqlite3/storage.go | 4 +- .../storage/tables/event_json_table_test.go | 80 +++++++++++++++++++ roomserver/storage/tables/interface.go | 5 +- 6 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 roomserver/storage/tables/event_json_table_test.go diff --git a/roomserver/storage/postgres/event_json_table.go b/roomserver/storage/postgres/event_json_table.go index b3220effd..d8fc80bce 100644 --- a/roomserver/storage/postgres/event_json_table.go +++ b/roomserver/storage/postgres/event_json_table.go @@ -59,12 +59,12 @@ type eventJSONStatements struct { bulkSelectEventJSONStmt *sql.Stmt } -func createEventJSONTable(db *sql.DB) error { +func CreateEventJSONTable(db *sql.DB) error { _, err := db.Exec(eventJSONSchema) return err } -func prepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) { +func PrepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) { s := &eventJSONStatements{} return s, sqlutil.StatementList{ diff --git a/roomserver/storage/postgres/storage.go b/roomserver/storage/postgres/storage.go index da8d25848..5cab67c21 100644 --- a/roomserver/storage/postgres/storage.go +++ b/roomserver/storage/postgres/storage.go @@ -74,7 +74,7 @@ func (d *Database) create(db *sql.DB) error { if err := createEventTypesTable(db); err != nil { return err } - if err := createEventJSONTable(db); err != nil { + if err := CreateEventJSONTable(db); err != nil { return err } if err := createEventsTable(db); err != nil { @@ -120,7 +120,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room if err != nil { return err } - eventJSON, err := prepareEventJSONTable(db) + eventJSON, err := PrepareEventJSONTable(db) if err != nil { return err } diff --git a/roomserver/storage/sqlite3/event_json_table.go b/roomserver/storage/sqlite3/event_json_table.go index f470ea326..086b18bb9 100644 --- a/roomserver/storage/sqlite3/event_json_table.go +++ b/roomserver/storage/sqlite3/event_json_table.go @@ -52,12 +52,12 @@ type eventJSONStatements struct { bulkSelectEventJSONStmt *sql.Stmt } -func createEventJSONTable(db *sql.DB) error { +func CreateEventJSONTable(db *sql.DB) error { _, err := db.Exec(eventJSONSchema) return err } -func prepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) { +func PrepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) { s := &eventJSONStatements{ db: db, } diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go index e6cf1a53f..42723058d 100644 --- a/roomserver/storage/sqlite3/storage.go +++ b/roomserver/storage/sqlite3/storage.go @@ -83,7 +83,7 @@ func (d *Database) create(db *sql.DB) error { if err := createEventTypesTable(db); err != nil { return err } - if err := createEventJSONTable(db); err != nil { + if err := CreateEventJSONTable(db); err != nil { return err } if err := createEventsTable(db); err != nil { @@ -129,7 +129,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room if err != nil { return err } - eventJSON, err := prepareEventJSONTable(db) + eventJSON, err := PrepareEventJSONTable(db) if err != nil { return err } diff --git a/roomserver/storage/tables/event_json_table_test.go b/roomserver/storage/tables/event_json_table_test.go new file mode 100644 index 000000000..a14653b4b --- /dev/null +++ b/roomserver/storage/tables/event_json_table_test.go @@ -0,0 +1,80 @@ +package tables_test + +import ( + "context" + "fmt" + "reflect" + "testing" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/dendrite/roomserver/storage/postgres" + "github.com/matrix-org/dendrite/roomserver/storage/sqlite3" + "github.com/matrix-org/dendrite/roomserver/storage/tables" + "github.com/matrix-org/dendrite/roomserver/types" + "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/dendrite/test" +) + +func mustCreateTable(t *testing.T, dbType test.DBType) (tables.EventJSON, func()) { + t.Helper() + connStr, close := test.PrepareDBConnectionString(t, dbType) + db, err := sqlutil.Open(&config.DatabaseOptions{ + ConnectionString: config.DataSource(connStr), + }, sqlutil.NewExclusiveWriter()) + if err != nil { + t.Fatalf("failed to open db: %s", err) + } + var tab tables.EventJSON + switch dbType { + case test.DBTypePostgres: + err = postgres.CreateEventJSONTable(db) + if err != nil { + t.Fatalf("failed to create EventJSON table: %s", err) + } + tab, err = postgres.PrepareEventJSONTable(db) + case test.DBTypeSQLite: + err = sqlite3.CreateEventJSONTable(db) + if err != nil { + t.Fatalf("failed to create EventJSON table: %s", err) + } + tab, err = sqlite3.PrepareEventJSONTable(db) + } + if err != nil { + t.Fatalf("failed to create table: %s", err) + } + + return tab, close +} + +func Test_EventJSONTable(t *testing.T) { + test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) { + tab, close := mustCreateTable(t, dbType) + defer close() + // create some dummy data + for i := 0; i < 10; i++ { + if err := tab.InsertEventJSON( + context.Background(), nil, types.EventNID(i), + []byte(fmt.Sprintf(`{"value":%d"}`, i)), + ); err != nil { + t.Fatalf("unable to insert eventJSON: %s", err) + } + } + // select a subset of the data + values, err := tab.BulkSelectEventJSON(context.Background(), nil, []types.EventNID{1, 2, 3, 4, 5}) + if err != nil { + t.Fatalf("unable to query eventJSON: %s", err) + } + if len(values) != 5 { + t.Fatalf("expected 5 events, got %d", len(values)) + } + for i, v := range values { + if v.EventNID != types.EventNID(i+1) { + t.Fatalf("expected eventNID %d, got %d", i+1, v.EventNID) + } + wantValue := []byte(fmt.Sprintf(`{"value":%d"}`, i+1)) + if !reflect.DeepEqual(wantValue, v.EventJSON) { + t.Fatalf("expected JSON to be %s, got %s", string(wantValue), string(v.EventJSON)) + } + } + }) +} diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go index 97e4afcff..b05647626 100644 --- a/roomserver/storage/tables/interface.go +++ b/roomserver/storage/tables/interface.go @@ -10,9 +10,8 @@ import ( ) type EventJSONPair struct { - EventNID types.EventNID - RoomVersion gomatrixserverlib.RoomVersion - EventJSON []byte + EventNID types.EventNID + EventJSON []byte } type EventJSON interface {