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 rawdatastore | |
6 | |
7 import ( | |
8 "math" | |
9 "testing" | |
10 "time" | |
11 | |
12 "github.com/luci/gae/service/blobstore" | |
13 . "github.com/smartystreets/goconvey/convey" | |
14 ) | |
15 | |
16 type myint int | |
17 type mybool bool | |
18 type mystring string | |
19 type myfloat float32 | |
20 | |
21 func TestProperties(t *testing.T) { | |
22 t.Parallel() | |
23 | |
24 Convey("Test Property", t, func() { | |
25 Convey("Construction", func() { | |
26 Convey("empty", func() { | |
27 pv := Property{} | |
28 So(pv.Value(), ShouldBeNil) | |
29 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
30 So(pv.Type().String(), ShouldEqual, "PTNull") | |
31 }) | |
32 Convey("set", func() { | |
33 pv := MkPropertyNI(100) | |
34 So(pv.Value(), ShouldHaveSameTypeAs, int64(100)) | |
35 So(pv.Value(), ShouldEqual, 100) | |
36 So(pv.IndexSetting(), ShouldEqual, NoIndex) | |
37 So(pv.Type().String(), ShouldEqual, "PTInt") | |
38 | |
39 pv.SetValue(nil, ShouldIndex) | |
40 So(pv.Value(), ShouldBeNil) | |
41 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
42 So(pv.Type().String(), ShouldEqual, "PTNull") | |
43 }) | |
44 Convey("derived types", func() { | |
45 Convey("int", func() { | |
46 pv := MkProperty(19) | |
47 So(pv.Value(), ShouldHaveSameTypeAs, int
64(19)) | |
48 So(pv.Value(), ShouldEqual, 19) | |
49 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
50 So(pv.Type().String(), ShouldEqual, "PTI
nt") | |
51 }) | |
52 Convey("bool (true)", func() { | |
53 pv := MkProperty(mybool(true)) | |
54 So(pv.Value(), ShouldBeTrue) | |
55 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
56 So(pv.Type().String(), ShouldEqual, "PTB
oolTrue") | |
57 }) | |
58 Convey("string", func() { | |
59 pv := MkProperty(mystring("sup")) | |
60 So(pv.Value(), ShouldEqual, "sup") | |
61 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
62 So(pv.Type().String(), ShouldEqual, "PTS
tring") | |
63 }) | |
64 Convey("blobstore.Key is distinquished", func()
{ | |
65 pv := MkProperty(blobstore.Key("sup")) | |
66 So(pv.Value(), ShouldEqual, blobstore.Ke
y("sup")) | |
67 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
68 So(pv.Type().String(), ShouldEqual, "PTB
lobKey") | |
69 }) | |
70 Convey("float", func() { | |
71 pv := Property{} | |
72 pv.SetValue(myfloat(19.7), ShouldIndex) | |
73 So(pv.Value(), ShouldHaveSameTypeAs, flo
at64(19.7)) | |
74 So(pv.Value(), ShouldEqual, float32(19.7
)) | |
75 So(pv.IndexSetting(), ShouldEqual, Shoul
dIndex) | |
76 So(pv.Type().String(), ShouldEqual, "PTF
loat") | |
77 }) | |
78 }) | |
79 Convey("bad type", func() { | |
80 pv := Property{} | |
81 err := pv.SetValue(complex(100, 29), ShouldIndex
) | |
82 So(err.Error(), ShouldContainSubstring, "has bad
type complex") | |
83 So(pv.Value(), ShouldBeNil) | |
84 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
85 So(pv.Type().String(), ShouldEqual, "PTNull") | |
86 }) | |
87 Convey("invalid GeoPoint", func() { | |
88 pv := Property{} | |
89 err := pv.SetValue(GeoPoint{-1000, 0}, ShouldInd
ex) | |
90 So(err.Error(), ShouldContainSubstring, "invalid
GeoPoint value") | |
91 So(pv.Value(), ShouldBeNil) | |
92 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
93 So(pv.Type().String(), ShouldEqual, "PTNull") | |
94 }) | |
95 Convey("invalid time", func() { | |
96 pv := Property{} | |
97 err := pv.SetValue(time.Now(), ShouldIndex) | |
98 So(err.Error(), ShouldContainSubstring, "time va
lue has wrong Location") | |
99 | |
100 err = pv.SetValue(time.Unix(math.MaxInt64, 0).UT
C(), ShouldIndex) | |
101 So(err.Error(), ShouldContainSubstring, "time va
lue out of range") | |
102 So(pv.Value(), ShouldBeNil) | |
103 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
104 So(pv.Type().String(), ShouldEqual, "PTNull") | |
105 }) | |
106 Convey("time gets rounded", func() { | |
107 pv := Property{} | |
108 now := time.Now().In(time.UTC) | |
109 now = now.Round(time.Microsecond).Add(time.Nanos
econd * 313) | |
110 pv.SetValue(now, ShouldIndex) | |
111 So(pv.Value(), ShouldHappenBefore, now) | |
112 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
113 So(pv.Type().String(), ShouldEqual, "PTTime") | |
114 }) | |
115 Convey("[]byte coerces IndexSetting", func() { | |
116 pv := Property{} | |
117 pv.SetValue([]byte("hello"), ShouldIndex) | |
118 So(pv.Value(), ShouldResemble, []byte("hello")) | |
119 So(pv.IndexSetting(), ShouldEqual, NoIndex) | |
120 So(pv.Type().String(), ShouldEqual, "PTBytes") | |
121 }) | |
122 Convey("ByteString allows !IndexSetting", func() { | |
123 pv := Property{} | |
124 pv.SetValue(ByteString("hello"), ShouldIndex) | |
125 So(pv.Value(), ShouldResemble, ByteString("hello
")) | |
126 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) | |
127 So(pv.Type().String(), ShouldEqual, "PTBytes") | |
128 }) | |
129 }) | |
130 }) | |
131 } | |
132 | |
133 func TestDSPropertyMapImpl(t *testing.T) { | |
134 t.Parallel() | |
135 | |
136 Convey("PropertyMap load/save err conditions", t, func() { | |
137 Convey("empty", func() { | |
138 pm := PropertyMap{} | |
139 err := pm.Load(PropertyMap{"hello": {Property{}}}) | |
140 So(err, ShouldBeNil) | |
141 So(pm, ShouldResemble, PropertyMap{"hello": {Property{}}
}) | |
142 | |
143 npm, _ := pm.Save(false) | |
144 So(npm, ShouldResemble, pm) | |
145 }) | |
146 Convey("meta", func() { | |
147 Convey("working", func() { | |
148 pm := PropertyMap{"": {MkProperty("trap!")}} | |
149 _, err := pm.GetMeta("foo") | |
150 So(err, ShouldEqual, ErrMetaFieldUnset) | |
151 | |
152 err = pm.SetMeta("foo", 100) | |
153 So(err, ShouldBeNil) | |
154 | |
155 v, err := pm.GetMeta("foo") | |
156 So(err, ShouldBeNil) | |
157 So(v, ShouldEqual, 100) | |
158 | |
159 npm, err := pm.Save(false) | |
160 So(err, ShouldBeNil) | |
161 So(len(npm), ShouldEqual, 1) | |
162 }) | |
163 | |
164 Convey("errors", func() { | |
165 Convey("too many values", func() { | |
166 pm := PropertyMap{ | |
167 "$bad": {MkProperty(100), MkProp
erty(200)}, | |
168 } | |
169 _, err := pm.GetMeta("bad") | |
170 So(err.Error(), ShouldContainSubstring,
"too many values") | |
171 }) | |
172 | |
173 Convey("weird value", func() { | |
174 pm := PropertyMap{} | |
175 err := pm.SetMeta("sup", complex(100, 20
)) | |
176 So(err.Error(), ShouldContainSubstring,
"bad type") | |
177 }) | |
178 }) | |
179 }) | |
180 }) | |
181 } | |
OLD | NEW |