-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateHandler_test.go
105 lines (88 loc) · 2.38 KB
/
updateHandler_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
// +build !appengine
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/PuerkitoBio/goquery"
)
func TestUpdateReturnsOK(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(Update))
defer server.Close()
if resp, err := http.DefaultClient.Get(server.URL); err != nil || resp.StatusCode != http.StatusOK {
t.FailNow()
}
}
func TestUpdateReturnsSolicitations(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(Update))
defer server.Close()
resp, err := http.DefaultClient.Get(server.URL)
if err != nil || resp.StatusCode != http.StatusOK {
t.FailNow()
}
defer resp.Body.Close()
dump, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.FailNow()
}
var solicitations []Solicitation
err = json.Unmarshal(dump, &solicitations)
if err != nil {
t.FailNow()
}
}
func TestUpdateFindsUpdatedPropertyValues(t *testing.T) {
tests := []struct {
file string
updates int
solicitations int
}{
{"sample-feed.html", 5, 5},
{"sample-feed-award.html", 1, 5},
}
for _, test := range tests {
file, err := os.Open(test.file)
if err != nil {
t.Errorf("Could not open '%s'", test.file)
}
doc, err := goquery.NewDocumentFromReader(file)
if err != nil {
t.Errorf("Could not parse '%s'", test.file)
}
updates, solicitations, err := parseDocument(nil, doc)
if err != nil && len(updates) != test.updates && len(solicitations) != test.solicitations {
t.FailNow()
}
}
}
func TestUpdateFindsNewSolicitation(t *testing.T) {
tests := []struct {
file string
updates int
solicitations int
}{
{"testdata/sample-feed.html", 5, 5},
{"testdata/sample-feed-new.html", 1, 6},
}
for _, test := range tests {
file, err := os.Open(test.file)
if err != nil {
t.Errorf("Could not open '%s'", test.file)
}
doc, err := goquery.NewDocumentFromReader(file)
if err != nil {
t.Errorf("Could not parse '%s'", test.file)
}
updates, solicitations, err := parseDocument(nil, doc)
if err != nil {
t.Errorf("Error during parsing: %v", err)
} else if len(solicitations) != test.solicitations {
t.Errorf("In %s expected %d but received %d solicitations", test.file, test.solicitations, len(solicitations))
} else if len(updates) != test.updates {
t.Errorf("In %s expected %d but received %d updates", test.file, test.updates, len(updates))
}
}
}