pashagolub / pgxmock

pgx mock driver for golang to test database interactions
Other
386 stars 49 forks source link

Sweep: Extend support for batch operation mocking / assertions #149

Closed acline443 closed 1 year ago

acline443 commented 1 year ago

Details

I may be misusing the library, so correct me if I am wrong. But I would like to write unit tests for functions that queue batches and then send the batch at the end. I would like to assert on the Updates/Insertions that are executed.

It seems to me like this is not currently supported as the SendBatch method does basically nothing:

func (c *pgxmock) SendBatch(context.Context, *pgx.Batch) pgx.BatchResults {
    return nil
}

Best assertion I can do is assert that the queued batch length is greater than 0.

Checklist - [X] `pgxmock.go` > * Add a new type `ExpectedBatch` with fields for the expected SQL statements and their arguments, and methods to set the expected results or errors. > • Modify the `SendBatch` method to return mock batch results based on the queued expectations. > • Add a new method `ExpectBatch` to the `pgxmock` type to queue an expected batch operation. This method should return an `ExpectedBatch` instance. > • Update the `ExpectationsWereMet` method to also check if all expected batch operations were met. - [ ] `pgxmock_test.go` > * Add unit tests for the new `ExpectBatch` method and the modified `SendBatch` method. These tests should cover various scenarios, including successful batch operations, batch operations that result in errors, and batch operations that do not meet the expectations.
pashagolub commented 1 year ago

Yes, unfortunately, SendBatch() is not currently supported. I do have plans for it. However, this will require some spare weeks to implement. One needs to implement pgx.BatchResults from scratch and incorporate this into the expectations paradigm. Of course, pull requests are more than welcome!