aboutsummaryrefslogtreecommitdiff
path: root/roomserver/storage/sqlite3
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2022-05-09 15:30:32 +0200
committerGitHub <noreply@github.com>2022-05-09 15:30:32 +0200
commitf69ebc6af2dfeeb7af7eaabbe0609976c397a685 (patch)
tree35d3acb93b83393f923c990063bc25c26991f896 /roomserver/storage/sqlite3
parent09d754cfbf9268044d0f59fbe509640b8d71e011 (diff)
Add roomserver tests (1/?) (#2434)
* Add EventJSONTable tests * Add eventJSON tests * Add EventStateKeysTable tests * Add EventTypesTable tests * Add Events Table tests Move variable declaration outside loops Switch to testify/assert for tests * Move variable declaration outside loop * Remove random data * Fix issue where the EventReferenceSHA256 is not set * Add more tests * Revert "Fix issue where the EventReferenceSHA256 is not set" This reverts commit 8ae34c4e5f78584f0edb479f5a893556d2b95d19. * Update GMSL * Add tests for duplicate entries * Test what happens if we select non-existing NIDs * Add test for non-existing eventType * Really update GMSL
Diffstat (limited to 'roomserver/storage/sqlite3')
-rw-r--r--roomserver/storage/sqlite3/event_json_table.go6
-rw-r--r--roomserver/storage/sqlite3/event_state_keys_table.go12
-rw-r--r--roomserver/storage/sqlite3/event_types_table.go8
-rw-r--r--roomserver/storage/sqlite3/events_table.go35
-rw-r--r--roomserver/storage/sqlite3/storage.go16
5 files changed, 39 insertions, 38 deletions
diff --git a/roomserver/storage/sqlite3/event_json_table.go b/roomserver/storage/sqlite3/event_json_table.go
index f470ea32..dc26885b 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,
}
@@ -101,9 +101,9 @@ func (s *eventJSONStatements) BulkSelectEventJSON(
// We might get fewer results than NIDs so we adjust the length of the slice before returning it.
results := make([]tables.EventJSONPair, len(eventNIDs))
i := 0
+ var eventNID int64
for ; rows.Next(); i++ {
result := &results[i]
- var eventNID int64
if err := rows.Scan(&eventNID, &result.EventJSON); err != nil {
return nil, err
}
diff --git a/roomserver/storage/sqlite3/event_state_keys_table.go b/roomserver/storage/sqlite3/event_state_keys_table.go
index f97541f4..347524a8 100644
--- a/roomserver/storage/sqlite3/event_state_keys_table.go
+++ b/roomserver/storage/sqlite3/event_state_keys_table.go
@@ -71,12 +71,12 @@ type eventStateKeyStatements struct {
bulkSelectEventStateKeyStmt *sql.Stmt
}
-func createEventStateKeysTable(db *sql.DB) error {
+func CreateEventStateKeysTable(db *sql.DB) error {
_, err := db.Exec(eventStateKeysSchema)
return err
}
-func prepareEventStateKeysTable(db *sql.DB) (tables.EventStateKeys, error) {
+func PrepareEventStateKeysTable(db *sql.DB) (tables.EventStateKeys, error) {
s := &eventStateKeyStatements{
db: db,
}
@@ -128,9 +128,9 @@ func (s *eventStateKeyStatements) BulkSelectEventStateKeyNID(
}
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKeyNID: rows.close() failed")
result := make(map[string]types.EventStateKeyNID, len(eventStateKeys))
+ var stateKey string
+ var stateKeyNID int64
for rows.Next() {
- var stateKey string
- var stateKeyNID int64
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
return nil, err
}
@@ -159,9 +159,9 @@ func (s *eventStateKeyStatements) BulkSelectEventStateKey(
}
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventStateKey: rows.close() failed")
result := make(map[types.EventStateKeyNID]string, len(eventStateKeyNIDs))
+ var stateKey string
+ var stateKeyNID int64
for rows.Next() {
- var stateKey string
- var stateKeyNID int64
if err := rows.Scan(&stateKey, &stateKeyNID); err != nil {
return nil, err
}
diff --git a/roomserver/storage/sqlite3/event_types_table.go b/roomserver/storage/sqlite3/event_types_table.go
index c49cc509..0581ec19 100644
--- a/roomserver/storage/sqlite3/event_types_table.go
+++ b/roomserver/storage/sqlite3/event_types_table.go
@@ -79,12 +79,12 @@ type eventTypeStatements struct {
bulkSelectEventTypeNIDStmt *sql.Stmt
}
-func createEventTypesTable(db *sql.DB) error {
+func CreateEventTypesTable(db *sql.DB) error {
_, err := db.Exec(eventTypesSchema)
return err
}
-func prepareEventTypesTable(db *sql.DB) (tables.EventTypes, error) {
+func PrepareEventTypesTable(db *sql.DB) (tables.EventTypes, error) {
s := &eventTypeStatements{
db: db,
}
@@ -139,9 +139,9 @@ func (s *eventTypeStatements) BulkSelectEventTypeNID(
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventTypeNID: rows.close() failed")
result := make(map[string]types.EventTypeNID, len(eventTypes))
+ var eventType string
+ var eventTypeNID int64
for rows.Next() {
- var eventType string
- var eventTypeNID int64
if err := rows.Scan(&eventType, &eventTypeNID); err != nil {
return nil, err
}
diff --git a/roomserver/storage/sqlite3/events_table.go b/roomserver/storage/sqlite3/events_table.go
index 45b49e5c..feb06150 100644
--- a/roomserver/storage/sqlite3/events_table.go
+++ b/roomserver/storage/sqlite3/events_table.go
@@ -68,7 +68,8 @@ const bulkSelectStateEventByIDSQL = "" +
const bulkSelectStateEventByNIDSQL = "" +
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
" WHERE event_nid IN ($1)"
- // Rest of query is built by BulkSelectStateEventByNID
+
+// Rest of query is built by BulkSelectStateEventByNID
const bulkSelectStateAtEventByIDSQL = "" +
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid, is_rejected FROM roomserver_events" +
@@ -126,12 +127,12 @@ type eventStatements struct {
//selectRoomNIDsForEventNIDsStmt *sql.Stmt
}
-func createEventsTable(db *sql.DB) error {
+func CreateEventsTable(db *sql.DB) error {
_, err := db.Exec(eventsSchema)
return err
}
-func prepareEventsTable(db *sql.DB) (tables.Events, error) {
+func PrepareEventsTable(db *sql.DB) (tables.Events, error) {
s := &eventStatements{
db: db,
}
@@ -404,15 +405,15 @@ func (s *eventStatements) BulkSelectStateAtEventAndReference(
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateAtEventAndReference: rows.close() failed")
results := make([]types.StateAtEventAndReference, len(eventNIDs))
i := 0
+ var (
+ eventTypeNID int64
+ eventStateKeyNID int64
+ eventNID int64
+ stateSnapshotNID int64
+ eventID string
+ eventSHA256 []byte
+ )
for ; rows.Next(); i++ {
- var (
- eventTypeNID int64
- eventStateKeyNID int64
- eventNID int64
- stateSnapshotNID int64
- eventID string
- eventSHA256 []byte
- )
if err = rows.Scan(
&eventTypeNID, &eventStateKeyNID, &eventNID, &stateSnapshotNID, &eventID, &eventSHA256,
); err != nil {
@@ -491,9 +492,9 @@ func (s *eventStatements) BulkSelectEventID(ctx context.Context, txn *sql.Tx, ev
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventID: rows.close() failed")
results := make(map[types.EventNID]string, len(eventNIDs))
i := 0
+ var eventNID int64
+ var eventID string
for ; rows.Next(); i++ {
- var eventNID int64
- var eventID string
if err = rows.Scan(&eventNID, &eventID); err != nil {
return nil, err
}
@@ -545,9 +546,9 @@ func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, e
}
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventNID: rows.close() failed")
results := make(map[string]types.EventNID, len(eventIDs))
+ var eventID string
+ var eventNID int64
for rows.Next() {
- var eventID string
- var eventNID int64
if err = rows.Scan(&eventID, &eventNID); err != nil {
return nil, err
}
@@ -595,9 +596,9 @@ func (s *eventStatements) SelectRoomNIDsForEventNIDs(
}
defer internal.CloseAndLogIfError(ctx, rows, "selectRoomNIDsForEventNIDsStmt: rows.close() failed")
result := make(map[types.EventNID]types.RoomNID)
+ var eventNID types.EventNID
+ var roomNID types.RoomNID
for rows.Next() {
- var eventNID types.EventNID
- var roomNID types.RoomNID
if err = rows.Scan(&eventNID, &roomNID); err != nil {
return nil, err
}
diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go
index e6cf1a53..9522d305 100644
--- a/roomserver/storage/sqlite3/storage.go
+++ b/roomserver/storage/sqlite3/storage.go
@@ -77,16 +77,16 @@ func Open(base *base.BaseDendrite, dbProperties *config.DatabaseOptions, cache c
}
func (d *Database) create(db *sql.DB) error {
- if err := createEventStateKeysTable(db); err != nil {
+ if err := CreateEventStateKeysTable(db); err != nil {
return err
}
- if err := createEventTypesTable(db); err != nil {
+ 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 {
+ if err := CreateEventsTable(db); err != nil {
return err
}
if err := createRoomsTable(db); err != nil {
@@ -121,19 +121,19 @@ func (d *Database) create(db *sql.DB) error {
}
func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.RoomServerCaches) error {
- eventStateKeys, err := prepareEventStateKeysTable(db)
+ eventStateKeys, err := PrepareEventStateKeysTable(db)
if err != nil {
return err
}
- eventTypes, err := prepareEventTypesTable(db)
+ eventTypes, err := PrepareEventTypesTable(db)
if err != nil {
return err
}
- eventJSON, err := prepareEventJSONTable(db)
+ eventJSON, err := PrepareEventJSONTable(db)
if err != nil {
return err
}
- events, err := prepareEventsTable(db)
+ events, err := PrepareEventsTable(db)
if err != nil {
return err
}