From bc0aabeb473847129f4115314b0a6ac6841d92b9 Mon Sep 17 00:00:00 2001 From: Steve Munene <61874077+nyagamunene@users.noreply.github.com> Date: Tue, 9 Apr 2024 20:14:00 +0300 Subject: [PATCH] MG-2143 - Generate mocks with mockery for Invitations service (#2144) Signed-off-by: nyagamunene Signed-off-by: JeffMboya --- .github/workflows/check-generated-files.yml | 7 +- invitations/invitations.go | 3 + invitations/mocks/invitations.go | 80 ---------- invitations/mocks/repository.go | 159 ++++++++++++++++++++ invitations/mocks/service.go | 138 ++++++++++++----- 5 files changed, 271 insertions(+), 116 deletions(-) delete mode 100644 invitations/mocks/invitations.go create mode 100644 invitations/mocks/repository.go diff --git a/.github/workflows/check-generated-files.yml b/.github/workflows/check-generated-files.yml index 308a5a6fb4f..6c458412bab 100644 --- a/.github/workflows/check-generated-files.yml +++ b/.github/workflows/check-generated-files.yml @@ -23,7 +23,7 @@ jobs: with: go-version: 1.21.x cache-dependency-path: "go.sum" - + - name: Check for changes in go.mod run: | go mod tidy @@ -60,6 +60,7 @@ jobs: - "pkg/groups/groups.go" - "bootstrap/service.go" - "bootstrap/configs.go" + - "invitations/invitations.go" - name: Set up protoc if: steps.changes.outputs.proto == 'true' @@ -125,6 +126,8 @@ jobs: mv ./pkg/groups/mocks/service.go ./pkg/groups/mocks/service.go.tmp mv ./bootstrap/mocks/service.go ./bootstrap/mocks/service.go.tmp mv ./bootstrap/mocks/configs.go ./bootstrap/mocks/configs.go.tmp + mv ./invitations/mocks/service.go ./invitations/mocks/service.go.tmp + mv ./invitations/mocks/repository.go ./invitations/mocks/repository.go.tmp make mocks @@ -159,3 +162,5 @@ jobs: check_mock_changes ./pkg/groups/mocks/service.go "Groups Service ./pkg/groups/mocks/service.go" check_mock_changes ./bootstrap/mocks/service.go "Bootstrap Service ./bootstrap/mocks/service.go" check_mock_changes ./bootstrap/mocks/configs.go "Bootstrap Repository ./bootstrap/mocks/configs.go" + check_mock_changes ./invitations/mocks/service.go "Invitations Service ./invitations/mocks/service.go" + check_mock_changes ./invitations/mocks/repository.go "Invitations Repository ./invitations/mocks/repository.go" diff --git a/invitations/invitations.go b/invitations/invitations.go index 0cf6c5a2a7b..2d75a55fc66 100644 --- a/invitations/invitations.go +++ b/invitations/invitations.go @@ -61,6 +61,8 @@ func (page InvitationPage) MarshalJSON() ([]byte, error) { } // Service is an interface that defines methods for managing invitations. +// +//go:generate mockery --name Service --output=./mocks --filename service.go --quiet --note "Copyright (c) Abstract Machines" type Service interface { // SendInvitation sends an invitation to the given user. // Only domain administrators and platform administrators can send invitations. @@ -93,6 +95,7 @@ type Service interface { DeleteInvitation(ctx context.Context, token, userID, domainID string) (err error) } +//go:generate mockery --name Repository --output=./mocks --filename repository.go --quiet --note "Copyright (c) Abstract Machines" type Repository interface { // Create creates an invitation. Create(ctx context.Context, invitation Invitation) (err error) diff --git a/invitations/mocks/invitations.go b/invitations/mocks/invitations.go deleted file mode 100644 index 057e1b2910b..00000000000 --- a/invitations/mocks/invitations.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) Abstract Machines -// SPDX-License-Identifier: Apache-2.0 - -package mocks - -import ( - "context" - - "github.com/absmach/magistrala/invitations" - repoerr "github.com/absmach/magistrala/pkg/errors/repository" - "github.com/stretchr/testify/mock" -) - -const Invalid = "invalid" - -var _ invitations.Repository = (*Repository)(nil) - -type Repository struct { - mock.Mock -} - -func (m *Repository) Create(ctx context.Context, invitation invitations.Invitation) error { - ret := m.Called(ctx, invitation) - - if invitation.UserID == Invalid || invitation.DomainID == Invalid || invitation.InvitedBy == Invalid { - return repoerr.ErrNotFound - } - - return ret.Error(0) -} - -func (m *Repository) Retrieve(ctx context.Context, userID, domainID string) (invitations.Invitation, error) { - ret := m.Called(ctx, userID, domainID) - - if userID == Invalid || domainID == Invalid { - return invitations.Invitation{}, repoerr.ErrNotFound - } - - return ret.Get(0).(invitations.Invitation), ret.Error(1) -} - -func (m *Repository) RetrieveAll(ctx context.Context, page invitations.Page) (invitations.InvitationPage, error) { - ret := m.Called(ctx, page) - - if page.UserID == Invalid || page.DomainID == Invalid { - return invitations.InvitationPage{}, repoerr.ErrNotFound - } - - return ret.Get(0).(invitations.InvitationPage), ret.Error(1) -} - -func (m *Repository) UpdateToken(ctx context.Context, invitation invitations.Invitation) error { - ret := m.Called(ctx, invitation) - - if invitation.UserID == Invalid || invitation.DomainID == Invalid { - return repoerr.ErrNotFound - } - - return ret.Error(0) -} - -func (m *Repository) UpdateConfirmation(ctx context.Context, invitation invitations.Invitation) error { - ret := m.Called(ctx, invitation) - - if invitation.UserID == Invalid || invitation.DomainID == Invalid { - return repoerr.ErrNotFound - } - - return ret.Error(0) -} - -func (m *Repository) Delete(ctx context.Context, userID, domainID string) error { - ret := m.Called(ctx, userID, domainID) - - if userID == Invalid || domainID == Invalid { - return repoerr.ErrNotFound - } - - return ret.Error(0) -} diff --git a/invitations/mocks/repository.go b/invitations/mocks/repository.go new file mode 100644 index 00000000000..7ca02425c71 --- /dev/null +++ b/invitations/mocks/repository.go @@ -0,0 +1,159 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +// Copyright (c) Abstract Machines + +package mocks + +import ( + context "context" + + invitations "github.com/absmach/magistrala/invitations" + mock "github.com/stretchr/testify/mock" +) + +// Repository is an autogenerated mock type for the Repository type +type Repository struct { + mock.Mock +} + +// Create provides a mock function with given fields: ctx, invitation +func (_m *Repository) Create(ctx context.Context, invitation invitations.Invitation) error { + ret := _m.Called(ctx, invitation) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, invitations.Invitation) error); ok { + r0 = rf(ctx, invitation) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Delete provides a mock function with given fields: ctx, userID, domainID +func (_m *Repository) Delete(ctx context.Context, userID string, domainID string) error { + ret := _m.Called(ctx, userID, domainID) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string) error); ok { + r0 = rf(ctx, userID, domainID) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Retrieve provides a mock function with given fields: ctx, userID, domainID +func (_m *Repository) Retrieve(ctx context.Context, userID string, domainID string) (invitations.Invitation, error) { + ret := _m.Called(ctx, userID, domainID) + + if len(ret) == 0 { + panic("no return value specified for Retrieve") + } + + var r0 invitations.Invitation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string) (invitations.Invitation, error)); ok { + return rf(ctx, userID, domainID) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string) invitations.Invitation); ok { + r0 = rf(ctx, userID, domainID) + } else { + r0 = ret.Get(0).(invitations.Invitation) + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok { + r1 = rf(ctx, userID, domainID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RetrieveAll provides a mock function with given fields: ctx, page +func (_m *Repository) RetrieveAll(ctx context.Context, page invitations.Page) (invitations.InvitationPage, error) { + ret := _m.Called(ctx, page) + + if len(ret) == 0 { + panic("no return value specified for RetrieveAll") + } + + var r0 invitations.InvitationPage + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, invitations.Page) (invitations.InvitationPage, error)); ok { + return rf(ctx, page) + } + if rf, ok := ret.Get(0).(func(context.Context, invitations.Page) invitations.InvitationPage); ok { + r0 = rf(ctx, page) + } else { + r0 = ret.Get(0).(invitations.InvitationPage) + } + + if rf, ok := ret.Get(1).(func(context.Context, invitations.Page) error); ok { + r1 = rf(ctx, page) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// UpdateConfirmation provides a mock function with given fields: ctx, invitation +func (_m *Repository) UpdateConfirmation(ctx context.Context, invitation invitations.Invitation) error { + ret := _m.Called(ctx, invitation) + + if len(ret) == 0 { + panic("no return value specified for UpdateConfirmation") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, invitations.Invitation) error); ok { + r0 = rf(ctx, invitation) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UpdateToken provides a mock function with given fields: ctx, invitation +func (_m *Repository) UpdateToken(ctx context.Context, invitation invitations.Invitation) error { + ret := _m.Called(ctx, invitation) + + if len(ret) == 0 { + panic("no return value specified for UpdateToken") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, invitations.Invitation) error); ok { + r0 = rf(ctx, invitation) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewRepository creates a new instance of Repository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *Repository { + mock := &Repository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/invitations/mocks/service.go b/invitations/mocks/service.go index 6e65ace3c1a..bdce83b464d 100644 --- a/invitations/mocks/service.go +++ b/invitations/mocks/service.go @@ -1,73 +1,141 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + // Copyright (c) Abstract Machines -// SPDX-License-Identifier: Apache-2.0 package mocks import ( - "context" + context "context" - "github.com/absmach/magistrala/invitations" - repoerr "github.com/absmach/magistrala/pkg/errors/repository" - svcerr "github.com/absmach/magistrala/pkg/errors/service" - "github.com/stretchr/testify/mock" + invitations "github.com/absmach/magistrala/invitations" + mock "github.com/stretchr/testify/mock" ) -var _ invitations.Service = (*Service)(nil) - +// Service is an autogenerated mock type for the Service type type Service struct { mock.Mock } -func (svc *Service) SendInvitation(ctx context.Context, token string, invitation invitations.Invitation) (err error) { - ret := svc.Called(ctx, token, invitation) +// AcceptInvitation provides a mock function with given fields: ctx, token, domainID +func (_m *Service) AcceptInvitation(ctx context.Context, token string, domainID string) error { + ret := _m.Called(ctx, token, domainID) - if token == Invalid || invitation.UserID == Invalid || invitation.DomainID == Invalid || invitation.InvitedBy == Invalid { - return repoerr.ErrNotFound + if len(ret) == 0 { + panic("no return value specified for AcceptInvitation") } - return ret.Error(0) + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string) error); ok { + r0 = rf(ctx, token, domainID) + } else { + r0 = ret.Error(0) + } + + return r0 } -func (svc *Service) ViewInvitation(ctx context.Context, token, userID, domainID string) (invitation invitations.Invitation, err error) { - ret := svc.Called(ctx, token, userID, domainID) +// DeleteInvitation provides a mock function with given fields: ctx, token, userID, domainID +func (_m *Service) DeleteInvitation(ctx context.Context, token string, userID string, domainID string) error { + ret := _m.Called(ctx, token, userID, domainID) + + if len(ret) == 0 { + panic("no return value specified for DeleteInvitation") + } - if token == Invalid || userID == Invalid || domainID == Invalid { - return invitations.Invitation{}, repoerr.ErrNotFound + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) error); ok { + r0 = rf(ctx, token, userID, domainID) + } else { + r0 = ret.Error(0) } - return ret.Get(0).(invitations.Invitation), ret.Error(1) + return r0 } -func (svc *Service) ListInvitations(ctx context.Context, token string, page invitations.Page) (invitations.InvitationPage, error) { - ret := svc.Called(ctx, token, page) +// ListInvitations provides a mock function with given fields: ctx, token, page +func (_m *Service) ListInvitations(ctx context.Context, token string, page invitations.Page) (invitations.InvitationPage, error) { + ret := _m.Called(ctx, token, page) + + if len(ret) == 0 { + panic("no return value specified for ListInvitations") + } + + var r0 invitations.InvitationPage + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, invitations.Page) (invitations.InvitationPage, error)); ok { + return rf(ctx, token, page) + } + if rf, ok := ret.Get(0).(func(context.Context, string, invitations.Page) invitations.InvitationPage); ok { + r0 = rf(ctx, token, page) + } else { + r0 = ret.Get(0).(invitations.InvitationPage) + } - if token == Invalid { - return invitations.InvitationPage{}, svcerr.ErrAuthentication + if rf, ok := ret.Get(1).(func(context.Context, string, invitations.Page) error); ok { + r1 = rf(ctx, token, page) + } else { + r1 = ret.Error(1) } - return ret.Get(0).(invitations.InvitationPage), ret.Error(1) + return r0, r1 } -func (svc *Service) AcceptInvitation(ctx context.Context, token, domainID string) (err error) { - ret := svc.Called(ctx, token, domainID) +// SendInvitation provides a mock function with given fields: ctx, token, invitation +func (_m *Service) SendInvitation(ctx context.Context, token string, invitation invitations.Invitation) error { + ret := _m.Called(ctx, token, invitation) + + if len(ret) == 0 { + panic("no return value specified for SendInvitation") + } - if token == Invalid { - return svcerr.ErrAuthentication + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, invitations.Invitation) error); ok { + r0 = rf(ctx, token, invitation) + } else { + r0 = ret.Error(0) } - return ret.Error(0) + return r0 } -func (svc *Service) DeleteInvitation(ctx context.Context, token, userID, domainID string) (err error) { - ret := svc.Called(ctx, token, userID, domainID) +// ViewInvitation provides a mock function with given fields: ctx, token, userID, domainID +func (_m *Service) ViewInvitation(ctx context.Context, token string, userID string, domainID string) (invitations.Invitation, error) { + ret := _m.Called(ctx, token, userID, domainID) + + if len(ret) == 0 { + panic("no return value specified for ViewInvitation") + } - if token == Invalid { - return svcerr.ErrAuthentication + var r0 invitations.Invitation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (invitations.Invitation, error)); ok { + return rf(ctx, token, userID, domainID) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) invitations.Invitation); ok { + r0 = rf(ctx, token, userID, domainID) + } else { + r0 = ret.Get(0).(invitations.Invitation) } - if userID == Invalid || domainID == Invalid { - return repoerr.ErrNotFound + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, token, userID, domainID) + } else { + r1 = ret.Error(1) } - return ret.Error(0) + return r0, r1 +} + +// NewService creates a new instance of Service. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewService(t interface { + mock.TestingT + Cleanup(func()) +}) *Service { + mock := &Service{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock }