-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_test.go
150 lines (137 loc) · 2.84 KB
/
data_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"os"
"testing"
"time"
)
func init() {
os.Setenv("PG_USER", "adicu")
os.Setenv("PG_PASSWORD", "adicu")
os.Setenv("PG_DB", "density")
os.Setenv("PG_HOST", "localhost")
os.Setenv("PG_PORT", "5432")
os.Setenv("PG_SSL", "disable")
}
// struct w/ regular encoding
var data1 = `{
"name" : "Lerner 3",
"client_count" : 70,
"parent_id" : 84
}`
// struct w/ string-encoded numbers
var data2 = `{
"name" : "Lerner 3",
"client_count" : "70",
"parent_id" : "84"
}`
// TestUnmarshalData tests that we now properly unmarshal both forms
// of the data.
func TestUnmarshalData(t *testing.T) {
var d dumpFormat
err := json.Unmarshal([]byte(data1), &d)
if err != nil {
t.Fatalf("Failed to unmarshal data with integers => {%s}", err)
}
err = json.Unmarshal([]byte(data2), &d)
if err != nil {
t.Fatalf("Failed to unmarshal data with strings => {%s}", err)
}
}
var testingData1 = `{
"152" : {
"name" : "Lerner 3",
"client_count" : 70,
"parent_id" : 84
},
"131" : {
"name" : "Butler Library 3",
"client_count" : 328,
"parent_id" : 103
},
"155" : {
"name" : "JJ's Place",
"client_count" : 90,
"parent_id" : 75
},
"130" : {
"name" : "Butler Library 2",
"client_count" : 412,
"parent_id" : 103
}
}`
var testingData2 = `{
"152" : {
"name" : "Lerner 3",
"client_count" : "70",
"parent_id" : "84"
},
"131" : {
"name" : "Butler Library 3",
"client_count" : "328",
"parent_id" : "103"
},
"155" : {
"name" : "JJ's Place",
"client_count" : "90",
"parent_id" : "75"
},
"130" : {
"name" : "Butler Library 2",
"client_count" : "412",
"parent_id" : "103"
}
}`
var expectedData = []dumpFormat{
{
GroupID: 152,
GroupName: "Lerner 3",
ClientCount: 70,
ParentID: 84,
ParentName: "Lerner",
},
{
GroupID: 131,
GroupName: "Butler Library 3",
ClientCount: 328,
ParentID: 103,
ParentName: "Butler",
},
{
GroupID: 155,
GroupName: "JJ's Place",
ClientCount: 90,
ParentID: 75,
ParentName: "John Jay",
},
{
GroupID: 130,
GroupName: "Butler Library 2",
ClientCount: 412,
ParentID: 103,
ParentName: "Butler",
},
}
// TestParseData parses the raw data in `testingData` and confirms that it
// correctly configures all data fields.
func TestParseData(t *testing.T) {
for _, dataset := range []string{testingData1, testingData2} {
data, err := parseData(time.Time{}, []byte(dataset))
if err != nil {
t.Fatal(err)
}
// make sure that the parsed data is in the expected data
expected := func(d dumpFormat, t *testing.T) {
for _, e := range expectedData {
if d == e {
return
}
}
t.Errorf("No match in expected data for %#v\n", d)
}
// n^2 because n == 4....
for _, d := range data {
expected(d, t)
}
}
}