Add eventJSON tests
This commit is contained in:
parent
4705f5761e
commit
41ac01242b
|
@ -59,12 +59,12 @@ type eventJSONStatements struct {
|
||||||
bulkSelectEventJSONStmt *sql.Stmt
|
bulkSelectEventJSONStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func createEventJSONTable(db *sql.DB) error {
|
func CreateEventJSONTable(db *sql.DB) error {
|
||||||
_, err := db.Exec(eventJSONSchema)
|
_, err := db.Exec(eventJSONSchema)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) {
|
func PrepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) {
|
||||||
s := &eventJSONStatements{}
|
s := &eventJSONStatements{}
|
||||||
|
|
||||||
return s, sqlutil.StatementList{
|
return s, sqlutil.StatementList{
|
||||||
|
|
|
@ -74,7 +74,7 @@ func (d *Database) create(db *sql.DB) error {
|
||||||
if err := createEventTypesTable(db); err != nil {
|
if err := createEventTypesTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createEventJSONTable(db); err != nil {
|
if err := CreateEventJSONTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createEventsTable(db); err != nil {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
eventJSON, err := prepareEventJSONTable(db)
|
eventJSON, err := PrepareEventJSONTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,12 +52,12 @@ type eventJSONStatements struct {
|
||||||
bulkSelectEventJSONStmt *sql.Stmt
|
bulkSelectEventJSONStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func createEventJSONTable(db *sql.DB) error {
|
func CreateEventJSONTable(db *sql.DB) error {
|
||||||
_, err := db.Exec(eventJSONSchema)
|
_, err := db.Exec(eventJSONSchema)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) {
|
func PrepareEventJSONTable(db *sql.DB) (tables.EventJSON, error) {
|
||||||
s := &eventJSONStatements{
|
s := &eventJSONStatements{
|
||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ func (d *Database) create(db *sql.DB) error {
|
||||||
if err := createEventTypesTable(db); err != nil {
|
if err := createEventTypesTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createEventJSONTable(db); err != nil {
|
if err := CreateEventJSONTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createEventsTable(db); err != nil {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
eventJSON, err := prepareEventJSONTable(db)
|
eventJSON, err := PrepareEventJSONTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
80
roomserver/storage/tables/event_json_table_test.go
Normal file
80
roomserver/storage/tables/event_json_table_test.go
Normal file
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -10,9 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type EventJSONPair struct {
|
type EventJSONPair struct {
|
||||||
EventNID types.EventNID
|
EventNID types.EventNID
|
||||||
RoomVersion gomatrixserverlib.RoomVersion
|
EventJSON []byte
|
||||||
EventJSON []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventJSON interface {
|
type EventJSON interface {
|
||||||
|
|
Loading…
Reference in a new issue