首頁 > 後端開發 > Golang > 主體

Golang 與 Gin、Gorm、Testify、PostgreSQL 的整合測試

DDD
發布: 2024-10-26 15:10:03
原創
487 人瀏覽過

Golang Integration Test With Gin, Gorm, Testify, PostgreSQL

使用 Gin、GORM、Testify 和 PostgreSQL 在 Golang 中建立全面的整合測試設定涉及設定測試資料庫、為 CRUD 操作編寫測試以及使用 Testify 進行斷言。這是幫助您入門的逐步指南:

先決條件

  • 安裝
  • Docker 已安裝
  • 函式庫:gin-gonic/gin、gorm.io/gorm、gorm.io/driver/postgres、testify、testcontainers-go

專案結構

myapp/
|-- main.go
|-- models/
|   |-- models.go
|-- handlers/
|   |-- handlers.go
|-- tests/
|   |-- integration_test.go
|-- go.mod
|-- go.sum
登入後複製
登入後複製
登入後複製

1. 設定模型 (models/models.go)

使用 GORM 標籤定義模型以進行資料庫對應。

package models

import (
    "time"
    "gorm.io/gorm"
)

type User struct {
    ID        uint           `gorm:"primaryKey"`
    Name      string         `gorm:"not null"`
    Email     string         `gorm:"unique;not null"`
    CreatedAt time.Time
}

type Book struct {
    ID            uint           `gorm:"primaryKey"`
    Title         string         `gorm:"not null"`
    Author        string         `gorm:"not null"`
    PublishedDate time.Time      `gorm:"not null"`
}

type BorrowLog struct {
    ID         uint           `gorm:"primaryKey"`
    UserID     uint           `gorm:"not null"`
    BookID     uint           `gorm:"not null"`
    BorrowedAt time.Time      `gorm:"default:CURRENT_TIMESTAMP"`
    ReturnedAt *time.Time
}
登入後複製

2. 設定處理程序 (handlers/handlers.go)

使用 Gin 定義 CRUD 操作的路由和處理程序。

package handlers

import (
    "myapp/models"
    "net/http"

    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
)

type Handler struct {
    DB *gorm.DB
}

func (h *Handler) CreateUser(c *gin.Context) {
    var user models.User
    if err := c.ShouldBindJSON(&user); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    if err := h.DB.Create(&user).Error; err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusCreated, user)
}

func (h *Handler) GetUser(c *gin.Context) {
    var user models.User
    if err := h.DB.First(&user, c.Param("id")).Error; err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
        return
    }

    c.JSON(http.StatusOK, user)
}

func (h *Handler) UpdateUser(c *gin.Context) {
    var user models.User
    if err := h.DB.First(&user, c.Param("id")).Error; err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
        return
    }

    if err := c.ShouldBindJSON(&user); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    if err := h.DB.Save(&user).Error; err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusOK, user)
}

func (h *Handler) DeleteUser(c *gin.Context) {
    if err := h.DB.Delete(&models.User{}, c.Param("id")).Error; err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusOK, gin.H{"message": "User deleted"})
}
登入後複製

3. 主應用程式(main.go)

設定資料庫連接和路由。

package main

import (
    "myapp/handlers"
    "myapp/models"
    "github.com/gin-gonic/gin"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "log"
    "os"
)

func main() {
    dsn := "host=localhost user=postgres password=yourpassword dbname=testdb port=5432 sslmode=disable"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    if err != nil {
        log.Fatalf("failed to connect to database: %v", err)
    }

    // Auto migrate the models
    db.AutoMigrate(&models.User{}, &models.Book{}, &models.BorrowLog{})

    h := handlers.Handler{DB: db}

    r := gin.Default()

    r.POST("/users", h.CreateUser)
    r.GET("/users/:id", h.GetUser)
    r.PUT("/users/:id", h.UpdateUser)
    r.DELETE("/users/:id", h.DeleteUser)

    r.Run(":8080")
}
登入後複製

4.整合測試(tests/integration_test.go)

使用 Testify 設定和斷言測試結果。

對於資料庫,我們可以使用 Dockerized PostgreSQL 實例進行測試,該實例是隔離的,測試後可以快速拆除。以下是如何使用 testcontainers-go 在 Golang 中進行設定:

安裝 testcontainers-go:

go get github.com/testcontainers/testcontainers-go
登入後複製

以下是integration_test.go 文件,用於設定測試的 PostgreSQL 容器:

myapp/
|-- main.go
|-- models/
|   |-- models.go
|-- handlers/
|   |-- handlers.go
|-- tests/
|   |-- integration_test.go
|-- go.mod
|-- go.sum
登入後複製
登入後複製
登入後複製

解釋

  • SetupTestDB:使用 GORM 設定 PostgreSQL 資料庫連線進行測試。
  • TestCreateUser:發送 POST 請求以建立新使用者並斷​​言回應。
  • TestGetUser:透過 ID 擷取使用者並檢查資料是否與插入的內容相符。
  • 測試更新用戶
    • 建立使用者並使用 PUT /users/:id 端點更新它。
    • 斷言回應狀態為 200 OK。
    • 驗證使用者的詳細資訊是否已在回應中更新。
    • 從資料庫取得使用者並確認變更已保留。
  • 測試刪除使用者
    • 建立使用者並使用 DELETE /users/:id 端點刪除它。
    • 斷言回應狀態為 200 OK 並檢查是否有成功訊息。
    • 嘗試從資料庫中取得已刪除的使用者以確保該使用者不再存在,並斷言 gorm.ErrRecordNotFound 錯誤。
  • testcontainers-go:該程式庫允許您直接從 Go 程式碼啟動 Docker 容器。它非常適合建立臨時 PostgreSQL 實例以進行整合測試。
  • setupTestDB:此函數啟動 PostgreSQL Docker 容器,使用 gorm 連接到它,並設定資料庫架構。它還確保測試完成後容器得到清理。
  • defer postgresC.Terminate(ctx):確保 PostgreSQL 容器在測試完成後終止,模擬記憶體方法。
  • 動態主機和連接埠:使用容器動態分配的主機和連接埠來連接資料庫。

運行測試

使用以下命令執行測試:

myapp/
|-- main.go
|-- models/
|   |-- models.go
|-- handlers/
|   |-- handlers.go
|-- tests/
|   |-- integration_test.go
|-- go.mod
|-- go.sum
登入後複製
登入後複製
登入後複製
使用 testcontainers-go 的好處:
  1. 隔離:每次測試運行都會取得新的 PostgreSQL 實例,確保測試之間不會發生資料外洩。
  2. 複製生產環境:針對真實的 PostgreSQL 實例進行測試可提供比使用記憶體資料庫更可靠的結果。
  3. 自動化:自動啟動和停止 PostgreSQL 容器,使其易於在 CI/CD 管道中使用。

重點

  • 使用測試資料庫:使用單獨的 PostgreSQL 資料庫(例如:容器化資料庫)進行測試是一個很好的做法,以避免影響生產資料。
  • 設定和清理:確保在測試之間清理資料庫以保持一致性。
  • Testify:提供強​​大的斷言方法來驗證結果。
  • Gin 的測試伺服器:使用 httptest 模擬針對 Gin 伺服器的 HTTP 請求。

透過此設置,您可以測試使用者模型的 CRUD 操作,確保 API 按預期與 PostgreSQL 搭配使用。您可以類似地對 Book 和 BorrowLog 模型擴展測試。

以上是Golang 與 Gin、Gorm、Testify、PostgreSQL 的整合測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!