Tests: more unified way to do database tests

This commit is contained in:
Sybren A. Stüvel 2022-03-04 12:33:45 +01:00
parent f497ac8536
commit dbb9c71df8
4 changed files with 31 additions and 38 deletions

@ -33,10 +33,7 @@ import (
)
func TestStoreAuthoredJob(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
defer cancel()
job := createTestAuthoredJobWithTasks()
@ -297,12 +294,7 @@ func createTestAuthoredJobWithTasks() job_compilers.AuthoredJob {
}
func jobTasksTestFixtures(t *testing.T) (context.Context, context.CancelFunc, *DB, *Job, job_compilers.AuthoredJob) {
db, dbCloser := CreateTestDB(t)
ctx, ctxCancel := context.WithTimeout(context.Background(), 1*time.Second)
cancel := func() {
ctxCancel()
dbCloser()
}
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
authoredJob := createTestAuthoredJobWithTasks()
err := db.StoreAuthoredJob(ctx, authoredJob)

@ -32,13 +32,11 @@ import (
"git.blender.org/flamenco/pkg/api"
)
const testContextTimeout = 100 * time.Millisecond
const schedulerTestTimeout = 100 * time.Millisecond
func TestNoTasks(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
defer ctxCancel()
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
defer cancel()
w := linuxWorker(t, db)
@ -48,10 +46,8 @@ func TestNoTasks(t *testing.T) {
}
func TestOneJobOneTask(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
defer ctxCancel()
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
defer cancel()
w := linuxWorker(t, db)
@ -85,10 +81,8 @@ func TestOneJobOneTask(t *testing.T) {
}
func TestOneJobThreeTasksByPrio(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
defer ctxCancel()
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
defer cancel()
w := linuxWorker(t, db)
@ -118,10 +112,8 @@ func TestOneJobThreeTasksByPrio(t *testing.T) {
}
func TestOneJobThreeTasksByDependencies(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
defer ctxCancel()
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
defer cancel()
w := linuxWorker(t, db)
@ -146,10 +138,8 @@ func TestOneJobThreeTasksByDependencies(t *testing.T) {
}
func TestTwoJobsThreeTasks(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
defer ctxCancel()
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
defer cancel()
w := linuxWorker(t, db)

@ -22,9 +22,11 @@ package persistence
* ***** END GPL LICENSE BLOCK ***** */
import (
"context"
"database/sql"
"os"
"testing"
"time"
"github.com/glebarez/sqlite"
"github.com/rs/zerolog"
@ -78,3 +80,17 @@ func CreateTestDB(t *testing.T) (db *DB, closer func()) {
return db, closer
}
// persistenceTestFixtures creates a test database and returns it and a context.
// Tests should call the returned cancel function when they're done.
func persistenceTestFixtures(t *testing.T, testContextTimeout time.Duration) (context.Context, context.CancelFunc, *DB) {
db, dbCloser := CreateTestDB(t)
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
cancel := func() {
ctxCancel()
dbCloser()
}
return ctx, cancel, db
}

@ -27,15 +27,12 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"git.blender.org/flamenco/pkg/api"
)
func TestCreateFetchWorker(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
defer cancel()
w := Worker{
@ -69,9 +66,7 @@ func TestCreateFetchWorker(t *testing.T) {
}
func TestSaveWorker(t *testing.T) {
db, dbCloser := CreateTestDB(t)
defer dbCloser()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
defer cancel()
w := Worker{