Skip to content

Commit

Permalink
feat: add go releaser and unitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjanrak committed Jun 24, 2022
1 parent 6e90387 commit 76c8dd3
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Run Tests

on: [push, pull_request]
jobs:
test:
strategy:
matrix:
go-version: [1.16.x, 1.17.x, 1.18.x]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: go test -v ./...
9 changes: 9 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
- CGO_ENABLED=0
builds:
- skip: true
archives:
- format: tar.gz
files:
- README.md
- LICENSE
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
DeadHTTP: []int{400, 403, 429, 500, 502},
})
// Request message
queueMsg := deadletterqueue.InputMsg{
queueMsg := deadletterqueue.InputMsg{
Name: "Place TCS Order",
Url: "https://api.kite.trade/orders/regular",
ReqMethod: "POST",
Expand Down
110 changes: 110 additions & 0 deletions dead_letter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package deadletterqueue

import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/go-redis/redis/v8"
"github.com/go-redis/redismock/v8"
"github.com/stretchr/testify/assert"
)

var (
db *redis.Client
mock redismock.ClientMock
cli Client
reqMsgOrd InputMsg
reqMsgSess InputMsg
)

// MockRedis sets redis dbclient and mockclient
func MockRedis() {
db, mock = redismock.NewClientMock()
cli = Client{
redisCli: db,
queueName: "ReqQueue",
ctx: context.TODO(),
deadHTTP: []int{400, 429, 502},
}
}

func TestAddMessage(t *testing.T) {
// Initialize the mock redis
MockRedis()
// Request message
reqMsgOrd = InputMsg{
Name: "Place TCS Order",
Url: "https://api.kite.trade/orders/regular",
ReqMethod: "POST",
PostParam: map[string]interface{}{
"exchange": "NSE",
"tradingsymbol": "TCS",
"transaction_type": "BUY",
"quantity": 1,
"product": "CNC",
"order_type": "MARKET",
"validity": "DAY",
},
Headers: map[string]interface{}{
"x-kite-version": 3,
"authorization": "token abcd123:efgh1234",
"content-type": "application/x-www-form-urlencoded",
},
}
// mock to set reqMsg for AddMessage call
mock.ExpectSet("ReqQueue", structToJson([]InputMsg{reqMsgOrd}), 0).SetVal("OK")

err := cli.AddMessage(reqMsgOrd)
assert.Nil(t, err)
}

func TestDeleteReqMsg(t *testing.T) {
// Request message
reqMsgSess = InputMsg{
Name: "Post session token",
Url: "https://api.kite.trade/session/token",
ReqMethod: "POST",
PostParam: map[string]interface{}{
"api_key": "api_key",
"request_token": "request_token",
"checksum": "checksum",
},
Headers: map[string]interface{}{
"x-kite-version": 3,
},
}
// Add mock to add both reqMsg and reqMsgSess in ReqQueue
mock.ExpectGet("ReqQueue").SetVal(string(structToJson([]InputMsg{reqMsgOrd, reqMsgSess})))
// Inspect only reqMsgSess message is left post removal of "Place TCS Order" message
// from ReqQueue
mock.ExpectSet("ReqQueue", structToJson([]InputMsg{reqMsgSess}), 0).SetVal("OK")

err := cli.DeleteReqMsg("Place TCS Order")
assert.Nil(t, err)
}

func TestDeleteDeadMsg(t *testing.T) {
// Add Get and Set mock for all dead http key
mock.ExpectGet("400").SetVal(string(structToJson([]InputMsg{reqMsgOrd, reqMsgSess})))
mock.ExpectSet("400", structToJson([]InputMsg{reqMsgSess}), 0).SetVal("OK")

mock.ExpectGet("429").SetVal(string(structToJson([]InputMsg{reqMsgOrd, reqMsgSess})))
mock.ExpectSet("429", structToJson([]InputMsg{reqMsgSess}), 0).SetVal("OK")

mock.ExpectGet("502").SetVal(string(structToJson([]InputMsg{reqMsgOrd, reqMsgSess})))
mock.ExpectSet("502", structToJson([]InputMsg{reqMsgSess}), 0).SetVal("OK")

err := cli.DeleteDeadMsg("Place TCS Order")
assert.Nil(t, err)
}

// structToString parses struct to json for redis mock
func structToJson(msg []InputMsg) []byte {
jsonMessage, err := json.Marshal(msg)
if err != nil {
fmt.Printf("%v", err)
}
return jsonMessage
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.16
require (
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redismock/v8 v8.0.6
github.com/stretchr/testify v1.7.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
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=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
Expand Down Expand Up @@ -42,9 +43,11 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg=
Expand Down Expand Up @@ -107,4 +110,5 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 76c8dd3

Please sign in to comment.