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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 } | 51 } |
52 pm := PropertyMap{"Value": {MkProperty(i)}} | 52 pm := PropertyMap{"Value": {MkProperty(i)}} |
53 if !cb(k, pm, nil) { | 53 if !cb(k, pm, nil) { |
54 break | 54 break |
55 } | 55 } |
56 } | 56 } |
57 return nil | 57 return nil |
58 } | 58 } |
59 | 59 |
60 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { | 60 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { |
61 » if keys[0].Last().Kind == "FailAll" { | 61 » if keys[0].Kind() == "FailAll" { |
62 return errors.New("PutMulti fail all") | 62 return errors.New("PutMulti fail all") |
63 } | 63 } |
64 assertExtra := false | 64 assertExtra := false |
65 if _, err := vals[0].GetMeta("assertExtra"); err == nil { | 65 if _, err := vals[0].GetMeta("assertExtra"); err == nil { |
66 assertExtra = true | 66 assertExtra = true |
67 } | 67 } |
68 for i, k := range keys { | 68 for i, k := range keys { |
69 err := error(nil) | 69 err := error(nil) |
70 » » if k.Last().Kind == "Fail" { | 70 » » if k.Kind() == "Fail" { |
71 err = errors.New("PutMulti fail") | 71 err = errors.New("PutMulti fail") |
72 } else { | 72 } else { |
73 So(vals[i]["Value"], ShouldResemble, []Property{MkProper
ty(i)}) | 73 So(vals[i]["Value"], ShouldResemble, []Property{MkProper
ty(i)}) |
74 if assertExtra { | 74 if assertExtra { |
75 So(vals[i]["Extra"], ShouldResemble, []Property{
MkProperty("whoa")}) | 75 So(vals[i]["Extra"], ShouldResemble, []Property{
MkProperty("whoa")}) |
76 } | 76 } |
77 if k.Incomplete() { | 77 if k.Incomplete() { |
78 » » » » k = NewKey(k.AppID(), k.Namespace(), k.Last().Ki
nd, "", int64(i+1), k.Parent()) | 78 » » » » k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "
", int64(i+1), k.Parent()) |
79 } | 79 } |
80 } | 80 } |
81 cb(k, err) | 81 cb(k, err) |
82 } | 82 } |
83 return nil | 83 return nil |
84 } | 84 } |
85 | 85 |
86 func (f *fakeDatastore) GetMulti(keys []*Key, _meta MultiMetaGetter, cb GetMulti
CB) error { | 86 func (f *fakeDatastore) GetMulti(keys []*Key, _meta MultiMetaGetter, cb GetMulti
CB) error { |
87 » if keys[0].Last().Kind == "FailAll" { | 87 » if keys[0].Kind() == "FailAll" { |
88 return errors.New("GetMulti fail all") | 88 return errors.New("GetMulti fail all") |
89 } | 89 } |
90 for i, k := range keys { | 90 for i, k := range keys { |
91 » » if k.Last().Kind == "Fail" { | 91 » » if k.Kind() == "Fail" { |
92 cb(nil, errors.New("GetMulti fail")) | 92 cb(nil, errors.New("GetMulti fail")) |
93 } else { | 93 } else { |
94 cb(PropertyMap{"Value": {MkProperty(i + 1)}}, nil) | 94 cb(PropertyMap{"Value": {MkProperty(i + 1)}}, nil) |
95 } | 95 } |
96 } | 96 } |
97 return nil | 97 return nil |
98 } | 98 } |
99 | 99 |
100 func (f *fakeDatastore) DeleteMulti(keys []*Key, cb DeleteMultiCB) error { | 100 func (f *fakeDatastore) DeleteMulti(keys []*Key, cb DeleteMultiCB) error { |
101 » if keys[0].Last().Kind == "FailAll" { | 101 » if keys[0].Kind() == "FailAll" { |
102 return errors.New("DeleteMulti fail all") | 102 return errors.New("DeleteMulti fail all") |
103 } | 103 } |
104 for _, k := range keys { | 104 for _, k := range keys { |
105 » » if k.Last().Kind == "Fail" { | 105 » » if k.Kind() == "Fail" { |
106 cb(errors.New("DeleteMulti fail")) | 106 cb(errors.New("DeleteMulti fail")) |
107 } else { | 107 } else { |
108 cb(nil) | 108 cb(nil) |
109 } | 109 } |
110 } | 110 } |
111 return nil | 111 return nil |
112 } | 112 } |
113 | 113 |
114 type badStruct struct { | 114 type badStruct struct { |
115 ID int64 `gae:"$id"` | 115 ID int64 `gae:"$id"` |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 for i, fpls := range fplss { | 416 for i, fpls := range fplss { |
417 expect := int64(i + 1) | 417 expect := int64(i + 1) |
418 if i == 4 { | 418 if i == 4 { |
419 expect = 200 | 419 expect = 200 |
420 } | 420 } |
421 So(fpls.IntID, ShouldEqual, expect) | 421 So(fpls.IntID, ShouldEqual, expect) |
422 } | 422 } |
423 | 423 |
424 pm := PropertyMap{"Value": {MkProperty(0)}, "$ki
nd": {MkPropertyNI("Pmap")}} | 424 pm := PropertyMap{"Value": {MkProperty(0)}, "$ki
nd": {MkPropertyNI("Pmap")}} |
425 So(ds.Put(pm), ShouldBeNil) | 425 So(ds.Put(pm), ShouldBeNil) |
426 » » » » So(ds.KeyForObj(pm).Last().IntID, ShouldEqual, 1
) | 426 » » » » So(ds.KeyForObj(pm).IntID(), ShouldEqual, 1) |
427 }) | 427 }) |
428 | 428 |
429 Convey("[]P (map)", func() { | 429 Convey("[]P (map)", func() { |
430 pms := make([]PropertyMap, 7) | 430 pms := make([]PropertyMap, 7) |
431 for i := range pms { | 431 for i := range pms { |
432 pms[i] = PropertyMap{ | 432 pms[i] = PropertyMap{ |
433 "$kind": {MkProperty("Pmap")}, | 433 "$kind": {MkProperty("Pmap")}, |
434 "Value": {MkProperty(i)}, | 434 "Value": {MkProperty(i)}, |
435 } | 435 } |
436 if i == 4 { | 436 if i == 4 { |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 } | 681 } |
682 }) | 682 }) |
683 | 683 |
684 Convey("*[]P (map)", func() { | 684 Convey("*[]P (map)", func() { |
685 output := []PropertyMap(nil) | 685 output := []PropertyMap(nil) |
686 So(ds.GetAll(q, &output), ShouldBeNil) | 686 So(ds.GetAll(q, &output), ShouldBeNil) |
687 So(len(output), ShouldEqual, 5) | 687 So(len(output), ShouldEqual, 5) |
688 for i, o := range output { | 688 for i, o := range output { |
689 k, err := o.GetMeta("key") | 689 k, err := o.GetMeta("key") |
690 So(err, ShouldBeNil) | 690 So(err, ShouldBeNil) |
691 » » » » » So(k.(*Key).Last().IntID, ShouldEqual, i
+1) | 691 » » » » » So(k.(*Key).IntID(), ShouldEqual, i+1) |
692 So(o["Value"][0].Value().(int64), Should
Equal, i) | 692 So(o["Value"][0].Value().(int64), Should
Equal, i) |
693 } | 693 } |
694 }) | 694 }) |
695 | 695 |
696 Convey("*[]*P", func() { | 696 Convey("*[]*P", func() { |
697 output := []*FakePLS(nil) | 697 output := []*FakePLS(nil) |
698 So(ds.GetAll(q, &output), ShouldBeNil) | 698 So(ds.GetAll(q, &output), ShouldBeNil) |
699 So(len(output), ShouldEqual, 5) | 699 So(len(output), ShouldEqual, 5) |
700 for i, o := range output { | 700 for i, o := range output { |
701 So(o.gotLoaded, ShouldBeTrue) | 701 So(o.gotLoaded, ShouldBeTrue) |
702 So(o.IntID, ShouldEqual, i+1) | 702 So(o.IntID, ShouldEqual, i+1) |
703 So(o.Value, ShouldEqual, i) | 703 So(o.Value, ShouldEqual, i) |
704 } | 704 } |
705 }) | 705 }) |
706 | 706 |
707 Convey("*[]*P (map)", func() { | 707 Convey("*[]*P (map)", func() { |
708 output := []*PropertyMap(nil) | 708 output := []*PropertyMap(nil) |
709 So(ds.GetAll(q, &output), ShouldBeNil) | 709 So(ds.GetAll(q, &output), ShouldBeNil) |
710 So(len(output), ShouldEqual, 5) | 710 So(len(output), ShouldEqual, 5) |
711 for i, op := range output { | 711 for i, op := range output { |
712 o := *op | 712 o := *op |
713 k, err := o.GetMeta("key") | 713 k, err := o.GetMeta("key") |
714 So(err, ShouldBeNil) | 714 So(err, ShouldBeNil) |
715 » » » » » So(k.(*Key).Last().IntID, ShouldEqual, i
+1) | 715 » » » » » So(k.(*Key).IntID(), ShouldEqual, i+1) |
716 So(o["Value"][0].Value().(int64), Should
Equal, i) | 716 So(o["Value"][0].Value().(int64), Should
Equal, i) |
717 } | 717 } |
718 }) | 718 }) |
719 | 719 |
720 Convey("*[]*Key", func() { | 720 Convey("*[]*Key", func() { |
721 output := []*Key(nil) | 721 output := []*Key(nil) |
722 So(ds.GetAll(q, &output), ShouldBeNil) | 722 So(ds.GetAll(q, &output), ShouldBeNil) |
723 So(len(output), ShouldEqual, 5) | 723 So(len(output), ShouldEqual, 5) |
724 for i, k := range output { | 724 for i, k := range output { |
725 » » » » » So(k.Last().IntID, ShouldEqual, i+1) | 725 » » » » » So(k.IntID(), ShouldEqual, i+1) |
726 } | 726 } |
727 }) | 727 }) |
728 | 728 |
729 }) | 729 }) |
730 }) | 730 }) |
731 } | 731 } |
732 | 732 |
733 func TestRun(t *testing.T) { | 733 func TestRun(t *testing.T) { |
734 t.Parallel() | 734 t.Parallel() |
735 | 735 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 i++ | 821 i++ |
822 return true | 822 return true |
823 }), ShouldBeNil) | 823 }), ShouldBeNil) |
824 }) | 824 }) |
825 | 825 |
826 Convey("*P (map)", func() { | 826 Convey("*P (map)", func() { |
827 i := 0 | 827 i := 0 |
828 So(ds.Run(q, func(pm *PropertyMap, _ CursorCB) b
ool { | 828 So(ds.Run(q, func(pm *PropertyMap, _ CursorCB) b
ool { |
829 k, err := pm.GetMeta("key") | 829 k, err := pm.GetMeta("key") |
830 So(err, ShouldBeNil) | 830 So(err, ShouldBeNil) |
831 » » » » » So(k.(*Key).Last().IntID, ShouldEqual, i
+1) | 831 » » » » » So(k.(*Key).IntID(), ShouldEqual, i+1) |
832 So((*pm)["Value"][0].Value(), ShouldEqua
l, i) | 832 So((*pm)["Value"][0].Value(), ShouldEqua
l, i) |
833 i++ | 833 i++ |
834 return true | 834 return true |
835 }), ShouldBeNil) | 835 }), ShouldBeNil) |
836 }) | 836 }) |
837 | 837 |
838 Convey("S", func() { | 838 Convey("S", func() { |
839 i := 0 | 839 i := 0 |
840 So(ds.Run(q, func(cs CommonStruct, _ CursorCB) b
ool { | 840 So(ds.Run(q, func(cs CommonStruct, _ CursorCB) b
ool { |
841 So(cs.ID, ShouldEqual, i+1) | 841 So(cs.ID, ShouldEqual, i+1) |
(...skipping 12 matching lines...) Expand all Loading... |
854 i++ | 854 i++ |
855 return true | 855 return true |
856 }), ShouldBeNil) | 856 }), ShouldBeNil) |
857 }) | 857 }) |
858 | 858 |
859 Convey("P (map)", func() { | 859 Convey("P (map)", func() { |
860 i := 0 | 860 i := 0 |
861 So(ds.Run(q, func(pm PropertyMap, _ CursorCB) bo
ol { | 861 So(ds.Run(q, func(pm PropertyMap, _ CursorCB) bo
ol { |
862 k, err := pm.GetMeta("key") | 862 k, err := pm.GetMeta("key") |
863 So(err, ShouldBeNil) | 863 So(err, ShouldBeNil) |
864 » » » » » So(k.(*Key).Last().IntID, ShouldEqual, i
+1) | 864 » » » » » So(k.(*Key).IntID(), ShouldEqual, i+1) |
865 So(pm["Value"][0].Value(), ShouldEqual,
i) | 865 So(pm["Value"][0].Value(), ShouldEqual,
i) |
866 i++ | 866 i++ |
867 return true | 867 return true |
868 }), ShouldBeNil) | 868 }), ShouldBeNil) |
869 }) | 869 }) |
870 | 870 |
871 Convey("Key", func() { | 871 Convey("Key", func() { |
872 i := 0 | 872 i := 0 |
873 So(ds.Run(q, func(k *Key, _ CursorCB) bool { | 873 So(ds.Run(q, func(k *Key, _ CursorCB) bool { |
874 » » » » » So(k.Last().IntID, ShouldEqual, i+1) | 874 » » » » » So(k.IntID(), ShouldEqual, i+1) |
875 i++ | 875 i++ |
876 return true | 876 return true |
877 }), ShouldBeNil) | 877 }), ShouldBeNil) |
878 }) | 878 }) |
879 | 879 |
880 }) | 880 }) |
881 }) | 881 }) |
882 } | 882 } |
OLD | NEW |