mirror of
https://github.com/ebosas/microservices.git
synced 2024-11-16 10:08:29 +02:00
Create/update unit tests
This commit is contained in:
parent
0fc96245d1
commit
bd08ff7fdc
68
cmd/cache/cache_test.go
vendored
Normal file
68
cmd/cache/cache_test.go
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ebosas/microservices/internal/models"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
// TestUpdateRedis tests a message insertion into cache
|
||||
func TestUpdateRedis(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("miniredis connection: %s", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
c := redis.NewClient(&redis.Options{Addr: s.Addr()})
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
var tests = []struct {
|
||||
message string
|
||||
source string
|
||||
time int64
|
||||
}{
|
||||
{"Hello", "back", now},
|
||||
{"Another test!", "back", now},
|
||||
{"1", "front", now},
|
||||
{" ", "back", now - 60*60*1000},
|
||||
}
|
||||
for _, test := range tests {
|
||||
d := testArguments(t, test.message, test.source, test.time)
|
||||
updateRedis(*d, c)
|
||||
}
|
||||
|
||||
if got, err := s.Get("total"); err != nil || got != strconv.Itoa(len(tests)) {
|
||||
t.Error("'total' has the wrong value")
|
||||
}
|
||||
|
||||
list, err := s.List("messages")
|
||||
if err != nil {
|
||||
t.Errorf("list 'messages': %s", err)
|
||||
}
|
||||
|
||||
if len(list) != len(tests) {
|
||||
t.Error("'messages' has wrong length")
|
||||
}
|
||||
|
||||
// TODO: compare each message
|
||||
}
|
||||
|
||||
// testArguments produces arguments for the function being tested
|
||||
func testArguments(t *testing.T, message, source string, time int64) *amqp.Delivery {
|
||||
inputMsg := models.Message{Text: message, Source: source, Time: time}
|
||||
messageJson, err := json.Marshal(inputMsg)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal message: %s", err)
|
||||
}
|
||||
d := &amqp.Delivery{Body: messageJson}
|
||||
|
||||
return d
|
||||
}
|
@ -11,8 +11,7 @@ import (
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
// TestInsertToDB tests whether a Rabbit message is inserted
|
||||
// correctly into the database
|
||||
// TestInsertToDB tests a message insertion into a database
|
||||
func TestInsertToDB(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
|
68
cmd/server/api_test.go
Normal file
68
cmd/server/api_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
// http.HandleFunc("/api/cache", handleAPICache(connR))
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ebosas/microservices/internal/models"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
// TestAPICache tests the cache API
|
||||
func TestAPICache(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("miniredis connection: %s", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
c := redis.NewClient(&redis.Options{Addr: s.Addr()})
|
||||
|
||||
testMsg := "This is a test!"
|
||||
testUpdateRedis(t, c, testMsg)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/cache", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler := handleAPICache(c)
|
||||
handler(w, req)
|
||||
res := w.Result()
|
||||
defer res.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Errorf("expected error to be nil got %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), testMsg) {
|
||||
t.Errorf("test message not found: %q", testMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// testUpdateRedis inserts a marshalled message into mock redis
|
||||
func testUpdateRedis(t *testing.T, c *redis.Client, message string) {
|
||||
time := time.Now().UnixMilli()
|
||||
inputMsg := models.Message{Text: message, Source: "back", Time: time}
|
||||
messageJson, err := json.Marshal(inputMsg)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal message: %s", err)
|
||||
}
|
||||
|
||||
if _, err := c.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
pipe.LPush(ctx, "messages", messageJson)
|
||||
pipe.LTrim(ctx, "messages", 0, 9)
|
||||
pipe.Incr(ctx, "total")
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("update redis: %s", err)
|
||||
}
|
||||
}
|
3
go.mod
3
go.mod
@ -4,6 +4,7 @@ go 1.17
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/alicebob/miniredis/v2 v2.16.0
|
||||
github.com/go-redis/redis/v8 v8.11.4
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
github.com/lib/pq v1.10.4
|
||||
@ -11,8 +12,10 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
|
10
go.sum
10
go.sum
@ -1,7 +1,14 @@
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||
github.com/alicebob/miniredis/v2 v2.16.0 h1:ALkyFg7bSTEd1Mkrb4ppq4fnwjklA59dVtIehXCUZkU=
|
||||
github.com/alicebob/miniredis/v2 v2.16.0/go.mod h1:gquAfGbzn92jvtrSC69+6zZnwSODVXVpYDRaGhWaL6I=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
@ -48,6 +55,8 @@ github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1Sd
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da h1:NimzV1aGyq29m5ukMK0AMWEhFaL/lrEOaephfuoiARg=
|
||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@ -64,6 +73,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestFormatDuration tests duration formatting
|
||||
func TestFormatDuration(t *testing.T) {
|
||||
now := time.Now().Unix() * 1000
|
||||
var tests = []struct {
|
||||
|
Loading…
Reference in New Issue
Block a user