Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: service/rawdatastore/properties_test.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « service/rawdatastore/properties.go ('k') | service/rawdatastore/reflect.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package gae 5 package rawdatastore
6 6
7 import ( 7 import (
8 "math" 8 "math"
9 "testing" 9 "testing"
10 "time" 10 "time"
11 11
12 "github.com/luci/gae/service/blobstore"
12 . "github.com/smartystreets/goconvey/convey" 13 . "github.com/smartystreets/goconvey/convey"
13 ) 14 )
14 15
15 type myint int 16 type myint int
16 type mybool bool 17 type mybool bool
17 type mystring string 18 type mystring string
18 type myfloat float32 19 type myfloat float32
19 20
20 func TestProperties(t *testing.T) { 21 func TestProperties(t *testing.T) {
21 t.Parallel() 22 t.Parallel()
22 23
23 » Convey("Test DSProperty", t, func() { 24 » Convey("Test Property", t, func() {
24 Convey("Construction", func() { 25 Convey("Construction", func() {
25 Convey("empty", func() { 26 Convey("empty", func() {
26 » » » » pv := DSProperty{} 27 » » » » pv := Property{}
27 So(pv.Value(), ShouldBeNil) 28 So(pv.Value(), ShouldBeNil)
28 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 29 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
29 » » » » So(pv.Type().String(), ShouldEqual, "DSPTNull") 30 » » » » So(pv.Type().String(), ShouldEqual, "PTNull")
30 }) 31 })
31 Convey("set", func() { 32 Convey("set", func() {
32 » » » » pv := MkDSPropertyNI(100) 33 » » » » pv := MkPropertyNI(100)
33 So(pv.Value(), ShouldHaveSameTypeAs, int64(100)) 34 So(pv.Value(), ShouldHaveSameTypeAs, int64(100))
34 So(pv.Value(), ShouldEqual, 100) 35 So(pv.Value(), ShouldEqual, 100)
35 So(pv.IndexSetting(), ShouldEqual, NoIndex) 36 So(pv.IndexSetting(), ShouldEqual, NoIndex)
36 » » » » So(pv.Type().String(), ShouldEqual, "DSPTInt") 37 » » » » So(pv.Type().String(), ShouldEqual, "PTInt")
37 38
38 pv.SetValue(nil, ShouldIndex) 39 pv.SetValue(nil, ShouldIndex)
39 So(pv.Value(), ShouldBeNil) 40 So(pv.Value(), ShouldBeNil)
40 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 41 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
41 » » » » So(pv.Type().String(), ShouldEqual, "DSPTNull") 42 » » » » So(pv.Type().String(), ShouldEqual, "PTNull")
42 }) 43 })
43 Convey("derived types", func() { 44 Convey("derived types", func() {
44 Convey("int", func() { 45 Convey("int", func() {
45 » » » » » pv := MkDSProperty(19) 46 » » » » » pv := MkProperty(19)
46 So(pv.Value(), ShouldHaveSameTypeAs, int 64(19)) 47 So(pv.Value(), ShouldHaveSameTypeAs, int 64(19))
47 So(pv.Value(), ShouldEqual, 19) 48 So(pv.Value(), ShouldEqual, 19)
48 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex) 49 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex)
49 » » » » » So(pv.Type().String(), ShouldEqual, "DSP TInt") 50 » » » » » So(pv.Type().String(), ShouldEqual, "PTI nt")
50 }) 51 })
51 Convey("bool (true)", func() { 52 Convey("bool (true)", func() {
52 » » » » » pv := MkDSProperty(mybool(true)) 53 » » » » » pv := MkProperty(mybool(true))
53 So(pv.Value(), ShouldBeTrue) 54 So(pv.Value(), ShouldBeTrue)
54 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex) 55 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex)
55 » » » » » So(pv.Type().String(), ShouldEqual, "DSP TBoolTrue") 56 » » » » » So(pv.Type().String(), ShouldEqual, "PTB oolTrue")
56 }) 57 })
57 Convey("string", func() { 58 Convey("string", func() {
58 » » » » » pv := MkDSProperty(mystring("sup")) 59 » » » » » pv := MkProperty(mystring("sup"))
59 So(pv.Value(), ShouldEqual, "sup") 60 So(pv.Value(), ShouldEqual, "sup")
60 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex) 61 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex)
61 » » » » » So(pv.Type().String(), ShouldEqual, "DSP TString") 62 » » » » » So(pv.Type().String(), ShouldEqual, "PTS tring")
62 }) 63 })
63 » » » » Convey("BSKey is distinquished", func() { 64 » » » » Convey("blobstore.Key is distinquished", func() {
64 » » » » » pv := MkDSProperty(BSKey("sup")) 65 » » » » » pv := MkProperty(blobstore.Key("sup"))
65 » » » » » So(pv.Value(), ShouldEqual, BSKey("sup") ) 66 » » » » » So(pv.Value(), ShouldEqual, blobstore.Ke y("sup"))
66 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex) 67 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex)
67 » » » » » So(pv.Type().String(), ShouldEqual, "DSP TBlobKey") 68 » » » » » So(pv.Type().String(), ShouldEqual, "PTB lobKey")
68 }) 69 })
69 Convey("float", func() { 70 Convey("float", func() {
70 » » » » » pv := DSProperty{} 71 » » » » » pv := Property{}
71 pv.SetValue(myfloat(19.7), ShouldIndex) 72 pv.SetValue(myfloat(19.7), ShouldIndex)
72 So(pv.Value(), ShouldHaveSameTypeAs, flo at64(19.7)) 73 So(pv.Value(), ShouldHaveSameTypeAs, flo at64(19.7))
73 So(pv.Value(), ShouldEqual, float32(19.7 )) 74 So(pv.Value(), ShouldEqual, float32(19.7 ))
74 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex) 75 So(pv.IndexSetting(), ShouldEqual, Shoul dIndex)
75 » » » » » So(pv.Type().String(), ShouldEqual, "DSP TFloat") 76 » » » » » So(pv.Type().String(), ShouldEqual, "PTF loat")
76 }) 77 })
77 }) 78 })
78 Convey("bad type", func() { 79 Convey("bad type", func() {
79 » » » » pv := DSProperty{} 80 » » » » pv := Property{}
80 err := pv.SetValue(complex(100, 29), ShouldIndex ) 81 err := pv.SetValue(complex(100, 29), ShouldIndex )
81 So(err.Error(), ShouldContainSubstring, "has bad type complex") 82 So(err.Error(), ShouldContainSubstring, "has bad type complex")
82 So(pv.Value(), ShouldBeNil) 83 So(pv.Value(), ShouldBeNil)
83 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 84 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
84 » » » » So(pv.Type().String(), ShouldEqual, "DSPTNull") 85 » » » » So(pv.Type().String(), ShouldEqual, "PTNull")
85 }) 86 })
86 Convey("invalid GeoPoint", func() { 87 Convey("invalid GeoPoint", func() {
87 » » » » pv := DSProperty{} 88 » » » » pv := Property{}
88 » » » » err := pv.SetValue(DSGeoPoint{-1000, 0}, ShouldI ndex) 89 » » » » err := pv.SetValue(GeoPoint{-1000, 0}, ShouldInd ex)
89 So(err.Error(), ShouldContainSubstring, "invalid GeoPoint value") 90 So(err.Error(), ShouldContainSubstring, "invalid GeoPoint value")
90 So(pv.Value(), ShouldBeNil) 91 So(pv.Value(), ShouldBeNil)
91 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 92 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
92 » » » » So(pv.Type().String(), ShouldEqual, "DSPTNull") 93 » » » » So(pv.Type().String(), ShouldEqual, "PTNull")
93 }) 94 })
94 Convey("invalid time", func() { 95 Convey("invalid time", func() {
95 » » » » pv := DSProperty{} 96 » » » » pv := Property{}
96 err := pv.SetValue(time.Now(), ShouldIndex) 97 err := pv.SetValue(time.Now(), ShouldIndex)
97 So(err.Error(), ShouldContainSubstring, "time va lue has wrong Location") 98 So(err.Error(), ShouldContainSubstring, "time va lue has wrong Location")
98 99
99 err = pv.SetValue(time.Unix(math.MaxInt64, 0).UT C(), ShouldIndex) 100 err = pv.SetValue(time.Unix(math.MaxInt64, 0).UT C(), ShouldIndex)
100 So(err.Error(), ShouldContainSubstring, "time va lue out of range") 101 So(err.Error(), ShouldContainSubstring, "time va lue out of range")
101 So(pv.Value(), ShouldBeNil) 102 So(pv.Value(), ShouldBeNil)
102 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 103 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
103 » » » » So(pv.Type().String(), ShouldEqual, "DSPTNull") 104 » » » » So(pv.Type().String(), ShouldEqual, "PTNull")
104 }) 105 })
105 Convey("time gets rounded", func() { 106 Convey("time gets rounded", func() {
106 » » » » pv := DSProperty{} 107 » » » » pv := Property{}
107 now := time.Now().In(time.UTC) 108 now := time.Now().In(time.UTC)
108 now = now.Round(time.Microsecond).Add(time.Nanos econd * 313) 109 now = now.Round(time.Microsecond).Add(time.Nanos econd * 313)
109 pv.SetValue(now, ShouldIndex) 110 pv.SetValue(now, ShouldIndex)
110 So(pv.Value(), ShouldHappenBefore, now) 111 So(pv.Value(), ShouldHappenBefore, now)
111 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 112 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
112 » » » » So(pv.Type().String(), ShouldEqual, "DSPTTime") 113 » » » » So(pv.Type().String(), ShouldEqual, "PTTime")
113 }) 114 })
114 Convey("[]byte coerces IndexSetting", func() { 115 Convey("[]byte coerces IndexSetting", func() {
115 » » » » pv := DSProperty{} 116 » » » » pv := Property{}
116 pv.SetValue([]byte("hello"), ShouldIndex) 117 pv.SetValue([]byte("hello"), ShouldIndex)
117 So(pv.Value(), ShouldResemble, []byte("hello")) 118 So(pv.Value(), ShouldResemble, []byte("hello"))
118 So(pv.IndexSetting(), ShouldEqual, NoIndex) 119 So(pv.IndexSetting(), ShouldEqual, NoIndex)
119 » » » » So(pv.Type().String(), ShouldEqual, "DSPTBytes") 120 » » » » So(pv.Type().String(), ShouldEqual, "PTBytes")
120 }) 121 })
121 Convey("ByteString allows !IndexSetting", func() { 122 Convey("ByteString allows !IndexSetting", func() {
122 » » » » pv := DSProperty{} 123 » » » » pv := Property{}
123 » » » » pv.SetValue(DSByteString("hello"), ShouldIndex) 124 » » » » pv.SetValue(ByteString("hello"), ShouldIndex)
124 » » » » So(pv.Value(), ShouldResemble, DSByteString("hel lo")) 125 » » » » So(pv.Value(), ShouldResemble, ByteString("hello "))
125 So(pv.IndexSetting(), ShouldEqual, ShouldIndex) 126 So(pv.IndexSetting(), ShouldEqual, ShouldIndex)
126 » » » » So(pv.Type().String(), ShouldEqual, "DSPTBytes") 127 » » » » So(pv.Type().String(), ShouldEqual, "PTBytes")
127 }) 128 })
128 }) 129 })
129 }) 130 })
130 } 131 }
131 132
132 func TestDSPropertyMapImpl(t *testing.T) { 133 func TestDSPropertyMapImpl(t *testing.T) {
133 t.Parallel() 134 t.Parallel()
134 135
135 » Convey("DSPropertyMap load/save err conditions", t, func() { 136 » Convey("PropertyMap load/save err conditions", t, func() {
136 Convey("empty", func() { 137 Convey("empty", func() {
137 » » » pm := DSPropertyMap{} 138 » » » pm := PropertyMap{}
138 » » » err := pm.Load(DSPropertyMap{"hello": {DSProperty{}}}) 139 » » » err := pm.Load(PropertyMap{"hello": {Property{}}})
139 So(err, ShouldBeNil) 140 So(err, ShouldBeNil)
140 » » » So(pm, ShouldResemble, DSPropertyMap{"hello": {DSPropert y{}}}) 141 » » » So(pm, ShouldResemble, PropertyMap{"hello": {Property{}} })
141 142
142 npm, _ := pm.Save(false) 143 npm, _ := pm.Save(false)
143 So(npm, ShouldResemble, pm) 144 So(npm, ShouldResemble, pm)
144 }) 145 })
145 Convey("meta", func() { 146 Convey("meta", func() {
146 Convey("working", func() { 147 Convey("working", func() {
147 » » » » pm := DSPropertyMap{"": {MkDSProperty("trap!")}} 148 » » » » pm := PropertyMap{"": {MkProperty("trap!")}}
148 _, err := pm.GetMeta("foo") 149 _, err := pm.GetMeta("foo")
149 » » » » So(err, ShouldEqual, ErrDSMetaFieldUnset) 150 » » » » So(err, ShouldEqual, ErrMetaFieldUnset)
150 151
151 err = pm.SetMeta("foo", 100) 152 err = pm.SetMeta("foo", 100)
152 So(err, ShouldBeNil) 153 So(err, ShouldBeNil)
153 154
154 v, err := pm.GetMeta("foo") 155 v, err := pm.GetMeta("foo")
155 So(err, ShouldBeNil) 156 So(err, ShouldBeNil)
156 So(v, ShouldEqual, 100) 157 So(v, ShouldEqual, 100)
157 158
158 npm, err := pm.Save(false) 159 npm, err := pm.Save(false)
159 So(err, ShouldBeNil) 160 So(err, ShouldBeNil)
160 So(len(npm), ShouldEqual, 1) 161 So(len(npm), ShouldEqual, 1)
161 }) 162 })
162 163
163 Convey("errors", func() { 164 Convey("errors", func() {
164 Convey("too many values", func() { 165 Convey("too many values", func() {
165 » » » » » pm := DSPropertyMap{ 166 » » » » » pm := PropertyMap{
166 » » » » » » "$bad": {MkDSProperty(100), MkDS Property(200)}, 167 » » » » » » "$bad": {MkProperty(100), MkProp erty(200)},
167 } 168 }
168 _, err := pm.GetMeta("bad") 169 _, err := pm.GetMeta("bad")
169 So(err.Error(), ShouldContainSubstring, "too many values") 170 So(err.Error(), ShouldContainSubstring, "too many values")
170 }) 171 })
171 172
172 Convey("weird value", func() { 173 Convey("weird value", func() {
173 » » » » » pm := DSPropertyMap{} 174 » » » » » pm := PropertyMap{}
174 err := pm.SetMeta("sup", complex(100, 20 )) 175 err := pm.SetMeta("sup", complex(100, 20 ))
175 So(err.Error(), ShouldContainSubstring, "bad type") 176 So(err.Error(), ShouldContainSubstring, "bad type")
176 }) 177 })
177 }) 178 })
178 }) 179 })
179 }) 180 })
180 } 181 }
OLDNEW
« no previous file with comments | « service/rawdatastore/properties.go ('k') | service/rawdatastore/reflect.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698