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 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
752 | 752 |
753 Convey("Test Run", t, func() { | 753 Convey("Test Run", t, func() { |
754 c := info.Set(context.Background(), fakeInfo{}) | 754 c := info.Set(context.Background(), fakeInfo{}) |
755 c = SetRawFactory(c, fakeDatastoreFactory) | 755 c = SetRawFactory(c, fakeDatastoreFactory) |
756 ds := Get(c) | 756 ds := Get(c) |
757 So(ds, ShouldNotBeNil) | 757 So(ds, ShouldNotBeNil) |
758 | 758 |
759 q := ds.NewQuery("").Limit(5) | 759 q := ds.NewQuery("").Limit(5) |
760 | 760 |
761 Convey("bad", func() { | 761 Convey("bad", func() { |
762 badTypeCheck := func(cb interface{}) { | |
763 defer func() { | |
764 err, _ := recover().(error) | |
765 So(err, ShouldNotBeNil) | |
766 So(err.Error(), ShouldContainSubstring, | |
767 "cb does not match the required callback signature") | |
768 }() | |
769 ds.Run(q, cb) | |
dnj
2015/08/07 20:44:28
nit: It's not clear that "badTypeCheck" is checkin
| |
770 } | |
771 | |
762 Convey("bad proto type", func() { | 772 Convey("bad proto type", func() { |
763 » » » » So(ds.Run(q, 100, func(obj interface{}, _ func() (Cursor, error)) bool { | 773 » » » » badTypeCheck(func(v int, _ CursorCB) bool { |
764 panic("never here!") | 774 panic("never here!") |
765 » » » » }).Error(), ShouldContainSubstring, "invalid Run proto type") | 775 » » » » }) |
776 » » » }) | |
777 | |
778 » » » Convey("wrong # args", func() { | |
779 » » » » badTypeCheck(func(v CommonStruct, _ CursorCB) { | |
780 » » » » » panic("never here!") | |
781 » » » » }) | |
782 » » » }) | |
783 | |
784 » » » Convey("wrong ret type", func() { | |
785 » » » » badTypeCheck(func(v CommonStruct, _ CursorCB) er ror { | |
786 » » » » » panic("never here!") | |
787 » » » » }) | |
788 » » » }) | |
789 | |
790 » » » Convey("bad 2nd arg", func() { | |
791 » » » » badTypeCheck(func(v CommonStruct, _ Cursor) bool { | |
792 » » » » » panic("never here!") | |
793 » » » » }) | |
766 }) | 794 }) |
767 | 795 |
768 Convey("early abort on error", func() { | 796 Convey("early abort on error", func() { |
769 rq := q.(*fakeQuery).Fail(3) | 797 rq := q.(*fakeQuery).Fail(3) |
770 i := 0 | 798 i := 0 |
771 » » » » So(ds.Run(rq, CommonStruct{}, func(_ interface{} , _ func() (Cursor, error)) bool { | 799 » » » » So(ds.Run(rq, func(c CommonStruct, _ CursorCB) b ool { |
772 i++ | 800 i++ |
773 return true | 801 return true |
774 }).Error(), ShouldEqual, "Query fail") | 802 }).Error(), ShouldEqual, "Query fail") |
775 So(i, ShouldEqual, 3) | 803 So(i, ShouldEqual, 3) |
776 }) | 804 }) |
777 | 805 |
778 Convey("return error on serialization failure", func() { | 806 Convey("return error on serialization failure", func() { |
779 » » » » So(ds.Run(q, permaBad{}, func(_ interface{}, _ f unc() (Cursor, error)) bool { | 807 » » » » So(ds.Run(q, func(_ permaBad, _ CursorCB) bool { |
780 panic("never here") | 808 panic("never here") |
781 }).Error(), ShouldEqual, "permaBad") | 809 }).Error(), ShouldEqual, "permaBad") |
782 }) | 810 }) |
783 }) | 811 }) |
784 | 812 |
785 Convey("ok", func() { | 813 Convey("ok", func() { |
786 Convey("*S", func() { | 814 Convey("*S", func() { |
787 i := 0 | 815 i := 0 |
788 » » » » So(ds.Run(q, (*CommonStruct)(nil), func(obj inte rface{}, _ func() (Cursor, error)) bool { | 816 » » » » So(ds.Run(q, func(cs *CommonStruct, _ CursorCB) bool { |
789 » » » » » cs := obj.(*CommonStruct) | |
790 So(cs.ID, ShouldEqual, i+1) | 817 So(cs.ID, ShouldEqual, i+1) |
791 So(cs.Value, ShouldEqual, i) | 818 So(cs.Value, ShouldEqual, i) |
792 i++ | 819 i++ |
793 return true | 820 return true |
794 }), ShouldBeNil) | 821 }), ShouldBeNil) |
795 }) | 822 }) |
796 | 823 |
797 Convey("*P", func() { | 824 Convey("*P", func() { |
798 i := 0 | 825 i := 0 |
799 » » » » So(ds.Run(q.Limit(12), (*FakePLS)(nil), func(obj interface{}, _ func() (Cursor, error)) bool { | 826 » » » » So(ds.Run(q.Limit(12), func(fpls *FakePLS, _ Cur sorCB) bool { |
800 » » » » » fpls := obj.(*FakePLS) | |
801 So(fpls.gotLoaded, ShouldBeTrue) | 827 So(fpls.gotLoaded, ShouldBeTrue) |
802 if i == 10 { | 828 if i == 10 { |
803 So(fpls.StringID, ShouldEqual, " eleven") | 829 So(fpls.StringID, ShouldEqual, " eleven") |
804 } else { | 830 } else { |
805 So(fpls.IntID, ShouldEqual, i+1) | 831 So(fpls.IntID, ShouldEqual, i+1) |
806 } | 832 } |
807 So(fpls.Value, ShouldEqual, i) | 833 So(fpls.Value, ShouldEqual, i) |
808 i++ | 834 i++ |
809 return true | 835 return true |
810 }), ShouldBeNil) | 836 }), ShouldBeNil) |
811 }) | 837 }) |
812 | 838 |
813 Convey("*P (map)", func() { | 839 Convey("*P (map)", func() { |
814 i := 0 | 840 i := 0 |
815 » » » » So(ds.Run(q, (*PropertyMap)(nil), func(obj inter face{}, _ func() (Cursor, error)) bool { | 841 » » » » So(ds.Run(q, func(pm *PropertyMap, _ CursorCB) b ool { |
816 » » » » » pm := *obj.(*PropertyMap) | |
817 k, err := pm.GetMeta("key") | 842 k, err := pm.GetMeta("key") |
818 So(err, ShouldBeNil) | 843 So(err, ShouldBeNil) |
819 So(k.(Key).IntID(), ShouldEqual, i+1) | 844 So(k.(Key).IntID(), ShouldEqual, i+1) |
820 » » » » » So(pm["Value"][0].Value(), ShouldEqual, i) | 845 » » » » » So((*pm)["Value"][0].Value(), ShouldEqua l, i) |
821 i++ | 846 i++ |
822 return true | 847 return true |
823 }), ShouldBeNil) | 848 }), ShouldBeNil) |
824 }) | 849 }) |
825 | 850 |
826 Convey("S", func() { | 851 Convey("S", func() { |
827 i := 0 | 852 i := 0 |
828 » » » » So(ds.Run(q, CommonStruct{}, func(obj interface{ }, _ func() (Cursor, error)) bool { | 853 » » » » So(ds.Run(q, func(cs CommonStruct, _ CursorCB) b ool { |
829 » » » » » cs := obj.(CommonStruct) | |
830 So(cs.ID, ShouldEqual, i+1) | 854 So(cs.ID, ShouldEqual, i+1) |
831 So(cs.Value, ShouldEqual, i) | 855 So(cs.Value, ShouldEqual, i) |
832 i++ | 856 i++ |
833 return true | 857 return true |
834 }), ShouldBeNil) | 858 }), ShouldBeNil) |
835 }) | 859 }) |
836 | 860 |
837 Convey("P", func() { | 861 Convey("P", func() { |
838 i := 0 | 862 i := 0 |
839 » » » » So(ds.Run(q, FakePLS{}, func(obj interface{}, _ func() (Cursor, error)) bool { | 863 » » » » So(ds.Run(q, func(fpls FakePLS, _ CursorCB) bool { |
840 » » » » » fpls := obj.(FakePLS) | |
841 So(fpls.gotLoaded, ShouldBeTrue) | 864 So(fpls.gotLoaded, ShouldBeTrue) |
842 So(fpls.IntID, ShouldEqual, i+1) | 865 So(fpls.IntID, ShouldEqual, i+1) |
843 So(fpls.Value, ShouldEqual, i) | 866 So(fpls.Value, ShouldEqual, i) |
844 i++ | 867 i++ |
845 return true | 868 return true |
846 }), ShouldBeNil) | 869 }), ShouldBeNil) |
847 }) | 870 }) |
848 | 871 |
849 Convey("P (map)", func() { | 872 Convey("P (map)", func() { |
850 i := 0 | 873 i := 0 |
851 » » » » So(ds.Run(q, (PropertyMap)(nil), func(obj interf ace{}, _ func() (Cursor, error)) bool { | 874 » » » » So(ds.Run(q, func(pm PropertyMap, _ CursorCB) bo ol { |
852 » » » » » pm := obj.(PropertyMap) | |
853 k, err := pm.GetMeta("key") | 875 k, err := pm.GetMeta("key") |
854 So(err, ShouldBeNil) | 876 So(err, ShouldBeNil) |
855 So(k.(Key).IntID(), ShouldEqual, i+1) | 877 So(k.(Key).IntID(), ShouldEqual, i+1) |
856 So(pm["Value"][0].Value(), ShouldEqual, i) | 878 So(pm["Value"][0].Value(), ShouldEqual, i) |
857 i++ | 879 i++ |
858 return true | 880 return true |
859 }), ShouldBeNil) | 881 }), ShouldBeNil) |
860 }) | 882 }) |
861 | 883 |
862 Convey("*Key", func() { | 884 Convey("*Key", func() { |
863 i := 0 | 885 i := 0 |
864 » » » » So(ds.Run(q, (*Key)(nil), func(obj interface{}, _ func() (Cursor, error)) bool { | 886 » » » » So(ds.Run(q, func(k Key, _ CursorCB) bool { |
865 » » » » » So(obj.(Key).IntID(), ShouldEqual, i+1) | 887 » » » » » So(k.IntID(), ShouldEqual, i+1) |
866 i++ | 888 i++ |
867 return true | 889 return true |
868 }), ShouldBeNil) | 890 }), ShouldBeNil) |
869 }) | 891 }) |
870 | 892 |
871 }) | 893 }) |
872 }) | 894 }) |
873 } | 895 } |
OLD | NEW |