-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_test.go
More file actions
126 lines (96 loc) · 2.67 KB
/
data_test.go
File metadata and controls
126 lines (96 loc) · 2.67 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package phant
import (
"fmt"
"net/http"
"testing"
)
type testType struct {
Derek string `json:"derek"`
Test string `json:"test"`
Timestamp string `json:"timestamp"`
}
func TestAllData_ReturnACorrectResponse(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/output/12345.json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `[{"derek":"1","test":"2","timestamp":"2014-07-23T03:17:28.061Z"},{"derek":"3","test":"4","timestamp":"2014-07-23T03:17:28.061Z"}]`)
})
var results []testType
err := AllData("12345", &results)
if err != nil {
t.Error("expected no error: " + err.Error())
}
if len(results) != 2 {
t.Error("expected len(results) to be 2")
}
if results[0].Derek != "1" {
t.Error("expected 1 in [0].Derek")
}
if results[0].Test != "2" {
t.Error("expected 2 in [0].Test")
}
if results[1].Derek != "3" {
t.Error("expected 1 in [0].Derek")
}
if results[1].Test != "4" {
t.Error("expected 4 in [1].Test")
}
}
func TestDataOnPage_ReturnACorrectResponse(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/output/12345.json", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("page") != "2" {
t.Error("expected page parameter to be 2")
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `[{"derek":"1","test":"2","timestamp":"2014-07-23T03:17:28.061Z"},{"derek":"3","test":"4","timestamp":"2014-07-23T03:17:28.061Z"}]`)
})
var results []testType
err := DataOnPage("12345", 2, &results)
if err != nil {
t.Error("expected no error: " + err.Error())
}
if len(results) != 2 {
t.Error("expected len(results) to be 2")
}
if results[0].Derek != "1" {
t.Error("expected 1 in [0].Derek")
}
if results[0].Test != "2" {
t.Error("expected 2 in [0].Test")
}
if results[1].Derek != "3" {
t.Error("expected 1 in [0].Derek")
}
if results[1].Test != "4" {
t.Error("expected 4 in [1].Test")
}
}
func TestAllData_HandlesA404(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/output/12345.json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, `{"success":false,"message":"notfound"}`)
})
var results []testType
err := AllData("12345", &results)
if err == nil {
t.Error("expected error to not be nil")
}
}
func TestDataOnPage_HandlesA404(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/output/12345.json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, `{"success":false,"message":"notfound"}`)
})
var results []testType
err := DataOnPage("12345", 2, &results)
if err == nil {
t.Error("expected error to not be nil")
}
}