-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation.commits_test.go
177 lines (120 loc) · 3.34 KB
/
transformation.commits_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
package github
import (
"bytes"
"context"
"crypto/sha256"
"encoding/csv"
"fmt"
"io"
"os"
"testing"
"github.com/whosonfirst/go-webhookd/v3"
"github.com/whosonfirst/go-webhookd/v3/transformation"
)
func TestGitHubCommitsTransformation(t *testing.T) {
expected_hash := "696a396febbe79310b6f54e576c753a28e97ccbbfa1614e72de050d041cc81c5"
expected_rows := 1607
msg := "fixtures/events/flights.json"
fh, err := os.Open(msg)
if err != nil {
t.Fatalf("Failed to open %s, %v", msg, err)
}
defer fh.Close()
body, err := io.ReadAll(fh)
if err != nil {
t.Fatalf("Failed to read %s, %v", msg, err)
}
ctx := context.Background()
tr, err := transformation.NewTransformation(ctx, "githubcommits://")
if err != nil {
t.Fatalf("Failed to create new transformation, %v", err)
}
data, err2 := tr.Transform(ctx, body)
if err2 != nil {
t.Fatalf("Failed to transform message, %v", err2)
}
sum := sha256.Sum256([]byte(data))
hash := fmt.Sprintf("%x", sum)
if hash != expected_hash {
t.Fatalf("Unexpected hash of commit data: %s", hash)
}
r := bytes.NewReader(data)
csv_r := csv.NewReader(r)
rows, err := csv_r.ReadAll()
if err != nil {
t.Fatalf("Failed to read CSV data, %v", err)
}
if len(rows) != expected_rows {
t.Fatalf("Unexpected row coutn: %d", len(rows))
}
}
func TestGitHubCommitsTransformationWithPrepend(t *testing.T) {
expected_hash := "1f7cff82034f23c836682db2102e2a79541dcc9afb64e2fe189b306c8591c004"
expected_rows := 1608
msg := "fixtures/events/flights.json"
fh, err := os.Open(msg)
if err != nil {
t.Fatalf("Failed to open %s, %v", msg, err)
}
defer fh.Close()
body, err := io.ReadAll(fh)
if err != nil {
t.Fatalf("Failed to read %s, %v", msg, err)
}
ctx := context.Background()
tr, err := transformation.NewTransformation(ctx, "githubcommits://?prepend_message=true")
if err != nil {
t.Fatalf("Failed to create new transformation, %v", err)
}
data, err2 := tr.Transform(ctx, body)
if err2 != nil {
t.Fatalf("Failed to transform message, %v", err2)
}
sum := sha256.Sum256([]byte(data))
hash := fmt.Sprintf("%x", sum)
if hash != expected_hash {
t.Fatalf("Unexpected hash of commit data: %s", hash)
}
r := bytes.NewReader(data)
csv_r := csv.NewReader(r)
rows, err := csv_r.ReadAll()
if err != nil {
t.Fatalf("Failed to read CSV data, %v", err)
}
if len(rows) != expected_rows {
t.Fatalf("Unexpected row coutn: %d", len(rows))
}
r = bytes.NewReader(data)
csv_r = csv.NewReader(r)
row, err := csv_r.Read()
if err != nil {
t.Fatalf("Failed to read row, %v", err)
}
if row[0] != "#message append SWIM data for 20200521" {
t.Fatalf("Unexpected value for first row '%s'", row[0])
}
}
func TestGitHubCommitsTransformationWithHalt(t *testing.T) {
msg := "fixtures/events/flights.json"
fh, err := os.Open(msg)
if err != nil {
t.Fatalf("Failed to open %s, %v", msg, err)
}
defer fh.Close()
body, err := io.ReadAll(fh)
if err != nil {
t.Fatalf("Failed to read %s, %v", msg, err)
}
ctx := context.Background()
tr, err := transformation.NewTransformation(ctx, "githubcommits://?halt_on_message=SWIM")
if err != nil {
t.Fatalf("Failed to create new transformation, %v", err)
}
_, err2 := tr.Transform(ctx, body)
if err2 == nil {
t.Fatalf("Expected error transforming message")
}
if err2.Code != webhookd.HaltEvent {
t.Fatalf("Expected halt event")
}
}