简体   繁体   中英

How to mock sql update in go

I'm trying to write a unit test for updating record in postgresql in golang

database function is

CREATE OR REPLACE FUNCTION test.update(param_id text, param_data jsonb, id2 text) RETURNS void AS
$$
BEGIN
    UPDATE test.test_table
    SET data = param_data,
    WHERE id = (SELECT id FROM test.test_table2 WHERE name = id2)
END;
$$
LANGUAGE plpgsql;

Unit test is

rows := sqlmock.NewRows([]string{"result"}).AddRow("")

mock.ExpectBegin()
mock.ExpectQuery(fmt.Sprintf("SELECT %v.update(.+)", schema)).WithArgs(1, data).WillReturnRows(rows)
mock.ExpectCommit()

Do I also need to mock the select query inside? How can I mock a void function?

I've used the following package

import (sql "database/sql")

this enables me to check the following,

.... var rows sql.Result ... <execute query > ... rows.RowsAffected()

rows.RowsAffected() provides the number of rows updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM