-
Notifications
You must be signed in to change notification settings - Fork 4
/
cucumber_test.go.disabled
230 lines (198 loc) · 5.36 KB
/
cucumber_test.go.disabled
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* file: $GOPATH/src/godogs/godogs_test.go */
package main
import (
"github.com/DATA-DOG/godog"
"net/http"
"io/ioutil"
"encoding/json"
"testing"
"os"
"time"
"strings"
"fmt"
"net/url"
)
func resolveBindAddr() string {
bindAddr := os.Getenv("BIND_ADDR")
if len(bindAddr) == 0 {
bindAddr = ":10001"
}
return bindAddr
}
var bindAddr string = resolveBindAddr()
type Description struct {
NextRelease string
ReleaseDate time.Time
DatasetUri string
Published bool `json:published,omitempty`
Cancelled bool `json:cancelled,omitempty`
Title string
}
type Source struct {
Uri string
Description Description
}
type RecordHits struct {
Id string `json:"_id"`
Score float64 `json:"_score"`
Source Source `json:"_source"`
Type string `json:"_type"`
Index string `json:"_index"`
Sort [] float64
}
type Hits struct {
Total int64
Hits []RecordHits
}
type Responses struct {
Took int
Hits Hits
}
type HttpResponse struct {
Responses []Responses
}
var httpResponse HttpResponse
func TestMain(m *testing.M) {
go main()
status := godog.RunWithOptions("search", func(s *godog.Suite) {
FeatureContext(s)
}, godog.Options{
Format: "progress",
Paths: []string{"features"},
})
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
func buildURL(params map[string]string) string {
query := "http://localhost" + bindAddr + "/search?"
for key, value := range params {
fmt.Println("Key:", key, "Value:", value)
query = query + "&" + url.PathEscape(key) + "=" + url.PathEscape(value)
}
return query
}
func search(params map[string]string) error {
i := buildURL(params)
req, err := http.NewRequest("GET", i, nil)
if err != nil {
panic(err)
return err
}
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
panic(err)
return err
}
var response []byte
response, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
return err
}
var localHttpResponse HttpResponse
ioutil.WriteFile("/tmp/testResponse.json", response, 0644)
err = json.Unmarshal(response, &localHttpResponse)
if err != nil {
panic(err)
return err
}
httpResponse = localHttpResponse
f,_ := ioutil.TempFile("/tmp","responseParsed")
str,err := json.Marshal(httpResponse)
if err != nil {
panic(err)
return err
}
f.Write(str)
o,err := ioutil.TempFile("/tmp","responseOrigin")
o.Write(response)
if err != nil {
panic(err)
return err
}
return nil
}
func searchForTerm(term string) error {
return search(map[string]string{"term": term})
}
func onlyReceiveFromDatasetURI(uri string) error {
for _, r := range httpResponse.Responses {
for _, hit := range r.Hits.Hits {
if !strings.HasPrefix(uri, hit.Source.Description.DatasetUri) {
return fmt.Errorf("URI %s does not match expected %s", hit.Source.Description.DatasetUri, uri)
}
}
}
return nil
}
func theResultsAreInDateDescendingOrder() error {
var lastHit RecordHits
for _, r := range httpResponse.Responses {
for _, hit := range r.Hits.Hits {
currentDate := hit.Source.Description.ReleaseDate
lastTime := lastHit.Source.Description.ReleaseDate
if lastHit.Id != "" && lastHit.Score == hit.Score && lastTime.Before(currentDate) {
return fmt.Errorf("date order is not valid date last Hit %s is before %s current %s which is %s",
lastHit.Id, lastTime, hit.Id, currentDate, lastTime)
}
lastHit = hit
}
}
return nil
}
func filterReleaseCalendar(pubOrUpComing string) error {
params := map[string]string{pubOrUpComing:"true", "size":"1000"}
return search(params)
}
/**
Upcoming means that the documents are not release
not published and not cancelled OR are published and are due
*/
func checkUpComing() error {
for _, r := range httpResponse.Responses {
for _, hit := range r.Hits.Hits {
description := hit.Source.Description
if !((!description.Cancelled && !description.Published) ||
!(description.Published && description.ReleaseDate.Before(time.Now()))) {
str,_ := json.Marshal(hit)
return fmt.Errorf("Document is not Upcoming", string(str))
}
}
}
//Upcomm
return nil
}
/**
Published means that the documents are published and not cancelled OR are cancelled and are due
*/
func checkPublished() error {
for _, r := range httpResponse.Responses {
for _, hit := range r.Hits.Hits {
description := hit.Source.Description
if !((!description.Cancelled && description.Published) ||
(description.Cancelled && description.ReleaseDate.Before(time.Now()))) {
str,_ := json.Marshal(hit)
return fmt.Errorf("Document is not Published", string(str))
}
}
}
return nil
}
func FeatureContext(s *godog.Suite) {
s.Step(`^a user searches for the term\(s\) "([a-zA-Z\s]*)"$`, searchForTerm)
s.Step(`^the user will receive the first page with documents only from this uri prefix (.*)$`, onlyReceiveFromDatasetURI)
s.Step(`^the results with the same score are in date descending order$`, theResultsAreInDateDescendingOrder)
s.Step(`^a user filters the release calendar for "([^"]*)" documents$`, filterReleaseCalendar)
s.Step(`^user will receive a list of the documents are upcoming$`, checkUpComing)
s.Step(`^user will receive a list of the documents are published$`, checkPublished)
}