-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
169 lines (144 loc) · 3.55 KB
/
db_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"encoding/json"
"reflect"
"strings"
"testing"
)
const jsonTestData = `
{
"posts": [
{
"id": 1,
"title": "Testing",
"author": "Foo"
},
{
"id": 2,
"title": "Testing Post ID 2",
"author": "Bar"
}
],
"comments": [
{
"id": 1,
"body": "Testing",
"postId": 1
},
{
"id": 2,
"body": "Testing Comment ID 2",
"postId": 2
}
]
}
`
func TestParseJsonFile(t *testing.T) {
// `TestMain` will have already called parseJsonFile for the initial setup,
// so this is just a quick check to make sure that actually succeeded
if maxIds["posts"] != 2 || maxIds["comments"] != 2 {
t.Fatal("Failing TestParseJsonFile fails all tests")
}
}
func TestItemTypes(t *testing.T) {
itemTypes := serverData.ItemTypes()
itemTypesFound := make(map[string]bool)
expectedTypes := []string{"posts", "comments"}
if len(itemTypes) != len(expectedTypes) {
t.Errorf("Expected %d item types, got %d\n", len(expectedTypes), len(itemTypes))
}
for _, itemType := range expectedTypes {
itemTypesFound[itemType] = false
}
for _, itemType := range itemTypes {
itemTypesFound[itemType] = true
}
for itemType, found := range itemTypesFound {
if !found {
t.Errorf("Item type %s not found\n", itemType)
}
}
}
func TestFetchingRecord(t *testing.T) {
type Record struct {
Type string
Id int64
Expected map[string]interface{}
}
recordsToFetch := []Record{
Record{
Type: "posts",
Id: 1,
Expected: map[string]interface{}{
"id": int64(1),
"title": "Testing",
"author": "Foo",
},
},
Record{
Type: "comments",
Id: 2,
Expected: map[string]interface{}{
"id": int64(2),
"body": "Testing Comment ID 2",
"postId": int64(2),
},
},
}
for _, expectedRecord := range recordsToFetch {
actualRecord, err := serverData.RecordWithId(expectedRecord.Type, expectedRecord.Id)
if err != nil {
t.Error("No error was expected:", err)
continue
}
if len(actualRecord) != len(expectedRecord.Expected) {
t.Errorf("Invalid number of columns in returned record. Expected %d, got %d", len(expectedRecord.Expected), len(actualRecord))
}
for key, expectedValue := range expectedRecord.Expected {
actualValue, ok := actualRecord[key]
if !ok {
t.Error("invalid key", key)
continue
}
if expectedValue != actualValue {
expectedType := reflect.TypeOf(expectedValue)
actualType := reflect.TypeOf(actualValue)
// Special handling for json.Number
if jsonNumber, ok := actualValue.(json.Number); ok {
int64Number, err := jsonNumber.Int64()
if err == nil && int64Number == expectedValue {
continue
}
}
t.Errorf("expected %#v of type %s, got %#v of type %s", expectedValue, expectedType, actualValue, actualType)
continue
}
}
}
// Test getting a bad record
errorMessage := "Should have gotten an error, none given"
if _, err := serverData.RecordWithId("posts", -1); err == nil {
t.Error(errorMessage)
}
if _, err := serverData.RecordWithId("non-existant", -1); err == nil {
t.Error(errorMessage)
}
}
func TestJsonDecode(t *testing.T) {
reader := strings.NewReader(`{"number": 2}`)
data := make(map[string]interface{})
err := decodeJson(reader, &data)
if err != nil {
t.Fail()
}
// kind of quicker than reflect.TypeOf?
switch data["number"].(type) {
case int64:
break
default:
t.Fail()
}
}
// TODO: Need to add tests for db. Most of the functionality will also be covered by the handlers, but there
// should also be isolated tests
//