Skip to content

Commit

Permalink
added tests and added actions/setup-go@v4
Browse files Browse the repository at this point in the history
  • Loading branch information
zephinzer committed Aug 9, 2023
1 parent ecf7326 commit 6f3aeb2
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 26 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ name: app
run-name: ${{ github.actor }} is running pipeline
on: [push]
jobs:
hello-world:
installation:
runs-on: ubuntu-latest
steps:
- run: |
echo "hello world"
- name: Install system dependencies
uses: actions/setup-go@v4
with:
go-version: '>=1.20.0'
- name: Check out repository code
uses: actions/checkout@v3
- name: Install code dependencies
run: make deps
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ start-redis:
@docker-compose run redis

test:
@go test -v -coverpkg=./... -coverprofile=./tests/cover.out ./... -not -path './vendor/*'
@go test -v -coverpkg=./... -coverprofile=./tests/cover.out ./...
@go tool cover -func ./tests/cover.out
@go tool cover -html ./tests/cover.out -o ./tests/cover.html
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/segmentio/kafka-go v0.4.42
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
github.com/swaggo/fiber-swagger v1.3.0
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand All @@ -75,12 +75,12 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
26 changes: 13 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -376,7 +376,7 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -414,8 +414,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -437,8 +437,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/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-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -485,8 +485,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -500,8 +500,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -556,8 +556,8 @@ golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
23 changes: 23 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package api

import (
"testing"

"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/suite"
)

type ApiTests struct {
suite.Suite
}

func TestApi(t *testing.T) {
suite.Run(t, &ApiTests{})
}

func (t ApiTests) Test_WithFiber() {
app := fiber.New()
t.Empty(app.GetRoutes())
WithFiber(app)
t.NotEmpty(app.GetRoutes())
}
7 changes: 5 additions & 2 deletions internal/api/types/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type HttpResponse struct {
// successful
Error error `json:"error,omitempty"`

// Message is an optional message
// Message is an optional message that will be overridden
// with the error message if an error occurred
Message *string `json:"message,omitempty"`

// StatusCode is the HTTP status code
Expand All @@ -27,7 +28,9 @@ func (hr HttpResponse) WithFiber(c *fiber.Ctx) error {
statusCode := http.StatusOK
if hr.Error != nil {
statusCode = http.StatusInternalServerError
logrus.Warnf("failed to execute controller: %s", hr.Error)
logrus.Warnf("http[%v] returned: %s", statusCode, hr.Error)
errorMessage := hr.Error.Error()
hr.Message = &errorMessage
hr.Error = nil
}
if hr.StatusCode > 0 {
Expand Down
55 changes: 55 additions & 0 deletions internal/api/types/http_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types

import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/suite"
"github.com/valyala/fasthttp"
)

type HttpResponseTests struct {
suite.Suite
}

func TestApi(t *testing.T) {
suite.Run(t, &HttpResponseTests{})
}

func (t HttpResponseTests) Test_WithFiber() {
app := fiber.New()
bodyData := "hello world"
errorMessage := "error"
customMessage := "this will be overridden by the error"
statusCode := http.StatusTeapot

c := app.AcquireCtx(&fasthttp.RequestCtx{})
r := HttpResponse{
Data: bodyData,
Error: errors.New(errorMessage),
Message: &customMessage,
StatusCode: statusCode,
}
r.WithFiber(c)
t.EqualValues(statusCode, c.Response().StatusCode())
t.EqualValues(
fmt.Sprintf(`{"data":"%s","message":"%s"}`, bodyData, errorMessage),
string(c.Response().Body()),
"message field should be overridden if there are errors",
)
r = HttpResponse{
Data: bodyData,
Message: &customMessage,
StatusCode: statusCode,
}
r.WithFiber(c)
t.EqualValues(statusCode, c.Response().StatusCode())
t.EqualValues(
fmt.Sprintf(`{"data":"%s","message":"%s"}`, bodyData, customMessage),
string(c.Response().Body()),
"message field should not be overridden if there are no errors",
)
}
23 changes: 23 additions & 0 deletions internal/database/kafka/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kafka

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
23 changes: 23 additions & 0 deletions internal/database/mongo/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mongo

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
23 changes: 23 additions & 0 deletions internal/database/mysql/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mysql

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
23 changes: 23 additions & 0 deletions internal/database/nats/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nats

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
23 changes: 23 additions & 0 deletions internal/database/postgres/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package postgres

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
23 changes: 23 additions & 0 deletions internal/database/redis/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package redis

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
)

type ConfigTests struct {
suite.Suite
}

func TestConfig(t *testing.T) {
suite.Run(t, &ConfigTests{})
}

func (t ConfigTests) Test_WithCobra() {
c := cobra.Command{}
t.False(c.HasFlags())
WithCobra(&c)
t.True(c.HasFlags())
}
Loading

0 comments on commit 6f3aeb2

Please sign in to comment.