-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstarter_test.go
507 lines (427 loc) · 14.2 KB
/
starter_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2/dbtest"
)
var router *mux.Router
var mockDBServer dbtest.DBServer
var tweets []Tweet
var invalidTweet Tweet
var existingAccessKey AccessKey
var notExistingAccessKey AccessKey
var invalidArrayPayload []byte
var invalidObjectPayload []byte
func TestMain(m *testing.M) {
fmt.Println("--- Start Tests")
setup()
// run the test cases defined in this file
retCode := m.Run()
tearDown()
// call with result of m.Run()
os.Exit(retCode)
}
func setup() {
fmt.Println("--- --- setup")
setupRouter()
setupDB()
fillDB()
}
func setupRouter() {
router = makeRouter()
}
func setupDB() {
tempDir, _ := ioutil.TempDir("", "testing")
mockDBServer.SetPath(tempDir)
mongoClient = mockDBServer.Session()
MongoCreateCollectionIndexes(mongoClient)
}
func fillDB() {
/*
* Insert fake tweets
*/
fmt.Println("Insert fake tweets")
tweets = append(tweets, Tweet{
CreatedAt: 20180121,
CreatedAtFull: "Mon Jan 21 12:28:28 +0000 2019",
FavoriteCount: 0,
RetweetCount: 0,
Text: "@Tre_It complimenti per Luca, un vostro collaboratore che lavora presso MediaWord di Cinisello Balsamo. Una persona attenta, precisa e sempre disponibile nei confronti dei clienti. #bellapersona",
StatusID: "1",
UserName: "nytwitt",
InReplyToScreenName: "Tre_It",
Hashtags: []string{"bellapersona"},
Lang: "it",
Sentiment: "NEUTRAL",
SentimentScore: 0,
TweetClass: "irrelevant",
ClassifierCertainty: 0,
})
tweets = append(tweets, Tweet{
CreatedAt: 20180121,
CreatedAtFull: "Mon Jan 21 12:28:28 +0000 2019",
FavoriteCount: 0,
RetweetCount: 0,
Text: "@WindItalia complimenti per Luca, un vostro collaboratore che lavora presso MediaWord di Cinisello Balsamo. Una persona attenta, precisa e sempre disponibile nei confronti dei clienti. #bellapersona",
StatusID: "2",
UserName: "katast",
InReplyToScreenName: "WindItalia",
Hashtags: []string{"bellapersona"},
Lang: "it",
Sentiment: "NEUTRAL",
SentimentScore: 0,
TweetClass: "problem_report",
ClassifierCertainty: 0,
})
tweets = append(tweets, Tweet{
CreatedAt: 20180121,
CreatedAtFull: "Mon Jan 21 12:28:28 +0000 2019",
FavoriteCount: 0,
RetweetCount: 0,
Text: "@Tre_It complimenti per Luca, un vostro collaboratore che lavora presso MediaWord di Cinisello Balsamo. Una persona attenta, precisa e sempre disponibile nei confronti dei clienti. #bellapersona",
StatusID: "3",
UserName: "creat",
InReplyToScreenName: "Tre_It",
Hashtags: []string{"bellapersona"},
Lang: "it",
Sentiment: "NEUTRAL",
SentimentScore: 0,
TweetClass: "",
ClassifierCertainty: 0,
})
dateOfCurrentWeek, _ := strconv.Atoi(time.Now().AddDate(0, 0, -5).Format("20060102"))
tweets = append(tweets, Tweet{
CreatedAt: dateOfCurrentWeek,
CreatedAtFull: "Mon Jan 21 12:28:28 +0000 2019",
FavoriteCount: 0,
RetweetCount: 0,
Text: "@Tre_It complimenti per Luca, un vostro collaboratore che lavora presso MediaWord di Cinisello Balsamo. Una persona attenta, precisa e sempre disponibile nei confronti dei clienti. #bellapersona",
StatusID: "4",
UserName: "charl",
InReplyToScreenName: "Tre_It",
Hashtags: []string{"bellapersona"},
Lang: "it",
Sentiment: "NEUTRAL",
SentimentScore: 0,
TweetClass: "inquiry",
ClassifierCertainty: 0,
})
tweetBulk := mongoClient.DB(database).C(collectionTweet).Bulk()
for _, tweet := range tweets {
tweetBulk.Insert(tweet)
}
_, err := tweetBulk.Run()
if err != nil {
panic(err)
}
invalidTweet = Tweet{}
invalidArrayPayload = []byte(`[{ "wrong_json_format": true }]`)
invalidObjectPayload = []byte(`{ "wrong_json_format": true }`)
notExistingAccessKey = AccessKey{
Key: "notindb",
Configuration: AccessKeyConfiguration{
TwitterAccounts: []string{"Tre_It"},
GooglePlayStoreAccounts: []string{},
Topics: []string{"Network"},
},
}
existingAccessKey = AccessKey{
Key: "indb",
Configuration: AccessKeyConfiguration{
TwitterAccounts: []string{"Tre_It"},
GooglePlayStoreAccounts: []string{"Wind Tre S.p.A."},
Topics: []string{"Contract", "Devices"},
},
}
err = mongoClient.DB(database).C(collectionAccessKeys).Insert(existingAccessKey)
if err != nil {
panic(err)
}
/*
* Insert fake observables
*/
err = mongoClient.DB(database).C(collectionObservableTwitter).Insert(ObservableTwitter{
AccountName: "TestObserverable",
Interval: "2h",
Lang: "en",
})
if err != nil {
panic(err)
}
/*
* Insert fake labels
*/
err = mongoClient.DB(database).C(collectionTweetLabel).Insert(TweetLabel{
Date: 20180118,
Label: "problem_report",
PreviousLabel: "problem_report",
StatusID: "4",
})
if err != nil {
panic(err)
}
}
func tearDown() {
fmt.Println("--- --- tear down")
mongoClient.Close()
mockDBServer.Stop() // Stop shuts down the temporary server and removes data on disk.
}
type endpoint struct {
method string
url string
}
func (e endpoint) withVars(vs ...interface{}) endpoint {
e.url = fmt.Sprintf(e.url, vs...)
return e
}
func (e endpoint) executeRequest(payload interface{}) (error, *httptest.ResponseRecorder) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(payload)
if err != nil {
return err, nil
}
req, err := http.NewRequest(e.method, e.url, body)
if err != nil {
return err, nil
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
return nil, rr
}
func (e endpoint) mustExecuteRequest(payload interface{}) *httptest.ResponseRecorder {
err, rr := e.executeRequest(payload)
if err != nil {
panic(errors.Wrap(err, `Could not execute request`))
}
return rr
}
func isSuccess(code int) bool {
return code >= 200 && code < 300
}
func assertSuccess(t *testing.T, rr *httptest.ResponseRecorder) {
if !isSuccess(rr.Code) {
t.Errorf("Status code differs. Expected success.\n Got status %d (%s) instead", rr.Code, http.StatusText(rr.Code))
}
}
func assertFailure(t *testing.T, rr *httptest.ResponseRecorder) {
if isSuccess(rr.Code) {
t.Errorf("Status code differs. Expected failure.\n Got status %d (%s) instead", rr.Code, http.StatusText(rr.Code))
}
}
func assertJsonDecodes(t *testing.T, rr *httptest.ResponseRecorder, v interface{}) {
err := json.Unmarshal(rr.Body.Bytes(), v)
if err != nil {
t.Error(errors.Wrap(err, "Expected valid json array"))
}
}
func TestPostTweet(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/store/tweet/"}
assertFailure(t, ep.mustExecuteRequest(invalidArrayPayload))
assertFailure(t, ep.mustExecuteRequest(invalidTweet))
assertSuccess(t, ep.mustExecuteRequest(tweets))
}
func TestPostClassifiedTweet(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/store/classified/tweet/"}
assertFailure(t, ep.mustExecuteRequest(invalidArrayPayload))
assertFailure(t, ep.mustExecuteRequest(invalidTweet))
tweet := tweets[0]
tweet.TweetClass = "problem_report"
tweet.ClassifierCertainty = 80 // TODO
tweet.Sentiment = "NEGATIVE"
tweet.SentimentScore = -2
assertSuccess(t, ep.mustExecuteRequest([]Tweet{tweet}))
}
func TestPostObservableTwitter(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/store/observable/"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
assertFailure(t, ep.mustExecuteRequest(ObservableTwitter{
AccountName: "Test",
Interval: "2h",
}))
// Test for success
correctObservable := ObservableTwitter{
AccountName: "Test",
Interval: "2h",
Lang: "en",
}
assertSuccess(t, ep.mustExecuteRequest(correctObservable))
MongoDeleteObservableTwitter(mongoClient, correctObservable)
}
func TestPostLabelTwitter(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/label/tweet/"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
assertFailure(t, ep.mustExecuteRequest(TweetLabel{}))
// Test for success
tweetLabel := TweetLabel{
Date: 20190131,
Label: "problem_report",
PreviousLabel: "problem_report",
StatusID: "1234",
}
assertSuccess(t, ep.mustExecuteRequest(tweetLabel))
err := mongoClient.DB(database).C(collectionTweetLabel).Remove(tweetLabel)
assert.NoError(t, err, "Could not remove tweet label fro db")
}
func TestPostTweetTopics(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/store/topics"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
tweet := tweets[0]
tweet.Topics = TweetTopics{
FirstClass: TweetClass{
Label: "Network",
Score: 0.3,
},
SecondClass: TweetClass{
Label: "Devices",
Score: 0.6,
},
}
assertSuccess(t, ep.mustExecuteRequest(tweet))
}
func TestPostCheckAccessKey(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/access_key"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
response := ep.mustExecuteRequest(existingAccessKey)
var message ResponseMessage
assertJsonDecodes(t, response, &message)
assert.True(t, message.Status)
response = ep.mustExecuteRequest(notExistingAccessKey)
assertJsonDecodes(t, response, &message)
assert.False(t, message.Status)
}
func TestPostUpdateAccessKeyConfiguration(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/access_key/update"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
key := existingAccessKey
key.Configuration.Topics = []string{"Contract", "Network"}
assertSuccess(t, ep.mustExecuteRequest(key))
}
func TestGetTweetOfClass(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/%s/class/%s"}
// Test for failure
response := ep.withVars("should", "fail").mustExecuteRequest(nil)
var content []Tweet
assertJsonDecodes(t, response, &content)
assert.Empty(t, content)
// Test for success
response = ep.withVars("WindItalia", "problem_report").mustExecuteRequest(nil)
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 1)
}
func TestGetAllTweetsOfAccount(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/%s/all"}
// Test for failure
response := ep.withVars("shouldfail").mustExecuteRequest(nil)
var content []Tweet
assertJsonDecodes(t, response, &content)
assert.Empty(t, content)
// Test for success
response = ep.withVars("Tre_It").mustExecuteRequest(nil)
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 3)
}
func TestGetAllUnlabeledTweetsOfAccount(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/%s/all/unlabeled"}
// Test for failure
response := ep.withVars("shouldfail").mustExecuteRequest(nil)
assertFailure(t, response)
// Test for success
response = ep.withVars("Tre_It").mustExecuteRequest(nil)
var content []Tweet
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 2)
}
func TestGetAllTweetsOfAccountForCurrentWeek(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/%s/currentweek"}
// Test for failure
response := ep.withVars("shouldfail").mustExecuteRequest(nil)
var content []Tweet
assertJsonDecodes(t, response, &content)
assert.Empty(t, content)
// Test for success
response = ep.withVars("Tre_It").mustExecuteRequest(nil)
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 1)
}
func TestGetAllUnclassifiedTweetsOfAccount(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/%s/lang/%s/unclassified"}
// Test for success
response := ep.withVars("Tre_It", "it").mustExecuteRequest(nil)
var tweets []Tweet
assertJsonDecodes(t, response, &tweets)
assert.Len(t, tweets, 1)
}
func TestGetAllTwitterAccountNames(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/account_name/all"}
// Test for success
response := ep.mustExecuteRequest(nil)
var content TwitterAccount
assertJsonDecodes(t, response, &content)
assert.Len(t, content.Names, 2)
}
func TestGetAllLabeledTweets(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/labeledtweets/all"}
// Test for success
response := ep.mustExecuteRequest(nil)
var content []TweetLabel
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 1)
}
func TestGetObservablesTwitter(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/twitter/observables"}
// Test for success
response := ep.mustExecuteRequest(nil)
var content []TweetLabel
assertJsonDecodes(t, response, &content)
assert.Len(t, content, 1)
}
func TestPostAccessKeyConfiguration(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/twitter/access_key/configuration"}
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
notExistingKeyBody := map[string]string{"access_key": notExistingAccessKey.Key}
assertFailure(t, ep.mustExecuteRequest(notExistingKeyBody))
existingKeyBody := map[string]string{"access_key": existingAccessKey.Key}
assertSuccess(t, ep.mustExecuteRequest(existingKeyBody))
response := ep.mustExecuteRequest(existingKeyBody)
assertSuccess(t, response)
var config AccessKeyConfiguration
assertJsonDecodes(t, response, &config)
}
func TestDeleteObservableTwitter(t *testing.T) {
ep := endpoint{"DELETE", "/hitec/repository/twitter/observables"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
ep.mustExecuteRequest(ObservableTwitter{
AccountName: "Test",
Interval: "2h",
})
observables := MongoGetAllObservableTwitter(mongoClient)
assert.Len(t, observables, 1)
// Test for success
assertSuccess(t, ep.mustExecuteRequest(ObservableTwitter{
AccountName: "TestObserverable",
Interval: "2h",
Lang: "en",
}))
observables = MongoGetAllObservableTwitter(mongoClient)
assert.Empty(t, observables)
}