forked from TykTechnologies/tyk-pump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
119 lines (92 loc) · 2.71 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
package main
import (
"context"
"fmt"
"testing"
"time"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk-pump/pumps"
)
type MockedPump struct {
CounterRequest int
pumps.CommonPumpConfig
}
func (p *MockedPump) GetName() string {
return "Mocked Pump"
}
func (p *MockedPump) New() pumps.Pump {
return &MockedPump{}
}
func (p *MockedPump) Init(config interface{}) error {
return nil
}
func (p *MockedPump) WriteData(ctx context.Context, keys []interface{}) error {
for range keys {
p.CounterRequest++
}
return nil
}
func TestFilterData(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api123"},
},
)
keys := make([]interface{}, 3)
keys[0] = analytics.AnalyticsRecord{APIID: "api111"}
keys[1] = analytics.AnalyticsRecord{APIID: "api123"}
keys[2] = analytics.AnalyticsRecord{APIID: "api321"}
filteredKeys := filterData(mockedPump, keys)
if len(keys) == len(filteredKeys) {
t.Fatal("keys and filtered keys have the same lenght")
}
}
func TestOmitDetailsFilterData(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetOmitDetailedRecording(true)
keys := make([]interface{}, 1)
keys[0] = analytics.AnalyticsRecord{APIID: "api111", RawResponse: "test", RawRequest: "test"}
filteredKeys := filterData(mockedPump, keys)
if len(filteredKeys) == 0 {
t.Fatal("it shouldn't have filtered a key.")
}
record := filteredKeys[0].(analytics.AnalyticsRecord)
if record.RawRequest != "" || record.RawResponse != "" {
t.Fatal("raw_request and raw_response should be empty")
}
}
func TestWriteData(t *testing.T) {
mockedPump := &MockedPump{}
Pumps = []pumps.Pump{mockedPump}
keys := make([]interface{}, 3)
keys[0] = analytics.AnalyticsRecord{APIID: "api111"}
keys[1] = analytics.AnalyticsRecord{APIID: "api123"}
keys[2] = analytics.AnalyticsRecord{APIID: "api321"}
job := instrument.NewJob("TestJob")
writeToPumps(keys, job, time.Now(), 2)
mockedPump = Pumps[0].(*MockedPump)
if mockedPump.CounterRequest != 3 {
t.Fatal("MockedPump should have 3 requests")
}
}
func TestWriteDataWithFilters(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api123"},
},
)
Pumps = []pumps.Pump{mockedPump}
keys := make([]interface{}, 3)
keys[0] = analytics.AnalyticsRecord{APIID: "api111"}
keys[1] = analytics.AnalyticsRecord{APIID: "api123"}
keys[2] = analytics.AnalyticsRecord{APIID: "api321"}
job := instrument.NewJob("TestJob")
writeToPumps(keys, job, time.Now(), 2)
mockedPump = Pumps[0].(*MockedPump)
if mockedPump.CounterRequest != 1 {
fmt.Println(mockedPump.CounterRequest)
t.Fatal("MockedPump with filter should have 3 requests")
}
}