mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-16 03:13:11 -06:00
Add simple tests to validate bugfix (#601)
Signed-off-by: Maximilian Seifert <max.seifert@drglitch.net>
This commit is contained in:
parent
329900f644
commit
b617eee4cb
|
|
@ -2,62 +2,21 @@ package storage
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dataSource string
|
||||
var insideCi = false
|
||||
var insideDocker = false
|
||||
const dataSourceName = "postgres://dendrite:itsasecret@postgres/dendrite_syncapi?sslmode=disable"
|
||||
|
||||
const dbName = "dendrite_syncapi"
|
||||
//const dataSourceName = "postgres://dendrite:itsasecret@localhost:15432/dendrite_syncapi?sslmode=disable"
|
||||
|
||||
func init() {
|
||||
for _, val := range os.Environ() {
|
||||
tokens := strings.Split(val, "=")
|
||||
if tokens[0] == "CI" && tokens[1] == "true" {
|
||||
insideCi = true
|
||||
}
|
||||
}
|
||||
if !insideCi {
|
||||
if _, err := os.Open("/.dockerenv"); err == nil {
|
||||
insideDocker = true
|
||||
}
|
||||
}
|
||||
|
||||
if insideCi {
|
||||
dataSource = fmt.Sprintf("postgres://postgres@localhost/%s?sslmode=disable", dbName)
|
||||
} else if insideDocker {
|
||||
dataSource = fmt.Sprintf("postgres://dendrite:itsasecret@postgres/%s?sslmode=disable", dbName)
|
||||
} else {
|
||||
dataSource = fmt.Sprintf("postgres://dendrite:itsasecret@localhost:15432/%s?sslmode=disable", dbName)
|
||||
}
|
||||
|
||||
if insideCi {
|
||||
database := "dendrite_syncapi"
|
||||
cmd := exec.Command("psql", "postgres")
|
||||
cmd.Stdin = strings.NewReader(
|
||||
fmt.Sprintf("DROP DATABASE IF EXISTS %s; CREATE DATABASE %s;", database, database),
|
||||
)
|
||||
// Send stdout and stderr to our stderr so that we see error messages from
|
||||
// the psql process
|
||||
cmd.Stdout = os.Stderr
|
||||
cmd.Stderr = os.Stderr
|
||||
_ = cmd.Run()
|
||||
}
|
||||
}
|
||||
|
||||
const testEventID = "$test-event-id:test-domain.example.com"
|
||||
const testEventId = "test-event-id"
|
||||
|
||||
func Test_sanityCheckOutputRoomEvents(t *testing.T) {
|
||||
db, err := NewSyncServerDatabase(dataSource)
|
||||
db, err := NewSyncServerDatabase(dataSourceName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = db.events.prepare(db.db)
|
||||
|
|
@ -70,20 +29,20 @@ func Test_sanityCheckOutputRoomEvents(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSyncServerDatabase_selectEventsWithEventIDs(t *testing.T) {
|
||||
db, err := NewSyncServerDatabase(dataSource)
|
||||
db, err := NewSyncServerDatabase(dataSourceName)
|
||||
assert.Nil(t, err)
|
||||
insertTestEvent(t, db)
|
||||
ctx := context.Background()
|
||||
txn, err := db.db.Begin()
|
||||
assert.Nil(t, err)
|
||||
|
||||
var eventIDs = []string{testEventID}
|
||||
var eventIDs = []string{testEventId}
|
||||
events, err := db.fetchMissingStateEvents(ctx, txn, eventIDs)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, events)
|
||||
assert.Condition(t, func() bool {
|
||||
return len(events) > 0
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func insertTestEvent(t *testing.T, db *SyncServerDatabase) {
|
||||
|
|
@ -92,23 +51,21 @@ func insertTestEvent(t *testing.T, db *SyncServerDatabase) {
|
|||
|
||||
keyBytes := []byte("1122334455667788112233445566778811223344556677881122334455667788")
|
||||
eventBuilder := gomatrixserverlib.EventBuilder{
|
||||
RoomID: "!test_room_id:test-domain.example.com",
|
||||
RoomID: "test-room-id",
|
||||
Content: []byte(`{"RawContent": "test-raw-content"}`),
|
||||
Sender: "@test-user:test-domain.example.com",
|
||||
}
|
||||
event, err := eventBuilder.Build(
|
||||
testEventID,
|
||||
testEventId,
|
||||
time.Now(),
|
||||
"test-domain.example.com",
|
||||
"test-server-name",
|
||||
"test-key-id",
|
||||
keyBytes)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
var addState, removeState []string
|
||||
transactionID := api.TransactionID{
|
||||
DeviceID: "test-device-id",
|
||||
TransactionID: "test-transaction-id",
|
||||
DeviceID: "test-device-id",
|
||||
TransactionID:"test-transaction-id",
|
||||
}
|
||||
|
||||
newEventID, err := db.events.insertEvent(
|
||||
|
|
@ -131,7 +88,7 @@ func insertTestEvent(t *testing.T, db *SyncServerDatabase) {
|
|||
func selectTestEvent(t *testing.T, db *SyncServerDatabase) {
|
||||
ctx := context.Background()
|
||||
|
||||
var eventIDs = []string{testEventID}
|
||||
var eventIDs = []string{testEventId}
|
||||
res, err := db.Events(ctx, eventIDs)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, res)
|
||||
|
|
|
|||
Loading…
Reference in a new issue