-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_test.go
126 lines (107 loc) · 3.15 KB
/
job_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
package gowup
import (
"encoding/json"
"github.com/stretchr/testify/suite"
"net/url"
"testing"
"time"
)
type JobSummaryTest struct {
suite.Suite
}
func TestJobSummary(t *testing.T) {
suite.Run(t, new(JobSummaryTest))
}
func (j *JobSummaryTest) TestUrlUnmarshaler() {
actual, urlstring := Url{}, "https://google.com/asdf"
data, _ := json.Marshal(urlstring)
parsed, _ := url.Parse(urlstring)
expected := Url{URL: parsed}
json.Unmarshal(data, &actual)
j.Equal(expected, actual, "should unmarshal urls to a Url type")
j.Equal(actual.Path, "/asdf", "should have the correct url content")
}
func (j *JobSummaryTest) TestUrlUnmarshalerInSummary() {
job, urlstring := JobSummary{}, "https://herp.us/derp?foo=bar"
data, _ := json.Marshal(map[string]string{"url": urlstring})
parsed, _ := url.Parse(urlstring)
expected := Url{URL: parsed}
json.Unmarshal(data, &job)
j.Equal(expected, job.Url, "should unmarshal url fields to Urls")
}
func (j *JobSummaryTest) TestTimeUnmarshaler() {
job, start := JobSummary{}, Time{Time: time.Unix(1396972009, 0)}
data, _ := json.Marshal(map[string]int64{"start_time": start.Unix()})
json.Unmarshal(data, &job)
j.Equal(start, job.StartTime, "should unmarshal time fields correctly")
}
func (j *JobSummaryTest) TestExpireTimeUnmarshaler() {
job, start := JobSummary{}, Time{Time: time.Unix(1396972009, 0)}
data, _ := json.Marshal(map[string]map[string]int64{"expiry": {"sec": start.Unix()}})
json.Unmarshal(data, &job)
j.Equal(start, job.ExpireTime, "should unmarshal expiration time correctly")
}
func (j *JobSummaryTest) TestServicesUnmarshaler() {
data := []byte(`{ "services": [
{
"city": "denver",
"server": "denver",
"checks": [
"http",
"ping",
"trace",
"fast",
"dig"
]
},
{
"city": "sydney",
"server": "sydney",
"checks": [
"http",
"ping",
"trace",
"fast",
"dig"
]
},
{
"city": "riga",
"server": "riga",
"checks": [
"http",
"ping",
"trace",
"fast",
"dig"
]
}
]}`)
job := JobSummary{}
json.Unmarshal(data, &job)
j.Equal("http", job.Services[2].Tests[0], "should unmarshal the nested slices")
j.Equal("sydney", job.Services[1].Server, "should unmarshal the strings")
}
func (j *JobSummaryTest) TestJobDetailUnmarshaler() {
data := []byte(`{
"denver": {
"fast": {
"raw": {},
"summary": {"herp": "derp"}
}
},
"tokyo": {
"trace": {
"raw": {},
"summary": [1, 2]
}
}
}`)
detail := JobDetail{}
json.Unmarshal(data, &detail)
j.Equal(map[string]interface{}{"herp": "derp"}, detail["denver"]["fast"], "should unmarshal populated objects")
j.Equal([]interface{}{1, 2}, detail["tokyo"]["trace"], "should unmarshal populated arrays")
data = []byte(`[]`)
json.Unmarshal(data, &detail)
j.Equal(JobDetail{}, detail)
}