OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package gae | |
6 | |
7 import ( | |
8 "math" | |
9 "testing" | |
10 "time" | |
11 | |
12 . "github.com/smartystreets/goconvey/convey" | |
13 ) | |
14 | |
15 type myint int | |
16 type mybool bool | |
17 type mystring string | |
18 type myfloat float32 | |
19 | |
20 func TestProperties(t *testing.T) { | |
21 t.Parallel() | |
22 | |
23 Convey("Test DSProperty", t, func() { | |
24 Convey("Construction", func() { | |
25 Convey("empty", func() { | |
26 pv := DSProperty{} | |
27 So(pv.Value(), ShouldBeNil) | |
28 So(pv.NoIndex(), ShouldBeFalse) | |
29 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
30 }) | |
31 Convey("set", func() { | |
32 pv := DSProperty{} | |
33 pv.SetValue(100, true) | |
34 So(pv.Value(), ShouldHaveSameTypeAs, int64(100)) | |
35 So(pv.Value(), ShouldEqual, 100) | |
36 So(pv.NoIndex(), ShouldBeTrue) | |
37 So(pv.Type().String(), ShouldEqual, "DSPTInt") | |
38 | |
39 pv.SetValue(nil, false) | |
40 So(pv.Value(), ShouldBeNil) | |
41 So(pv.NoIndex(), ShouldBeFalse) | |
42 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
43 }) | |
44 Convey("derived types", func() { | |
45 Convey("int", func() { | |
46 pv := DSProperty{} | |
47 pv.SetValue(myint(19), false) | |
48 So(pv.Value(), ShouldHaveSameTypeAs, int
64(19)) | |
49 So(pv.Value(), ShouldEqual, 19) | |
50 So(pv.NoIndex(), ShouldBeFalse) | |
51 So(pv.Type().String(), ShouldEqual, "DSP
TInt") | |
52 }) | |
53 Convey("bool (true)", func() { | |
54 pv := DSProperty{} | |
55 pv.SetValue(mybool(true), false) | |
56 So(pv.Value(), ShouldBeTrue) | |
57 So(pv.NoIndex(), ShouldBeFalse) | |
58 So(pv.Type().String(), ShouldEqual, "DSP
TBoolTrue") | |
59 }) | |
60 Convey("string", func() { | |
61 pv := DSProperty{} | |
62 pv.SetValue(mystring("sup"), false) | |
63 So(pv.Value(), ShouldEqual, "sup") | |
64 So(pv.NoIndex(), ShouldBeFalse) | |
65 So(pv.Type().String(), ShouldEqual, "DSP
TString") | |
66 }) | |
67 Convey("BSKey is distinquished", func() { | |
68 pv := DSProperty{} | |
69 pv.SetValue(BSKey("sup"), false) | |
70 So(pv.Value(), ShouldEqual, BSKey("sup")
) | |
71 So(pv.NoIndex(), ShouldBeFalse) | |
72 So(pv.Type().String(), ShouldEqual, "DSP
TBlobKey") | |
73 }) | |
74 Convey("float", func() { | |
75 pv := DSProperty{} | |
76 pv.SetValue(myfloat(19.7), false) | |
77 So(pv.Value(), ShouldHaveSameTypeAs, flo
at64(19.7)) | |
78 So(pv.Value(), ShouldEqual, float32(19.7
)) | |
79 So(pv.NoIndex(), ShouldBeFalse) | |
80 So(pv.Type().String(), ShouldEqual, "DSP
TFloat") | |
81 }) | |
82 }) | |
83 Convey("bad type", func() { | |
84 pv := DSProperty{} | |
85 err := pv.SetValue(complex(100, 29), false) | |
86 So(err.Error(), ShouldContainSubstring, "has bad
type complex") | |
87 So(pv.Value(), ShouldBeNil) | |
88 So(pv.NoIndex(), ShouldBeFalse) | |
89 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
90 }) | |
91 Convey("invalid GeoPoint", func() { | |
92 pv := DSProperty{} | |
93 err := pv.SetValue(DSGeoPoint{-1000, 0}, false) | |
94 So(err.Error(), ShouldContainSubstring, "invalid
GeoPoint value") | |
95 So(pv.Value(), ShouldBeNil) | |
96 So(pv.NoIndex(), ShouldBeFalse) | |
97 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
98 }) | |
99 Convey("invalid time", func() { | |
100 pv := DSProperty{} | |
101 err := pv.SetValue(time.Now(), false) | |
102 So(err.Error(), ShouldContainSubstring, "time va
lue has wrong Location") | |
103 | |
104 err = pv.SetValue(time.Unix(math.MaxInt64, 0).UT
C(), false) | |
105 So(err.Error(), ShouldContainSubstring, "time va
lue out of range") | |
106 So(pv.Value(), ShouldBeNil) | |
107 So(pv.NoIndex(), ShouldBeFalse) | |
108 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
109 }) | |
110 Convey("time gets rounded", func() { | |
111 pv := DSProperty{} | |
112 now := time.Now().In(time.UTC) | |
113 now = now.Round(time.Microsecond).Add(time.Nanos
econd * 313) | |
114 pv.SetValue(now, false) | |
115 So(pv.Value(), ShouldHappenBefore, now) | |
116 So(pv.NoIndex(), ShouldBeFalse) | |
117 So(pv.Type().String(), ShouldEqual, "DSPTTime") | |
118 }) | |
119 Convey("[]byte coerces NoIndex", func() { | |
120 pv := DSProperty{} | |
121 pv.SetValue([]byte("hello"), false) | |
122 So(pv.Value(), ShouldResemble, []byte("hello")) | |
123 So(pv.NoIndex(), ShouldBeTrue) | |
124 So(pv.Type().String(), ShouldEqual, "DSPTBytes") | |
125 }) | |
126 Convey("ByteString allows !NoIndex", func() { | |
127 pv := DSProperty{} | |
128 pv.SetValue(DSByteString("hello"), false) | |
129 So(pv.Value(), ShouldResemble, DSByteString("hel
lo")) | |
130 So(pv.NoIndex(), ShouldBeFalse) | |
131 So(pv.Type().String(), ShouldEqual, "DSPTBytes") | |
132 }) | |
133 }) | |
134 }) | |
135 } | |
136 | |
137 func TestDSPropertyMapImpl(t *testing.T) { | |
138 t.Parallel() | |
139 | |
140 Convey("DSPropertyMap load/save err conditions", t, func() { | |
141 Convey("nil", func() { | |
142 pm := (*DSPropertyMap)(nil) | |
143 _, err := pm.Load(nil) | |
144 So(err.Error(), ShouldContainSubstring, "nil DSPropertyM
ap") | |
145 | |
146 _, err = pm.Save() | |
147 So(err.Error(), ShouldContainSubstring, "nil DSPropertyM
ap") | |
148 }) | |
149 Convey("empty", func() { | |
150 pm := DSPropertyMap{} | |
151 _, err := pm.Load(DSPropertyMap{"hello": {DSProperty{}}}
) | |
152 So(err, ShouldBeNil) | |
153 So(pm, ShouldResemble, DSPropertyMap{"hello": {DSPropert
y{}}}) | |
154 | |
155 // it can also self-initialize | |
156 pm = DSPropertyMap(nil) | |
157 _, err = pm.Load(DSPropertyMap{"hello": {DSProperty{}}}) | |
158 So(err, ShouldBeNil) | |
159 So(pm, ShouldResemble, DSPropertyMap{"hello": {DSPropert
y{}}}) | |
160 | |
161 npm, _ := pm.Save() | |
162 So(npm, ShouldResemble, pm) | |
163 }) | |
164 }) | |
165 } | |
OLD | NEW |