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.IndexSetting(), ShouldEqual, ShouldIndex) | |
29 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
30 }) | |
31 Convey("set", func() { | |
32 pv := MkDSPropertyNI(100) | |
33 So(pv.Value(), ShouldHaveSameTypeAs, int64(100)) | |
34 So(pv.Value(), ShouldEqual, 100) | |
35 So(pv.IndexSetting(), ShouldEqual, NoIndex) | |
36 So(pv.Type().String(), ShouldEqual, "DSPTInt") | |
37 | |
38 pv.SetValue(nil, ShouldIndex) | |
39 So(pv.Value(), ShouldBeNil) | |
40 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
41 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
42 }) | |
43 Convey("derived types", func() { | |
44 Convey("int", func() { | |
45 pv := MkDSProperty(19) | |
46 So(pv.Value(), ShouldHaveSameTypeAs, int
64(19)) | |
47 So(pv.Value(), ShouldEqual, 19) | |
48 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
49 So(pv.Type().String(), ShouldEqual, "DSP
TInt") | |
50 }) | |
51 Convey("bool (true)", func() { | |
52 pv := MkDSProperty(mybool(true)) | |
53 So(pv.Value(), ShouldBeTrue) | |
54 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
55 So(pv.Type().String(), ShouldEqual, "DSP
TBoolTrue") | |
56 }) | |
57 Convey("string", func() { | |
58 pv := MkDSProperty(mystring("sup")) | |
59 So(pv.Value(), ShouldEqual, "sup") | |
60 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
61 So(pv.Type().String(), ShouldEqual, "DSP
TString") | |
62 }) | |
63 Convey("BSKey is distinquished", func() { | |
64 pv := MkDSProperty(BSKey("sup")) | |
65 So(pv.Value(), ShouldEqual, BSKey("sup")
) | |
66 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
67 So(pv.Type().String(), ShouldEqual, "DSP
TBlobKey") | |
68 }) | |
69 Convey("float", func() { | |
70 pv := DSProperty{} | |
71 pv.SetValue(myfloat(19.7), ShouldIndex) | |
72 So(pv.Value(), ShouldHaveSameTypeAs, flo
at64(19.7)) | |
73 So(pv.Value(), ShouldEqual, float32(19.7
)) | |
74 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
75 So(pv.Type().String(), ShouldEqual, "DSP
TFloat") | |
76 }) | |
77 }) | |
78 Convey("bad type", func() { | |
79 pv := DSProperty{} | |
80 err := pv.SetValue(complex(100, 29), ShouldIndex
) | |
81 So(err.Error(), ShouldContainSubstring, "has bad
type complex") | |
82 So(pv.Value(), ShouldBeNil) | |
83 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
84 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
85 }) | |
86 Convey("invalid GeoPoint", func() { | |
87 pv := DSProperty{} | |
88 err := pv.SetValue(DSGeoPoint{-1000, 0}, ShouldI
ndex) | |
89 So(err.Error(), ShouldContainSubstring, "invalid
GeoPoint value") | |
90 So(pv.Value(), ShouldBeNil) | |
91 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
92 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
93 }) | |
94 Convey("invalid time", func() { | |
95 pv := DSProperty{} | |
96 err := pv.SetValue(time.Now(), ShouldIndex) | |
97 So(err.Error(), ShouldContainSubstring, "time va
lue has wrong Location") | |
98 | |
99 err = pv.SetValue(time.Unix(math.MaxInt64, 0).UT
C(), ShouldIndex) | |
100 So(err.Error(), ShouldContainSubstring, "time va
lue out of range") | |
101 So(pv.Value(), ShouldBeNil) | |
102 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
103 So(pv.Type().String(), ShouldEqual, "DSPTNull") | |
104 }) | |
105 Convey("time gets rounded", func() { | |
106 pv := DSProperty{} | |
107 now := time.Now().In(time.UTC) | |
108 now = now.Round(time.Microsecond).Add(time.Nanos
econd * 313) | |
109 pv.SetValue(now, ShouldIndex) | |
110 So(pv.Value(), ShouldHappenBefore, now) | |
111 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
112 So(pv.Type().String(), ShouldEqual, "DSPTTime") | |
113 }) | |
114 Convey("[]byte coerces IndexSetting", func() { | |
115 pv := DSProperty{} | |
116 pv.SetValue([]byte("hello"), ShouldIndex) | |
117 So(pv.Value(), ShouldResemble, []byte("hello")) | |
118 So(pv.IndexSetting(), ShouldEqual, NoIndex) | |
119 So(pv.Type().String(), ShouldEqual, "DSPTBytes") | |
120 }) | |
121 Convey("ByteString allows !IndexSetting", func() { | |
122 pv := DSProperty{} | |
123 pv.SetValue(DSByteString("hello"), ShouldIndex) | |
124 So(pv.Value(), ShouldResemble, DSByteString("hel
lo")) | |
125 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
126 So(pv.Type().String(), ShouldEqual, "DSPTBytes") | |
127 }) | |
128 }) | |
129 }) | |
130 } | |
131 | |
132 func TestDSPropertyMapImpl(t *testing.T) { | |
133 t.Parallel() | |
134 | |
135 Convey("DSPropertyMap load/save err conditions", t, func() { | |
136 Convey("empty", func() { | |
137 pm := DSPropertyMap{} | |
138 err := pm.Load(DSPropertyMap{"hello": {DSProperty{}}}) | |
139 So(err, ShouldBeNil) | |
140 So(pm, ShouldResemble, DSPropertyMap{"hello": {DSPropert
y{}}}) | |
141 | |
142 npm, _ := pm.Save(false) | |
143 So(npm, ShouldResemble, pm) | |
144 }) | |
145 Convey("meta", func() { | |
146 Convey("working", func() { | |
147 pm := DSPropertyMap{"": {MkDSProperty("trap!")}} | |
148 _, err := pm.GetMeta("foo") | |
149 So(err, ShouldEqual, ErrDSMetaFieldUnset) | |
150 | |
151 err = pm.SetMeta("foo", 100) | |
152 So(err, ShouldBeNil) | |
153 | |
154 v, err := pm.GetMeta("foo") | |
155 So(err, ShouldBeNil) | |
156 So(v, ShouldEqual, 100) | |
157 | |
158 npm, err := pm.Save(false) | |
159 So(err, ShouldBeNil) | |
160 So(len(npm), ShouldEqual, 1) | |
161 }) | |
162 | |
163 Convey("errors", func() { | |
164 Convey("too many values", func() { | |
165 pm := DSPropertyMap{ | |
166 "$bad": {MkDSProperty(100), MkDS
Property(200)}, | |
167 } | |
168 _, err := pm.GetMeta("bad") | |
169 So(err.Error(), ShouldContainSubstring,
"too many values") | |
170 }) | |
171 | |
172 Convey("weird value", func() { | |
173 pm := DSPropertyMap{} | |
174 err := pm.SetMeta("sup", complex(100, 20
)) | |
175 So(err.Error(), ShouldContainSubstring,
"bad type") | |
176 }) | |
177 }) | |
178 }) | |
179 }) | |
180 } | |
OLD | NEW |