目次
正确答案
handlers/handlers.go 文件
repo/repo.go 文件
handlers/handlers_test.go 文件
片尾
ホームページ バックエンド開発 Golang Golang はハンドラーを使用してモック データベースを作成し、インターフェイスを使用してデータベースを呼び出します

Golang はハンドラーを使用してモック データベースを作成し、インターフェイスを使用してデータベースを呼び出します

Feb 06, 2024 am 08:42 AM

Golang 使用处理程序创建模拟数据库并使用接口调用数据库

问题内容

我正在尝试对我的注册处理程序和数据库调用实施单元测试。但是,它在我的注册处理程序中的数据库调用上引发紧急错误。它是一个简单的注册处理程序,用于接收包含用户名、密码和电子邮件的 json。然后,我将使用 select 语句来检查该用户名是否在 signup 处理程序本身内重复。

当我向该处理程序发送我的发布请求时,这一切都有效。然而,当我实际进行单元测试时,它不起作用并给我抛出了两条错误消息。我觉得这是因为数据库没有在测试环境中初始化,但我不知道如何在不使用第三方框架进行模拟数据库的情况下做到这一点。

错误消息

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
ログイン後にコピー

signup.go

package handler

type signupjson struct {
    username string `json:"username"`
    password string `json:"password"`
    email    string `json:"email"`
}

func signup(w http.responsewriter, r *http.request) {
    // set headers
    w.header().set("content-type", "application/json")
    var newuser auth_management.signupjson

    // reading the request body and unmarshal the body to the loginjson struct
    bs, _ := io.readall(req.body)
    if err := json.unmarshal(bs, &newuser); err != nil {
        utils.responsejson(w, http.statusinternalservererror, "internal server error")
        log.println("internal server error in unmarshal json body in signup route:", err)
        return
    }

    ctx := context.background()
    ctx, cancel = context.withtimeout(ctx, time.minute * 2)
    defer cancel()

    // check if username already exists in database (duplicates not allowed)
    isexistingusername := database.getusername(ctx, newuser.username) // throws panic error here when testing
    if isexistingusername {
        utils.responsejson(w, http.statusbadrequest, "username has already been taken. please try again.")
        return
    }

    // other code logic...
}
ログイン後にコピー

sqlquery.go

package database

var sql_select_from_users = "select %s from users where %s = $1;"

func getusername(ctx context.context, username string) bool {
    row := conn.queryrow(ctx, fmt.sprintf(sql_select_from_users, "username", "username"), username)
    return row.scan() != pgx.errnorows
}
ログイン後にコピー

signup_test.go

package handler

func test_signup(t *testing.t) {

    var tests = []struct {
        name               string
        posteddata         signupjson
        expectedstatuscode int
    }{
        {
            name: "valid login",
            posteddata: signupjson{
                username: "testusername",
                password: "testpassword",
                email:    "[email protected]",
            },
            expectedstatuscode: 200,
        },
    }

    for _, e := range tests {
        jsonstr, err := json.marshal(e.posteddata)
        if err != nil {
            t.fatal(err)
        }

        // setting a request for testing
        req, _ := http.newrequest(http.methodpost, "/signup", strings.newreader(string(jsonstr)))
        req.header.set("content-type", "application/json")

        // setting and recording the response
        res := httptest.newrecorder()
        handler := http.handlerfunc(signup)

        handler.servehttp(res, req)

        if res.code != e.expectedstatuscode {
            t.errorf("%s: returned wrong status code; expected %d but got %d", e.name, e.expectedstatuscode, res.code)
        }
    }
}
ログイン後にコピー

setup_test.go

func TestMain(m *testing.M) {

    os.Exit(m.Run())
}

ログイン後にコピー

我在这里看到了一个类似的问题,但不确定这是否是正确的方法,因为没有响应,而且答案很混乱:how to write an unit test for a handler that invokes a function that invokes a function that intersted with db in golang using pgx 驱动程序?


正确答案


让我尝试帮助您弄清楚如何实现这些目标。我对你的代码进行了一些重构,但总体思路和使用的工具仍然与你的相同。首先,我将分享分为两个文件的生产代码:handlers/handlers.gorepo/repo.go

handlers/handlers.go 文件

package handlers

import (
    "context"
    "database/sql"
    "encoding/json"
    "io"
    "net/http"
    "time"

    "handlertest/repo"
)

type signupjson struct {
    username string `json:"username"`
    password string `json:"password"`
    email    string `json:"email"`
}

