-
Notifications
You must be signed in to change notification settings - Fork 59
/
annotations_test.go
126 lines (108 loc) · 3.95 KB
/
annotations_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
package buildkite
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestAnnotationsService_ListByBuild(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/awesome-build/annotations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{
"id": "de0d4ab5-6360-467a-a34b-e5ef5db5320d",
"context": "default",
"style": "info",
"body_html": "<h1>My Markdown Heading</h1>\n<img src=\"artifact://indy.png\" alt=\"Belongs in a museum\" height=250 />",
"created_at": "2019-04-09T18:07:15.775Z",
"updated_at": "2019-08-06T20:58:49.396Z"
},
{
"id": "5b3ceff6-78cb-4fe9-88ae-51be5f145977",
"context": "coverage",
"style": "info",
"body_html": "Read the <a href=\"artifact://coverage/index.html\">uploaded coverage report</a>",
"created_at": "2019-04-09T18:07:16.320Z",
"updated_at": "2019-04-09T18:07:16.320Z"
}]`)
})
annotations, _, err := client.Annotations.ListByBuild(context.Background(), "my-great-org", "sup-keith", "awesome-build", nil)
if err != nil {
t.Errorf("ListByBuild returned error: %v", err)
}
want := []Annotation{
{
ID: "de0d4ab5-6360-467a-a34b-e5ef5db5320d",
Context: "default",
Style: "info",
BodyHTML: "<h1>My Markdown Heading</h1>\n<img src=\"artifact://indy.png\" alt=\"Belongs in a museum\" height=250 />",
CreatedAt: NewTimestamp(time.Date(2019, 4, 9, 18, 7, 15, 775000000, time.UTC)),
UpdatedAt: NewTimestamp(time.Date(2019, 8, 6, 20, 58, 49, 396000000, time.UTC)),
},
{
ID: "5b3ceff6-78cb-4fe9-88ae-51be5f145977",
Context: "coverage",
Style: "info",
BodyHTML: "Read the <a href=\"artifact://coverage/index.html\">uploaded coverage report</a>",
CreatedAt: NewTimestamp(time.Date(2019, 4, 9, 18, 7, 16, 320000000, time.UTC)),
UpdatedAt: NewTimestamp(time.Date(2019, 4, 9, 18, 7, 16, 320000000, time.UTC)),
},
}
if diff := cmp.Diff(annotations, want); diff != "" {
t.Errorf("ListByBuild diff: (-got +want)\n%s", diff)
}
}
func TestAnnotationsService_Create(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
input := AnnotationCreate{
Style: "info",
Context: "default",
Body: "<h1>My Markdown Heading</h1>\n<p>An example annotation!</p>",
Append: false,
}
server.HandleFunc("/v2/organizations/my-great-org/pipelines/my-great-pipeline/builds/10/annotations", func(w http.ResponseWriter, r *http.Request) {
var v AnnotationCreate
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("Error parsing json body: %v", err)
}
testMethod(t, r, "POST")
if diff := cmp.Diff(v, input); diff != "" {
t.Errorf("Request body diff: (-got +want)\n%s", diff)
}
fmt.Fprint(w,
`
{
"id": "68aef727-f754-48e1-aad8-5f5da8a9960c",
"context": "default",
"style": "info",
"body_html": "<h1>My Markdown Heading</h1>\n<p>An example annotation!</p>",
"created_at": "2023-08-21T08:50:05.824Z",
"updated_at": "2023-08-21T08:50:05.824Z"
}`)
})
annotation, _, err := client.Annotations.Create(context.Background(), "my-great-org", "my-great-pipeline", "10", input)
if err != nil {
t.Errorf("TestAnnotations.Create returned error: %v", err)
}
annotationCreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-21T08:50:05.824Z"))
annotationUpatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-21T08:50:05.824Z"))
want := Annotation{
ID: "68aef727-f754-48e1-aad8-5f5da8a9960c",
Context: "default",
Style: "info",
BodyHTML: "<h1>My Markdown Heading</h1>\n<p>An example annotation!</p>",
CreatedAt: NewTimestamp(annotationCreatedAt),
UpdatedAt: NewTimestamp(annotationUpatedAt),
}
if diff := cmp.Diff(annotation, want); diff != "" {
t.Errorf("TestAnnotations.Create diff: (-got +want)\n%s", diff)
}
}