-
|
I'm loving river. It's such a straightforward and robust library. I'm trying to write some tests for a worker using the For context, I am using SQLite as the database for my tests. I am also using From what I understand,
Thus, my question is: is it possible to use I strongly suspect that I am doing something wrong or misunderstanding something, so hopefully someone can point me in the right direction. Here's some pseudo-code showing what I'm trying to do in case it helps: type Instance struct {
Uuid string
}
type CreateArgs struct {
Instance *Instance
}
func (CreateArgs) Kind() string { return "create" }
type CreateWorker struct {
river.WorkerDefaults[CreateArgs]
db *gorm.DB
}
func (w *CreateWorker) Work(ctx context.Context, job *river.Job[CreateArgs]) error {
err := w.db.Save(job.Args.Instance).Error
return err
}
func TestCreateWorker(t *testing.T) {
// logic to initialize database as `db` and `riverClient` excluded
tx := db.Begin()
if err := tx.Error; err != nil {
t.Fatal(err)
}
defer tx.Rollback()
sqlTx := tx.Statement.ConnPool.(*sql.Tx)
testWorker := rivertest.NewWorker(t, riversqlite.New(nil), &river.Config{}, &CreateWorker{
db: db,
})
result, err := testWorker.Work(test.ctx, t, sqlTx, CreateArgs{
Instance: &Instance{Uuid: "fake-uuid"},
}, nil)
if err != nil {
t.Fatal(err)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Hm, that looks right (I think?). It might be work adding some logging in various places just to make sure the transaction is indeed being rolled back as expected (i.e. putting some in the Also, try running the test case on its own (i.e. |
Beta Was this translation helpful? Give feedback.
Ah! Okay, I see what's going on here.
Yeah, since it's SQLite, you won't be able to do another DB operation from the worker unless it's sharing the transaction send to the test worker.
Below, I've copy/pasted a full example that shows how you might accomplish this by putting a transaction in your context. So in your test you'd do something like:
Then in a worker: