OLD | NEW |
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 // adapted from github.com/golang/appengine/datastore | 5 // adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package datastore | 7 package datastore |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 109 } |
110 if KeyIncomplete(k) { | 110 if KeyIncomplete(k) { |
111 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "
", int64(i+1), k.Parent()) | 111 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "
", int64(i+1), k.Parent()) |
112 } | 112 } |
113 } | 113 } |
114 cb(k, err) | 114 cb(k, err) |
115 } | 115 } |
116 return nil | 116 return nil |
117 } | 117 } |
118 | 118 |
119 func (f *fakeDatastore) GetMulti(keys []Key, cb GetMultiCB) error { | 119 func (f *fakeDatastore) GetMulti(keys []Key, _meta MultiMetaGetter, cb GetMultiC
B) error { |
120 if keys[0].Kind() == "FailAll" { | 120 if keys[0].Kind() == "FailAll" { |
121 return errors.New("GetMulti fail all") | 121 return errors.New("GetMulti fail all") |
122 } | 122 } |
123 for i, k := range keys { | 123 for i, k := range keys { |
124 if k.Kind() == "Fail" { | 124 if k.Kind() == "Fail" { |
125 cb(nil, errors.New("GetMulti fail")) | 125 cb(nil, errors.New("GetMulti fail")) |
126 } else { | 126 } else { |
127 cb(PropertyMap{"Value": {MkProperty(i + 1)}}, nil) | 127 cb(PropertyMap{"Value": {MkProperty(i + 1)}}, nil) |
128 } | 128 } |
129 } | 129 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 } | 165 } |
166 | 166 |
167 type FakePLS struct { | 167 type FakePLS struct { |
168 IntID int64 | 168 IntID int64 |
169 StringID string | 169 StringID string |
170 Kind string | 170 Kind string |
171 | 171 |
172 Value int64 | 172 Value int64 |
173 gotLoaded bool | 173 gotLoaded bool |
174 | 174 |
175 » failGetMeta bool | 175 » failGetMeta bool |
176 » failLoad bool | 176 » failLoad bool |
177 » failProblem bool | 177 » failProblem bool |
178 » failSave bool | 178 » failSave bool |
179 » failSaveMeta bool | 179 » failSetMeta bool |
180 } | 180 } |
181 | 181 |
| 182 var _ PropertyLoadSaver = (*FakePLS)(nil) |
| 183 |
182 func (f *FakePLS) Load(pm PropertyMap) error { | 184 func (f *FakePLS) Load(pm PropertyMap) error { |
183 if f.failLoad { | 185 if f.failLoad { |
184 return errors.New("FakePLS.Load") | 186 return errors.New("FakePLS.Load") |
185 } | 187 } |
186 f.gotLoaded = true | 188 f.gotLoaded = true |
187 f.Value = pm["Value"][0].Value().(int64) | 189 f.Value = pm["Value"][0].Value().(int64) |
188 return nil | 190 return nil |
189 } | 191 } |
190 | 192 |
191 func (f *FakePLS) Save(withMeta bool) (PropertyMap, error) { | 193 func (f *FakePLS) Save(withMeta bool) (PropertyMap, error) { |
(...skipping 10 matching lines...) Expand all Loading... |
202 if f.Kind == "" { | 204 if f.Kind == "" { |
203 ret.SetMeta("kind", "FakePLS") | 205 ret.SetMeta("kind", "FakePLS") |
204 } else { | 206 } else { |
205 ret.SetMeta("kind", f.Kind) | 207 ret.SetMeta("kind", f.Kind) |
206 } | 208 } |
207 ret.SetMeta("assertExtra", true) | 209 ret.SetMeta("assertExtra", true) |
208 } | 210 } |
209 return ret, nil | 211 return ret, nil |
210 } | 212 } |
211 | 213 |
| 214 func (f *FakePLS) GetMetaDefault(key string, dflt interface{}) interface{} { |
| 215 return GetMetaDefaultImpl(f.GetMeta, key, dflt) |
| 216 } |
| 217 |
212 func (f *FakePLS) GetMeta(key string) (interface{}, error) { | 218 func (f *FakePLS) GetMeta(key string) (interface{}, error) { |
213 if f.failGetMeta { | 219 if f.failGetMeta { |
214 return nil, errors.New("FakePLS.GetMeta") | 220 return nil, errors.New("FakePLS.GetMeta") |
215 } | 221 } |
216 switch key { | 222 switch key { |
217 case "id": | 223 case "id": |
218 if f.StringID != "" { | 224 if f.StringID != "" { |
219 return f.StringID, nil | 225 return f.StringID, nil |
220 } | 226 } |
221 return f.IntID, nil | 227 return f.IntID, nil |
222 case "kind": | 228 case "kind": |
223 if f.Kind == "" { | 229 if f.Kind == "" { |
224 return "FakePLS", nil | 230 return "FakePLS", nil |
225 } | 231 } |
226 return f.Kind, nil | 232 return f.Kind, nil |
227 } | 233 } |
228 return nil, ErrMetaFieldUnset | 234 return nil, ErrMetaFieldUnset |
229 } | 235 } |
230 | 236 |
231 func (f *FakePLS) SetMeta(key string, val interface{}) error { | 237 func (f *FakePLS) SetMeta(key string, val interface{}) error { |
232 » if f.failSaveMeta { | 238 » if f.failSetMeta { |
233 return errors.New("FakePL.SetMeta") | 239 return errors.New("FakePL.SetMeta") |
234 } | 240 } |
235 if key == "id" { | 241 if key == "id" { |
236 switch x := val.(type) { | 242 switch x := val.(type) { |
237 case int64: | 243 case int64: |
238 f.IntID = x | 244 f.IntID = x |
239 case string: | 245 case string: |
240 f.StringID = x | 246 f.StringID = x |
241 } | 247 } |
242 return nil | 248 return nil |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 | 601 |
596 Convey("get multi error for individual failures", func()
{ | 602 Convey("get multi error for individual failures", func()
{ |
597 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} | 603 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} |
598 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) | 604 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) |
599 }) | 605 }) |
600 | 606 |
601 Convey("get with non-modifiable type is an error", func(
) { | 607 Convey("get with non-modifiable type is an error", func(
) { |
602 cs := CommonStruct{} | 608 cs := CommonStruct{} |
603 So(ds.Get(cs).Error(), ShouldContainSubstring, "
invalid Get input type") | 609 So(ds.Get(cs).Error(), ShouldContainSubstring, "
invalid Get input type") |
604 }) | 610 }) |
| 611 |
| 612 Convey("failure to save metadata is an issue too", func(
) { |
| 613 cs := &FakePLS{failSave: true} |
| 614 So(ds.Get(cs).Error(), ShouldContainSubstring, "
FakePLS.Save") |
| 615 }) |
605 }) | 616 }) |
606 | 617 |
607 Convey("ok", func() { | 618 Convey("ok", func() { |
608 Convey("Get", func() { | 619 Convey("Get", func() { |
609 cs := &CommonStruct{ID: 1} | 620 cs := &CommonStruct{ID: 1} |
610 So(ds.Get(cs), ShouldBeNil) | 621 So(ds.Get(cs), ShouldBeNil) |
611 So(cs.Value, ShouldEqual, 1) | 622 So(cs.Value, ShouldEqual, 1) |
612 }) | 623 }) |
613 | 624 |
614 Convey("Raw access too", func() { | 625 Convey("Raw access too", func() { |
615 rds := ds.Raw() | 626 rds := ds.Raw() |
616 » » » » So(rds.GetMulti([]Key{rds.NewKey("Kind", "", 1,
nil)}, func(pm PropertyMap, err error) { | 627 » » » » keys := []Key{rds.NewKey("Kind", "", 1, nil)} |
| 628 » » » » So(rds.GetMulti(keys, nil, func(pm PropertyMap,
err error) { |
617 So(err, ShouldBeNil) | 629 So(err, ShouldBeNil) |
618 So(pm["Value"][0].Value(), ShouldEqual,
1) | 630 So(pm["Value"][0].Value(), ShouldEqual,
1) |
619 }), ShouldBeNil) | 631 }), ShouldBeNil) |
620 }) | 632 }) |
621 }) | 633 }) |
622 | 634 |
623 }) | 635 }) |
624 } | 636 } |
625 | 637 |
626 func TestGetAll(t *testing.T) { | 638 func TestGetAll(t *testing.T) { |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 So(ds.Run(q, (*Key)(nil), func(obj interface{},
_ func() (Cursor, error)) bool { | 864 So(ds.Run(q, (*Key)(nil), func(obj interface{},
_ func() (Cursor, error)) bool { |
853 So(obj.(Key).IntID(), ShouldEqual, i+1) | 865 So(obj.(Key).IntID(), ShouldEqual, i+1) |
854 i++ | 866 i++ |
855 return true | 867 return true |
856 }), ShouldBeNil) | 868 }), ShouldBeNil) |
857 }) | 869 }) |
858 | 870 |
859 }) | 871 }) |
860 }) | 872 }) |
861 } | 873 } |
OLD | NEW |