-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
164 lines (133 loc) · 4.36 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
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
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"github.com/go-pg/pg"
"github.com/google/jsonapi"
"github.com/gorilla/mux"
"github.com/jfo84/cleopatchra/api/db"
"github.com/jfo84/cleopatchra/api/exports"
"github.com/jfo84/cleopatchra/api/pull"
"github.com/jfo84/cleopatchra/api/pulls"
"github.com/jfo84/factory-go/factory"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// To avoid collisions with other keys
type key string
// PullFactory is a factory for generating temporary rows on the pulls table
var PullFactory = factory.NewFactory(
&db.Pull{},
).SeqInt("ID", func(n int) (interface{}, error) {
return n, nil
}).Attr("Data", func(args factory.Args) (interface{}, error) {
pull := args.Instance().(*db.Pull)
fileName := fmt.Sprintf("./testing/fixtures/pulls/%d.json", pull.ID)
data, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
return string(data[:]), nil
}).SeqInt("RepoID", func(n int) (interface{}, error) {
// Return 1, 1, 2, 2, 3, 3, etc.
// TODO: Maybe pass around context or add a way to access parent factory values
if n%2 == 1 {
return (n + 1) / 2, nil
}
return n / 2, nil
}).OnCreate(func(args factory.Args) error {
const txKey key = "tx"
tx := args.Context().Value(txKey).(*pg.Tx)
return tx.Insert(args.Instance())
}).SubSliceFactory("Comments", CommentFactory, func() int { return 2 })
// CommentFactory is a factory for generating temporary rows on the comments table
var CommentFactory = factory.NewFactory(
&db.Comment{},
).SeqInt("ID", func(n int) (interface{}, error) {
return n, nil
}).SeqInt("PullID", func(n int) (interface{}, error) {
// Return 1, 1, 2, 2, 3, 3, etc.
// TODO: Maybe pass around context or add a way to access parent factory values
if n%2 == 1 {
return (n + 1) / 2, nil
}
return n / 2, nil
}).Attr("Data", func(args factory.Args) (interface{}, error) {
comment := args.Instance().(*db.Comment)
fileName := fmt.Sprintf("./testing/fixtures/comments/%d.json", comment.ID)
data, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
return string(data[:]), nil
}).OnCreate(func(args factory.Args) error {
const txKey key = "tx"
tx := args.Context().Value(txKey).(*pg.Tx)
return tx.Insert(args.Instance())
})
var _ = Describe("TestCleopatchra", func() {
var (
req *http.Request
err error
)
router := mux.NewRouter().StrictSlash(true)
dbWrap := db.OpenTestDB()
for i := 0; i < 3; i++ {
tx := dbWrap.BeginTx()
const txKey key = "tx"
ctx := context.WithValue(context.Background(), txKey, tx)
_, err := PullFactory.CreateWithContext(ctx)
if err != nil {
panic(err)
}
tx.Commit()
}
Context("Pull Requests", func() {
It("Should correctly return a pull", func() {
req, err = http.NewRequest("GET", "/pulls/1", nil)
recorder := httptest.NewRecorder()
pullController := pull.NewController(dbWrap)
router.HandleFunc("/pulls/{pullID}", pullController.Get)
router.ServeHTTP(recorder, req)
// Confirm the returned json is what we expected
var eBytes []byte
eBytes, err = ioutil.ReadFile("./testing/fixtures/expected_pull.json")
if err != nil {
panic(err)
}
expectedPull := new(exports.Pull)
reader := bytes.NewReader(eBytes)
jsonapi.UnmarshalPayload(reader, expectedPull)
reader = bytes.NewReader(recorder.Body.Bytes())
actualPull := new(exports.Pull)
jsonapi.UnmarshalPayload(reader, actualPull)
Expect(recorder.Code).To(Equal(http.StatusOK))
Expect(expectedPull).To(BeEquivalentTo(actualPull))
})
It("Should correctly return pulls", func() {
req, err = http.NewRequest("GET", "/repos/1/pulls", nil)
recorder := httptest.NewRecorder()
pullsController := pulls.NewController(dbWrap)
router.HandleFunc("/repos/{repoID}/pulls", pullsController.Get)
router.ServeHTTP(recorder, req)
// Confirm the returned json is what we expected
eBytes, err := ioutil.ReadFile("./testing/fixtures/expected_pulls.json")
if err != nil {
panic(err)
}
var ePulls []*exports.Pull
pullType := reflect.TypeOf(ePulls)
reader := bytes.NewReader(eBytes)
expectedPulls, err := jsonapi.UnmarshalManyPayload(reader, pullType)
reader = bytes.NewReader(recorder.Body.Bytes())
actualPulls, err := jsonapi.UnmarshalManyPayload(reader, pullType)
Expect(recorder.Code).To(Equal(http.StatusOK))
Expect(expectedPulls).To(BeEquivalentTo(actualPulls))
})
})
})