当前位置:网站首页>Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
2022-07-03 13:47:00 【Lao Liu, you are so awesome】
One , Libraries required for installation
1,gorm Official website :
2, install gorm
[email protected]:~$ go get -u gorm.io/gorm3,go-sqlmock The address of :
https://github.com/DATA-DOG/go-sqlmock
4, Install... From the command line go-sqlmock:
[email protected]:~$ go get -u github.com/DATA-DOG/go-sqlmock explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two , Information about the demonstration project
1, Address :
https://github.com/liuhongdi/unittest04
2, Functional specifications : Demonstrates the use of sqlmock Simulate the data records of the database
3, Project structure : Pictured :

3、 ... and ,go Code instructions
1,global/db.go
package global
import (
"gorm.io/gorm"
"time"
"gorm.io/driver/mysql"
)
var (
DBLink *gorm.DB
)
// establish mysql link
func SetupDBLink() (error) {
var err error
dsn:="root:[email protected](127.0.0.1:3306)/business?charset=utf8&parseTime=True&loc=Local";
DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
//DBLink.Logger.LogMode(true)
if err != nil {
return err
}
sqlDB, err := DBLink.DB()
// SetMaxIdleConns Set the maximum number of connections in the free connection pool
sqlDB.SetMaxIdleConns(10)
// SetMaxOpenConns Set the maximum number of open database connections
sqlDB.SetMaxOpenConns(30)
// SetConnMaxLifetime Set the maximum time that the connection can be reused
sqlDB.SetConnMaxLifetime(time.Hour)
return nil
}2,controller/userController.go
package controller
import (
"github.com/liuhongdi/unittest04/global"
"github.com/liuhongdi/unittest04/model"
)
func GoodsOne(goodsId int) (*model.Goods, error) {
goodsOne:=&model.Goods{}
err := global.DBLink.Where("goodsId=?",goodsId).First(&goodsOne).Error
//fmt.Println(err)
if (err != nil) {
return nil,err
} else {
return goodsOne,nil
}
}3,model/goods.go
package model
type Goods struct {
GoodsId int64 `gorm:"column:goodsId",json:"goodsid"` // Self increasing
GoodsName string `gorm:"column:goodsName",json:"goodsname"` //
Subject string `gorm:"column:subject",json:"subject"`
Price string `gorm:"column:price",json:"price"`
Stock int `gorm:"column:stock",json:"stock"`
}
func (Goods) TableName() string {
return "goods"
}4,main.go
package main
import (
"fmt"
"github.com/liuhongdi/unittest04/controller"
"github.com/liuhongdi/unittest04/global"
"log"
)
// Define an addition method
func add(a, b int) int {
return a + b
}
func init() {
//mysql link
err := global.SetupDBLink()
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
}
func main() {
goods,err := controller.GoodsOne(1)
if (err != nil){
log.Fatalf("err:%v",err)
} else {
fmt.Println(goods)
}
}5,main_test.go
package main
import (
"database/sql"
"errors"
"fmt"
"github.com/DATA-DOG/go-sqlmock"
"github.com/liuhongdi/unittest04/controller"
"github.com/liuhongdi/unittest04/global"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"testing"
)
var mock sqlmock.Sqlmock
// initialization
func init() {
// establish sqlmock
var err error
var db *sql.DB
db, mock, err = sqlmock.New()
if nil != err {
log.Fatalf("Init sqlmock failed, err %v", err)
}
// combination gorm、sqlmock
global.DBLink, err = gorm.Open(mysql.New(mysql.Config{
SkipInitializeWithVersion: true,
Conn: db,
}), &gorm.Config{})
if nil != err {
log.Fatalf("Init DB with sqlmock failed, err %v", err)
}
}
// Test the information of a product , Enable sqlmock, Put back the result set
func TestOneMockRes(t *testing.T) {
goodsId:=1
// Create database records
rows := sqlmock.NewRows([]string{"goodsId", "goodsName", "subject", "price", "stock"}).
AddRow(2, "Moonii Ceramic lunar lamp ", " This is a test product ", "5.32", "33")
mock.ExpectQuery("^SELECT \\* FROM `goods` WHERE goodsId=\\? ORDER BY `goods`.`goodsId` LIMIT 1").
WithArgs(goodsId).WillReturnRows(rows)
// perform
goods,err:=controller.GoodsOne(goodsId)
if (err != nil) {
t.Fatalf("goodsId: %d, err:%v", goodsId, err)
}else {
fmt.Println(goods)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("there were unfulfilled expectations: %s", err)
}
}
// Test the information of a product , Enable sqlmock, No result set
func TestOneMockNoRes(t *testing.T) {
goodsId:=1
// Create database records
rows := sqlmock.NewRows([]string{"goodsId", "goodsName", "subject", "price", "stock"})
//AddRow(2, "Moonii Ceramic lunar lamp ", " This is a test product ", "5.32", "33")
mock.ExpectQuery("^SELECT \\* FROM `goods` WHERE goodsId=\\? ORDER BY `goods`.`goodsId` LIMIT 1").
WithArgs(goodsId).WillReturnRows(rows)
// perform
goods,err:=controller.GoodsOne(goodsId)
if (err != nil) {
t.Fatalf("goodsId: %d, err:%v", goodsId, err)
}else {
fmt.Println(goods)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("there were unfulfilled expectations: %s", err)
}
}
// Test the information of a product , Enable sqlmock, Return database error
func TestOneMockError(t *testing.T) {
goodsId:=1
// Pass on sql
mock.ExpectQuery("^SELECT \\* FROM `goods` WHERE goodsId=\\? ORDER BY `goods`.`goodsId` LIMIT 1").
WithArgs(goodsId).WillReturnError(errors.New("some error"))
// perform
goods,err:=controller.GoodsOne(goodsId)
if (err != nil) {
t.Fatalf("goodsId: %d, err:%v", goodsId, err)
}else {
fmt.Println(goods)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("there were unfulfilled expectations: %s", err)
}
}
Four , The test results
Execute test command :
[email protected]:/data/go/unittest04# go test -vreturn :
[email protected]:/data/go/unittest04# go test -v
=== RUN TestOneMockRes
&{2 Moonii Ceramic lunar lamp This is a test product 5.32 33}
--- PASS: TestOneMockRes (0.00s)
=== RUN TestOneMockNoRes
2021/02/01 10:20:56 /data/go/unittest04/controller/userController.go:10 record not found
[0.075ms] [rows:0] SELECT * FROM `goods` WHERE goodsId=1 ORDER BY `goods`.`goodsId` LIMIT 1
main_test.go:69: goodsId: 1, err:record not found
--- FAIL: TestOneMockNoRes (0.00s)
=== RUN TestOneMockError
2021/02/01 10:20:56 /data/go/unittest04/controller/userController.go:10 some error
[0.038ms] [rows:0] SELECT * FROM `goods` WHERE goodsId=1 ORDER BY `goods`.`goodsId` LIMIT 1
main_test.go:87: goodsId: 1, err:some error
--- FAIL: TestOneMockError (0.00s)
FAIL
exit status 1
FAIL github.com/liuhongdi/unittest04 0.012s
5、 ... and , View the version of the library :
module github.com/liuhongdi/unittest04
go 1.15
require (
gorm.io/driver/mysql v1.0.1
gorm.io/gorm v1.20.6
github.com/DATA-DOG/go-sqlmock v1.5.0
)
边栏推荐
- PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
- Libuv库 - 设计概述(中文版)
- When updating mysql, the condition is a query
- Screenshot of the operation steps of upload labs level 4-level 9
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
- MySQL functions and related cases and exercises
- [redis] cache warm-up, cache avalanche and cache breakdown
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
- Unity render streaming communicates with unity through JS
- The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
猜你喜欢

RichView TRVStyle ListStyle 列表样式(项目符号编号)

Universal dividend source code, supports the dividend of any B on the BSC

When updating mysql, the condition is a query

Go language unit test 3: go language uses gocovey library to do unit test

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场

Unity EmbeddedBrowser浏览器插件事件通讯

The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti

Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?

Flutter dynamic | fair 2.5.0 new version features

The difference between stratifiedkfold (classification) and kfold (regression)
随机推荐
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
Father and basketball
There is nothing new under the sun. Can the meta universe go higher?
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
JS convert pseudo array to array
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
The network card fails to start after the cold migration of the server hard disk
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
JSON serialization case summary
Static linked list (subscript of array instead of pointer)
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
父亲和篮球
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
User and group command exercises
Spark实战1:单节点本地模式搭建Spark运行环境
[technology development-24]: characteristics of existing IOT communication technology
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
[sort] bucket sort
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?