func signup(w http.responsewriter, r *http.request) {
    w.header().set("content-type", "application/json")

    var newuser signupjson
    bs, _ := io.readall(r.body)
    if err := json.unmarshal(bs, &newuser); err != nil {
        w.writeheader(http.statusbadrequest)
        w.write([]byte(err.error()))
        return
    }

    ctx, cancel := context.withtimeout(r.context(), time.minute*2)
    defer cancel()

    db, _ := ctx.value("db").(*sql.db)
    if isexistingusername := repo.getusername(ctx, db, newuser.username); isexistingusername {
        w.writeheader(http.statusbadrequest)
        w.write([]byte("username already present"))
        return
    }
    w.writeheader(http.statusok)
}
ログイン後にコピー

这里有两个主要区别:

  1. 使用的 context。您不必实例化另一个 ctx,只需使用与 http.request 一起提供的那个即可。
  2. 使用的 sql 客户端。正确的方法是通过context.context来传递。对于这种情况,您不必构建任何结构或使用任何接口等。只需编写一个需要 *sql.db 作为参数的函数即可。请记住这一点,函数是一等公民

当然,还有重构的空间。 "db" 应该是一个常量,我们必须检查上下文值中是否存在此条目,但为了简洁起见,我省略了这些检查。

repo/repo.go 文件

package repo

import (
    "context"
    "database/sql"

    "github.com/jackc/pgx/v5"
)

func getusername(ctx context.context, db *sql.db, username string) bool {
    row := db.queryrowcontext(ctx, "select username from users where username = $1", username)
    return row.scan() != pgx.errnorows
}
ログイン後にコピー

这里的代码与您的非常相似,除了以下两个小问题:

  1. 当您希望考虑上下文时,有一个名为 queryrowcontext 的专用方法。
  2. 当您必须构建 sql 查询时,请使用准备好的语句功能。不要将内容与 fmt.sprintf 连接起来,原因有两个:安全性和可测试性。

现在,我们要看看测试代码。

handlers/handlers_test.go 文件

package handlers

import (
    "context"
    "net/http"
    "net/http/httptest"
    "strings"
    "testing"

    "github.com/DATA-DOG/go-sqlmock"
    "github.com/jackc/pgx/v5"
    "github.com/stretchr/testify/assert"
)

func TestSignUp(t *testing.T) {
    db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    if err != nil {
        t.Fatalf("err not expected while open a mock db, %v", err)
    }
    defer db.Close()
    t.Run("NewUser", func(t *testing.T) {
        mock.ExpectQuery("SELECT username FROM users WHERE username = $1").WithArgs("<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="ec86838482c2888389ac89948d819c8089c28f8381">[email&#160;protected]</a>").WillReturnError(pgx.ErrNoRows)

        w := httptest.NewRecorder()
        r := httptest.NewRequest(http.MethodPost, "/signup", strings.NewReader(`{"username": "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="3c56535452125853597c59445d514c5059125f5351">[email&#160;protected]</a>", "password": "1234", "email": "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="670d080f094903080227021f060a170b024904080a">[email&#160;protected]</a>"}`))

        ctx := context.WithValue(r.Context(), "DB", db)
        r = r.WithContext(ctx)

        SignUp(w, r)

        assert.Equal(t, http.StatusOK, w.Code)
        if err := mock.ExpectationsWereMet(); err != nil {
            t.Errorf("not all expectations were met: %v", err)
        }
    })

    t.Run("AlreadyExistentUser", func(t *testing.T) {
        rows := sqlmock.NewRows([]string{"username"}).AddRow("<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="9df7f2f5f3b3f9f2f8ddf8e5fcf0edf1f8b3fef2f0">[email&#160;protected]</a>")
        mock.ExpectQuery("SELECT username FROM users WHERE username = $1").WithArgs("<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="107a7f787e3e747f75507568717d607c753e737f7d">[email&#160;protected]</a>").WillReturnRows(rows)

        w := httptest.NewRecorder()
        r := httptest.NewRequest(http.MethodPost, "/signup", strings.NewReader(`{"username": "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="33595c5b5d1d575c5673564b525e435f561d505c5e">[email&#160;protected]</a>", "password": "1234", "email": "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="a3c9cccbcd8dc7ccc6e3c6dbc2ced3cfc68dc0ccce">[email&#160;protected]</a>"}`))

        ctx := context.WithValue(r.Context(), "DB", db)
        r = r.WithContext(ctx)

        SignUp(w, r)

        assert.Equal(t, http.StatusBadRequest, w.Code)
        if err := mock.ExpectationsWereMet(); err != nil {
            t.Errorf("not all expectations were met: %v", err)
        }
    })
}
ログイン後にコピー

