如何使用gorm进行单元测试。

4

我刚开始学习Gounit test。在我的项目中,我正在使用Gogorm连接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)
}

我尝试创建下面这个单元测试文件,但它无法运行,显示如下所示:enter image description here 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())
}

请帮我编写单元测试文件。
2个回答

6
“如何进行单元测试”这是一个非常广泛的问题,因为它取决于您要测试什么。在您的示例中,您正在使用远程连接到数据库,通常在单元测试中会模拟这种情况。不清楚您是否正在寻找这样的解决方案,也没有必要这样做。通过观察您使用不同的数据库,我预计意图并不是模拟。
首先看一下文章,已经回答了关于如何使用TestMaintesting.M的问题。
如果您的测试名称命名正确,则您当前的代码所做的(如果您的测试名称将被命名为TestMain)是在您的其他测试周围添加一个方法来设置和拆除,但是您没有任何其他测试可用于利用此设置和拆除,因此您将得到结果无测试运行
虽然这不是您问题的一部分,但我建议在感到自信测试Go代码之前尝试避免使用testing.M。使用testing.T并测试单独的单元可能更容易理解。您可以通过在测试中调用initDB()并使初始化程序接受参数来实现几乎相同的效果。
func initDB(dbToUse string) {
    // ...
    db.Exec("USE "+dbToUse)
}

您可以从主文件调用initDB("test"),从测试中调用initDB("test111")。您可以在pkg.go.dev/testing上阅读有关Go测试包的信息,您还将找到testing.Ttesting.M之间的区别。
以下是一个简短的示例,其中包含一些基本测试,不需要任何设置或拆卸,并且使用testing.T而不是testing.Mmain.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能帮助您进行断言并在测试失败时输出漂亮的输出,表驱动测试 是 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)
        }
    }
}

2
似乎没有关于模拟GORM的示例。 - ahuigo

1

你可以使用 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)
    }
}

2
目前你的回答不够清晰。请编辑并添加更多细节,以帮助其他人理解它如何回答所提出的问题。你可以在帮助中心找到有关如何撰写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接