From 4800bb3284c978b8b6d25e0e32224236eb6d8a01 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Thu, 30 May 2024 15:47:27 +0700 Subject: [PATCH] Adding test and bench example --- .golangci.yaml | 3 ++ internal/monero/prober_test.go | 84 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 internal/monero/prober_test.go diff --git a/.golangci.yaml b/.golangci.yaml index 97c7c4a..86d336c 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -2,3 +2,6 @@ issues: exclude-rules: - path: frontend/embed.go text: "pattern build/\\*: no matching files found" + - path: _test.go + linters: + - errcheck diff --git a/internal/monero/prober_test.go b/internal/monero/prober_test.go new file mode 100644 index 0000000..ac34237 --- /dev/null +++ b/internal/monero/prober_test.go @@ -0,0 +1,84 @@ +package monero + +import ( + "fmt" + "os" + "strconv" + "testing" + "xmr-remote-nodes/internal/config" + "xmr-remote-nodes/internal/database" +) + +var testMySQL = true + +func init() { + // load test db config from OS environment variable + // + // Example: + // TEST_DB_HOST=127.0.0.1 \ + // TEST_DB_PORT=3306 \ + // TEST_DB_USER=testuser \ + // TEST_DB_PASSWORD=testpass \ + // TEST_DB_NAME=testdb go test ./... -v + // + // To run benchmark only, add `-bench=. -run=^#` to the `go test` command + config.DBCfg().Host = os.Getenv("TEST_DB_HOST") + config.DBCfg().Port, _ = strconv.Atoi(os.Getenv("TEST_DB_PORT")) + config.DBCfg().User = os.Getenv("TEST_DB_USER") + config.DBCfg().Password = os.Getenv("TEST_DB_PASSWORD") + config.DBCfg().Name = os.Getenv("TEST_DB_NAME") + + if err := database.ConnectDB(); err != nil { + testMySQL = false + } +} + +// TODO: Add database test table and then clean it up + +func TestProberRepo_CheckApi(t *testing.T) { + if !testMySQL { + fmt.Println("Skip test, not connected to database") + t.Skip("Skip test, not connected to database") + } + tests := []struct { + name string + apiKey string + want Prober + wantErr bool + }{ + { + name: "Empty key", + apiKey: "", + want: Prober{}, + wantErr: true, + }, + { + name: "Invalid key", + apiKey: "invalid", + want: Prober{}, + wantErr: true, + }, + } + + repo := NewProber() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := repo.CheckApi(tt.apiKey) + if (err != nil) != tt.wantErr { + t.Errorf("ProberRepo.CheckApi() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } +} + +func BenchmarkProberRepo_CheckApi(b *testing.B) { + if !testMySQL { + fmt.Println("Skip bench, not connected to database") + b.Skip("Skip bench, not connected to database") + } + repo := NewProber() + for i := 0; i < b.N; i++ { + repo.CheckApi("") + } +}