这里,与您的版本相比有很多变化。让我快速回顾一下:

  • 使用子测试功能为测试提供层次结构。
  • 使用 httptest 包,它提供了用于构建和断言 http 请求和响应的内容。
  • 使用 sqlmock 包。模拟数据库的事实上的标准。
  • 使用 context 传递 sql 客户端以及 http.request
  • 已使用 github.com/stretchr/testify/assert 包完成断言。

这同样适用于这里:有重构的空间(例如,您可以使用表驱动测试功能重新设计测试)。

片尾

这可以被认为是编写 go 代码的惯用方式。我知道这可能非常具有挑战性,尤其是在一开始。如果您需要有关某些部分的更多详细信息,请告诉我,我将很乐意为您提供帮助,谢谢!

以上がGolang はハンドラーを使用してモック データベースを作成し、インターフェイスを使用してデータベースを呼び出しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Debian OpenSSLの脆弱性は何ですか Debian OpenSSLの脆弱性は何ですか Apr 02, 2025 am 07:30 AM

OpenSSLは、安全な通信で広く使用されているオープンソースライブラリとして、暗号化アルゴリズム、キー、証明書管理機能を提供します。ただし、その歴史的バージョンにはいくつかの既知のセキュリティの脆弱性があり、その一部は非常に有害です。この記事では、Debian SystemsのOpenSSLの共通の脆弱性と対応測定に焦点を当てます。 Debianopensslの既知の脆弱性:OpenSSLは、次のようないくつかの深刻な脆弱性を経験しています。攻撃者は、この脆弱性を、暗号化キーなどを含む、サーバー上の不正な読み取りの敏感な情報に使用できます。

PPROFツールを使用してGOパフォーマンスを分析しますか? PPROFツールを使用してGOパフォーマンスを分析しますか? Mar 21, 2025 pm 06:37 PM

この記事では、プロファイリングの有効化、データの収集、CPUやメモリの問題などの一般的なボトルネックの識別など、GOパフォーマンスを分析するためにPPROFツールを使用する方法について説明します。

Goでユニットテストをどのように書きますか? Goでユニットテストをどのように書きますか? Mar 21, 2025 pm 06:34 PM

この記事では、GOでユニットテストを書くことで、ベストプラクティス、モッキングテクニック、効率的なテスト管理のためのツールについて説明します。

GOの浮動小数点番号操作に使用されるライブラリは何ですか? GOの浮動小数点番号操作に使用されるライブラリは何ですか? Apr 02, 2025 pm 02:06 PM

GO言語の浮動小数点数操作に使用されるライブラリは、精度を確保する方法を紹介します...

Go's Crawler Collyのキュースレッドの問題は何ですか? Go's Crawler Collyのキュースレッドの問題は何ですか? Apr 02, 2025 pm 02:09 PM

Go Crawler Collyのキュースレッドの問題は、Go言語でColly Crawler Libraryを使用する問題を調査します。 �...

フロントエンドからバックエンドの開発に変身すると、JavaやGolangを学ぶことはより有望ですか? フロントエンドからバックエンドの開発に変身すると、JavaやGolangを学ぶことはより有望ですか? Apr 02, 2025 am 09:12 AM

バックエンド学習パス:フロントエンドからバックエンドへの探査の旅は、フロントエンド開発から変わるバックエンド初心者として、すでにNodeJSの基盤を持っています...

go.modファイルで依存関係をどのように指定しますか? go.modファイルで依存関係をどのように指定しますか? Mar 27, 2025 pm 07:14 PM

この記事では、go.modを介してGOモジュールの依存関係の管理、仕様、更新、競合解決をカバーすることについて説明します。セマンティックバージョンや定期的な更新などのベストプラクティスを強調しています。

GOでテーブル駆動型テストをどのように使用しますか? GOでテーブル駆動型テストをどのように使用しますか? Mar 21, 2025 pm 06:35 PM

この記事では、GOでテーブル駆動型のテストを使用して説明します。これは、テストのテーブルを使用して複数の入力と結果を持つ関数をテストする方法です。読みやすさの向上、重複の減少、スケーラビリティ、一貫性、および

See all articles