Add Redactions tests

Fix bug in SQLite markRedactionValidatedSQL
This commit is contained in:
Till Faelligen 2022-05-10 08:09:29 +02:00
parent 2489c52639
commit 3c27b9db5f
5 changed files with 99 additions and 10 deletions

View file

@ -59,12 +59,12 @@ type redactionStatements struct {
markRedactionValidatedStmt *sql.Stmt
}
func createRedactionsTable(db *sql.DB) error {
func CreateRedactionsTable(db *sql.DB) error {
_, err := db.Exec(redactionsSchema)
return err
}
func prepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
func PrepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
s := &redactionStatements{}
return s, sqlutil.StatementList{

View file

@ -104,7 +104,7 @@ func (d *Database) create(db *sql.DB) error {
if err := CreatePublishedTable(db); err != nil {
return err
}
if err := createRedactionsTable(db); err != nil {
if err := CreateRedactionsTable(db); err != nil {
return err
}
@ -160,7 +160,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room
if err != nil {
return err
}
redactions, err := prepareRedactionsTable(db)
redactions, err := PrepareRedactionsTable(db)
if err != nil {
return err
}

View file

@ -48,7 +48,7 @@ const selectRedactionInfoByEventBeingRedactedSQL = "" +
" WHERE redacts_event_id = $1"
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 {
db *sql.DB
@ -58,12 +58,12 @@ type redactionStatements struct {
markRedactionValidatedStmt *sql.Stmt
}
func createRedactionsTable(db *sql.DB) error {
func CreateRedactionsTable(db *sql.DB) error {
_, err := db.Exec(redactionsSchema)
return err
}
func prepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
func PrepareRedactionsTable(db *sql.DB) (tables.Redactions, error) {
s := &redactionStatements{
db: db,
}
@ -118,6 +118,6 @@ func (s *redactionStatements) MarkRedactionValidated(
ctx context.Context, txn *sql.Tx, redactionEventID string, validated bool,
) error {
stmt := sqlutil.TxStmt(txn, s.markRedactionValidatedStmt)
_, err := stmt.ExecContext(ctx, redactionEventID, validated)
_, err := stmt.ExecContext(ctx, validated, redactionEventID)
return err
}

View file

@ -113,7 +113,7 @@ func (d *Database) create(db *sql.DB) error {
if err := CreatePublishedTable(db); err != nil {
return err
}
if err := createRedactionsTable(db); err != nil {
if err := CreateRedactionsTable(db); err != nil {
return err
}
@ -169,7 +169,7 @@ func (d *Database) prepare(db *sql.DB, writer sqlutil.Writer, cache caching.Room
if err != nil {
return err
}
redactions, err := prepareRedactionsTable(db)
redactions, err := PrepareRedactionsTable(db)
if err != nil {
return err
}

View 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)
})
}