使用 GORM 进行单元测试的方法
来源:stackoverflow
时间:2024-02-18 10:09:23 197浏览 收藏
一分耕耘,一分收获!既然打开了这篇文章《使用 GORM 进行单元测试的方法》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!
我是 go 和 unit test 的新用户。在我的项目中,我将 go 与 gorm 一起使用并连接 mysql 数据库。
我的疑问是如何对我的代码进行单元测试:
我的代码如下(main.go):
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type jobs struct {
jobid uint `json: "jobid" gorm:"primary_key;auto_increment"`
sourcepath string `json: "sourcepath"`
priority int64 `json: "priority"`
internalpriority string `json: "internalpriority"`
executionenvironmentid string `json: "executionenvironmentid"`
}
type executionenvironment struct {
id uint `json: "id" gorm:"primary_key;auto_increment"`
executionenvironmentid string `json: "executionenvironmentid"`
cloudprovidertype string `json: "cloudprovidertype"`
infrastructuretype string `json: "infrastructuretype"`
cloudregion string `json: "cloudregion"`
createdat time.time `json: "createdat"`
}
var db *gorm.db
func initdb() {
var err error
datasourcename := "root:@tcp(localhost:3306)/?parsetime=true"
db, err = gorm.open("mysql", datasourcename)
if err != nil {
fmt.println(err)
panic("failed to connect database")
}
//db.exec("create database test")
db.logmode(true)
db.exec("use test")
db.automigrate(&jobs{}, &executionenvironment{})
}
func getalljobs(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
fmt.println("executing get all jobs function")
var jobs []jobs
if err := db.select("jobs.*, execution_environments.*").joins("join execution_environments on execution_environments.execution_environment_id = jobs.execution_environment_id").find(&jobs).error; err != nil {
fmt.println(err)
}
fmt.println()
if len(jobs) == 0 {
json.newencoder(w).encode("no data found")
} else {
json.newencoder(w).encode(jobs)
}
}
// create job
func createjob(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
fmt.println("executing create jobs function")
var jobs jobs
json.newdecoder(r.body).decode(&jobs)
db.create(&jobs)
json.newencoder(w).encode(jobs)
}
// get job by id
func getjobbyid(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
params := mux.vars(r)
jobid := params["jobid"]
//var job []jobs
//db.preload("items").first(&job, jobid)
var jobs []jobs
var executionenvironments []executionenvironment
if err := db.table("jobs").select("jobs.*, execution_environments.*").joins("join execution_environments on execution_environments.execution_environment_id = jobs.execution_environment_id").where("job_id =?", jobid).find(&jobs).scan(&executionenvironments).error; err != nil {
fmt.println(err)
}
if len(jobs) == 0 {
json.newencoder(w).encode("no data found")
} else {
json.newencoder(w).encode(jobs)
}
}
// delete job by id
func deletejobbyid(w http.responsewriter, r *http.request) {
params := mux.vars(r)
jobid := params["jobid"]
// check data
var job []jobs
db.table("jobs").select("jobs.*").where("job_id=?", jobid).find(&job)
if len(job) == 0 {
json.newencoder(w).encode("invalid jobid")
} else {
id64, _ := strconv.parseuint(jobid, 10, 64)
idtodelete := uint(id64)
db.where("job_id = ?", idtodelete).delete(&jobs{})
//db.where("jobid = ?", idtodelete).delete(&executionenvironment{})
json.newencoder(w).encode("job deleted successfully")
w.writeheader(http.statusnocontent)
}
}
// create execution environments
func createenvironments(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
fmt.println("executing create execution environments function")
var executionenvironments executionenvironment
json.newdecoder(r.body).decode(&executionenvironments)
db.create(&executionenvironments)
json.newencoder(w).encode(executionenvironments)
}
// get job cloud region
func getjobcloudregion(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
fmt.println("executing get job cloud region function")
params := mux.vars(r)
jobid := params["jobid"]
//var jobs []jobs
var executionenvironment []executionenvironment
db.table("jobs").select("execution_environments.*").joins("join execution_environments on execution_environments.execution_environment_id = jobs.execution_environment_id").where("jobs.job_id =?", jobid).find(&executionenvironment)
var puuid []string
for _, uuid := range executionenvironment {
puuid = append(puuid, uuid.cloudregion)
}
json.newencoder(w).encode(puuid)
}
func main() {
// router
router := mux.newrouter()
// access url
router.handlefunc("/getalljobs", getalljobs).methods("get")
router.handlefunc("/createjob", createjob).methods("post")
router.handlefunc("/getjobbyid/{jobid}", getjobbyid).methods("get")
router.handlefunc("/deletejobbyid/{jobid}", deletejobbyid).methods("delete")
router.handlefunc("/createenvironments", createenvironments).methods("post")
router.handlefunc("/getjobcloudregion/{jobid}", getjobcloudregion).methods("get")
// initialize db connection
initdb()
// config port
fmt.printf("starting server at 8000 \n")
http.listenandserve(":8000", router)
}
我尝试在下面创建单元测试文件,但它没有运行,显示如下
main_test.go:
package main
import (
"log"
"os"
"testing"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func TestinitDB(m *testing.M) {
dataSourceName := "root:@tcp(localhost:3306)/?parseTime=True"
db, err := gorm.Open("mysql", dataSourceName)
if err != nil {
log.Fatal("failed to connect database")
}
//db.Exec("CREATE DATABASE test")
db.LogMode(true)
db.Exec("USE test111")
os.Exit(m.Run())
}
请帮我写单元测试文件
正确答案
“如何进行单元测试”是一个非常广泛的问题,因为它取决于您想要测试的内容。在您的示例中,您正在使用与数据库的远程连接,这通常是在单元测试中模拟的内容。目前尚不清楚这是否是您正在寻找的东西,也不是必须这样做。通过看到您使用不同的数据库,我希望您的意图不是嘲笑。
首先查看 this 帖子,该帖子已经回答了您关于 TestMain 和 testing.m 如何工作的问题。
您的代码当前所做的(如果您的测试名称正确命名为 testmain)是在其他测试周围添加一个方法来进行设置和拆卸,但是您没有任何其他测试来使用此设置和拆卸因此,您将获得运行 的 no 测试结果。
这不是你问题的一部分,但我建议尝试避免 testing.m ,直到你对测试 go 代码有信心为止。使用 testing.t 并测试单独的单元可能更容易理解。您只需在测试中调用 initdb() 并使初始化程序接受参数即可实现几乎相同的效果。
func initdb(dbtouse string) {
// ...
db.exec("use "+dbtouse)
}
然后,您可以从主文件中调用 initdb("test") ,并从测试中调用 initdb("test111") 。
您可以在 pkg.go.dev/testing 阅读有关 go 测试包的信息,您还可以在其中找到 testing.t 和 testing.m 之间的差异。
这是一个简短的示例,其中包含一些基本测试,不需要任何设置或拆卸,并且使用 testing.t 而不是 testing.m。
main.go
package main
import "fmt"
func main() {
fmt.println(add(1, 2))
}
func add(a, b int) int {
return a + b
}
main_test.go
package main
import "testing"
func testadd(t *testing.t) {
t.run("add 2 + 2", func(t *testing.t) {
want := 4
// call the function you want to test.
got := add(2, 2)
// assert that you got your expected response
if got != want {
t.fail()
}
})
}
此测试将测试您的方法 add 并确保当您传递 2、2 作为参数时它返回正确的值。 t.run 的使用是可选的,但它会为您创建一个子测试,这使得读取输出更容易一些。
由于您在包级别进行测试,因此如果您不使用递归地使用三点格式(包括每个包),则需要指定要测试的包。
要运行上面示例中的测试,请指定您的包和 -v 以获取详细输出。
$ go test ./ -v
=== run testadd
=== run testadd/add_2_+_2
--- pass: testadd (0.00s)
--- pass: testadd/add_2_+_2 (0.00s)
pass
ok x (cached)
围绕这个主题还有很多东西需要学习,例如测试框架和测试模式。例如,测试框架 testify 可以帮助您进行断言并在测试失败时打印漂亮的输出,而 table driven tests 是 go 中非常常见的模式。
您还在编写一个 http 服务器,它通常需要额外的测试设置才能正确测试。幸运的是,标准库中的 http 包附带了一个名为 httptest 的子包,它可以帮助您记录外部请求或为外部请求启动本地服务器。您还可以通过使用手动构建的请求直接调用处理程序来测试您的处理程序。
它看起来像这样。
func testsomehandler(t *testing.t) {
// create a request to pass to our handler. we don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.newrequest("get", "/some-endpoint", nil)
if err != nil {
t.fatal(err)
}
// we create a responserecorder (which satisfies http.responsewriter) to record the response.
rr := httptest.newrecorder()
handler := http.handlerfunc(somehandler)
// our handlers satisfy http.handler, so we can call their servehttp method
// directly and pass in our request and responserecorder.
handler.servehttp(rr, req)
// check the status code is what we expect.
if status := rr.code; status != http.statusok {
t.errorf("handler returned wrong status code: got %v want %v",
status, http.statusok)
}
现在,测试您的一些代码。我们可以运行 init 方法并使用响应记录器调用您的任何服务。
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func testgetalljobs(t *testing.t) {
// initialize the db
initdb("test111")
req, err := http.newrequest("get", "/getalljobs", nil)
if err != nil {
t.fatal(err)
}
rr := httptest.newrecorder()
handler := http.handlerfunc(getalljobs)
handler.servehttp(rr, req)
// check the status code is what we expect.
if status := rr.code; status != http.statusok {
t.errorf("handler returned wrong status code: got %v want %v",
status, http.statusok)
}
var response []jobs
if err := json.unmarshal(rr.body.bytes(), &response); err != nil {
t.errorf("got invalid response, expected list of jobs, got: %v", rr.body.string())
}
if len(response) < 1 {
t.errorf("expected at least 1 job, got %v", len(response))
}
for _, job := range response {
if job.sourcepath == "" {
t.errorf("expected job id %d to have a source path, was empty", job.jobid)
}
}
}
你可以使用go-sqlmock:
package main
import (
"database/sql"
"regexp"
"testing"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Student struct {
//*gorm.Model
Name string
ID string
}
type v2Suite struct {
db *gorm.DB
mock sqlmock.Sqlmock
student Student
}
func TestGORMV2(t *testing.T) {
s := &v2Suite{}
var (
db *sql.DB
err error
)
db, s.mock, err = sqlmock.New()
if err != nil {
t.Errorf("Failed to open mock sql db, got error: %v", err)
}
if db == nil {
t.Error("mock db is null")
}
if s.mock == nil {
t.Error("sqlmock is null")
}
dialector := postgres.New(postgres.Config{
DSN: "sqlmock_db_0",
DriverName: "postgres",
Conn: db,
PreferSimpleProtocol: true,
})
s.db, err = gorm.Open(dialector, &gorm.Config{})
if err != nil {
t.Errorf("Failed to open gorm v2 db, got error: %v", err)
}
if s.db == nil {
t.Error("gorm db is null")
}
s.student = Student{
ID: "123456",
Name: "Test 1",
}
defer db.Close()
s.mock.MatchExpectationsInOrder(false)
s.mock.ExpectBegin()
s.mock.ExpectQuery(regexp.QuoteMeta(
`INSERT INTO "students" ("id","name")
VALUES ($1,$2) RETURNING "students"."id"`)).
WithArgs(s.student.ID, s.student.Name).
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(s.student.ID))
s.mock.ExpectCommit()
if err = s.db.Create(&s.student).Error; err != nil {
t.Errorf("Failed to insert to gorm db, got error: %v", err)
}
err = s.mock.ExpectationsWereMet()
if err != nil {
t.Errorf("Failed to meet expectations, got error: %v", err)
}
}理论要掌握,实操不能落!以上关于《使用 GORM 进行单元测试的方法》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习