Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
test(ui): add service layer tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
  • Loading branch information
rodneyosodo committed Mar 13, 2024
1 parent 65b5b55 commit 19cbb8a
Show file tree
Hide file tree
Showing 16 changed files with 4,391 additions and 90 deletions.
33 changes: 32 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ jobs:
go mod tidy
git diff --exit-code
- name: Check Mocks are up to Date
if: steps.changes.outputs.mocks == 'true'
run: |
MOCKERY_VERSION=v2.42.0
go install github.com/vektra/mockery/v2@$MOCKERY_VERSION
mv ./ui/mocks/repository.go ./ui/mocks/repository.go.tmp
make mocks
check_mock_changes() {
local file_path=$1
local tmp_file_path=$1.tmp
local entity_name=$2
if ! cmp -s "$file_path" "$tmp_file_path"; then
echo "Error: Generated mocks for $entity_name are out of sync!"
echo "Please run 'make mocks' with mockery version $MOCKERY_VERSION and commit the changes."
exit 1
fi
}
check_mock_changes ./ui/mocks/repository.go "Dashboard Repository ./ui/mocks/repository.go"
- name: Install node dependencies
run: npm install

Expand Down Expand Up @@ -62,5 +86,12 @@ jobs:
- name: Run Tests
run: |
go mod vendor
make test
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV }}
file: ./coverage.out
name: codecov-umbrella
verbose: true
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GOARCH ?= amd64
VERSION ?= $(shell git describe --abbrev=0 --tags || echo "none")
COMMIT ?= $(shell git rev-parse HEAD)
TIME ?= $(shell date +%F_%T)
MOCKERY_VERSION=v2.42.0

define compile_service
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \
Expand Down Expand Up @@ -55,8 +56,12 @@ cleandocker:
install:
cp ${BUILD_DIR}/$(SVC) $(GOBIN)/magistrala-${SVC}

mocks:
@which mockery > /dev/null || go install github.com/vektra/mockery/v2@$(MOCKERY_VERSION)
@unset MOCKERY_VERSION && go generate ./...

test:
go test -v -race -count 1 -tags test $(shell go list ./... | grep -v 'cmd')
go test -v -race -count 1 -tags test -coverprofile=coverage.out $(shell go list ./... | grep -v 'cmd')

lint:
golangci-lint run --config .golangci.yml
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/absmach/magistrala-ui
go 1.21.5

require (
github.com/0x6flab/namegenerator v1.2.0
github.com/absmach/agent v0.0.0-20240202075640-cc619e6685c8
github.com/absmach/magistrala v0.14.1-0.20240305111255-42d433a92f39
github.com/absmach/senml v1.0.5
github.com/caarlos0/env/v10 v10.0.0
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/go-chi/chi/v5 v5.0.12
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-kit/kit v0.13.0
github.com/go-zoo/bone v1.3.0
github.com/golang-jwt/jwt v3.2.2+incompatible
Expand All @@ -22,6 +22,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.0
github.com/rubenv/sql-migrate v1.6.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/oauth2 v0.18.0
Expand All @@ -35,8 +36,10 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/edgexfoundry/go-mod-core-contracts v0.1.70 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
Expand All @@ -46,14 +49,17 @@ require (
github.com/mainflux/export v0.1.1-0.20230724124847-67d0bc7f38cb // indirect
github.com/mainflux/mainflux v0.12.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiy
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/0x6flab/namegenerator v1.2.0 h1:RuHRO7NDGQpZ9ajRFP5ALcl66cyi0hqjs1fXDV+3pZE=
github.com/0x6flab/namegenerator v1.2.0/go.mod h1:h28wadnJ13Q7PxpUzAHckVj9ToyAEdx5T186Zj1kp+k=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -729,6 +731,8 @@ github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand All @@ -739,6 +743,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down
22 changes: 11 additions & 11 deletions ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func updateThingSecretEndpoint(svc ui.Service) endpoint.Endpoint {
return nil, err
}

if err := svc.UpdateThingSecret(req.token, req.ID, req.Credentials.Secret); err != nil {
if err := svc.UpdateThingSecret(req.token, req.Thing); err != nil {
return nil, err
}

Expand Down Expand Up @@ -1910,13 +1910,13 @@ func deleteInvitationEndpoint(svc ui.Service, prefix string) endpoint.Endpoint {
}

func viewDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(viewDashboardReq)
if err := req.validate(); err != nil {
return nil, err
}

res, err := svc.ViewDashboard(req.Session, req.DashboardID)
res, err := svc.ViewDashboard(ctx, req.Session, req.DashboardID)
if err != nil {
return nil, err
}
Expand All @@ -1929,7 +1929,7 @@ func viewDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
}

func createDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createDashboardReq)
if err := req.validate(); err != nil {
return nil, err
Expand All @@ -1940,7 +1940,7 @@ func createDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
Layout: req.Layout,
}

res, err := svc.CreateDashboard(req.token, dr)
res, err := svc.CreateDashboard(ctx, req.token, dr)
if err != nil {
return nil, err
}
Expand All @@ -1953,13 +1953,13 @@ func createDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
}

func listDashboardsEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listDashboardsReq)
if err := req.validate(); err != nil {
return nil, err
}

