mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-04 12:43:10 -06:00
Add Redactions tests
Fix bug in SQLite markRedactionValidatedSQL
This commit is contained in:
parent
2489c52639
commit
3c27b9db5f
|
|
@ -59,12 +59,12 @@ type redactionStatements struct {
|
||||||
markRedactionValidatedStmt *sql.Stmt
|
markRedactionValidatedStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRedactionsTable(db *sql.DB) error {
|
func CreateRedactionsTable(db *sql.DB) error {
|
||||||
_, err := db.Exec(redactionsSchema)
|
_, err := db.Exec(redactionsSchema)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
|
func PrepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
|
||||||
s := &redactionStatements{}
|
s := &redactionStatements{}
|
||||||
|
|
||||||
return s, sqlutil.StatementList{
|
return s, sqlutil.StatementList{
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ func (d *Database) create(db *sql.DB) error {
|
||||||
if err := CreatePublishedTable(db); err != nil {
|
if err := CreatePublishedTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createRedactionsTable(db); err != nil {
|
if err := CreateRedactionsTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,7 +160,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
redactions, err := prepareRedactionsTable(db)
|
redactions, err := PrepareRedactionsTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ const selectRedactionInfoByEventBeingRedactedSQL = "" +
|
||||||
" WHERE redacts_event_id = $1"
|
" WHERE redacts_event_id = $1"
|
||||||
|
|
||||||
const markRedactionValidatedSQL = "" +
|
const markRedactionValidatedSQL = "" +
|
||||||
" UPDATE roomserver_redactions SET validated = $2 WHERE redaction_event_id = $1"
|
" UPDATE roomserver_redactions SET validated = $1 WHERE redaction_event_id = $2"
|
||||||
|
|
||||||
type redactionStatements struct {
|
type redactionStatements struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
|
|
@ -58,12 +58,12 @@ type redactionStatements struct {
|
||||||
markRedactionValidatedStmt *sql.Stmt
|
markRedactionValidatedStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRedactionsTable(db *sql.DB) error {
|
func CreateRedactionsTable(db *sql.DB) error {
|
||||||
_, err := db.Exec(redactionsSchema)
|
_, err := db.Exec(redactionsSchema)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
|
func PrepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
|
||||||
s := &redactionStatements{
|
s := &redactionStatements{
|
||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
@ -118,6 +118,6 @@ func (s *redactionStatements) MarkRedactionValidated(
|
||||||
ctx context.Context, txn *sql.Tx, redactionEventID string, validated bool,
|
ctx context.Context, txn *sql.Tx, redactionEventID string, validated bool,
|
||||||
) error {
|
) error {
|
||||||
stmt := sqlutil.TxStmt(txn, s.markRedactionValidatedStmt)
|
stmt := sqlutil.TxStmt(txn, s.markRedactionValidatedStmt)
|
||||||
_, err := stmt.ExecContext(ctx, redactionEventID, validated)
|
_, err := stmt.ExecContext(ctx, validated, redactionEventID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ func (d *Database) create(db *sql.DB) error {
|
||||||
if err := CreatePublishedTable(db); err != nil {
|
if err := CreatePublishedTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := createRedactionsTable(db); err != nil {
|
if err := CreateRedactionsTable(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -169,7 +169,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
redactions, err := prepareRedactionsTable(db)
|
redactions, err := PrepareRedactionsTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
89
roomserver/storage/tables/redactions_table_test.go
Normal file
89
roomserver/storage/tables/redactions_table_test.go
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
package tables_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"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/setup/config"
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mustCreateRedactionsTable(t *testing.T, dbType test.DBType) (tab tables.Redactions, close func()) {
|
||||||
|
t.Helper()
|
||||||
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||||
|
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||||
|
ConnectionString: config.DataSource(connStr),
|
||||||
|
}, sqlutil.NewExclusiveWriter())
|
||||||
|
assert.NoError(t, err)
|
||||||
|
switch dbType {
|
||||||
|
case test.DBTypePostgres:
|
||||||
|
err = postgres.CreateRedactionsTable(db)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tab, err = postgres.PrepareRedactionsTable(db)
|
||||||
|
case test.DBTypeSQLite:
|
||||||
|
err = sqlite3.CreateRedactionsTable(db)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tab, err = sqlite3.PrepareRedactionsTable(db)
|
||||||
|
}
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
return tab, close
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedactionsTable(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
|
tab, close := mustCreateRedactionsTable(t, dbType)
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
// insert and verify some redactions
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
redactionEventID, redactsEventID := util.RandomString(16), util.RandomString(16)
|
||||||
|
wantRedactionInfo := tables.RedactionInfo{
|
||||||
|
Validated: false,
|
||||||
|
RedactsEventID: redactsEventID,
|
||||||
|
RedactionEventID: redactionEventID,
|
||||||
|
}
|
||||||
|
err := tab.InsertRedaction(ctx, nil, wantRedactionInfo)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// verify the redactions are inserted as expected
|
||||||
|
redactionInfo, err := tab.SelectRedactionInfoByRedactionEventID(ctx, nil, redactionEventID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, &wantRedactionInfo, redactionInfo)
|
||||||
|
|
||||||
|
redactionInfo, err = tab.SelectRedactionInfoByEventBeingRedacted(ctx, nil, redactsEventID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, &wantRedactionInfo, redactionInfo)
|
||||||
|
|
||||||
|
// redact event
|
||||||
|
err = tab.MarkRedactionValidated(ctx, nil, redactionEventID, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
wantRedactionInfo.Validated = true
|
||||||
|
redactionInfo, err = tab.SelectRedactionInfoByRedactionEventID(ctx, nil, redactionEventID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, &wantRedactionInfo, redactionInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not fail, it just updates 0 rows
|
||||||
|
err := tab.MarkRedactionValidated(ctx, nil, "iDontExist", true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Should also not fail, but return a nil redactionInfo
|
||||||
|
redactionInfo, err := tab.SelectRedactionInfoByRedactionEventID(ctx, nil, "iDontExist")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, redactionInfo)
|
||||||
|
|
||||||
|
redactionInfo, err = tab.SelectRedactionInfoByEventBeingRedacted(ctx, nil, "iDontExist")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, redactionInfo)
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue