2

Here I have been trying to find out to mock the sqlboiler(an ORM generator) queries but could not find the way.

Can anyone work around mocking the sqlboiler queries?

Example:

func (m *Message) updateDevice(parentID int, data map[string]interface{}) (rows int64, err error) {
    rows, err = automodel.Devices(qm.Where(automodel.DeviceColumns.ParentID+"=?", parentID)).UpdateAll(context.Background(), db.GetMaster(), data)
    if err != nil {
        return 0, err
    }
    return rows, err
} 


func GetDeviceByID(ID int) (res *automodel.Device, err error) {
    res, err = automodel.Devices(qm.Where("id=?", ID)).One(context.Background(), db.GetSlave())
    if err != nil {
        return nil, err
    }
    return res, nil
}

FYI: automodel is the package where all generated models are present(like device.go for device table). We cannot changes these as these are generated by sqlboiler command and need to generate again if new tables are added or schema change happens to a table.

How do I mock the sqlboiler queries in the above two methods while writing unit tests?

Majid Alaeinia
  • 962
  • 2
  • 11
  • 27
Siyaram Malav
  • 4,414
  • 2
  • 31
  • 31
  • 1
    IMO opinion mock will not be the right way for generated SQL or any SQL queries that are complex. It is possible that you re-generate the code and it will break all your mocks. I usually set up a real db with docker and run unit tests against it. Its much simpler approach. – Rengas Aug 21 '20 at 23:23

0 Answers0