Drop related tables prior to running tests

This commit is contained in:
Maximilian Seifert 2019-10-01 22:14:53 +02:00
parent ce09d7c498
commit ca588f30f6
3 changed files with 22 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package devices
import (
"context"
"database/sql"
"fmt"
"os"
"os/exec"
@ -61,6 +62,7 @@ type deviceSpec struct {
}
func TestDatabase_GetDevicesByLocalpart(t *testing.T) {
dropTable(dataSource)
db, err := NewDatabase(dataSource, "localhost")
assert.Nil(t, err)
@ -122,3 +124,11 @@ func createTestDevice(devSpecScheme *deviceSpec, count int) (devices []*authtype
}
return devices, nil
}
func dropTable(dataSource string) {
if db, err := sql.Open("postgres", dataSource); err == nil && db != nil {
_, _ = db.Exec("DROP TABLE device_devices;")
} else {
panic("Error! Unable to refresh the database!")
}
}

2
go.mod
View file

@ -43,3 +43,5 @@ require (
gopkg.in/h2non/bimg.v1 v1.0.18
gopkg.in/yaml.v2 v2.2.2
)
go 1.13

View file

@ -2,6 +2,7 @@ package storage
import (
"context"
"database/sql"
"fmt"
"os"
"os/exec"
@ -58,6 +59,7 @@ func init() {
const testEventID = "$test-event-id:test-domain.example.com"
func Test_sanityCheckOutputRoomEvents(t *testing.T) {
dropTable(dataSource)
db, err := NewSyncServerDatasource(dataSource)
assert.Nil(t, err)
@ -146,6 +148,14 @@ func selectTestEvent(t *testing.T, db *SyncServerDatasource) {
})
}
func dropTable(dataSource string) {
if db, err := sql.Open("postgres", dataSource); err == nil && db != nil {
_, _ = db.Exec("DROP TABLE syncapi_output_room_events;")
} else {
panic("Error! Unable to refresh the database!")
}
}
func truncateTable(t *testing.T, db *SyncServerDatasource) {
_, err := db.db.Exec("TRUNCATE syncapi_output_room_events")
assert.Nil(t, err)