-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulls_test.go
More file actions
109 lines (99 loc) · 3.03 KB
/
pulls_test.go
File metadata and controls
109 lines (99 loc) · 3.03 KB
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
package forge
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"dappco.re/go/core/forge/types"
)
func TestPullService_Good_List(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
w.Header().Set("X-Total-Count", "2")
json.NewEncoder(w).Encode([]types.PullRequest{
{ID: 1, Title: "add feature"},
{ID: 2, Title: "fix bug"},
})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
result, err := f.Pulls.List(context.Background(), Params{"owner": "core", "repo": "go-forge"}, DefaultList)
if err != nil {
t.Fatal(err)
}
if len(result.Items) != 2 {
t.Errorf("got %d items, want 2", len(result.Items))
}
if result.Items[0].Title != "add feature" {
t.Errorf("got title=%q, want %q", result.Items[0].Title, "add feature")
}
}
func TestPullService_Good_Get(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/pulls/1" {
t.Errorf("wrong path: %s", r.URL.Path)
}
json.NewEncoder(w).Encode(types.PullRequest{ID: 1, Title: "add feature", Index: 1})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
pr, err := f.Pulls.Get(context.Background(), Params{"owner": "core", "repo": "go-forge", "index": "1"})
if err != nil {
t.Fatal(err)
}
if pr.Title != "add feature" {
t.Errorf("got title=%q", pr.Title)
}
}
func TestPullService_Good_Create(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
var body types.CreatePullRequestOption
json.NewDecoder(r.Body).Decode(&body)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(types.PullRequest{ID: 1, Title: body.Title, Index: 1})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
pr, err := f.Pulls.Create(context.Background(), Params{"owner": "core", "repo": "go-forge"}, &types.CreatePullRequestOption{
Title: "new pull request",
Head: "feature-branch",
Base: "main",
})
if err != nil {
t.Fatal(err)
}
if pr.Title != "new pull request" {
t.Errorf("got title=%q", pr.Title)
}
}
func TestPullService_Good_Merge(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/pulls/7/merge" {
t.Errorf("wrong path: %s", r.URL.Path)
}
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
if body["Do"] != "merge" {
t.Errorf("got Do=%q, want %q", body["Do"], "merge")
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
err := f.Pulls.Merge(context.Background(), "core", "go-forge", 7, "merge")
if err != nil {
t.Fatal(err)
}
}