1

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*))

1 Answer 1

2

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>))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.