Tests: more unified way to do database tests
This commit is contained in:
parent
f497ac8536
commit
dbb9c71df8
@ -33,10 +33,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestStoreAuthoredJob(t *testing.T) {
|
func TestStoreAuthoredJob(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
||||||
defer dbCloser()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
job := createTestAuthoredJobWithTasks()
|
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) {
|
func jobTasksTestFixtures(t *testing.T) (context.Context, context.CancelFunc, *DB, *Job, job_compilers.AuthoredJob) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
||||||
cancel := func() {
|
|
||||||
ctxCancel()
|
|
||||||
dbCloser()
|
|
||||||
}
|
|
||||||
|
|
||||||
authoredJob := createTestAuthoredJobWithTasks()
|
authoredJob := createTestAuthoredJobWithTasks()
|
||||||
err := db.StoreAuthoredJob(ctx, authoredJob)
|
err := db.StoreAuthoredJob(ctx, authoredJob)
|
||||||
|
@ -32,13 +32,11 @@ import (
|
|||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
const testContextTimeout = 100 * time.Millisecond
|
const schedulerTestTimeout = 100 * time.Millisecond
|
||||||
|
|
||||||
func TestNoTasks(t *testing.T) {
|
func TestNoTasks(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
defer dbCloser()
|
defer cancel()
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
|
|
||||||
defer ctxCancel()
|
|
||||||
|
|
||||||
w := linuxWorker(t, db)
|
w := linuxWorker(t, db)
|
||||||
|
|
||||||
@ -48,10 +46,8 @@ func TestNoTasks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOneJobOneTask(t *testing.T) {
|
func TestOneJobOneTask(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
defer dbCloser()
|
defer cancel()
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
|
|
||||||
defer ctxCancel()
|
|
||||||
|
|
||||||
w := linuxWorker(t, db)
|
w := linuxWorker(t, db)
|
||||||
|
|
||||||
@ -85,10 +81,8 @@ func TestOneJobOneTask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOneJobThreeTasksByPrio(t *testing.T) {
|
func TestOneJobThreeTasksByPrio(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
defer dbCloser()
|
defer cancel()
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
|
|
||||||
defer ctxCancel()
|
|
||||||
|
|
||||||
w := linuxWorker(t, db)
|
w := linuxWorker(t, db)
|
||||||
|
|
||||||
@ -118,10 +112,8 @@ func TestOneJobThreeTasksByPrio(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOneJobThreeTasksByDependencies(t *testing.T) {
|
func TestOneJobThreeTasksByDependencies(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
defer dbCloser()
|
defer cancel()
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
|
|
||||||
defer ctxCancel()
|
|
||||||
|
|
||||||
w := linuxWorker(t, db)
|
w := linuxWorker(t, db)
|
||||||
|
|
||||||
@ -146,10 +138,8 @@ func TestOneJobThreeTasksByDependencies(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTwoJobsThreeTasks(t *testing.T) {
|
func TestTwoJobsThreeTasks(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, schedulerTestTimeout)
|
||||||
defer dbCloser()
|
defer cancel()
|
||||||
ctx, ctxCancel := context.WithTimeout(context.Background(), testContextTimeout)
|
|
||||||
defer ctxCancel()
|
|
||||||
|
|
||||||
w := linuxWorker(t, db)
|
w := linuxWorker(t, db)
|
||||||
|
|
||||||
|
@ -22,9 +22,11 @@ package persistence
|
|||||||
* ***** END GPL LICENSE BLOCK ***** */
|
* ***** END GPL LICENSE BLOCK ***** */
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@ -78,3 +80,17 @@ func CreateTestDB(t *testing.T) (db *DB, closer func()) {
|
|||||||
|
|
||||||
return db, closer
|
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/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateFetchWorker(t *testing.T) {
|
func TestCreateFetchWorker(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
||||||
defer dbCloser()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
w := Worker{
|
w := Worker{
|
||||||
@ -69,9 +66,7 @@ func TestCreateFetchWorker(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveWorker(t *testing.T) {
|
func TestSaveWorker(t *testing.T) {
|
||||||
db, dbCloser := CreateTestDB(t)
|
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
||||||
defer dbCloser()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
w := Worker{
|
w := Worker{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user