I am using GORM to batch insert multiple rows into a MySQL table and I want to test that the behaviour is the correct one using sqlmock. I haven't found anything online regarding mocking batch inserts with sqlmock.
For inserting a single row, we would have something similar to:
mock.ExpectExec("INSERT INTO product_viewers").WithArgs(2, 3).WillReturnResult(sqlmock.NewResult(1, 1))
But how should multiple rows' values be passed to ExpectExec
in order to represent a batch insert?
mock.ExpectExec("INSERT INTO product_viewers").WithArgs(???).WillReturnResult(sqlmock.NewResult(*numInsertedRows*, *numInsertedRows*))
The answer is, at least for MySQL, that sqlmock doesn't make a difference between different rows and columns, so you can just list all the values from all the rows, one after the other, comma separated, in .WithArgs
.
e.g.
mock.ExpectExec("INSERT INTO product_viewers").
WithArgs(row1col1, row1col2, row2col1, row2col2, ...).
WillReturnResult(sqlmock.NewResult(<numInsertedRows>, <numInsertedRows>))