You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The tests would fail with the message but it did return the expected error from the batch!
Returned error: "Some Error" // Correct error returned from the batch!
db_test.go:154: there were unfulfilled expectations: there is a remaining expectation which was not matched: ExpectedQuery => expecting call to Query() or to QueryRow():
- matches sql: 'SELECT'
- is without arguments
- returns data:
row 0 - [2]
So we thought, this can be an expected error since the first query returns an error, the second is not executed (even though a batch should execute all of them I believe). When we removed the second ExpectQuery we got a different error:
Returned error: "call to method Batch() was not expected"
db_test.go:154: there were unfulfilled expectations: there is a remaining expectation which was not matched: ExpectedBatch => expecting call to SendBatch()
This error is expected I believe as the batch has two select statements, to the "expector" is not expecting a Batch() as the second query was not expected.
The final attempt we did was to return the error on the second query instead:
funcTestNewBatchErrSuccess(t*testing.T) {
dbl:=tearUpMock(t)
defertearDownMock(t, dbl)
// Test successeb:=mock.Exp.ExpectBatch()
eb.ExpectQuery("SELECT").WillReturnRows(pgxmock.NewRows([]string{"first"}).AddRow(1))
eb.ExpectQuery("SELECT").WillReturnRows(pgxmock.NewRows([]string{"second"}).AddRow(2))
data, err:=dbl.TestBatch()
assert.Nil(t, err)
assert.Equal(t, 3, data)
}
// Great success!funcTestNewBatchErrorSuccess(t*testing.T) {
dbl:=tearUpMock(t)
defertearDownMock(t, dbl)
// Test erroreb:=mock.Exp.ExpectBatch()
eb.ExpectQuery("SELECT").WillReturnRows(pgxmock.NewRows([]string{"first"}).AddRow(1))
eb.ExpectQuery("SELECT").WillReturnError(errors.New("Some Error"))
data, err:=dbl.TestBatch()
assert.Equal(t, "Some Error", err.Error())
assert.Equal(t, 1, data)
}
// Expected to succeed, possible bug.funcTestNewBatchErrorFail1(t*testing.T) {
dbl:=tearUpMock(t)
defertearDownMock(t, dbl)
// Test erroreb:=mock.Exp.ExpectBatch()
eb.ExpectQuery("SELECT").WillReturnError(errors.New("Some Error"))
eb.ExpectQuery("SELECT").WillReturnRows(pgxmock.NewRows([]string{"second"}).AddRow(2))
data, err:=dbl.TestBatch()
assert.Equal(t, "Some Error", err.Error())
assert.Equal(t, 0, data)
}
// Expected to fail due to missing ExpectQuery, not a bug.funcTestNewBatchErrorFail2(t*testing.T) {
dbl:=tearUpMock(t)
defertearDownMock(t, dbl)
// Test erroreb:=mock.Exp.ExpectBatch()
eb.ExpectQuery("SELECT").WillReturnError(errors.New("Some Error"))
data, err:=dbl.TestBatch()
assert.Equal(t, "Some Error", err.Error())
assert.Equal(t, 0, data)
}
Expected behavior
I would expect the batch to return the expected error but not complain about missing expectations. I would expect testcase TestNewBatchErrorFail1 to succeed. But this might be a limitation of batch queries with PGX as well, I am not sure about this 🙂
The text was updated successfully, but these errors were encountered:
Describe the bug
We are implementing batch queries as in some cases we execute a batch of queries that can benefit from a single round trip.
When writing tests we wanted to mock errors as well when we noticed that when we do:
The tests would fail with the message but it did return the expected error from the batch!
So we thought, this can be an expected error since the first query returns an error, the second is not executed (even though a batch should execute all of them I believe). When we removed the second
ExpectQuery
we got a different error:This error is expected I believe as the batch has two select statements, to the "expector" is not expecting a
Batch()
as the second query was not expected.The final attempt we did was to return the error on the second query instead:
This made the testcase pass without expectation errors.
To Reproduce
A bit of pseudo code to simplify a testcase
Implementation on the database layer:
Testcases:
Expected behavior
I would expect the batch to return the expected error but not complain about missing expectations. I would expect testcase
TestNewBatchErrorFail1
to succeed. But this might be a limitation of batch queries with PGX as well, I am not sure about this 🙂The text was updated successfully, but these errors were encountered: