-
Notifications
You must be signed in to change notification settings - Fork 36
/
response_model_test.go
91 lines (76 loc) · 1.85 KB
/
response_model_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
package godata
import (
"encoding/json"
"testing"
)
type testResponseJson struct {
ODataContext string `json:"@odata.context"`
ODataCount int `json:"@odata.count"`
Value []testResponseValue `json:"value"`
}
type testResponseValue struct {
Name string
Age float64
Gender string
}
func TestResponseWriter(t *testing.T) {
test := &GoDataResponse{
Fields: map[string]*GoDataResponseField{
"@odata.context": &GoDataResponseField{
Value: "http://service.example",
},
"@odata.count": &GoDataResponseField{
Value: 8,
},
"value": &GoDataResponseField{
Value: []*GoDataResponseField{
&GoDataResponseField{
Value: map[string]*GoDataResponseField{
"Name": &GoDataResponseField{Value: "John Doe"},
"Age": &GoDataResponseField{11.01},
"Male": &GoDataResponseField{Value: "Female"},
},
},
&GoDataResponseField{
Value: map[string]*GoDataResponseField{
"Name": &GoDataResponseField{Value: "Jane \"Cool\" Doe"},
"Age": &GoDataResponseField{12.1},
"Gender": &GoDataResponseField{Value: "Female"},
},
},
},
},
},
}
written, err := test.Json()
if err != nil {
t.Error(err)
return
}
var result testResponseJson
err = json.Unmarshal(written, &result)
if err != nil {
t.Error(err)
return
}
if result.ODataContext != "http://service.example" {
t.Error("@odata.context is", result.ODataContext)
return
}
if result.ODataCount != 8 {
t.Error("@odata.count is", result.ODataCount)
return
}
if len(result.Value) != 2 {
t.Error("Result value is not length 2")
return
}
if result.Value[0].Name != "John Doe" {
t.Error("First value name is", result.Value[0].Name)
return
}
if result.Value[1].Name != "Jane \"Cool\" Doe" {
t.Error("Second value name is", result.Value[1].Name)
return
}
}