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 "bytes" | 10 "bytes" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 testKey1a = mkKey("aid", "", "kind", "name1") | 64 testKey1a = mkKey("aid", "", "kind", "name1") |
65 testKey1b = mkKey("aid", "", "kind", "name1") | 65 testKey1b = mkKey("aid", "", "kind", "name1") |
66 testKey2a = mkKey("aid", "", "kind", "name0", "kind", "name2") | 66 testKey2a = mkKey("aid", "", "kind", "name0", "kind", "name2") |
67 testKey2b = mkKey("aid", "", "kind", "name0", "kind", "name2") | 67 testKey2b = mkKey("aid", "", "kind", "name0", "kind", "name2") |
68 testGeoPt0 = GeoPoint{Lat: 1.2, Lng: 3.4} | 68 testGeoPt0 = GeoPoint{Lat: 1.2, Lng: 3.4} |
69 testGeoPt1 = GeoPoint{Lat: 5, Lng: 10} | 69 testGeoPt1 = GeoPoint{Lat: 5, Lng: 10} |
70 testBadGeoPt = GeoPoint{Lat: 1000, Lng: 34} | 70 testBadGeoPt = GeoPoint{Lat: 1000, Lng: 34} |
71 ) | 71 ) |
72 | 72 |
73 type B0 struct { | 73 type B0 struct { |
74 » B []byte | 74 » B []byte `gae:",noindex"` |
75 } | 75 } |
76 | 76 |
77 type B1 struct { | 77 type B1 struct { |
78 B []int8 | 78 B []int8 |
79 } | 79 } |
80 | 80 |
81 type B2 struct { | 81 type B2 struct { |
82 B myBlob | 82 B myBlob |
83 } | 83 } |
84 | 84 |
85 type B3 struct { | 85 type B3 struct { |
86 B []myByte | 86 B []myByte |
87 } | 87 } |
88 | 88 |
89 type B4 struct { | 89 type B4 struct { |
90 » B [][]byte | 90 » B [][]byte `gae:",noindex"` |
91 } | 91 } |
92 | 92 |
93 type B5 struct { | 93 type B5 struct { |
94 » B ByteString | 94 » B []byte |
95 } | 95 } |
96 | 96 |
97 type C0 struct { | 97 type C0 struct { |
98 I int | 98 I int |
99 C chan int | 99 C chan int |
100 } | 100 } |
101 | 101 |
102 type C1 struct { | 102 type C1 struct { |
103 I int | 103 I int |
104 C *chan int | 104 C *chan int |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 I int | 313 I int |
314 R []MutuallyRecursive1 | 314 R []MutuallyRecursive1 |
315 } | 315 } |
316 | 316 |
317 type MutuallyRecursive1 struct { | 317 type MutuallyRecursive1 struct { |
318 I int | 318 I int |
319 R []MutuallyRecursive0 | 319 R []MutuallyRecursive0 |
320 } | 320 } |
321 | 321 |
322 type ExoticTypes struct { | 322 type ExoticTypes struct { |
323 » BS blobstore.Key | 323 » BS blobstore.Key |
324 » DSBS ByteString | |
325 } | 324 } |
326 | 325 |
327 type Underspecified struct { | 326 type Underspecified struct { |
328 Iface PropertyConverter | 327 Iface PropertyConverter |
329 } | 328 } |
330 | 329 |
331 type MismatchTypes struct { | 330 type MismatchTypes struct { |
332 S string | 331 S string |
333 B bool | 332 B bool |
334 F float32 | 333 F float32 |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 | 641 |
643 type DerivedKey struct { | 642 type DerivedKey struct { |
644 K *genericKey | 643 K *genericKey |
645 } | 644 } |
646 | 645 |
647 type IfaceKey struct { | 646 type IfaceKey struct { |
648 K Key | 647 K Key |
649 } | 648 } |
650 | 649 |
651 type testCase struct { | 650 type testCase struct { |
652 » desc string | 651 » desc string |
653 » src interface{} | 652 » src interface{} |
654 » want interface{} | 653 » want interface{} |
655 » plsErr string | 654 » plsErr string |
656 » saveErr string | 655 » saveErr string |
657 » actualNoIndex bool | 656 » plsLoadErr string |
658 » plsLoadErr string | 657 » loadErr string |
659 » loadErr string | |
660 } | 658 } |
661 | 659 |
662 var testCases = []testCase{ | 660 var testCases = []testCase{ |
663 { | 661 { |
664 desc: "chan save fails", | 662 desc: "chan save fails", |
665 src: &C0{I: -1}, | 663 src: &C0{I: -1}, |
666 plsErr: `field "C" has invalid type: chan int`, | 664 plsErr: `field "C" has invalid type: chan int`, |
667 }, | 665 }, |
668 { | 666 { |
669 desc: "*chan save fails", | 667 desc: "*chan save fails", |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 makeUint8Slice(4), | 985 makeUint8Slice(4), |
988 makeUint8Slice(5), | 986 makeUint8Slice(5), |
989 }}, | 987 }}, |
990 want: &B4{B: [][]byte{ | 988 want: &B4{B: [][]byte{ |
991 makeUint8Slice(3), | 989 makeUint8Slice(3), |
992 makeUint8Slice(4), | 990 makeUint8Slice(4), |
993 makeUint8Slice(5), | 991 makeUint8Slice(5), |
994 }}, | 992 }}, |
995 }, | 993 }, |
996 { | 994 { |
997 » » desc: "short ByteString", | 995 » » desc: "short []byte", |
998 » » src: &B5{B: ByteString(makeUint8Slice(3))}, | 996 » » src: &B5{B: makeUint8Slice(3)}, |
999 » » want: &B5{B: ByteString(makeUint8Slice(3))}, | 997 » » want: &B5{B: makeUint8Slice(3)}, |
1000 }, | 998 }, |
1001 { | 999 { |
1002 desc: "short ByteString as props", | 1000 desc: "short ByteString as props", |
1003 » » src: &B5{B: ByteString(makeUint8Slice(3))}, | 1001 » » src: &B5{B: makeUint8Slice(3)}, |
1004 want: PropertyMap{ | 1002 want: PropertyMap{ |
1005 » » » "B": {mp(ByteString(makeUint8Slice(3)))}, | 1003 » » » "B": {mp(makeUint8Slice(3))}, |
1006 }, | 1004 }, |
1007 }, | 1005 }, |
1008 { | 1006 { |
1009 desc: "[]byte must be noindex", | |
1010 src: PropertyMap{ | |
1011 "B": {mp(makeUint8Slice(3))}, | |
1012 }, | |
1013 actualNoIndex: true, | |
1014 }, | |
1015 { | |
1016 desc: "save tagged load props", | 1007 desc: "save tagged load props", |
1017 src: &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6
, J: 7}, | 1008 src: &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6
, J: 7}, |
1018 want: PropertyMap{ | 1009 want: PropertyMap{ |
1019 // A and B are renamed to a and b; A and C are noindex,
I is ignored. | 1010 // A and B are renamed to a and b; A and C are noindex,
I is ignored. |
1020 // Indexed properties are loaded before raw properties.
Thus, the | 1011 // Indexed properties are loaded before raw properties.
Thus, the |
1021 // result is: b, b, b, D, E, a, c. | 1012 // result is: b, b, b, D, E, a, c. |
1022 "b1": { | 1013 "b1": { |
1023 mp(21), | 1014 mp(21), |
1024 mp(22), | 1015 mp(22), |
1025 mp(23), | 1016 mp(23), |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1267 }, | 1258 }, |
1268 want: &N0{ | 1259 want: &N0{ |
1269 X0: X0{S: "one", I: 2}, | 1260 X0: X0{S: "one", I: 2}, |
1270 Nonymous: X0{S: "four", I: 5}, | 1261 Nonymous: X0{S: "four", I: 5}, |
1271 Other: "other", | 1262 Other: "other", |
1272 }, | 1263 }, |
1273 }, | 1264 }, |
1274 { | 1265 { |
1275 desc: "exotic types", | 1266 desc: "exotic types", |
1276 src: &ExoticTypes{ | 1267 src: &ExoticTypes{ |
1277 » » » BS: "sup", | 1268 » » » BS: "sup", |
1278 » » » DSBS: ByteString("nerds"), | |
1279 }, | 1269 }, |
1280 want: &ExoticTypes{ | 1270 want: &ExoticTypes{ |
1281 » » » BS: "sup", | 1271 » » » BS: "sup", |
1282 » » » DSBS: ByteString("nerds"), | |
1283 }, | 1272 }, |
1284 }, | 1273 }, |
1285 { | 1274 { |
1286 desc: "underspecified types", | 1275 desc: "underspecified types", |
1287 src: &Underspecified{}, | 1276 src: &Underspecified{}, |
1288 plsErr: "non-concrete interface", | 1277 plsErr: "non-concrete interface", |
1289 }, | 1278 }, |
1290 { | 1279 { |
1291 desc: "mismatch (string)", | 1280 desc: "mismatch (string)", |
1292 src: PropertyMap{ | 1281 src: PropertyMap{ |
1293 "K": {mp(199)}, | 1282 "K": {mp(199)}, |
1294 "S": {mp([]byte("cats"))}, | 1283 "S": {mp([]byte("cats"))}, |
1295 » » » "F": {mp(ByteString("nurbs"))}, | 1284 » » » "F": {mp("nurbs")}, |
1296 }, | 1285 }, |
1297 want: &MismatchTypes{}, | 1286 want: &MismatchTypes{}, |
1298 loadErr: "type mismatch", | 1287 loadErr: "type mismatch", |
1299 }, | 1288 }, |
1300 { | 1289 { |
1301 desc: "mismatch (float)", | 1290 desc: "mismatch (float)", |
1302 src: PropertyMap{"F": {mp(blobstore.Key("wot"))}}, | 1291 src: PropertyMap{"F": {mp(blobstore.Key("wot"))}}, |
1303 want: &MismatchTypes{}, | 1292 want: &MismatchTypes{}, |
1304 loadErr: "type mismatch", | 1293 loadErr: "type mismatch", |
1305 }, | 1294 }, |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1600 return | 1589 return |
1601 } | 1590 } |
1602 So(pls, ShouldNotBeNil) | 1591 So(pls, ShouldNotBeNil) |
1603 | 1592 |
1604 savedProps, err := pls.Save(false) | 1593 savedProps, err := pls.Save(false) |
1605 if checkErr(err, tc.saveErr) { | 1594 if checkErr(err, tc.saveErr) { |
1606 return | 1595 return |
1607 } | 1596 } |
1608 So(savedProps, ShouldNotBeNil) | 1597 So(savedProps, ShouldNotBeNil) |
1609 | 1598 |
1610 if tc.actualNoIndex { | |
1611 for _, props := range savedProps { | |
1612 So(props[0].IndexSetting(), Shou
ldEqual, NoIndex) | |
1613 return | |
1614 } | |
1615 So(true, ShouldBeFalse) // shouldn't get
here | |
1616 } | |
1617 | |
1618 var got interface{} | 1599 var got interface{} |
1619 if _, ok := tc.want.(PropertyMap); ok { | 1600 if _, ok := tc.want.(PropertyMap); ok { |
1620 pls = PropertyMap{} | 1601 pls = PropertyMap{} |
1621 got = pls | 1602 got = pls |
1622 } else { | 1603 } else { |
1623 got = reflect.New(reflect.TypeOf(tc.want
).Elem()).Interface() | 1604 got = reflect.New(reflect.TypeOf(tc.want
).Elem()).Interface() |
1624 if pls, ok = got.(PropertyLoadSaver); !o
k { | 1605 if pls, ok = got.(PropertyLoadSaver); !o
k { |
1625 pls = GetPLS(got) | 1606 pls = GetPLS(got) |
1626 } | 1607 } |
1627 } | 1608 } |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1842 | 1823 |
1843 Convey("Bad default meta type", func() { | 1824 Convey("Bad default meta type", func() { |
1844 type BadDefault struct { | 1825 type BadDefault struct { |
1845 Val time.Time `gae:"$meta,tomorrow"` | 1826 Val time.Time `gae:"$meta,tomorrow"` |
1846 } | 1827 } |
1847 pls := GetPLS(&BadDefault{}) | 1828 pls := GetPLS(&BadDefault{}) |
1848 So(pls.Problem().Error(), ShouldContainSubstring, "bad t
ype") | 1829 So(pls.Problem().Error(), ShouldContainSubstring, "bad t
ype") |
1849 }) | 1830 }) |
1850 }) | 1831 }) |
1851 } | 1832 } |
OLD | NEW |