Gin http request test 筆記

gin 思路 參考相關文件、 repo ,在設計上的層級類似下列這樣 server => service (interface) => repository (interface) 將 interface 開出來,實作完之後一路使用注入的逐層掛進去。 開出 interface 除了規範 method 之外,還能在後續針對測試時, 方便撰寫 mock 注入。 server | - service | - repository 略 測試 以使用 gin 為例 get const wantGetMsg = "mock hello eric" func TestHelloByName(t *testing.T) { gin.SetMode(gin.TestMode) r := gin.Default() // di mock service service := MockHelloService{} server := NewHelloServer(service) // mount router r.GET("/hello/:name", server.SayHello) rq := httptest.NewRequest(http.MethodGet, "/hello/eric", nil) rw := httptest....

March 1, 2022 · 2 min · Me

Gorm Sqlite 測試相關問題

failed to initialize database 原因 golang + gorm 搭配 sqlite 練習測試的時候發現, 把 db 用 go-sqlmock 的替換掉後,在跑測試時會噴 failed to initialize database, got error all expectations were already fulfilled, call to Query 'select sqlite_version()' mysql 解法 如果 db 是用 mysql 的話,只要這樣設定 gDB, err := gorm.Open(mysql.New(mysql.Config{ Conn: DB, SkipInitializeWithVersion: true, }), &gorm.Config{}) 細節可以參考 source code sqlite 暫解 不過在 sqlite 上面看 code 是沒有開這個參數 source code 因為只是練習,所以我這邊先直接寫一個新的 MockDialector 把它掛進來, 然後把 sqlite.Dialector() 複製過來後把 version 不需要的地方拿掉。 實際上應該把相關的 code 都看過,確認是不是有必要開這個參數出來,然後發個 issue , 如果沒問題 pr 做出來之類的?...

February 10, 2022 · 2 min · Me

[2022-沒工作系列-03] Hugo PaperMod 主題安裝 Disqus

註冊 Disqus 進入 Disqus 註冊,基本上按步驟做,列出幾個比較重要的。 Select plan 選擇 Basic 方案 Select Platform 選擇手動安裝 會出現兩份要放入的 html ,用途應該分別為 comment 以及 count 後續把設定完成。 在 Hugo PaperMod 主題設定 因每個主題針對 comment 的設定可能不一樣,這以我用的 PaperMod 為例 調整專案根目錄下的 config.yml 新增 disqus 設定, shortname 按照在 disqus 的設定 在 params 下,調整 comments 為 true // 這邊 disqus: enable: true shortname: chzlab-net count: true // 到這邊 params: title: Deric's BLOG // 略 comments: true // 多這 在專案根目錄下建立資料夾 layouts 在 layouts 資料夾下 建立 partials 資料夾 從專案根目錄下的 /themes/PaperMod/layouts/partials/comments....

January 30, 2022 · 2 min · Me

[2022-沒工作系列-02] Golang Command 入門

基本用法 目前練習先使用 flag package func init() { flag.StringVar(&sourceFolder, "s", "", "source folder path [required]") // 設定要吃的參數,使用有帶 Var func 會把值存到 第一個參數中 // output -s [path] flag.Usage = usage // 執行沒帶參數 或帶 --help / -h 會顯示的提示訊息 } func usage() { fmt.Fprintln(os.Stderr, "Usage: parse [options]") flag.PrintDefaults() // 透過 flag 設定的參數說明都印出來 } 參考文件 Can command line flags in Go be set to mandatory? Go by Example: Command-Line Subcommands Golang 官方文件 flag 【Golang】如何讀取 command-line argument/flag?必知的幾種用法! 其他套件 go-flags cobra

January 30, 2022 · 1 min · Me