搜尋
首頁後端開發GolangGolang 與 Gin、Gorm、Testify、PostgreSQL 的整合測試

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
進行錯誤處理:最佳實踐和模式進行錯誤處理:最佳實踐和模式May 04, 2025 am 12:19 AM

在Go編程中,有效管理錯誤的方法包括:1)使用錯誤值而非異常,2)採用錯誤包裝技術,3)定義自定義錯誤類型,4)復用錯誤值以提高性能,5)謹慎使用panic和recover,6)確保錯誤消息清晰且一致,7)記錄錯誤處理策略,8)將錯誤視為一等公民,9)使用錯誤通道處理異步錯誤。這些做法和模式有助於編寫更健壯、可維護和高效的代碼。

您如何在GO中實施並發?您如何在GO中實施並發?May 04, 2025 am 12:13 AM

在Go中實現並發可以通過使用goroutines和channels來實現。 1)使用goroutines來並行執行任務,如示例中同時享受音樂和觀察朋友。 2)通過channels在goroutines之間安全傳遞數據,如生產者和消費者模式。 3)避免過度使用goroutines和死鎖,合理設計系統以優化並發程序。

在GO中構建並發數據結構在GO中構建並發數據結構May 04, 2025 am 12:09 AM

Gooffersmultipleapproachesforbuildingconcurrentdatastructures,includingmutexes,channels,andatomicoperations.1)Mutexesprovidesimplethreadsafetybutcancauseperformancebottlenecks.2)Channelsofferscalabilitybutmayblockiffullorempty.3)Atomicoperationsareef

將GO的錯誤處理與其他編程語言進行比較將GO的錯誤處理與其他編程語言進行比較May 04, 2025 am 12:09 AM

go'serrorhandlingisexplicit,治療eRROSASRETRATERTHANEXCEPTIONS,與pythonandjava.1)go'sapphifeensuresererrawaresserrorawarenessbutcanleadtoverbosecode.2)pythonandjavauseexeexceptionseforforforforforcleanerCodebutmaymobisserrors.3)

測試代碼依賴於INET功能的代碼測試代碼依賴於INET功能的代碼May 03, 2025 am 12:20 AM

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

將GO的錯誤處理方法與其他語言進行比較將GO的錯誤處理方法與其他語言進行比較May 03, 2025 am 12:20 AM

go'serrorhandlingurturnserrorsasvalues,與Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

設計有效界面的最佳實踐設計有效界面的最佳實踐May 03, 2025 am 12:18 AM

AnefactiveInterfaceingoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForabStractionToswaPimplementations withoutchangingCallingCode.3)

集中式錯誤處理策略集中式錯誤處理策略May 03, 2025 am 12:17 AM

集中式錯誤處理在Go語言中可以提升代碼的可讀性和可維護性。其實現方式和優勢包括:1.將錯誤處理邏輯從業務邏輯中分離,簡化代碼。 2.通過集中處理錯誤,確保錯誤處理的一致性。 3.使用defer和recover來捕獲和處理panic,增強程序健壯性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版