-
Notifications
You must be signed in to change notification settings - Fork 14
/
types_test.go
122 lines (101 loc) · 3.69 KB
/
types_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
package dynago_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/underarmour/dynago"
)
func TestNumberIntValReturnsTheValueAsAnInt(t *testing.T) {
num := dynago.Number("18")
intVal, err := num.IntVal()
assert.Equal(t, 18, intVal)
assert.Nil(t, err)
}
func TestNumberIntValReturnsAnErrorIfItCannotParseTheValue(t *testing.T) {
num := dynago.Number("nope")
intVal, err := num.IntVal()
assert.Equal(t, 0, intVal)
assert.Error(t, err)
}
func TestNumberInt64ValReturnsTheValueAsAnInt(t *testing.T) {
num := dynago.Number("18")
intVal, err := num.Int64Val()
assert.Equal(t, int64(18), intVal)
assert.Nil(t, err)
}
func TestNumberUint64ValReturnsTheValueAsAnInt(t *testing.T) {
num := dynago.Number("123456789012")
intVal, err := num.Uint64Val()
assert.Equal(t, uint64(123456789012), intVal)
assert.Nil(t, err)
}
func TestNumberInt64ValReturnsAnErrorIfItCannotParseTheValue(t *testing.T) {
num := dynago.Number("nope")
intVal, err := num.Int64Val()
assert.Equal(t, int64(0), intVal)
assert.Error(t, err)
}
func TestNumberFloatValReturnsTheValueAsAnfloat(t *testing.T) {
num := dynago.Number("18.12")
floatVal, err := num.FloatVal()
assert.Equal(t, float64(18.12), floatVal)
assert.Nil(t, err)
}
func TestNumberFloatValReturnsAnErrorIfItCannotParseTheValue(t *testing.T) {
num := dynago.Number("nope")
floatVal, err := num.FloatVal()
assert.Equal(t, float64(0), floatVal)
assert.Error(t, err)
}
func TestDocumentGetStringReturnsTheUnderlyingValueAsAString(t *testing.T) {
doc := dynago.Document{"name": "Timmy Testerson"}
assert.Equal(t, "Timmy Testerson", doc.GetString("name"))
}
func TestDocumentGetStringReturnsAnEmptyStringWhenTheKeyIsNotPresent(t *testing.T) {
doc := dynago.Document{}
assert.Equal(t, "", doc.GetString("name"))
}
func TestDocumentGetNumberReturnsTheDynagoNumberWrappingTheValue(t *testing.T) {
doc := dynago.Document{"id": dynago.Number("12")}
assert.Equal(t, dynago.Number("12"), doc.GetNumber("id"))
}
func TestDocumentGetNumberReturnsAnEmptyNumberWhenTheKeyIsNotPresent(t *testing.T) {
doc := dynago.Document{}
assert.Equal(t, dynago.Number(""), doc.GetNumber("id"))
}
func TestDocumentGetNumberPanicsIfTheUnderlyingTypeIsNotANumber(t *testing.T) {
doc := dynago.Document{"id": "not-a-dynago-number"}
assert.Panics(t, func() {
doc.GetNumber("id")
})
}
func TestDocumentGetStringSetReturnsTheStringSetValue(t *testing.T) {
doc := dynago.Document{"vals": dynago.StringSet{"val1", "val2"}}
assert.Equal(t, dynago.StringSet{"val1", "val2"}, doc.GetStringSet("vals"))
}
func TestDocumentGetStringSetReturnsAnEmptyStringSetWhenTheKeyDoesNotExist(t *testing.T) {
doc := dynago.Document{}
assert.Equal(t, dynago.StringSet{}, doc.GetStringSet("vals"))
}
func TestDocumentGetStringSetPanic(t *testing.T) {
doc := dynago.Document{"vals": "not-a-string-slice"}
assert.Panics(t, func() {
doc.GetStringSet("vals")
})
}
func TestDocumentGetTimeReturnsTheTimeValueFromISO8601(t *testing.T) {
doc := dynago.Document{"time": "1990-04-16T00:00:00Z"}
val, _ := time.Parse("2006-01-02T15:04:05Z", "1990-04-16T00:00:00Z")
assert.Equal(t, dynago.Time(&val), doc.GetTime("time"))
}
func TestDocumentGetTimeReturnsNilWhenTheKeyDoesNotExist(t *testing.T) {
doc := dynago.Document{}
assert.Nil(t, doc.GetTime("time"))
}
func TestDocumentMarshalJSONDoesNotIncludeEmptyValues(t *testing.T) {
doc := dynago.Document{"key1": "shows up", "key2": 9, "fields": dynago.StringSet([]string{"is", "present"}), "id": "", "name": nil, "tags": []string{}}
jsonDoc, _ := doc.MarshalJSON()
assert.Contains(t, string(jsonDoc), `"fields":{"SS":["is","present"]}`)
assert.Contains(t, string(jsonDoc), `"key1":{"S":"shows up"}`)
assert.Contains(t, string(jsonDoc), `"key2":{"N":"9"}`)
}