-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
257 lines (211 loc) · 7.34 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//go:build integration
// +build integration
package main_test
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"testing"
"time"
"github.com/MarkLai0317/Advertising/ad/repository"
"github.com/MarkLai0317/Advertising/test_data/integration_test_data/main_test_cases"
"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MainIntegrationTestSuite struct {
testMongoClient *mongo.Client
suite.Suite
}
func TestMainIntegrationTestSuite(t *testing.T) {
suite.Run(t, &MainIntegrationTestSuite{})
}
const mongoUri = "mongodb://mark:markpwd@localhost:27017"
// set up database for test
func (its *MainIntegrationTestSuite) SetupSuite() {
mongoClientOptions := options.Client().ApplyURI(mongoUri)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var mongoClient *mongo.Client
var err error
maxRetries := 3
for attempt := 1; attempt <= maxRetries; attempt++ {
// Attempt to connect to MongoDB
mongoClient, err = mongo.Connect(ctx, mongoClientOptions)
if err != nil {
log.Printf("Failed to connect to MongoDB (attempt %d/%d): %s\n", attempt, maxRetries, err)
time.Sleep(2 * time.Second)
continue
}
// Ping the MongoDB server to verify that the client is connected
err = mongoClient.Ping(ctx, nil)
if err != nil {
log.Printf("Failed to ping MongoDB (attempt %d/%d): %s\n", attempt, maxRetries, err)
if err := mongoClient.Disconnect(ctx); err != nil {
log.Printf("Failed to disconnect client: %s\n", err)
}
time.Sleep(2 * time.Second)
continue
}
// Connection successful, break out of the retry loop
break
}
if err != nil {
log.Fatalf("Max retries reached, unable to establish connection to MongoDB: %s\n", err)
}
its.testMongoClient = mongoClient
}
// disconnect mongo after test suite complete
func (its *MainIntegrationTestSuite) TearDownSuite() {
err := its.testMongoClient.Disconnect(context.TODO())
if err != nil {
log.Fatalf("Max retries reached, unable to establish connection to MongoDB: %s\n", err)
}
}
// clear database before and after every test
func (its *MainIntegrationTestSuite) SetupTest() {
ctx := context.TODO()
collection := its.testMongoClient.Database("advertising").Collection("advertisement")
filter := bson.M{}
_, err := collection.DeleteMany(ctx, filter)
if err != nil {
log.Fatal(err)
}
}
func (its *MainIntegrationTestSuite) TearDownTest() {
ctx := context.TODO()
collection := its.testMongoClient.Database("advertising").Collection("advertisement")
filter := bson.M{}
_, err := collection.DeleteMany(ctx, filter)
if err != nil {
log.Fatal(err)
}
}
type AdRequest struct {
Title string `json:"title"`
StartAt string `json:"startAt"`
EndAt string `json:"endAt"`
Conditions struct {
AgeStart int `json:"ageStart"`
AgeEnd int `json:"ageEnd"`
Genders []string `json:"gender"`
Countries []string `json:"country"`
Platforms []string `json:"platform"`
} `json:"conditions"`
}
// test create advertisement api
func (its *MainIntegrationTestSuite) TestCreateAdvertisement() {
// stub time.Now
patches := gomonkey.NewPatches()
defer patches.Reset()
patches.ApplyFunc(time.Now, func() time.Time {
fakeTime := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
return fakeTime
})
//create input body for post request
inputAdvertisements := AdRequest{
Title: "AD 1",
StartAt: "3000-12-10T03:00:00.000Z",
EndAt: "3001-12-31T16:00:00.000Z",
}
inputAdvertisements.Conditions.AgeStart = 1
inputAdvertisements.Conditions.AgeEnd = 100
inputAdvertisements.Conditions.Genders = []string{"M"}
inputAdvertisements.Conditions.Countries = []string{"TW", "JP"}
inputAdvertisements.Conditions.Platforms = []string{"ios", "android"}
// Marshal the struct to JSON
jsonData, err := json.Marshal(inputAdvertisements)
if err != nil {
panic(err)
}
// Create a new request using http to test server
req, err := http.NewRequest("POST", "http://localhost:80/api/v1/ad", bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
// Set the content type to application/json
req.Header.Set("Content-Type", "application/json")
// Send the request using a client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("error %s: check test cases", err.Error())
}
// statusCode should be 200
its.Equal(http.StatusOK, resp.StatusCode)
results := fetchDbData(its)
// connect to DB to see if insert success
// if the documents insert are successfully inserted by checking the value of each field
its.Equal(inputAdvertisements.Title, results[0].Title)
its.Equal(inputAdvertisements.StartAt, results[0].StartAt.Format("2006-01-02T15:04:05.000Z"))
its.Equal(inputAdvertisements.EndAt, results[0].EndAt.Format("2006-01-02T15:04:05.000Z"))
its.Equal(inputAdvertisements.Conditions.AgeStart, results[0].Conditions.AgeStart)
its.Equal(inputAdvertisements.Conditions.AgeEnd, results[0].Conditions.AgeEnd)
its.Equal(inputAdvertisements.Conditions.Genders, convertEnumSliceToStringSlice(results[0].Conditions.Genders))
its.Equal(inputAdvertisements.Conditions.Countries, convertEnumSliceToStringSlice(results[0].Conditions.Countries))
its.Equal(inputAdvertisements.Conditions.Platforms, convertEnumSliceToStringSlice(results[0].Conditions.Platforms))
}
// utility function to fetch data from database after calling api
func fetchDbData(its *MainIntegrationTestSuite) []repository.AdvertisementMongo {
ctx := context.TODO()
collection := its.testMongoClient.Database("advertising").Collection("advertisement")
filter := bson.D{{}}
opts := options.Find()
// run query on database
cursor, err := collection.Find(ctx, filter, opts)
if err != nil {
log.Fatalf("error processing cursor: %s", err.Error())
}
defer cursor.Close(ctx)
// Iterate through the cursor
var results []repository.AdvertisementMongo
err = cursor.All(ctx, &results)
if err != nil {
log.Fatalf("error processing cursor: %s", err.Error())
}
return results
}
func convertEnumSliceToStringSlice[T ~string](enumSlice []T) []string {
stringSlice := make([]string, len(enumSlice))
for i, enumValue := range enumSlice {
stringSlice[i] = string(enumValue)
}
return stringSlice
}
// test get advertisement api
func (its *MainIntegrationTestSuite) TestGetAdvertisement() {
testCases := main_test_cases.GetAdvertisementsTestCases()
for name, tc := range testCases {
its.Run(name, func() {
its.SetupTest()
// prepare testData in DB
collection := its.testMongoClient.Database("advertising").Collection("advertisement")
_, err := collection.InsertMany(context.TODO(), tc.TestData)
if err != nil {
log.Printf("insert document error %s", err.Error())
}
its.Equal(nil, err, "prepare data error. pleas check test: TestGetAadvertisements ")
url := tc.InputUrl
resp, err := http.Get(url)
if err != nil {
log.Printf("error %s: check test cases", err.Error())
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
its.Equal(nil, err)
var objBody, objExpect interface{}
// Unmarshal the JSON bytes and string into a Go data structure
err = json.Unmarshal(bodyBytes, &objBody)
its.Equal(nil, err)
err = json.Unmarshal([]byte(tc.Expects.ReturnData), &objExpect)
its.Equal(nil, err)
its.Equal(objExpect, objBody)
its.TearDownTest()
})
}
}