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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return f.IntID, nil | 194 return f.IntID, nil |
195 case "kind": | 195 case "kind": |
196 if f.Kind == "" { | 196 if f.Kind == "" { |
197 return "FakePLS", nil | 197 return "FakePLS", nil |
198 } | 198 } |
199 return f.Kind, nil | 199 return f.Kind, nil |
200 } | 200 } |
201 return nil, ErrMetaFieldUnset | 201 return nil, ErrMetaFieldUnset |
202 } | 202 } |
203 | 203 |
| 204 func (f *FakePLS) GetAllMeta() PropertyMap { |
| 205 ret := PropertyMap{} |
| 206 if id, err := f.GetMeta("id"); err != nil { |
| 207 So(ret.SetMeta("id", id), ShouldBeNil) |
| 208 } |
| 209 if kind, err := f.GetMeta("kind"); err != nil { |
| 210 So(ret.SetMeta("kind", kind), ShouldBeNil) |
| 211 } |
| 212 return ret |
| 213 } |
| 214 |
204 func (f *FakePLS) SetMeta(key string, val interface{}) error { | 215 func (f *FakePLS) SetMeta(key string, val interface{}) error { |
205 if f.failSetMeta { | 216 if f.failSetMeta { |
206 return errors.New("FakePL.SetMeta") | 217 return errors.New("FakePL.SetMeta") |
207 } | 218 } |
208 if key == "id" { | 219 if key == "id" { |
209 switch x := val.(type) { | 220 switch x := val.(type) { |
210 case int64: | 221 case int64: |
211 f.IntID = x | 222 f.IntID = x |
212 case string: | 223 case string: |
213 f.StringID = x | 224 f.StringID = x |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} | 581 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} |
571 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) | 582 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) |
572 }) | 583 }) |
573 | 584 |
574 Convey("get with non-modifiable type is an error", func(
) { | 585 Convey("get with non-modifiable type is an error", func(
) { |
575 cs := CommonStruct{} | 586 cs := CommonStruct{} |
576 So(ds.Get(cs).Error(), ShouldContainSubstring, "
invalid Get input type") | 587 So(ds.Get(cs).Error(), ShouldContainSubstring, "
invalid Get input type") |
577 }) | 588 }) |
578 | 589 |
579 Convey("failure to save metadata is an issue too", func(
) { | 590 Convey("failure to save metadata is an issue too", func(
) { |
580 » » » » cs := &FakePLS{failSave: true} | 591 » » » » cs := &FakePLS{failGetMeta: true} |
581 » » » » So(ds.Get(cs).Error(), ShouldContainSubstring, "
FakePLS.Save") | 592 » » » » So(ds.Get(cs).Error(), ShouldContainSubstring, "
unable to extract $kind") |
582 }) | 593 }) |
583 }) | 594 }) |
584 | 595 |
585 Convey("ok", func() { | 596 Convey("ok", func() { |
586 Convey("Get", func() { | 597 Convey("Get", func() { |
587 cs := &CommonStruct{ID: 1} | 598 cs := &CommonStruct{ID: 1} |
588 So(ds.Get(cs), ShouldBeNil) | 599 So(ds.Get(cs), ShouldBeNil) |
589 So(cs.Value, ShouldEqual, 1) | 600 So(cs.Value, ShouldEqual, 1) |
590 }) | 601 }) |
591 | 602 |
592 Convey("Raw access too", func() { | 603 Convey("Raw access too", func() { |
593 rds := ds.Raw() | 604 rds := ds.Raw() |
594 keys := []*Key{ds.MakeKey("Kind", 1)} | 605 keys := []*Key{ds.MakeKey("Kind", 1)} |
595 So(rds.GetMulti(keys, nil, func(pm PropertyMap,
err error) { | 606 So(rds.GetMulti(keys, nil, func(pm PropertyMap,
err error) { |
596 So(err, ShouldBeNil) | 607 So(err, ShouldBeNil) |
597 So(pm["Value"][0].Value(), ShouldEqual,
1) | 608 So(pm["Value"][0].Value(), ShouldEqual,
1) |
598 }), ShouldBeNil) | 609 }), ShouldBeNil) |
599 }) | 610 }) |
| 611 |
| 612 Convey("but general failure to save is fine on a Get", f
unc() { |
| 613 cs := &FakePLS{failSave: true, IntID: 7} |
| 614 So(ds.Get(cs), ShouldBeNil) |
| 615 }) |
600 }) | 616 }) |
601 | 617 |
602 }) | 618 }) |
603 } | 619 } |
604 | 620 |
605 func TestGetAll(t *testing.T) { | 621 func TestGetAll(t *testing.T) { |
606 t.Parallel() | 622 t.Parallel() |
607 | 623 |
608 Convey("Test GetAll", t, func() { | 624 Convey("Test GetAll", t, func() { |
609 c := info.Set(context.Background(), fakeInfo{}) | 625 c := info.Set(context.Background(), fakeInfo{}) |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 So(ds.Run(q, func(k *Key, _ CursorCB) bool { | 873 So(ds.Run(q, func(k *Key, _ CursorCB) bool { |
858 So(k.Last().IntID, ShouldEqual, i+1) | 874 So(k.Last().IntID, ShouldEqual, i+1) |
859 i++ | 875 i++ |
860 return true | 876 return true |
861 }), ShouldBeNil) | 877 }), ShouldBeNil) |
862 }) | 878 }) |
863 | 879 |
864 }) | 880 }) |
865 }) | 881 }) |
866 } | 882 } |
OLD | NEW |