@@ -3,16 +3,20 @@ package service
3
3
import (
4
4
"testing"
5
5
6
+ "github.com/aws/aws-sdk-go/service/lambda"
7
+ "github.com/dieg0code/serverles-api-scraper/api/data/request"
6
8
"github.com/dieg0code/shared/json/response"
7
9
"github.com/dieg0code/shared/mocks"
8
10
"github.com/dieg0code/shared/models"
9
11
"github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/mock"
10
13
)
11
14
12
15
func TestPoductService_GetAll (t * testing.T ) {
13
16
t .Run ("GetAll_Success" , func (t * testing.T ) {
14
17
mockRepo := new (mocks.MockProductRepository )
15
- productService := NewProductServiceImpl (mockRepo )
18
+ mockLambdaClient := new (mocks.MockLambdaClient )
19
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
16
20
17
21
expectedProducts := []response.ProductResponse {
18
22
{
@@ -58,7 +62,8 @@ func TestPoductService_GetAll(t *testing.T) {
58
62
59
63
t .Run ("GetAll_Error" , func (t * testing.T ) {
60
64
mockRepo := new (mocks.MockProductRepository )
61
- productService := NewProductServiceImpl (mockRepo )
65
+ mockLambdaClient := new (mocks.MockLambdaClient )
66
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
62
67
63
68
mockRepo .On ("GetAll" ).Return ([]models.Product {}, assert .AnError )
64
69
@@ -73,7 +78,8 @@ func TestPoductService_GetAll(t *testing.T) {
73
78
func TestPoductService_GetByID (t * testing.T ) {
74
79
t .Run ("GetByID_Success" , func (t * testing.T ) {
75
80
mockRepo := new (mocks.MockProductRepository )
76
- productService := NewProductServiceImpl (mockRepo )
81
+ mockLambdaClient := new (mocks.MockLambdaClient )
82
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
77
83
78
84
expectedProduct := response.ProductResponse {
79
85
ProductID : "test-id" ,
@@ -101,7 +107,8 @@ func TestPoductService_GetByID(t *testing.T) {
101
107
102
108
t .Run ("GetByID_Error" , func (t * testing.T ) {
103
109
mockRepo := new (mocks.MockProductRepository )
104
- productService := NewProductServiceImpl (mockRepo )
110
+ mockLambdaClient := new (mocks.MockLambdaClient )
111
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
105
112
106
113
mockRepo .On ("GetByID" , "test-id" ).Return (models.Product {}, assert .AnError )
107
114
@@ -111,3 +118,58 @@ func TestPoductService_GetByID(t *testing.T) {
111
118
assert .Equal (t , response.ProductResponse {}, product , "Expected product to be empty" )
112
119
})
113
120
}
121
+
122
+ func TestPoductService_UpdateData (t * testing.T ) {
123
+ t .Run ("UpdateData_Success" , func (t * testing.T ) {
124
+ mockRepo := new (mocks.MockProductRepository )
125
+ mockLambdaClient := new (mocks.MockLambdaClient )
126
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
127
+
128
+ updateReq := request.UpdateDataRequest {
129
+ UpdateData : true ,
130
+ }
131
+
132
+ mockLambdaClient .On ("Invoke" , mock .Anything ).Return (& lambda.InvokeOutput {}, nil )
133
+
134
+ success , err := productService .UpdateData (updateReq )
135
+
136
+ assert .NoError (t , err , "Expected no error, UpdateData() returned an error" )
137
+ assert .True (t , success , "Expected success to be true" )
138
+
139
+ mockLambdaClient .AssertExpectations (t )
140
+ })
141
+
142
+ t .Run ("UpdateData_InvokeError" , func (t * testing.T ) {
143
+ mockRepo := new (mocks.MockProductRepository )
144
+ mockLambdaClient := new (mocks.MockLambdaClient )
145
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
146
+
147
+ updateReq := request.UpdateDataRequest {
148
+ UpdateData : true ,
149
+ }
150
+
151
+ mockLambdaClient .On ("Invoke" , mock .Anything ).Return (& lambda.InvokeOutput {}, assert .AnError )
152
+
153
+ success , err := productService .UpdateData (updateReq )
154
+
155
+ assert .Error (t , err , "Expected error invoking lambda function" )
156
+ assert .False (t , success , "Expected success to be false" )
157
+
158
+ mockLambdaClient .AssertExpectations (t )
159
+ })
160
+
161
+ t .Run ("UpdateData_NoUpdate" , func (t * testing.T ) {
162
+ mockRepo := new (mocks.MockProductRepository )
163
+ mockLambdaClient := new (mocks.MockLambdaClient )
164
+ productService := NewProductServiceImpl (mockRepo , mockLambdaClient )
165
+
166
+ updateReq := request.UpdateDataRequest {
167
+ UpdateData : false ,
168
+ }
169
+
170
+ success , err := productService .UpdateData (updateReq )
171
+
172
+ assert .NoError (t , err , "Expected no error, UpdateData() returned an error" )
173
+ assert .False (t , success , "Expected success to be false" )
174
+ })
175
+ }
0 commit comments