forked from stoewer/go-nakadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_integration_test.go
159 lines (124 loc) · 4.65 KB
/
events_integration_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
// +build integration
package nakadi
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntegrationEventAPI_Get(t *testing.T) {
expected := &EventType{}
helperLoadTestData(t, "event-type-create.json", expected)
helperCreateEventTypes(t, expected)
defer helperDeleteEventTypes(t, expected)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
eventAPI := NewEventAPI(client, &EventOptions{Retry: true})
t.Run("fail not found", func(t *testing.T) {
_, err := eventAPI.Get("does-not-exist")
require.Error(t, err)
assert.Regexp(t, "does not exist", err)
})
t.Run("success", func(t *testing.T) {
eventType, err := eventAPI.Get(expected.Name)
require.NoError(t, err)
assert.Equal(t, expected.Name, eventType.Name)
assert.Equal(t, expected.OwningApplication, eventType.OwningApplication)
assert.Equal(t, expected.Category, eventType.Category)
assert.Equal(t, expected.EnrichmentStrategies, eventType.EnrichmentStrategies)
assert.Equal(t, expected.PartitionStrategy, eventType.PartitionStrategy)
assert.Equal(t, expected.Schema.Schema, eventType.Schema.Schema)
assert.Equal(t, expected.PartitionKeyFields, eventType.PartitionKeyFields)
})
}
func TestIntegrationEventAPI_List(t *testing.T) {
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
eventAPI := NewEventAPI(client, nil)
preconfiguredTypes, err := eventAPI.List()
require.NoError(t, err)
var expected []*EventType
helperLoadTestData(t, "event-types-create.json", &expected)
helperCreateEventTypes(t, expected...)
defer helperDeleteEventTypes(t, expected...)
allTypes, err := eventAPI.List()
require.NoError(t, err)
assert.Len(t, allTypes, len(preconfiguredTypes) + len(expected))
}
func TestIntegrationEventAPI_Create(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
eventAPI := NewEventAPI(client, nil)
t.Run("fail invalid event type", func(t *testing.T) {
err := eventAPI.Create(&EventType{Name: "foo"})
require.Error(t, err)
assert.Regexp(t, "unable to create event type", err)
})
t.Run("success", func(t *testing.T) {
err := eventAPI.Create(eventType)
require.NoError(t, err)
helperDeleteEventTypes(t, eventType)
})
}
func TestIntegrationEventAPI_Update(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
helperCreateEventTypes(t, eventType)
defer helperDeleteEventTypes(t, eventType)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
eventAPI := NewEventAPI(client, nil)
t.Run("fail not found", func(t *testing.T) {
copyEventType := *eventType
copyEventType.Name = "random"
err := eventAPI.Update(©EventType)
require.Error(t, err)
assert.Regexp(t, "does not exist", err)
})
t.Run("fail incompatible change", func(t *testing.T) {
copyEventType := *eventType
copyEventType.PartitionStrategy = "random"
err := eventAPI.Update(©EventType)
require.Error(t, err)
assert.Regexp(t, "changing partition_strategy is only allowed if the original strategy was 'random'", err)
})
t.Run("success", func(t *testing.T) {
schema := `{"properties":{"test":{"type":"string"},"test2":{"type":"string"}},"additionalProperties":true}`
eventType.Schema.Schema = schema
err := eventAPI.Update(eventType)
require.NoError(t, err)
})
}
func TestIntegrationEventAPI_Delete(t *testing.T) {
expected := &EventType{}
helperLoadTestData(t, "event-type-create.json", expected)
helperCreateEventTypes(t, expected)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
eventAPI := NewEventAPI(client, nil)
t.Run("fail not found", func(t *testing.T) {
err := eventAPI.Delete("does-not-exist")
require.Error(t, err)
assert.Regexp(t, "does not exist", err)
})
t.Run("success", func(t *testing.T) {
err := eventAPI.Delete(expected.Name)
require.NoError(t, err)
})
}
func helperCreateEventTypes(t *testing.T, eventTypes ...*EventType) {
for _, eventType := range eventTypes {
serialized, err := json.Marshal(eventType)
require.NoError(t, err)
_, err = http.DefaultClient.Post(defaultNakadiURL+"/event-types", "application/json", bytes.NewReader(serialized))
require.NoError(t, err)
}
}
func helperDeleteEventTypes(t *testing.T, eventTypes ...*EventType) {
for _, e := range eventTypes {
request, err := http.NewRequest("DELETE", defaultNakadiURL+"/event-types/"+e.Name, nil)
require.NoError(t, err)
_, err = http.DefaultClient.Do(request)
require.NoError(t, err)
}
}