res, err := svc.ListDashboards(req.token, req.page, req.limit)
res, err := svc.ListDashboards(ctx, req.token, req.page, req.limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1992,7 +1992,7 @@ func dashboardsEndpoint(svc ui.Service) endpoint.Endpoint {
}

func updateDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateDashboardReq)
if err := req.validate(); err != nil {
return nil, err
Expand All @@ -2004,7 +2004,7 @@ func updateDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
Layout: req.Layout,
Metadata: req.Metadata,
}
if err := svc.UpdateDashboard(req.token, req.ID, d); err != nil {
if err := svc.UpdateDashboard(ctx, req.token, req.ID, d); err != nil {
return nil, err
}

Expand All @@ -2015,13 +2015,13 @@ func updateDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
}

func deleteDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(deleteDashboardReq)
if err := req.validate(); err != nil {
return nil, err
}

if err := svc.DeleteDashboard(req.token, req.ID); err != nil {
if err := svc.DeleteDashboard(ctx, req.token, req.ID); err != nil {
return nil, err
}

Expand Down
26 changes: 13 additions & 13 deletions ui/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ func (lm *loggingMiddleware) UpdateThingTags(token string, thing sdk.Thing) (err
}

// UpdateThingSecret adds logging middleware to update thing secret method.
func (lm *loggingMiddleware) UpdateThingSecret(token, id, secret string) (err error) {
func (lm *loggingMiddleware) UpdateThingSecret(token string, thing sdk.Thing) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("thing_id", id),
slog.String("thing_id", thing.ID),
}
if err != nil {
args = append(args, slog.Any("error", err))
Expand All @@ -492,7 +492,7 @@ func (lm *loggingMiddleware) UpdateThingSecret(token, id, secret string) (err er
lm.logger.Info("Update thing secret completed successfully", args...)
}(time.Now())

return lm.svc.UpdateThingSecret(token, id, secret)
return lm.svc.UpdateThingSecret(token, thing)
}

// EnableThing adds logging middleware to enable thing method.
Expand Down Expand Up @@ -1686,7 +1686,7 @@ func (lm *loggingMiddleware) DeleteInvitation(token, userID, domainID string) (e
}

// ViewDashboard adds logging middleware to view dashboard method.
func (lm *loggingMiddleware) ViewDashboard(s ui.Session, dashboardID string) (b []byte, err error) {
func (lm *loggingMiddleware) ViewDashboard(ctx context.Context, s ui.Session, dashboardID string) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand All @@ -1700,11 +1700,11 @@ func (lm *loggingMiddleware) ViewDashboard(s ui.Session, dashboardID string) (b
lm.logger.Info("View dashboard completed successfully", args...)
}(time.Now())

return lm.svc.ViewDashboard(s, dashboardID)
return lm.svc.ViewDashboard(ctx, s, dashboardID)
}

// CreateDashboard adds logging middleware to create dashboard method.
func (lm *loggingMiddleware) CreateDashboard(token string, dashboardReq ui.DashboardReq) (b []byte, err error) {
func (lm *loggingMiddleware) CreateDashboard(ctx context.Context, token string, dashboardReq ui.DashboardReq) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand All @@ -1719,11 +1719,11 @@ func (lm *loggingMiddleware) CreateDashboard(token string, dashboardReq ui.Dashb
lm.logger.Info("Create dashboard completed successfully", args...)
}(time.Now())

return lm.svc.CreateDashboard(token, dashboardReq)
return lm.svc.CreateDashboard(ctx, token, dashboardReq)
}

// ListDashboards adds logging middleware to list dashboards method.
func (lm *loggingMiddleware) ListDashboards(token string, page, limit uint64) (b []byte, err error) {
func (lm *loggingMiddleware) ListDashboards(ctx context.Context, token string, page, limit uint64) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand All @@ -1738,7 +1738,7 @@ func (lm *loggingMiddleware) ListDashboards(token string, page, limit uint64) (b
lm.logger.Info("List dashboards completed successfully", args...)
}(time.Now())

return lm.svc.ListDashboards(token, page, limit)
return lm.svc.ListDashboards(ctx, token, page, limit)
}

// Dashboards adds logging middleware to dashboards method.
Expand All @@ -1756,7 +1756,7 @@ func (lm *loggingMiddleware) Dashboards(s ui.Session) (b []byte, err error) {
}

// UpdateDashboard adds logging middleware to update dashboard method.
func (lm *loggingMiddleware) UpdateDashboard(token, dashboardID string, dashboardReq ui.DashboardReq) (err error) {
func (lm *loggingMiddleware) UpdateDashboard(ctx context.Context, token, dashboardID string, dashboardReq ui.DashboardReq) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand All @@ -1772,11 +1772,11 @@ func (lm *loggingMiddleware) UpdateDashboard(token, dashboardID string, dashboar
lm.logger.Info("Update dashboard completed successfully", args...)
}(time.Now())

return lm.svc.UpdateDashboard(token, dashboardID, dashboardReq)
return lm.svc.UpdateDashboard(ctx, token, dashboardID, dashboardReq)
}

// DeleteDashboard adds logging middleware to delete dashboard method.
func (lm *loggingMiddleware) DeleteDashboard(token, dashboardID string) (err error) {
func (lm *loggingMiddleware) DeleteDashboard(ctx context.Context, token, dashboardID string) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand All @@ -1790,5 +1790,5 @@ func (lm *loggingMiddleware) DeleteDashboard(token, dashboardID string) (err err
lm.logger.Info("Delete dashboards completed successfully", args...)
}(time.Now())

return lm.svc.DeleteDashboard(token, dashboardID)
return lm.svc.DeleteDashboard(ctx, token, dashboardID)
}
Loading

0 comments on commit 19cbb8a

Please sign in to comment.