You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

3573 lines
72 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package datastore
  15. import (
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "reflect"
  21. "sort"
  22. "strings"
  23. "testing"
  24. "time"
  25. "cloud.google.com/go/internal/testutil"
  26. "github.com/golang/protobuf/proto"
  27. "github.com/google/go-cmp/cmp"
  28. pb "google.golang.org/genproto/googleapis/datastore/v1"
  29. "google.golang.org/grpc"
  30. )
  31. type (
  32. myBlob []byte
  33. myByte byte
  34. myString string
  35. )
  36. func makeMyByteSlice(n int) []myByte {
  37. b := make([]myByte, n)
  38. for i := range b {
  39. b[i] = myByte(i)
  40. }
  41. return b
  42. }
  43. func makeInt8Slice(n int) []int8 {
  44. b := make([]int8, n)
  45. for i := range b {
  46. b[i] = int8(i)
  47. }
  48. return b
  49. }
  50. func makeUint8Slice(n int) []uint8 {
  51. b := make([]uint8, n)
  52. for i := range b {
  53. b[i] = uint8(i)
  54. }
  55. return b
  56. }
  57. func newKey(stringID string, parent *Key) *Key {
  58. return NameKey("kind", stringID, parent)
  59. }
  60. var (
  61. testKey0 = newKey("name0", nil)
  62. testKey1a = newKey("name1", nil)
  63. testKey1b = newKey("name1", nil)
  64. testKey2a = newKey("name2", testKey0)
  65. testKey2b = newKey("name2", testKey0)
  66. testGeoPt0 = GeoPoint{Lat: 1.2, Lng: 3.4}
  67. testGeoPt1 = GeoPoint{Lat: 5, Lng: 10}
  68. testBadGeoPt = GeoPoint{Lat: 1000, Lng: 34}
  69. ts = time.Unix(1e9, 0).UTC()
  70. )
  71. type B0 struct {
  72. B []byte `datastore:",noindex"`
  73. }
  74. type B1 struct {
  75. B []int8
  76. }
  77. type B2 struct {
  78. B myBlob `datastore:",noindex"`
  79. }
  80. type B3 struct {
  81. B []myByte `datastore:",noindex"`
  82. }
  83. type B4 struct {
  84. B [][]byte
  85. }
  86. type C0 struct {
  87. I int
  88. C chan int
  89. }
  90. type C1 struct {
  91. I int
  92. C *chan int
  93. }
  94. type C2 struct {
  95. I int
  96. C []chan int
  97. }
  98. type C3 struct {
  99. C string
  100. }
  101. type c4 struct {
  102. C string
  103. }
  104. type E struct{}
  105. type G0 struct {
  106. G GeoPoint
  107. }
  108. type G1 struct {
  109. G []GeoPoint
  110. }
  111. type K0 struct {
  112. K *Key
  113. }
  114. type K1 struct {
  115. K []*Key
  116. }
  117. type S struct {
  118. St string
  119. }
  120. type NoOmit struct {
  121. A string
  122. B int `datastore:"Bb"`
  123. C bool `datastore:",noindex"`
  124. }
  125. type OmitAll struct {
  126. A string `datastore:",omitempty"`
  127. B int `datastore:"Bb,omitempty"`
  128. C bool `datastore:",omitempty,noindex"`
  129. D time.Time `datastore:",omitempty"`
  130. F []int `datastore:",omitempty"`
  131. }
  132. type Omit struct {
  133. A string `datastore:",omitempty"`
  134. B int `datastore:"Bb,omitempty"`
  135. C bool `datastore:",omitempty,noindex"`
  136. D time.Time `datastore:",omitempty"`
  137. F []int `datastore:",omitempty"`
  138. S `datastore:",omitempty"`
  139. }
  140. type NoOmits struct {
  141. No []NoOmit `datastore:",omitempty"`
  142. S `datastore:",omitempty"`
  143. Ss S `datastore:",omitempty"`
  144. }
  145. type N0 struct {
  146. X0
  147. Nonymous X0
  148. Ignore string `datastore:"-"`
  149. Other string
  150. }
  151. type N1 struct {
  152. X0
  153. Nonymous []X0
  154. Ignore string `datastore:"-"`
  155. Other string
  156. }
  157. type N2 struct {
  158. N1 `datastore:"red"`
  159. Green N1 `datastore:"green"`
  160. Blue N1
  161. White N1 `datastore:"-"`
  162. }
  163. type N3 struct {
  164. C3 `datastore:"red"`
  165. }
  166. type N4 struct {
  167. c4
  168. }
  169. type N5 struct {
  170. c4 `datastore:"red"`
  171. }
  172. type O0 struct {
  173. I int64
  174. }
  175. type O1 struct {
  176. I int32
  177. }
  178. type U0 struct {
  179. U uint
  180. }
  181. type U1 struct {
  182. U string
  183. }
  184. type T struct {
  185. T time.Time
  186. }
  187. type X0 struct {
  188. S string
  189. I int
  190. i int
  191. }
  192. type X1 struct {
  193. S myString
  194. I int32
  195. J int64
  196. }
  197. type X2 struct {
  198. Z string
  199. }
  200. type X3 struct {
  201. S bool
  202. I int
  203. }
  204. type Y0 struct {
  205. B bool
  206. F []float64
  207. G []float64
  208. }
  209. type Y1 struct {
  210. B bool
  211. F float64
  212. }
  213. type Y2 struct {
  214. B bool
  215. F []int64
  216. }
  217. type Pointers struct {
  218. Pi *int
  219. Ps *string
  220. Pb *bool
  221. Pf *float64
  222. Pg *GeoPoint
  223. Pt *time.Time
  224. }
  225. type PointersOmitEmpty struct {
  226. Pi *int `datastore:",omitempty"`
  227. Ps *string `datastore:",omitempty"`
  228. Pb *bool `datastore:",omitempty"`
  229. Pf *float64 `datastore:",omitempty"`
  230. Pg *GeoPoint `datastore:",omitempty"`
  231. Pt *time.Time `datastore:",omitempty"`
  232. }
  233. func populatedPointers() *Pointers {
  234. var (
  235. i int
  236. s string
  237. b bool
  238. f float64
  239. g GeoPoint
  240. t time.Time
  241. )
  242. return &Pointers{
  243. Pi: &i,
  244. Ps: &s,
  245. Pb: &b,
  246. Pf: &f,
  247. Pg: &g,
  248. Pt: &t,
  249. }
  250. }
  251. type Tagged struct {
  252. A int `datastore:"a,noindex"`
  253. B []int `datastore:"b"`
  254. C int `datastore:",noindex"`
  255. D int `datastore:""`
  256. E int
  257. I int `datastore:"-"`
  258. J int `datastore:",noindex" json:"j"`
  259. Y0 `datastore:"-"`
  260. Z chan int `datastore:"-"`
  261. }
  262. type InvalidTagged1 struct {
  263. I int `datastore:"\t"`
  264. }
  265. type InvalidTagged2 struct {
  266. I int
  267. J int `datastore:"I"`
  268. }
  269. type InvalidTagged3 struct {
  270. X string `datastore:"-,noindex"`
  271. }
  272. type InvalidTagged4 struct {
  273. X string `datastore:",garbage"`
  274. }
  275. type Inner1 struct {
  276. W int32
  277. X string
  278. }
  279. type Inner2 struct {
  280. Y float64
  281. }
  282. type Inner3 struct {
  283. Z bool
  284. }
  285. type Inner5 struct {
  286. WW int
  287. }
  288. type Inner4 struct {
  289. X Inner5
  290. }
  291. type Outer struct {
  292. A int16
  293. I []Inner1
  294. J Inner2
  295. Inner3
  296. }
  297. type OuterFlatten struct {
  298. A int16
  299. I []Inner1 `datastore:",flatten"`
  300. J Inner2 `datastore:",flatten,noindex"`
  301. Inner3 `datastore:",flatten"`
  302. K Inner4 `datastore:",flatten"`
  303. L *Inner2 `datastore:",flatten"`
  304. }
  305. type OuterEquivalent struct {
  306. A int16
  307. IDotW []int32 `datastore:"I.W"`
  308. IDotX []string `datastore:"I.X"`
  309. JDotY float64 `datastore:"J.Y"`
  310. Z bool
  311. }
  312. type Dotted struct {
  313. A DottedA `datastore:"A0.A1.A2"`
  314. }
  315. type DottedA struct {
  316. B DottedB `datastore:"B3"`
  317. }
  318. type DottedB struct {
  319. C int `datastore:"C4.C5"`
  320. }
  321. type SliceOfSlices struct {
  322. I int
  323. S []struct {
  324. J int
  325. F []float64
  326. } `datastore:",flatten"`
  327. }
  328. type Recursive struct {
  329. I int
  330. R []Recursive
  331. }
  332. type MutuallyRecursive0 struct {
  333. I int
  334. R []MutuallyRecursive1
  335. }
  336. type MutuallyRecursive1 struct {
  337. I int
  338. R []MutuallyRecursive0
  339. }
  340. type EntityWithKey struct {
  341. I int
  342. S string
  343. K *Key `datastore:"__key__"`
  344. }
  345. type WithNestedEntityWithKey struct {
  346. N EntityWithKey
  347. }
  348. type WithNonKeyField struct {
  349. I int
  350. K string `datastore:"__key__"`
  351. }
  352. type NestedWithNonKeyField struct {
  353. N WithNonKeyField
  354. }
  355. type Basic struct {
  356. A string
  357. }
  358. type PtrToStructField struct {
  359. B *Basic
  360. C *Basic `datastore:"c,noindex"`
  361. *Basic
  362. D []*Basic
  363. }
  364. type EmbeddedTime struct {
  365. time.Time
  366. }
  367. type SpecialTime struct {
  368. MyTime EmbeddedTime
  369. }
  370. type Doubler struct {
  371. S string
  372. I int64
  373. B bool
  374. }
  375. type Repeat struct {
  376. Key string
  377. Value []byte
  378. }
  379. type Repeated struct {
  380. Repeats []Repeat
  381. }
  382. func (d *Doubler) Load(props []Property) error {
  383. return LoadStruct(d, props)
  384. }
  385. func (d *Doubler) Save() ([]Property, error) {
  386. // Save the default Property slice to an in-memory buffer (a PropertyList).
  387. props, err := SaveStruct(d)
  388. if err != nil {
  389. return nil, err
  390. }
  391. var list PropertyList
  392. if err := list.Load(props); err != nil {
  393. return nil, err
  394. }
  395. // Edit that PropertyList, and send it on.
  396. for i := range list {
  397. switch v := list[i].Value.(type) {
  398. case string:
  399. // + means string concatenation.
  400. list[i].Value = v + v
  401. case int64:
  402. // + means integer addition.
  403. list[i].Value = v + v
  404. }
  405. }
  406. return list.Save()
  407. }
  408. var _ PropertyLoadSaver = (*Doubler)(nil)
  409. type Deriver struct {
  410. S, Derived, Ignored string
  411. }
  412. func (e *Deriver) Load(props []Property) error {
  413. for _, p := range props {
  414. if p.Name != "S" {
  415. continue
  416. }
  417. e.S = p.Value.(string)
  418. e.Derived = "derived+" + e.S
  419. }
  420. return nil
  421. }
  422. func (e *Deriver) Save() ([]Property, error) {
  423. return []Property{
  424. {
  425. Name: "S",
  426. Value: e.S,
  427. },
  428. }, nil
  429. }
  430. var _ PropertyLoadSaver = (*Deriver)(nil)
  431. type BadMultiPropEntity struct{}
  432. func (e *BadMultiPropEntity) Load(props []Property) error {
  433. return errors.New("unimplemented")
  434. }
  435. func (e *BadMultiPropEntity) Save() ([]Property, error) {
  436. // Write multiple properties with the same name "I".
  437. var props []Property
  438. for i := 0; i < 3; i++ {
  439. props = append(props, Property{
  440. Name: "I",
  441. Value: int64(i),
  442. })
  443. }
  444. return props, nil
  445. }
  446. var _ PropertyLoadSaver = (*BadMultiPropEntity)(nil)
  447. type testCase struct {
  448. desc string
  449. src interface{}
  450. want interface{}
  451. putErr string
  452. getErr string
  453. }
  454. var testCases = []testCase{
  455. {
  456. "chan save fails",
  457. &C0{I: -1},
  458. &E{},
  459. "unsupported struct field",
  460. "",
  461. },
  462. {
  463. "*chan save fails",
  464. &C1{I: -1},
  465. &E{},
  466. "unsupported struct field",
  467. "",
  468. },
  469. {
  470. "[]chan save fails",
  471. &C2{I: -1, C: make([]chan int, 8)},
  472. &E{},
  473. "unsupported struct field",
  474. "",
  475. },
  476. {
  477. "chan load fails",
  478. &C3{C: "not a chan"},
  479. &C0{},
  480. "",
  481. "type mismatch",
  482. },
  483. {
  484. "*chan load fails",
  485. &C3{C: "not a *chan"},
  486. &C1{},
  487. "",
  488. "type mismatch",
  489. },
  490. {
  491. "[]chan load fails",
  492. &C3{C: "not a []chan"},
  493. &C2{},
  494. "",
  495. "type mismatch",
  496. },
  497. {
  498. "empty struct",
  499. &E{},
  500. &E{},
  501. "",
  502. "",
  503. },
  504. {
  505. "geopoint",
  506. &G0{G: testGeoPt0},
  507. &G0{G: testGeoPt0},
  508. "",
  509. "",
  510. },
  511. {
  512. "geopoint invalid",
  513. &G0{G: testBadGeoPt},
  514. &G0{},
  515. "invalid GeoPoint value",
  516. "",
  517. },
  518. {
  519. "geopoint as props",
  520. &G0{G: testGeoPt0},
  521. &PropertyList{
  522. Property{Name: "G", Value: testGeoPt0, NoIndex: false},
  523. },
  524. "",
  525. "",
  526. },
  527. {
  528. "geopoint slice",
  529. &G1{G: []GeoPoint{testGeoPt0, testGeoPt1}},
  530. &G1{G: []GeoPoint{testGeoPt0, testGeoPt1}},
  531. "",
  532. "",
  533. },
  534. {
  535. "omit empty, all",
  536. &OmitAll{},
  537. new(PropertyList),
  538. "",
  539. "",
  540. },
  541. {
  542. "omit empty",
  543. &Omit{},
  544. &PropertyList{
  545. Property{Name: "St", Value: "", NoIndex: false},
  546. },
  547. "",
  548. "",
  549. },
  550. {
  551. "omit empty, fields populated",
  552. &Omit{
  553. A: "a",
  554. B: 10,
  555. C: true,
  556. F: []int{11},
  557. },
  558. &PropertyList{
  559. Property{Name: "A", Value: "a", NoIndex: false},
  560. Property{Name: "Bb", Value: int64(10), NoIndex: false},
  561. Property{Name: "C", Value: true, NoIndex: true},
  562. Property{Name: "F", Value: []interface{}{int64(11)}, NoIndex: false},
  563. Property{Name: "St", Value: "", NoIndex: false},
  564. },
  565. "",
  566. "",
  567. },
  568. {
  569. "omit empty, fields populated",
  570. &Omit{
  571. A: "a",
  572. B: 10,
  573. C: true,
  574. F: []int{11},
  575. S: S{St: "string"},
  576. },
  577. &PropertyList{
  578. Property{Name: "A", Value: "a", NoIndex: false},
  579. Property{Name: "Bb", Value: int64(10), NoIndex: false},
  580. Property{Name: "C", Value: true, NoIndex: true},
  581. Property{Name: "F", Value: []interface{}{int64(11)}, NoIndex: false},
  582. Property{Name: "St", Value: "string", NoIndex: false},
  583. },
  584. "",
  585. "",
  586. },
  587. {
  588. "omit empty does not propagate",
  589. &NoOmits{
  590. No: []NoOmit{
  591. {},
  592. },
  593. S: S{},
  594. Ss: S{},
  595. },
  596. &PropertyList{
  597. Property{Name: "No", Value: []interface{}{
  598. &Entity{
  599. Properties: []Property{
  600. {Name: "A", Value: "", NoIndex: false},
  601. {Name: "Bb", Value: int64(0), NoIndex: false},
  602. {Name: "C", Value: false, NoIndex: true},
  603. },
  604. },
  605. }, NoIndex: false},
  606. Property{Name: "Ss", Value: &Entity{
  607. Properties: []Property{
  608. {Name: "St", Value: "", NoIndex: false},
  609. },
  610. }, NoIndex: false},
  611. Property{Name: "St", Value: "", NoIndex: false},
  612. },
  613. "",
  614. "",
  615. },
  616. {
  617. "key",
  618. &K0{K: testKey1a},
  619. &K0{K: testKey1b},
  620. "",
  621. "",
  622. },
  623. {
  624. "key with parent",
  625. &K0{K: testKey2a},
  626. &K0{K: testKey2b},
  627. "",
  628. "",
  629. },
  630. {
  631. "nil key",
  632. &K0{},
  633. &K0{},
  634. "",
  635. "",
  636. },
  637. {
  638. "all nil keys in slice",
  639. &K1{[]*Key{nil, nil}},
  640. &K1{[]*Key{nil, nil}},
  641. "",
  642. "",
  643. },
  644. {
  645. "some nil keys in slice",
  646. &K1{[]*Key{testKey1a, nil, testKey2a}},
  647. &K1{[]*Key{testKey1b, nil, testKey2b}},
  648. "",
  649. "",
  650. },
  651. {
  652. "overflow",
  653. &O0{I: 1 << 48},
  654. &O1{},
  655. "",
  656. "overflow",
  657. },
  658. {
  659. "time",
  660. &T{T: time.Unix(1e9, 0)},
  661. &T{T: time.Unix(1e9, 0)},
  662. "",
  663. "",
  664. },
  665. {
  666. "time as props",
  667. &T{T: time.Unix(1e9, 0)},
  668. &PropertyList{
  669. Property{Name: "T", Value: time.Unix(1e9, 0), NoIndex: false},
  670. },
  671. "",
  672. "",
  673. },
  674. {
  675. "uint save",
  676. &U0{U: 1},
  677. &U0{},
  678. "unsupported struct field",
  679. "",
  680. },
  681. {
  682. "uint load",
  683. &U1{U: "not a uint"},
  684. &U0{},
  685. "",
  686. "type mismatch",
  687. },
  688. {
  689. "zero",
  690. &X0{},
  691. &X0{},
  692. "",
  693. "",
  694. },
  695. {
  696. "basic",
  697. &X0{S: "one", I: 2, i: 3},
  698. &X0{S: "one", I: 2},
  699. "",
  700. "",
  701. },
  702. {
  703. "save string/int load myString/int32",
  704. &X0{S: "one", I: 2, i: 3},
  705. &X1{S: "one", I: 2},
  706. "",
  707. "",
  708. },
  709. {
  710. "missing fields",
  711. &X0{S: "one", I: 2, i: 3},
  712. &X2{},
  713. "",
  714. "no such struct field",
  715. },
  716. {
  717. "save string load bool",
  718. &X0{S: "one", I: 2, i: 3},
  719. &X3{I: 2},
  720. "",
  721. "type mismatch",
  722. },
  723. {
  724. "basic slice",
  725. &Y0{B: true, F: []float64{7, 8, 9}},
  726. &Y0{B: true, F: []float64{7, 8, 9}},
  727. "",
  728. "",
  729. },
  730. {
  731. "save []float64 load float64",
  732. &Y0{B: true, F: []float64{7, 8, 9}},
  733. &Y1{B: true},
  734. "",
  735. "requires a slice",
  736. },
  737. {
  738. "save []float64 load []int64",
  739. &Y0{B: true, F: []float64{7, 8, 9}},
  740. &Y2{B: true},
  741. "",
  742. "type mismatch",
  743. },
  744. {
  745. "single slice is too long",
  746. &Y0{F: make([]float64, maxIndexedProperties+1)},
  747. &Y0{},
  748. "too many indexed properties",
  749. "",
  750. },
  751. {
  752. "two slices are too long",
  753. &Y0{F: make([]float64, maxIndexedProperties), G: make([]float64, maxIndexedProperties)},
  754. &Y0{},
  755. "too many indexed properties",
  756. "",
  757. },
  758. {
  759. "one slice and one scalar are too long",
  760. &Y0{F: make([]float64, maxIndexedProperties), B: true},
  761. &Y0{},
  762. "too many indexed properties",
  763. "",
  764. },
  765. {
  766. "slice of slices of bytes",
  767. &Repeated{
  768. Repeats: []Repeat{
  769. {
  770. Key: "key 1",
  771. Value: []byte("value 1"),
  772. },
  773. {
  774. Key: "key 2",
  775. Value: []byte("value 2"),
  776. },
  777. },
  778. },
  779. &Repeated{
  780. Repeats: []Repeat{
  781. {
  782. Key: "key 1",
  783. Value: []byte("value 1"),
  784. },
  785. {
  786. Key: "key 2",
  787. Value: []byte("value 2"),
  788. },
  789. },
  790. },
  791. "",
  792. "",
  793. },
  794. {
  795. "long blob",
  796. &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
  797. &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
  798. "",
  799. "",
  800. },
  801. {
  802. "long []int8 is too long",
  803. &B1{B: makeInt8Slice(maxIndexedProperties + 1)},
  804. &B1{},
  805. "too many indexed properties",
  806. "",
  807. },
  808. {
  809. "short []int8",
  810. &B1{B: makeInt8Slice(3)},
  811. &B1{B: makeInt8Slice(3)},
  812. "",
  813. "",
  814. },
  815. {
  816. "long myBlob",
  817. &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
  818. &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
  819. "",
  820. "",
  821. },
  822. {
  823. "short myBlob",
  824. &B2{B: makeUint8Slice(3)},
  825. &B2{B: makeUint8Slice(3)},
  826. "",
  827. "",
  828. },
  829. {
  830. "long []myByte",
  831. &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
  832. &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
  833. "",
  834. "",
  835. },
  836. {
  837. "short []myByte",
  838. &B3{B: makeMyByteSlice(3)},
  839. &B3{B: makeMyByteSlice(3)},
  840. "",
  841. "",
  842. },
  843. {
  844. "slice of blobs",
  845. &B4{B: [][]byte{
  846. makeUint8Slice(3),
  847. makeUint8Slice(4),
  848. makeUint8Slice(5),
  849. }},
  850. &B4{B: [][]byte{
  851. makeUint8Slice(3),
  852. makeUint8Slice(4),
  853. makeUint8Slice(5),
  854. }},
  855. "",
  856. "",
  857. },
  858. {
  859. "[]byte must be noindex",
  860. &PropertyList{
  861. Property{Name: "B", Value: makeUint8Slice(1501), NoIndex: false},
  862. },
  863. nil,
  864. "[]byte property too long to index",
  865. "",
  866. },
  867. {
  868. "string must be noindex",
  869. &PropertyList{
  870. Property{Name: "B", Value: strings.Repeat("x", 1501), NoIndex: false},
  871. },
  872. nil,
  873. "string property too long to index",
  874. "",
  875. },
  876. {
  877. "slice of []byte must be noindex",
  878. &PropertyList{
  879. Property{Name: "B", Value: []interface{}{
  880. []byte("short"),
  881. makeUint8Slice(1501),
  882. }, NoIndex: false},
  883. },
  884. nil,
  885. "[]byte property too long to index",
  886. "",
  887. },
  888. {
  889. "slice of string must be noindex",
  890. &PropertyList{
  891. Property{Name: "B", Value: []interface{}{
  892. "short",
  893. strings.Repeat("x", 1501),
  894. }, NoIndex: false},
  895. },
  896. nil,
  897. "string property too long to index",
  898. "",
  899. },
  900. {
  901. "save tagged load props",
  902. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
  903. &PropertyList{
  904. // A and B are renamed to a and b; A and C are noindex, I is ignored.
  905. // Order is sorted as per byName.
  906. Property{Name: "C", Value: int64(3), NoIndex: true},
  907. Property{Name: "D", Value: int64(4), NoIndex: false},
  908. Property{Name: "E", Value: int64(5), NoIndex: false},
  909. Property{Name: "J", Value: int64(7), NoIndex: true},
  910. Property{Name: "a", Value: int64(1), NoIndex: true},
  911. Property{Name: "b", Value: []interface{}{int64(21), int64(22), int64(23)}, NoIndex: false},
  912. },
  913. "",
  914. "",
  915. },
  916. {
  917. "save tagged load tagged",
  918. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
  919. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, J: 7},
  920. "",
  921. "",
  922. },
  923. {
  924. "invalid tagged1",
  925. &InvalidTagged1{I: 1},
  926. &InvalidTagged1{},
  927. "struct tag has invalid property name",
  928. "",
  929. },
  930. {
  931. "invalid tagged2",
  932. &InvalidTagged2{I: 1, J: 2},
  933. &InvalidTagged2{J: 2},
  934. "",
  935. "",
  936. },
  937. {
  938. "invalid tagged3",
  939. &InvalidTagged3{X: "hello"},
  940. &InvalidTagged3{},
  941. "struct tag has invalid property name: \"-\"",
  942. "",
  943. },
  944. {
  945. "invalid tagged4",
  946. &InvalidTagged4{X: "hello"},
  947. &InvalidTagged4{},
  948. "struct tag has invalid option: \"garbage\"",
  949. "",
  950. },
  951. {
  952. "doubler",
  953. &Doubler{S: "s", I: 1, B: true},
  954. &Doubler{S: "ss", I: 2, B: true},
  955. "",
  956. "",
  957. },
  958. {
  959. "save struct load props",
  960. &X0{S: "s", I: 1},
  961. &PropertyList{
  962. Property{Name: "I", Value: int64(1), NoIndex: false},
  963. Property{Name: "S", Value: "s", NoIndex: false},
  964. },
  965. "",
  966. "",
  967. },
  968. {
  969. "save props load struct",
  970. &PropertyList{
  971. Property{Name: "I", Value: int64(1), NoIndex: false},
  972. Property{Name: "S", Value: "s", NoIndex: false},
  973. },
  974. &X0{S: "s", I: 1},
  975. "",
  976. "",
  977. },
  978. {
  979. "nil-value props",
  980. &PropertyList{
  981. Property{Name: "I", Value: nil, NoIndex: false},
  982. Property{Name: "B", Value: nil, NoIndex: false},
  983. Property{Name: "S", Value: nil, NoIndex: false},
  984. Property{Name: "F", Value: nil, NoIndex: false},
  985. Property{Name: "K", Value: nil, NoIndex: false},
  986. Property{Name: "T", Value: nil, NoIndex: false},
  987. Property{Name: "J", Value: []interface{}{nil, int64(7), nil}, NoIndex: false},
  988. },
  989. &struct {
  990. I int64
  991. B bool
  992. S string
  993. F float64
  994. K *Key
  995. T time.Time
  996. J []int64
  997. }{
  998. J: []int64{0, 7, 0},
  999. },
  1000. "",
  1001. "",
  1002. },
  1003. {
  1004. "save outer load props flatten",
  1005. &OuterFlatten{
  1006. A: 1,
  1007. I: []Inner1{
  1008. {10, "ten"},
  1009. {20, "twenty"},
  1010. {30, "thirty"},
  1011. },
  1012. J: Inner2{
  1013. Y: 3.14,
  1014. },
  1015. Inner3: Inner3{
  1016. Z: true,
  1017. },
  1018. K: Inner4{
  1019. X: Inner5{
  1020. WW: 12,
  1021. },
  1022. },
  1023. L: &Inner2{
  1024. Y: 2.71,
  1025. },
  1026. },
  1027. &PropertyList{
  1028. Property{Name: "A", Value: int64(1), NoIndex: false},
  1029. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1030. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1031. Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
  1032. Property{Name: "K.X.WW", Value: int64(12), NoIndex: false},
  1033. Property{Name: "L.Y", Value: float64(2.71), NoIndex: false},
  1034. Property{Name: "Z", Value: true, NoIndex: false},
  1035. },
  1036. "",
  1037. "",
  1038. },
  1039. {
  1040. "load outer props flatten",
  1041. &PropertyList{
  1042. Property{Name: "A", Value: int64(1), NoIndex: false},
  1043. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1044. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1045. Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
  1046. Property{Name: "L.Y", Value: float64(2.71), NoIndex: false},
  1047. Property{Name: "Z", Value: true, NoIndex: false},
  1048. },
  1049. &OuterFlatten{
  1050. A: 1,
  1051. I: []Inner1{
  1052. {10, "ten"},
  1053. {20, "twenty"},
  1054. {30, "thirty"},
  1055. },
  1056. J: Inner2{
  1057. Y: 3.14,
  1058. },
  1059. Inner3: Inner3{
  1060. Z: true,
  1061. },
  1062. L: &Inner2{
  1063. Y: 2.71,
  1064. },
  1065. },
  1066. "",
  1067. "",
  1068. },
  1069. {
  1070. "save outer load props",
  1071. &Outer{
  1072. A: 1,
  1073. I: []Inner1{
  1074. {10, "ten"},
  1075. {20, "twenty"},
  1076. {30, "thirty"},
  1077. },
  1078. J: Inner2{
  1079. Y: 3.14,
  1080. },
  1081. Inner3: Inner3{
  1082. Z: true,
  1083. },
  1084. },
  1085. &PropertyList{
  1086. Property{Name: "A", Value: int64(1), NoIndex: false},
  1087. Property{Name: "I", Value: []interface{}{
  1088. &Entity{
  1089. Properties: []Property{
  1090. {Name: "W", Value: int64(10), NoIndex: false},
  1091. {Name: "X", Value: "ten", NoIndex: false},
  1092. },
  1093. },
  1094. &Entity{
  1095. Properties: []Property{
  1096. {Name: "W", Value: int64(20), NoIndex: false},
  1097. {Name: "X", Value: "twenty", NoIndex: false},
  1098. },
  1099. },
  1100. &Entity{
  1101. Properties: []Property{
  1102. {Name: "W", Value: int64(30), NoIndex: false},
  1103. {Name: "X", Value: "thirty", NoIndex: false},
  1104. },
  1105. },
  1106. }, NoIndex: false},
  1107. Property{Name: "J", Value: &Entity{
  1108. Properties: []Property{
  1109. {Name: "Y", Value: float64(3.14), NoIndex: false},
  1110. },
  1111. }, NoIndex: false},
  1112. Property{Name: "Z", Value: true, NoIndex: false},
  1113. },
  1114. "",
  1115. "",
  1116. },
  1117. {
  1118. "save props load outer-equivalent",
  1119. &PropertyList{
  1120. Property{Name: "A", Value: int64(1), NoIndex: false},
  1121. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1122. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1123. Property{Name: "J.Y", Value: float64(3.14), NoIndex: false},
  1124. Property{Name: "Z", Value: true, NoIndex: false},
  1125. },
  1126. &OuterEquivalent{
  1127. A: 1,
  1128. IDotW: []int32{10, 20, 30},
  1129. IDotX: []string{"ten", "twenty", "thirty"},
  1130. JDotY: 3.14,
  1131. Z: true,
  1132. },
  1133. "",
  1134. "",
  1135. },
  1136. {
  1137. "dotted names save",
  1138. &Dotted{A: DottedA{B: DottedB{C: 88}}},
  1139. &PropertyList{
  1140. Property{Name: "A0.A1.A2", Value: &Entity{
  1141. Properties: []Property{
  1142. {Name: "B3", Value: &Entity{
  1143. Properties: []Property{
  1144. {Name: "C4.C5", Value: int64(88), NoIndex: false},
  1145. },
  1146. }, NoIndex: false},
  1147. },
  1148. }, NoIndex: false},
  1149. },
  1150. "",
  1151. "",
  1152. },
  1153. {
  1154. "dotted names load",
  1155. &PropertyList{
  1156. Property{Name: "A0.A1.A2", Value: &Entity{
  1157. Properties: []Property{
  1158. {Name: "B3", Value: &Entity{
  1159. Properties: []Property{
  1160. {Name: "C4.C5", Value: 99, NoIndex: false},
  1161. },
  1162. }, NoIndex: false},
  1163. },
  1164. }, NoIndex: false},
  1165. },
  1166. &Dotted{A: DottedA{B: DottedB{C: 99}}},
  1167. "",
  1168. "",
  1169. },
  1170. {
  1171. "save struct load deriver",
  1172. &X0{S: "s", I: 1},
  1173. &Deriver{S: "s", Derived: "derived+s"},
  1174. "",
  1175. "",
  1176. },
  1177. {
  1178. "save deriver load struct",
  1179. &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"},
  1180. &X0{S: "s"},
  1181. "",
  1182. "",
  1183. },
  1184. {
  1185. "zero time.Time",
  1186. &T{T: time.Time{}},
  1187. &T{T: time.Time{}},
  1188. "",
  1189. "",
  1190. },
  1191. {
  1192. "time.Time near Unix zero time",
  1193. &T{T: time.Unix(0, 4e3)},
  1194. &T{T: time.Unix(0, 4e3)},
  1195. "",
  1196. "",
  1197. },
  1198. {
  1199. "time.Time, far in the future",
  1200. &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
  1201. &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
  1202. "",
  1203. "",
  1204. },
  1205. {
  1206. "time.Time, very far in the past",
  1207. &T{T: time.Date(-300000, 1, 1, 0, 0, 0, 0, time.UTC)},
  1208. &T{},
  1209. "time value out of range",
  1210. "",
  1211. },
  1212. {
  1213. "time.Time, very far in the future",
  1214. &T{T: time.Date(294248, 1, 1, 0, 0, 0, 0, time.UTC)},
  1215. &T{},
  1216. "time value out of range",
  1217. "",
  1218. },
  1219. {
  1220. "structs",
  1221. &N0{
  1222. X0: X0{S: "one", I: 2, i: 3},
  1223. Nonymous: X0{S: "four", I: 5, i: 6},
  1224. Ignore: "ignore",
  1225. Other: "other",
  1226. },
  1227. &N0{
  1228. X0: X0{S: "one", I: 2},
  1229. Nonymous: X0{S: "four", I: 5},
  1230. Other: "other",
  1231. },
  1232. "",
  1233. "",
  1234. },
  1235. {
  1236. "slice of structs",
  1237. &N1{
  1238. X0: X0{S: "one", I: 2, i: 3},
  1239. Nonymous: []X0{
  1240. {S: "four", I: 5, i: 6},
  1241. {S: "seven", I: 8, i: 9},
  1242. {S: "ten", I: 11, i: 12},
  1243. {S: "thirteen", I: 14, i: 15},
  1244. },
  1245. Ignore: "ignore",
  1246. Other: "other",
  1247. },
  1248. &N1{
  1249. X0: X0{S: "one", I: 2},
  1250. Nonymous: []X0{
  1251. {S: "four", I: 5},
  1252. {S: "seven", I: 8},
  1253. {S: "ten", I: 11},
  1254. {S: "thirteen", I: 14},
  1255. },
  1256. Other: "other",
  1257. },
  1258. "",
  1259. "",
  1260. },
  1261. {
  1262. "structs with slices of structs",
  1263. &N2{
  1264. N1: N1{
  1265. X0: X0{S: "rouge"},
  1266. Nonymous: []X0{
  1267. {S: "rosso0"},
  1268. {S: "rosso1"},
  1269. },
  1270. },
  1271. Green: N1{
  1272. X0: X0{S: "vert"},
  1273. Nonymous: []X0{
  1274. {S: "verde0"},
  1275. {S: "verde1"},
  1276. {S: "verde2"},
  1277. },
  1278. },
  1279. Blue: N1{
  1280. X0: X0{S: "bleu"},
  1281. Nonymous: []X0{
  1282. {S: "blu0"},
  1283. {S: "blu1"},
  1284. {S: "blu2"},
  1285. {S: "blu3"},
  1286. },
  1287. },
  1288. },
  1289. &N2{
  1290. N1: N1{
  1291. X0: X0{S: "rouge"},
  1292. Nonymous: []X0{
  1293. {S: "rosso0"},
  1294. {S: "rosso1"},
  1295. },
  1296. },
  1297. Green: N1{
  1298. X0: X0{S: "vert"},
  1299. Nonymous: []X0{
  1300. {S: "verde0"},
  1301. {S: "verde1"},
  1302. {S: "verde2"},
  1303. },
  1304. },
  1305. Blue: N1{
  1306. X0: X0{S: "bleu"},
  1307. Nonymous: []X0{
  1308. {S: "blu0"},
  1309. {S: "blu1"},
  1310. {S: "blu2"},
  1311. {S: "blu3"},
  1312. },
  1313. },
  1314. },
  1315. "",
  1316. "",
  1317. },
  1318. {
  1319. "save structs load props",
  1320. &N2{
  1321. N1: N1{
  1322. X0: X0{S: "rouge"},
  1323. Nonymous: []X0{
  1324. {S: "rosso0"},
  1325. {S: "rosso1"},
  1326. },
  1327. },
  1328. Green: N1{
  1329. X0: X0{S: "vert"},
  1330. Nonymous: []X0{
  1331. {S: "verde0"},
  1332. {S: "verde1"},
  1333. {S: "verde2"},
  1334. },
  1335. },
  1336. Blue: N1{
  1337. X0: X0{S: "bleu"},
  1338. Nonymous: []X0{
  1339. {S: "blu0"},
  1340. {S: "blu1"},
  1341. {S: "blu2"},
  1342. {S: "blu3"},
  1343. },
  1344. },
  1345. },
  1346. &PropertyList{
  1347. Property{Name: "Blue", Value: &Entity{
  1348. Properties: []Property{
  1349. {Name: "I", Value: int64(0), NoIndex: false},
  1350. {Name: "Nonymous", Value: []interface{}{
  1351. &Entity{
  1352. Properties: []Property{
  1353. {Name: "I", Value: int64(0), NoIndex: false},
  1354. {Name: "S", Value: "blu0", NoIndex: false},
  1355. },
  1356. },
  1357. &Entity{
  1358. Properties: []Property{
  1359. {Name: "I", Value: int64(0), NoIndex: false},
  1360. {Name: "S", Value: "blu1", NoIndex: false},
  1361. },
  1362. },
  1363. &Entity{
  1364. Properties: []Property{
  1365. {Name: "I", Value: int64(0), NoIndex: false},
  1366. {Name: "S", Value: "blu2", NoIndex: false},
  1367. },
  1368. },
  1369. &Entity{
  1370. Properties: []Property{
  1371. {Name: "I", Value: int64(0), NoIndex: false},
  1372. {Name: "S", Value: "blu3", NoIndex: false},
  1373. },
  1374. },
  1375. }, NoIndex: false},
  1376. {Name: "Other", Value: "", NoIndex: false},
  1377. {Name: "S", Value: "bleu", NoIndex: false},
  1378. },
  1379. }, NoIndex: false},
  1380. Property{Name: "green", Value: &Entity{
  1381. Properties: []Property{
  1382. {Name: "I", Value: int64(0), NoIndex: false},
  1383. {Name: "Nonymous", Value: []interface{}{
  1384. &Entity{
  1385. Properties: []Property{
  1386. {Name: "I", Value: int64(0), NoIndex: false},
  1387. {Name: "S", Value: "verde0", NoIndex: false},
  1388. },
  1389. },
  1390. &Entity{
  1391. Properties: []Property{
  1392. {Name: "I", Value: int64(0), NoIndex: false},
  1393. {Name: "S", Value: "verde1", NoIndex: false},
  1394. },
  1395. },
  1396. &Entity{
  1397. Properties: []Property{
  1398. {Name: "I", Value: int64(0), NoIndex: false},
  1399. {Name: "S", Value: "verde2", NoIndex: false},
  1400. },
  1401. },
  1402. }, NoIndex: false},
  1403. {Name: "Other", Value: "", NoIndex: false},
  1404. {Name: "S", Value: "vert", NoIndex: false},
  1405. },
  1406. }, NoIndex: false},
  1407. Property{Name: "red", Value: &Entity{
  1408. Properties: []Property{
  1409. {Name: "I", Value: int64(0), NoIndex: false},
  1410. {Name: "Nonymous", Value: []interface{}{
  1411. &Entity{
  1412. Properties: []Property{
  1413. {Name: "I", Value: int64(0), NoIndex: false},
  1414. {Name: "S", Value: "rosso0", NoIndex: false},
  1415. },
  1416. },
  1417. &Entity{
  1418. Properties: []Property{
  1419. {Name: "I", Value: int64(0), NoIndex: false},
  1420. {Name: "S", Value: "rosso1", NoIndex: false},
  1421. },
  1422. },
  1423. }, NoIndex: false},
  1424. {Name: "Other", Value: "", NoIndex: false},
  1425. {Name: "S", Value: "rouge", NoIndex: false},
  1426. },
  1427. }, NoIndex: false},
  1428. },
  1429. "",
  1430. "",
  1431. },
  1432. {
  1433. "nested entity with key",
  1434. &WithNestedEntityWithKey{
  1435. N: EntityWithKey{
  1436. I: 12,
  1437. S: "abcd",
  1438. K: testKey0,
  1439. },
  1440. },
  1441. &WithNestedEntityWithKey{
  1442. N: EntityWithKey{
  1443. I: 12,
  1444. S: "abcd",
  1445. K: testKey0,
  1446. },
  1447. },
  1448. "",
  1449. "",
  1450. },
  1451. {
  1452. "entity with key at top level",
  1453. &EntityWithKey{
  1454. I: 12,
  1455. S: "abc",
  1456. K: testKey0,
  1457. },
  1458. &EntityWithKey{
  1459. I: 12,
  1460. S: "abc",
  1461. K: testKey0,
  1462. },
  1463. "",
  1464. "",
  1465. },
  1466. {
  1467. "entity with key at top level (key is populated on load)",
  1468. &EntityWithKey{
  1469. I: 12,
  1470. S: "abc",
  1471. },
  1472. &EntityWithKey{
  1473. I: 12,
  1474. S: "abc",
  1475. K: testKey0,
  1476. },
  1477. "",
  1478. "",
  1479. },
  1480. {
  1481. "__key__ field not a *Key",
  1482. &NestedWithNonKeyField{
  1483. N: WithNonKeyField{
  1484. I: 12,
  1485. K: "abcd",
  1486. },
  1487. },
  1488. &NestedWithNonKeyField{
  1489. N: WithNonKeyField{
  1490. I: 12,
  1491. K: "abcd",
  1492. },
  1493. },
  1494. "datastore: __key__ field on struct datastore.WithNonKeyField is not a *datastore.Key",
  1495. "",
  1496. },
  1497. {
  1498. "save struct with ptr to struct fields",
  1499. &PtrToStructField{
  1500. &Basic{
  1501. A: "b",
  1502. },
  1503. &Basic{
  1504. A: "c",
  1505. },
  1506. &Basic{
  1507. A: "anon",
  1508. },
  1509. []*Basic{
  1510. {
  1511. A: "slice0",
  1512. },
  1513. {
  1514. A: "slice1",
  1515. },
  1516. },
  1517. },
  1518. &PropertyList{
  1519. Property{Name: "A", Value: "anon", NoIndex: false},
  1520. Property{Name: "B", Value: &Entity{
  1521. Properties: []Property{
  1522. {Name: "A", Value: "b", NoIndex: false},
  1523. },
  1524. }},
  1525. Property{Name: "D", Value: []interface{}{
  1526. &Entity{
  1527. Properties: []Property{
  1528. {Name: "A", Value: "slice0", NoIndex: false},
  1529. },
  1530. },
  1531. &Entity{
  1532. Properties: []Property{
  1533. {Name: "A", Value: "slice1", NoIndex: false},
  1534. },
  1535. },
  1536. }, NoIndex: false},
  1537. Property{Name: "c", Value: &Entity{
  1538. Properties: []Property{
  1539. {Name: "A", Value: "c", NoIndex: true},
  1540. },
  1541. }, NoIndex: true},
  1542. },
  1543. "",
  1544. "",
  1545. },
  1546. {
  1547. "save and load struct with ptr to struct fields",
  1548. &PtrToStructField{
  1549. &Basic{
  1550. A: "b",
  1551. },
  1552. &Basic{
  1553. A: "c",
  1554. },
  1555. &Basic{
  1556. A: "anon",
  1557. },
  1558. []*Basic{
  1559. {
  1560. A: "slice0",
  1561. },
  1562. {
  1563. A: "slice1",
  1564. },
  1565. },
  1566. },
  1567. &PtrToStructField{
  1568. &Basic{
  1569. A: "b",
  1570. },
  1571. &Basic{
  1572. A: "c",
  1573. },
  1574. &Basic{
  1575. A: "anon",
  1576. },
  1577. []*Basic{
  1578. {
  1579. A: "slice0",
  1580. },
  1581. {
  1582. A: "slice1",
  1583. },
  1584. },
  1585. },
  1586. "",
  1587. "",
  1588. },
  1589. {
  1590. "struct with nil ptr to struct fields",
  1591. &PtrToStructField{
  1592. nil,
  1593. nil,
  1594. nil,
  1595. nil,
  1596. },
  1597. new(PropertyList),
  1598. "",
  1599. "",
  1600. },
  1601. {
  1602. "nested load entity with key",
  1603. &WithNestedEntityWithKey{
  1604. N: EntityWithKey{
  1605. I: 12,
  1606. S: "abcd",
  1607. K: testKey0,
  1608. },
  1609. },
  1610. &PropertyList{
  1611. Property{Name: "N", Value: &Entity{
  1612. Key: testKey0,
  1613. Properties: []Property{
  1614. {Name: "I", Value: int64(12), NoIndex: false},
  1615. {Name: "S", Value: "abcd", NoIndex: false},
  1616. },
  1617. },
  1618. NoIndex: false},
  1619. },
  1620. "",
  1621. "",
  1622. },
  1623. {
  1624. "nested save entity with key",
  1625. &PropertyList{
  1626. Property{Name: "N", Value: &Entity{
  1627. Key: testKey0,
  1628. Properties: []Property{
  1629. {Name: "I", Value: int64(12), NoIndex: false},
  1630. {Name: "S", Value: "abcd", NoIndex: false},
  1631. },
  1632. }, NoIndex: false},
  1633. },
  1634. &WithNestedEntityWithKey{
  1635. N: EntityWithKey{
  1636. I: 12,
  1637. S: "abcd",
  1638. K: testKey0,
  1639. },
  1640. },
  1641. "",
  1642. "",
  1643. },
  1644. {
  1645. "anonymous field with tag",
  1646. &N3{
  1647. C3: C3{C: "s"},
  1648. },
  1649. &PropertyList{
  1650. Property{Name: "red", Value: &Entity{
  1651. Properties: []Property{
  1652. {Name: "C", Value: "s", NoIndex: false},
  1653. },
  1654. }, NoIndex: false},
  1655. },
  1656. "",
  1657. "",
  1658. },
  1659. {
  1660. "unexported anonymous field",
  1661. &N4{
  1662. c4: c4{C: "s"},
  1663. },
  1664. &PropertyList{
  1665. Property{Name: "C", Value: "s", NoIndex: false},
  1666. },
  1667. "",
  1668. "",
  1669. },
  1670. {
  1671. "unexported anonymous field with tag",
  1672. &N5{
  1673. c4: c4{C: "s"},
  1674. },
  1675. new(PropertyList),
  1676. "",
  1677. "",
  1678. },
  1679. {
  1680. "save props load structs with ragged fields",
  1681. &PropertyList{
  1682. Property{Name: "red.S", Value: "rot", NoIndex: false},
  1683. Property{Name: "green.Nonymous.I", Value: []interface{}{int64(10), int64(11), int64(12), int64(13)}, NoIndex: false},
  1684. Property{Name: "Blue.Nonymous.I", Value: []interface{}{int64(20), int64(21)}, NoIndex: false},
  1685. Property{Name: "Blue.Nonymous.S", Value: []interface{}{"blau0", "blau1", "blau2"}, NoIndex: false},
  1686. },
  1687. &N2{
  1688. N1: N1{
  1689. X0: X0{S: "rot"},
  1690. },
  1691. Green: N1{
  1692. Nonymous: []X0{
  1693. {I: 10},
  1694. {I: 11},
  1695. {I: 12},
  1696. {I: 13},
  1697. },
  1698. },
  1699. Blue: N1{
  1700. Nonymous: []X0{
  1701. {S: "blau0", I: 20},
  1702. {S: "blau1", I: 21},
  1703. {S: "blau2"},
  1704. },
  1705. },
  1706. },
  1707. "",
  1708. "",
  1709. },
  1710. {
  1711. "save structs with noindex tags",
  1712. &struct {
  1713. A struct {
  1714. X string `datastore:",noindex"`
  1715. Y string
  1716. } `datastore:",noindex"`
  1717. B struct {
  1718. X string `datastore:",noindex"`
  1719. Y string
  1720. }
  1721. }{},
  1722. &PropertyList{
  1723. Property{Name: "A", Value: &Entity{
  1724. Properties: []Property{
  1725. {Name: "X", Value: "", NoIndex: true},
  1726. {Name: "Y", Value: "", NoIndex: true},
  1727. },
  1728. }, NoIndex: true},
  1729. Property{Name: "B", Value: &Entity{
  1730. Properties: []Property{
  1731. {Name: "X", Value: "", NoIndex: true},
  1732. {Name: "Y", Value: "", NoIndex: false},
  1733. },
  1734. }, NoIndex: false},
  1735. },
  1736. "",
  1737. "",
  1738. },
  1739. {
  1740. "embedded struct with name override",
  1741. &struct {
  1742. Inner1 `datastore:"foo"`
  1743. }{},
  1744. &PropertyList{
  1745. Property{Name: "foo", Value: &Entity{
  1746. Properties: []Property{
  1747. {Name: "W", Value: int64(0), NoIndex: false},
  1748. {Name: "X", Value: "", NoIndex: false},
  1749. },
  1750. }, NoIndex: false},
  1751. },
  1752. "",
  1753. "",
  1754. },
  1755. {
  1756. "slice of slices",
  1757. &SliceOfSlices{},
  1758. nil,
  1759. "flattening nested structs leads to a slice of slices",
  1760. "",
  1761. },
  1762. {
  1763. "recursive struct",
  1764. &Recursive{},
  1765. &Recursive{},
  1766. "",
  1767. "",
  1768. },
  1769. {
  1770. "mutually recursive struct",
  1771. &MutuallyRecursive0{},
  1772. &MutuallyRecursive0{},
  1773. "",
  1774. "",
  1775. },
  1776. {
  1777. "non-exported struct fields",
  1778. &struct {
  1779. i, J int64
  1780. }{i: 1, J: 2},
  1781. &PropertyList{
  1782. Property{Name: "J", Value: int64(2), NoIndex: false},
  1783. },
  1784. "",
  1785. "",
  1786. },
  1787. {
  1788. "json.RawMessage",
  1789. &struct {
  1790. J json.RawMessage
  1791. }{
  1792. J: json.RawMessage("rawr"),
  1793. },
  1794. &PropertyList{
  1795. Property{Name: "J", Value: []byte("rawr"), NoIndex: false},
  1796. },
  1797. "",
  1798. "",
  1799. },
  1800. {
  1801. "json.RawMessage to myBlob",
  1802. &struct {
  1803. B json.RawMessage
  1804. }{
  1805. B: json.RawMessage("rawr"),
  1806. },
  1807. &B2{B: myBlob("rawr")},
  1808. "",
  1809. "",
  1810. },
  1811. {
  1812. "repeated property names",
  1813. &PropertyList{
  1814. Property{Name: "A", Value: ""},
  1815. Property{Name: "A", Value: ""},
  1816. },
  1817. nil,
  1818. "duplicate Property",
  1819. "",
  1820. },
  1821. {
  1822. "embedded time field",
  1823. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1824. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1825. "",
  1826. "",
  1827. },
  1828. {
  1829. "embedded time load",
  1830. &PropertyList{
  1831. Property{Name: "MyTime.Time", Value: ts},
  1832. },
  1833. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1834. "",
  1835. "",
  1836. },
  1837. {
  1838. "pointer fields: nil",
  1839. &Pointers{},
  1840. &Pointers{},
  1841. "",
  1842. "",
  1843. },
  1844. {
  1845. "pointer fields: populated with zeroes",
  1846. populatedPointers(),
  1847. populatedPointers(),
  1848. "",
  1849. "",
  1850. },
  1851. }
  1852. // checkErr returns the empty string if either both want and err are zero,
  1853. // or if want is a non-empty substring of err's string representation.
  1854. func checkErr(want string, err error) string {
  1855. if err != nil {
  1856. got := err.Error()
  1857. if want == "" || !strings.Contains(got, want) {
  1858. return got
  1859. }
  1860. } else if want != "" {
  1861. return fmt.Sprintf("want error %q", want)
  1862. }
  1863. return ""
  1864. }
  1865. func TestRoundTrip(t *testing.T) {
  1866. for _, tc := range testCases {
  1867. p, err := saveEntity(testKey0, tc.src)
  1868. if s := checkErr(tc.putErr, err); s != "" {
  1869. t.Errorf("%s: save: %s", tc.desc, s)
  1870. continue
  1871. }
  1872. if p == nil {
  1873. continue
  1874. }
  1875. var got interface{}
  1876. if _, ok := tc.want.(*PropertyList); ok {
  1877. got = new(PropertyList)
  1878. } else {
  1879. got = reflect.New(reflect.TypeOf(tc.want).Elem()).Interface()
  1880. }
  1881. err = loadEntityProto(got, p)
  1882. if s := checkErr(tc.getErr, err); s != "" {
  1883. t.Errorf("%s: load: %s", tc.desc, s)
  1884. continue
  1885. }
  1886. if pl, ok := got.(*PropertyList); ok {
  1887. // Sort by name to make sure we have a deterministic order.
  1888. sortPL(*pl)
  1889. }
  1890. if !testutil.Equal(got, tc.want, cmp.AllowUnexported(X0{}, X2{})) {
  1891. t.Errorf("%s: compare:\ngot: %+#v\nwant: %+#v", tc.desc, got, tc.want)
  1892. continue
  1893. }
  1894. }
  1895. }
  1896. type aPtrPLS struct {
  1897. Count int
  1898. }
  1899. func (pls *aPtrPLS) Load([]Property) error {
  1900. pls.Count++
  1901. return nil
  1902. }
  1903. func (pls *aPtrPLS) Save() ([]Property, error) {
  1904. return []Property{{Name: "Count", Value: 4}}, nil
  1905. }
  1906. type aValuePLS struct {
  1907. Count int
  1908. }
  1909. func (pls aValuePLS) Load([]Property) error {
  1910. pls.Count += 2
  1911. return nil
  1912. }
  1913. func (pls aValuePLS) Save() ([]Property, error) {
  1914. return []Property{{Name: "Count", Value: 8}}, nil
  1915. }
  1916. type aValuePtrPLS struct {
  1917. Count int
  1918. }
  1919. func (pls *aValuePtrPLS) Load([]Property) error {
  1920. pls.Count = 11
  1921. return nil
  1922. }
  1923. func (pls *aValuePtrPLS) Save() ([]Property, error) {
  1924. return []Property{{Name: "Count", Value: 12}}, nil
  1925. }
  1926. type aNotPLS struct {
  1927. Count int
  1928. }
  1929. type plsString string
  1930. func (s *plsString) Load([]Property) error {
  1931. *s = "LOADED"
  1932. return nil
  1933. }
  1934. func (s *plsString) Save() ([]Property, error) {
  1935. return []Property{{Name: "SS", Value: "SAVED"}}, nil
  1936. }
  1937. func ptrToplsString(s string) *plsString {
  1938. plsStr := plsString(s)
  1939. return &plsStr
  1940. }
  1941. type aSubPLS struct {
  1942. Foo string
  1943. Bar *aPtrPLS
  1944. Baz aValuePtrPLS
  1945. S plsString
  1946. }
  1947. type aSubNotPLS struct {
  1948. Foo string
  1949. Bar *aNotPLS
  1950. }
  1951. type aSubPLSErr struct {
  1952. Foo string
  1953. Bar aValuePLS
  1954. }
  1955. type aSubPLSNoErr struct {
  1956. Foo string
  1957. Bar aPtrPLS
  1958. }
  1959. type GrandparentFlatten struct {
  1960. Parent Parent `datastore:",flatten"`
  1961. }
  1962. type GrandparentOfPtrFlatten struct {
  1963. Parent ParentOfPtr `datastore:",flatten"`
  1964. }
  1965. type GrandparentOfSlice struct {
  1966. Parent ParentOfSlice
  1967. }
  1968. type GrandparentOfSlicePtrs struct {
  1969. Parent ParentOfSlicePtrs
  1970. }
  1971. type GrandparentOfSliceFlatten struct {
  1972. Parent ParentOfSlice `datastore:",flatten"`
  1973. }
  1974. type GrandparentOfSlicePtrsFlatten struct {
  1975. Parent ParentOfSlicePtrs `datastore:",flatten"`
  1976. }
  1977. type Grandparent struct {
  1978. Parent Parent
  1979. }
  1980. type Parent struct {
  1981. Child Child
  1982. String plsString
  1983. }
  1984. type ParentOfPtr struct {
  1985. Child *Child
  1986. String *plsString
  1987. }
  1988. type ParentOfSlice struct {
  1989. Children []Child
  1990. Strings []plsString
  1991. }
  1992. type ParentOfSlicePtrs struct {
  1993. Children []*Child
  1994. Strings []*plsString
  1995. }
  1996. type Child struct {
  1997. I int
  1998. Grandchild Grandchild
  1999. }
  2000. type Grandchild struct {
  2001. S string
  2002. }
  2003. func (c *Child) Load(props []Property) error {
  2004. for _, p := range props {
  2005. if p.Name == "I" {
  2006. c.I++
  2007. } else if p.Name == "Grandchild.S" {
  2008. c.Grandchild.S = "grandchild loaded"
  2009. }
  2010. }
  2011. return nil
  2012. }
  2013. func (c *Child) Save() ([]Property, error) {
  2014. v := c.I + 1
  2015. return []Property{
  2016. {Name: "I", Value: v},
  2017. {Name: "Grandchild.S", Value: fmt.Sprintf("grandchild saved %d", v)},
  2018. }, nil
  2019. }
  2020. func TestLoadSavePLS(t *testing.T) {
  2021. type testCase struct {
  2022. desc string
  2023. src interface{}
  2024. wantSave *pb.Entity
  2025. wantLoad interface{}
  2026. saveErr string
  2027. loadErr string
  2028. }
  2029. testCases := []testCase{
  2030. {
  2031. desc: "non-struct implements PLS (top-level)",
  2032. src: ptrToplsString("hello"),
  2033. wantSave: &pb.Entity{
  2034. Key: keyToProto(testKey0),
  2035. Properties: map[string]*pb.Value{
  2036. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2037. },
  2038. },
  2039. wantLoad: ptrToplsString("LOADED"),
  2040. },
  2041. {
  2042. desc: "substructs do implement PLS",
  2043. src: &aSubPLS{Foo: "foo", Bar: &aPtrPLS{Count: 2}, Baz: aValuePtrPLS{Count: 15}, S: "something"},
  2044. wantSave: &pb.Entity{
  2045. Key: keyToProto(testKey0),
  2046. Properties: map[string]*pb.Value{
  2047. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2048. "Bar": {ValueType: &pb.Value_EntityValue{
  2049. EntityValue: &pb.Entity{
  2050. Properties: map[string]*pb.Value{
  2051. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 4}},
  2052. },
  2053. },
  2054. }},
  2055. "Baz": {ValueType: &pb.Value_EntityValue{
  2056. EntityValue: &pb.Entity{
  2057. Properties: map[string]*pb.Value{
  2058. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 12}},
  2059. },
  2060. },
  2061. }},
  2062. "S": {ValueType: &pb.Value_EntityValue{
  2063. EntityValue: &pb.Entity{
  2064. Properties: map[string]*pb.Value{
  2065. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2066. },
  2067. },
  2068. }},
  2069. },
  2070. },
  2071. wantLoad: &aSubPLS{Foo: "foo", Bar: &aPtrPLS{Count: 1}, Baz: aValuePtrPLS{Count: 11}, S: "LOADED"},
  2072. },
  2073. {
  2074. desc: "substruct (ptr) does implement PLS, nil valued substruct",
  2075. src: &aSubPLS{Foo: "foo", S: "something"},
  2076. wantSave: &pb.Entity{
  2077. Key: keyToProto(testKey0),
  2078. Properties: map[string]*pb.Value{
  2079. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2080. "Baz": {ValueType: &pb.Value_EntityValue{
  2081. EntityValue: &pb.Entity{
  2082. Properties: map[string]*pb.Value{
  2083. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 12}},
  2084. },
  2085. },
  2086. }},
  2087. "S": {ValueType: &pb.Value_EntityValue{
  2088. EntityValue: &pb.Entity{
  2089. Properties: map[string]*pb.Value{
  2090. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2091. },
  2092. },
  2093. }},
  2094. },
  2095. },
  2096. wantLoad: &aSubPLS{Foo: "foo", Baz: aValuePtrPLS{Count: 11}, S: "LOADED"},
  2097. },
  2098. {
  2099. desc: "substruct (ptr) does not implement PLS",
  2100. src: &aSubNotPLS{Foo: "foo", Bar: &aNotPLS{Count: 2}},
  2101. wantSave: &pb.Entity{
  2102. Key: keyToProto(testKey0),
  2103. Properties: map[string]*pb.Value{
  2104. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2105. "Bar": {ValueType: &pb.Value_EntityValue{
  2106. EntityValue: &pb.Entity{
  2107. Properties: map[string]*pb.Value{
  2108. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  2109. },
  2110. },
  2111. }},
  2112. },
  2113. },
  2114. wantLoad: &aSubNotPLS{Foo: "foo", Bar: &aNotPLS{Count: 2}},
  2115. },
  2116. {
  2117. desc: "substruct (value) does implement PLS, error on save",
  2118. src: &aSubPLSErr{Foo: "foo", Bar: aValuePLS{Count: 2}},
  2119. wantSave: (*pb.Entity)(nil),
  2120. wantLoad: &aSubPLSErr{},
  2121. saveErr: "PropertyLoadSaver methods must be implemented on a pointer",
  2122. },
  2123. {
  2124. desc: "substruct (value) does implement PLS, error on load",
  2125. src: &aSubPLSNoErr{Foo: "foo", Bar: aPtrPLS{Count: 2}},
  2126. wantSave: &pb.Entity{
  2127. Key: keyToProto(testKey0),
  2128. Properties: map[string]*pb.Value{
  2129. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2130. "Bar": {ValueType: &pb.Value_EntityValue{
  2131. EntityValue: &pb.Entity{
  2132. Properties: map[string]*pb.Value{
  2133. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 4}},
  2134. },
  2135. },
  2136. }},
  2137. },
  2138. },
  2139. wantLoad: &aSubPLSErr{},
  2140. loadErr: "PropertyLoadSaver methods must be implemented on a pointer",
  2141. },
  2142. {
  2143. desc: "parent does not have flatten option, child impl PLS",
  2144. src: &Grandparent{
  2145. Parent: Parent{
  2146. Child: Child{
  2147. I: 9,
  2148. Grandchild: Grandchild{
  2149. S: "BAD",
  2150. },
  2151. },
  2152. String: plsString("something"),
  2153. },
  2154. },
  2155. wantSave: &pb.Entity{
  2156. Key: keyToProto(testKey0),
  2157. Properties: map[string]*pb.Value{
  2158. "Parent": {ValueType: &pb.Value_EntityValue{
  2159. EntityValue: &pb.Entity{
  2160. Properties: map[string]*pb.Value{
  2161. "Child": {ValueType: &pb.Value_EntityValue{
  2162. EntityValue: &pb.Entity{
  2163. Properties: map[string]*pb.Value{
  2164. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2165. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2166. },
  2167. },
  2168. }},
  2169. "String": {ValueType: &pb.Value_EntityValue{
  2170. EntityValue: &pb.Entity{
  2171. Properties: map[string]*pb.Value{
  2172. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2173. },
  2174. },
  2175. }},
  2176. },
  2177. },
  2178. }},
  2179. },
  2180. },
  2181. wantLoad: &Grandparent{
  2182. Parent: Parent{
  2183. Child: Child{
  2184. I: 1,
  2185. Grandchild: Grandchild{
  2186. S: "grandchild loaded",
  2187. },
  2188. },
  2189. String: "LOADED",
  2190. },
  2191. },
  2192. },
  2193. {
  2194. desc: "parent has flatten option enabled, child impl PLS",
  2195. src: &GrandparentFlatten{
  2196. Parent: Parent{
  2197. Child: Child{
  2198. I: 7,
  2199. Grandchild: Grandchild{
  2200. S: "BAD",
  2201. },
  2202. },
  2203. String: plsString("something"),
  2204. },
  2205. },
  2206. wantSave: &pb.Entity{
  2207. Key: keyToProto(testKey0),
  2208. Properties: map[string]*pb.Value{
  2209. "Parent.Child.I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2210. "Parent.Child.Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2211. "Parent.String.SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2212. },
  2213. },
  2214. wantLoad: &GrandparentFlatten{
  2215. Parent: Parent{
  2216. Child: Child{
  2217. I: 1,
  2218. Grandchild: Grandchild{
  2219. S: "grandchild loaded",
  2220. },
  2221. },
  2222. String: "LOADED",
  2223. },
  2224. },
  2225. },
  2226. {
  2227. desc: "parent has flatten option enabled, child (ptr to) impl PLS",
  2228. src: &GrandparentOfPtrFlatten{
  2229. Parent: ParentOfPtr{
  2230. Child: &Child{
  2231. I: 7,
  2232. Grandchild: Grandchild{
  2233. S: "BAD",
  2234. },
  2235. },
  2236. String: ptrToplsString("something"),
  2237. },
  2238. },
  2239. wantSave: &pb.Entity{
  2240. Key: keyToProto(testKey0),
  2241. Properties: map[string]*pb.Value{
  2242. "Parent.Child.I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2243. "Parent.Child.Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2244. "Parent.String.SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2245. },
  2246. },
  2247. wantLoad: &GrandparentOfPtrFlatten{
  2248. Parent: ParentOfPtr{
  2249. Child: &Child{
  2250. I: 1,
  2251. Grandchild: Grandchild{
  2252. S: "grandchild loaded",
  2253. },
  2254. },
  2255. String: ptrToplsString("LOADED"),
  2256. },
  2257. },
  2258. },
  2259. {
  2260. desc: "children (slice of) impl PLS",
  2261. src: &GrandparentOfSlice{
  2262. Parent: ParentOfSlice{
  2263. Children: []Child{
  2264. {
  2265. I: 7,
  2266. Grandchild: Grandchild{
  2267. S: "BAD",
  2268. },
  2269. },
  2270. {
  2271. I: 9,
  2272. Grandchild: Grandchild{
  2273. S: "BAD2",
  2274. },
  2275. },
  2276. },
  2277. Strings: []plsString{
  2278. "something1",
  2279. "something2",
  2280. },
  2281. },
  2282. },
  2283. wantSave: &pb.Entity{
  2284. Key: keyToProto(testKey0),
  2285. Properties: map[string]*pb.Value{
  2286. "Parent": {ValueType: &pb.Value_EntityValue{
  2287. EntityValue: &pb.Entity{
  2288. Properties: map[string]*pb.Value{
  2289. "Children": {ValueType: &pb.Value_ArrayValue{
  2290. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2291. {ValueType: &pb.Value_EntityValue{
  2292. EntityValue: &pb.Entity{
  2293. Properties: map[string]*pb.Value{
  2294. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2295. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2296. },
  2297. },
  2298. }},
  2299. {ValueType: &pb.Value_EntityValue{
  2300. EntityValue: &pb.Entity{
  2301. Properties: map[string]*pb.Value{
  2302. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2303. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2304. },
  2305. },
  2306. }},
  2307. }},
  2308. }},
  2309. "Strings": {ValueType: &pb.Value_ArrayValue{
  2310. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2311. {ValueType: &pb.Value_EntityValue{
  2312. EntityValue: &pb.Entity{
  2313. Properties: map[string]*pb.Value{
  2314. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2315. },
  2316. },
  2317. }},
  2318. {ValueType: &pb.Value_EntityValue{
  2319. EntityValue: &pb.Entity{
  2320. Properties: map[string]*pb.Value{
  2321. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2322. },
  2323. },
  2324. }},
  2325. }},
  2326. }},
  2327. },
  2328. },
  2329. }},
  2330. },
  2331. },
  2332. wantLoad: &GrandparentOfSlice{
  2333. Parent: ParentOfSlice{
  2334. Children: []Child{
  2335. {
  2336. I: 1,
  2337. Grandchild: Grandchild{
  2338. S: "grandchild loaded",
  2339. },
  2340. },
  2341. {
  2342. I: 1,
  2343. Grandchild: Grandchild{
  2344. S: "grandchild loaded",
  2345. },
  2346. },
  2347. },
  2348. Strings: []plsString{
  2349. "LOADED",
  2350. "LOADED",
  2351. },
  2352. },
  2353. },
  2354. },
  2355. {
  2356. desc: "children (slice of ptrs) impl PLS",
  2357. src: &GrandparentOfSlicePtrs{
  2358. Parent: ParentOfSlicePtrs{
  2359. Children: []*Child{
  2360. {
  2361. I: 7,
  2362. Grandchild: Grandchild{
  2363. S: "BAD",
  2364. },
  2365. },
  2366. {
  2367. I: 9,
  2368. Grandchild: Grandchild{
  2369. S: "BAD2",
  2370. },
  2371. },
  2372. },
  2373. Strings: []*plsString{
  2374. ptrToplsString("something1"),
  2375. ptrToplsString("something2"),
  2376. },
  2377. },
  2378. },
  2379. wantSave: &pb.Entity{
  2380. Key: keyToProto(testKey0),
  2381. Properties: map[string]*pb.Value{
  2382. "Parent": {ValueType: &pb.Value_EntityValue{
  2383. EntityValue: &pb.Entity{
  2384. Properties: map[string]*pb.Value{
  2385. "Children": {ValueType: &pb.Value_ArrayValue{
  2386. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2387. {ValueType: &pb.Value_EntityValue{
  2388. EntityValue: &pb.Entity{
  2389. Properties: map[string]*pb.Value{
  2390. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2391. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2392. },
  2393. },
  2394. }},
  2395. {ValueType: &pb.Value_EntityValue{
  2396. EntityValue: &pb.Entity{
  2397. Properties: map[string]*pb.Value{
  2398. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2399. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2400. },
  2401. },
  2402. }},
  2403. }},
  2404. }},
  2405. "Strings": {ValueType: &pb.Value_ArrayValue{
  2406. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2407. {ValueType: &pb.Value_EntityValue{
  2408. EntityValue: &pb.Entity{
  2409. Properties: map[string]*pb.Value{
  2410. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2411. },
  2412. },
  2413. }},
  2414. {ValueType: &pb.Value_EntityValue{
  2415. EntityValue: &pb.Entity{
  2416. Properties: map[string]*pb.Value{
  2417. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2418. },
  2419. },
  2420. }},
  2421. }},
  2422. }},
  2423. },
  2424. },
  2425. }},
  2426. },
  2427. },
  2428. wantLoad: &GrandparentOfSlicePtrs{
  2429. Parent: ParentOfSlicePtrs{
  2430. Children: []*Child{
  2431. {
  2432. I: 1,
  2433. Grandchild: Grandchild{
  2434. S: "grandchild loaded",
  2435. },
  2436. },
  2437. {
  2438. I: 1,
  2439. Grandchild: Grandchild{
  2440. S: "grandchild loaded",
  2441. },
  2442. },
  2443. },
  2444. Strings: []*plsString{
  2445. ptrToplsString("LOADED"),
  2446. ptrToplsString("LOADED"),
  2447. },
  2448. },
  2449. },
  2450. },
  2451. {
  2452. desc: "parent has flatten option, children (slice of) impl PLS",
  2453. src: &GrandparentOfSliceFlatten{
  2454. Parent: ParentOfSlice{
  2455. Children: []Child{
  2456. {
  2457. I: 7,
  2458. Grandchild: Grandchild{
  2459. S: "BAD",
  2460. },
  2461. },
  2462. {
  2463. I: 9,
  2464. Grandchild: Grandchild{
  2465. S: "BAD2",
  2466. },
  2467. },
  2468. },
  2469. Strings: []plsString{
  2470. "something1",
  2471. "something2",
  2472. },
  2473. },
  2474. },
  2475. wantSave: &pb.Entity{
  2476. Key: keyToProto(testKey0),
  2477. Properties: map[string]*pb.Value{
  2478. "Parent.Children.I": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2479. Values: []*pb.Value{
  2480. {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2481. {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2482. },
  2483. },
  2484. }},
  2485. "Parent.Children.Grandchild.S": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2486. Values: []*pb.Value{
  2487. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2488. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2489. },
  2490. },
  2491. }},
  2492. "Parent.Strings.SS": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2493. Values: []*pb.Value{
  2494. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2495. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2496. },
  2497. },
  2498. }},
  2499. },
  2500. },
  2501. wantLoad: &GrandparentOfSliceFlatten{
  2502. Parent: ParentOfSlice{
  2503. Children: []Child{
  2504. {
  2505. I: 1,
  2506. Grandchild: Grandchild{
  2507. S: "grandchild loaded",
  2508. },
  2509. },
  2510. {
  2511. I: 1,
  2512. Grandchild: Grandchild{
  2513. S: "grandchild loaded",
  2514. },
  2515. },
  2516. },
  2517. Strings: []plsString{
  2518. "LOADED",
  2519. "LOADED",
  2520. },
  2521. },
  2522. },
  2523. },
  2524. {
  2525. desc: "parent has flatten option, children (slice of ptrs) impl PLS",
  2526. src: &GrandparentOfSlicePtrsFlatten{
  2527. Parent: ParentOfSlicePtrs{
  2528. Children: []*Child{
  2529. {
  2530. I: 7,
  2531. Grandchild: Grandchild{
  2532. S: "BAD",
  2533. },
  2534. },
  2535. {
  2536. I: 9,
  2537. Grandchild: Grandchild{
  2538. S: "BAD2",
  2539. },
  2540. },
  2541. },
  2542. Strings: []*plsString{
  2543. ptrToplsString("something1"),
  2544. ptrToplsString("something1"),
  2545. },
  2546. },
  2547. },
  2548. wantSave: &pb.Entity{
  2549. Key: keyToProto(testKey0),
  2550. Properties: map[string]*pb.Value{
  2551. "Parent.Children.I": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2552. Values: []*pb.Value{
  2553. {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2554. {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2555. },
  2556. },
  2557. }},
  2558. "Parent.Children.Grandchild.S": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2559. Values: []*pb.Value{
  2560. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2561. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2562. },
  2563. },
  2564. }},
  2565. "Parent.Strings.SS": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2566. Values: []*pb.Value{
  2567. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2568. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2569. },
  2570. },
  2571. }},
  2572. },
  2573. },
  2574. wantLoad: &GrandparentOfSlicePtrsFlatten{
  2575. Parent: ParentOfSlicePtrs{
  2576. Children: []*Child{
  2577. {
  2578. I: 1,
  2579. Grandchild: Grandchild{
  2580. S: "grandchild loaded",
  2581. },
  2582. },
  2583. {
  2584. I: 1,
  2585. Grandchild: Grandchild{
  2586. S: "grandchild loaded",
  2587. },
  2588. },
  2589. },
  2590. Strings: []*plsString{
  2591. ptrToplsString("LOADED"),
  2592. ptrToplsString("LOADED"),
  2593. },
  2594. },
  2595. },
  2596. },
  2597. }
  2598. for _, tc := range testCases {
  2599. e, err := saveEntity(testKey0, tc.src)
  2600. if tc.saveErr == "" { // Want no error.
  2601. if err != nil {
  2602. t.Errorf("%s: save: %v", tc.desc, err)
  2603. continue
  2604. }
  2605. if !testutil.Equal(e, tc.wantSave) {
  2606. t.Errorf("%s: save: \ngot: %+v\nwant: %+v", tc.desc, e, tc.wantSave)
  2607. continue
  2608. }
  2609. } else { // Want error.
  2610. if err == nil {
  2611. t.Errorf("%s: save: want err", tc.desc)
  2612. continue
  2613. }
  2614. if !strings.Contains(err.Error(), tc.saveErr) {
  2615. t.Errorf("%s: save: \ngot err '%s'\nwant err '%s'", tc.desc, err.Error(), tc.saveErr)
  2616. }
  2617. continue
  2618. }
  2619. gota := reflect.New(reflect.TypeOf(tc.wantLoad).Elem()).Interface()
  2620. err = loadEntityProto(gota, e)
  2621. if tc.loadErr == "" { // Want no error.
  2622. if err != nil {
  2623. t.Errorf("%s: load: %v", tc.desc, err)
  2624. continue
  2625. }
  2626. if !testutil.Equal(gota, tc.wantLoad) {
  2627. t.Errorf("%s: load: \ngot: %+v\nwant: %+v", tc.desc, gota, tc.wantLoad)
  2628. continue
  2629. }
  2630. } else { // Want error.
  2631. if err == nil {
  2632. t.Errorf("%s: load: want err", tc.desc)
  2633. continue
  2634. }
  2635. if !strings.Contains(err.Error(), tc.loadErr) {
  2636. t.Errorf("%s: load: \ngot err '%s'\nwant err '%s'", tc.desc, err.Error(), tc.loadErr)
  2637. }
  2638. }
  2639. }
  2640. }
  2641. func TestQueryConstruction(t *testing.T) {
  2642. tests := []struct {
  2643. q, exp *Query
  2644. err string
  2645. }{
  2646. {
  2647. q: NewQuery("Foo"),
  2648. exp: &Query{
  2649. kind: "Foo",
  2650. limit: -1,
  2651. },
  2652. },
  2653. {
  2654. // Regular filtered query with standard spacing.
  2655. q: NewQuery("Foo").Filter("foo >", 7),
  2656. exp: &Query{
  2657. kind: "Foo",
  2658. filter: []filter{
  2659. {
  2660. FieldName: "foo",
  2661. Op: greaterThan,
  2662. Value: 7,
  2663. },
  2664. },
  2665. limit: -1,
  2666. },
  2667. },
  2668. {
  2669. // Filtered query with no spacing.
  2670. q: NewQuery("Foo").Filter("foo=", 6),
  2671. exp: &Query{
  2672. kind: "Foo",
  2673. filter: []filter{
  2674. {
  2675. FieldName: "foo",
  2676. Op: equal,
  2677. Value: 6,
  2678. },
  2679. },
  2680. limit: -1,
  2681. },
  2682. },
  2683. {
  2684. // Filtered query with funky spacing.
  2685. q: NewQuery("Foo").Filter(" foo< ", 8),
  2686. exp: &Query{
  2687. kind: "Foo",
  2688. filter: []filter{
  2689. {
  2690. FieldName: "foo",
  2691. Op: lessThan,
  2692. Value: 8,
  2693. },
  2694. },
  2695. limit: -1,
  2696. },
  2697. },
  2698. {
  2699. // Filtered query with multicharacter op.
  2700. q: NewQuery("Foo").Filter("foo >=", 9),
  2701. exp: &Query{
  2702. kind: "Foo",
  2703. filter: []filter{
  2704. {
  2705. FieldName: "foo",
  2706. Op: greaterEq,
  2707. Value: 9,
  2708. },
  2709. },
  2710. limit: -1,
  2711. },
  2712. },
  2713. {
  2714. // Query with ordering.
  2715. q: NewQuery("Foo").Order("bar"),
  2716. exp: &Query{
  2717. kind: "Foo",
  2718. order: []order{
  2719. {
  2720. FieldName: "bar",
  2721. Direction: ascending,
  2722. },
  2723. },
  2724. limit: -1,
  2725. },
  2726. },
  2727. {
  2728. // Query with reverse ordering, and funky spacing.
  2729. q: NewQuery("Foo").Order(" - bar"),
  2730. exp: &Query{
  2731. kind: "Foo",
  2732. order: []order{
  2733. {
  2734. FieldName: "bar",
  2735. Direction: descending,
  2736. },
  2737. },
  2738. limit: -1,
  2739. },
  2740. },
  2741. {
  2742. // Query with an empty ordering.
  2743. q: NewQuery("Foo").Order(""),
  2744. err: "empty order",
  2745. },
  2746. {
  2747. // Query with a + ordering.
  2748. q: NewQuery("Foo").Order("+bar"),
  2749. err: "invalid order",
  2750. },
  2751. }
  2752. for i, test := range tests {
  2753. if test.q.err != nil {
  2754. got := test.q.err.Error()
  2755. if !strings.Contains(got, test.err) {
  2756. t.Errorf("%d: error mismatch: got %q want something containing %q", i, got, test.err)
  2757. }
  2758. continue
  2759. }
  2760. if !testutil.Equal(test.q, test.exp, cmp.AllowUnexported(Query{})) {
  2761. t.Errorf("%d: mismatch: got %v want %v", i, test.q, test.exp)
  2762. }
  2763. }
  2764. }
  2765. func TestPutMultiTypes(t *testing.T) {
  2766. ctx := context.Background()
  2767. type S struct {
  2768. A int
  2769. B string
  2770. }
  2771. testCases := []struct {
  2772. desc string
  2773. src interface{}
  2774. wantErr bool
  2775. }{
  2776. // Test cases to check each of the valid input types for src.
  2777. // Each case has the same elements.
  2778. {
  2779. desc: "type []struct",
  2780. src: []S{
  2781. {1, "one"}, {2, "two"},
  2782. },
  2783. },
  2784. {
  2785. desc: "type []*struct",
  2786. src: []*S{
  2787. {1, "one"}, {2, "two"},
  2788. },
  2789. },
  2790. {
  2791. desc: "type []interface{} with PLS elems",
  2792. src: []interface{}{
  2793. &PropertyList{Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2794. &PropertyList{Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2795. },
  2796. },
  2797. {
  2798. desc: "type []interface{} with struct ptr elems",
  2799. src: []interface{}{
  2800. &S{1, "one"}, &S{2, "two"},
  2801. },
  2802. },
  2803. {
  2804. desc: "type []PropertyLoadSaver{}",
  2805. src: []PropertyLoadSaver{
  2806. &PropertyList{Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2807. &PropertyList{Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2808. },
  2809. },
  2810. {
  2811. desc: "type []P (non-pointer, *P implements PropertyLoadSaver)",
  2812. src: []PropertyList{
  2813. {Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2814. {Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2815. },
  2816. },
  2817. // Test some invalid cases.
  2818. {
  2819. desc: "type []interface{} with struct elems",
  2820. src: []interface{}{
  2821. S{1, "one"}, S{2, "two"},
  2822. },
  2823. wantErr: true,
  2824. },
  2825. {
  2826. desc: "PropertyList",
  2827. src: PropertyList{
  2828. Property{Name: "A", Value: 1},
  2829. Property{Name: "B", Value: "one"},
  2830. },
  2831. wantErr: true,
  2832. },
  2833. {
  2834. desc: "type []int",
  2835. src: []int{1, 2},
  2836. wantErr: true,
  2837. },
  2838. {
  2839. desc: "not a slice",
  2840. src: S{1, "one"},
  2841. wantErr: true,
  2842. },
  2843. }
  2844. // Use the same keys and expected entities for all tests.
  2845. keys := []*Key{
  2846. NameKey("testKind", "first", nil),
  2847. NameKey("testKind", "second", nil),
  2848. }
  2849. want := []*pb.Mutation{
  2850. {Operation: &pb.Mutation_Upsert{
  2851. Upsert: &pb.Entity{
  2852. Key: keyToProto(keys[0]),
  2853. Properties: map[string]*pb.Value{
  2854. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  2855. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  2856. },
  2857. }}},
  2858. {Operation: &pb.Mutation_Upsert{
  2859. Upsert: &pb.Entity{
  2860. Key: keyToProto(keys[1]),
  2861. Properties: map[string]*pb.Value{
  2862. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  2863. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  2864. },
  2865. }}},
  2866. }
  2867. for _, tt := range testCases {
  2868. // Set up a fake client which captures upserts.
  2869. var got []*pb.Mutation
  2870. client := &Client{
  2871. client: &fakeClient{
  2872. commitFn: func(req *pb.CommitRequest) (*pb.CommitResponse, error) {
  2873. got = req.Mutations
  2874. return &pb.CommitResponse{}, nil
  2875. },
  2876. },
  2877. }
  2878. _, err := client.PutMulti(ctx, keys, tt.src)
  2879. if err != nil {
  2880. if !tt.wantErr {
  2881. t.Errorf("%s: error %v", tt.desc, err)
  2882. }
  2883. continue
  2884. }
  2885. if tt.wantErr {
  2886. t.Errorf("%s: wanted error, but none returned", tt.desc)
  2887. continue
  2888. }
  2889. if len(got) != len(want) {
  2890. t.Errorf("%s: got %d entities, want %d", tt.desc, len(got), len(want))
  2891. continue
  2892. }
  2893. for i, e := range got {
  2894. if !proto.Equal(e, want[i]) {
  2895. t.Logf("%s: entity %d doesn't match\ngot: %v\nwant: %v", tt.desc, i, e, want[i])
  2896. }
  2897. }
  2898. }
  2899. }
  2900. func TestNoIndexOnSliceProperties(t *testing.T) {
  2901. // Check that ExcludeFromIndexes is set on the inner elements,
  2902. // rather than the top-level ArrayValue value.
  2903. pl := PropertyList{
  2904. Property{
  2905. Name: "repeated",
  2906. Value: []interface{}{
  2907. 123,
  2908. false,
  2909. "short",
  2910. strings.Repeat("a", 1503),
  2911. },
  2912. NoIndex: true,
  2913. },
  2914. }
  2915. key := NameKey("dummy", "dummy", nil)
  2916. entity, err := saveEntity(key, &pl)
  2917. if err != nil {
  2918. t.Fatalf("saveEntity: %v", err)
  2919. }
  2920. want := &pb.Value{
  2921. ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2922. {ValueType: &pb.Value_IntegerValue{IntegerValue: 123}, ExcludeFromIndexes: true},
  2923. {ValueType: &pb.Value_BooleanValue{BooleanValue: false}, ExcludeFromIndexes: true},
  2924. {ValueType: &pb.Value_StringValue{StringValue: "short"}, ExcludeFromIndexes: true},
  2925. {ValueType: &pb.Value_StringValue{StringValue: strings.Repeat("a", 1503)}, ExcludeFromIndexes: true},
  2926. }}},
  2927. }
  2928. if got := entity.Properties["repeated"]; !proto.Equal(got, want) {
  2929. t.Errorf("Entity proto differs\ngot: %v\nwant: %v", got, want)
  2930. }
  2931. }
  2932. type byName PropertyList
  2933. func (s byName) Len() int { return len(s) }
  2934. func (s byName) Less(i, j int) bool { return s[i].Name < s[j].Name }
  2935. func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  2936. // sortPL sorts the property list by property name, and
  2937. // recursively sorts any nested property lists, or nested slices of
  2938. // property lists.
  2939. func sortPL(pl PropertyList) {
  2940. sort.Stable(byName(pl))
  2941. for _, p := range pl {
  2942. switch p.Value.(type) {
  2943. case *Entity:
  2944. sortPL(p.Value.(*Entity).Properties)
  2945. case []interface{}:
  2946. for _, p2 := range p.Value.([]interface{}) {
  2947. if nent, ok := p2.(*Entity); ok {
  2948. sortPL(nent.Properties)
  2949. }
  2950. }
  2951. }
  2952. }
  2953. }
  2954. func TestValidGeoPoint(t *testing.T) {
  2955. testCases := []struct {
  2956. desc string
  2957. pt GeoPoint
  2958. want bool
  2959. }{
  2960. {
  2961. "valid",
  2962. GeoPoint{67.21, 13.37},
  2963. true,
  2964. },
  2965. {
  2966. "high lat",
  2967. GeoPoint{-90.01, 13.37},
  2968. false,
  2969. },
  2970. {
  2971. "low lat",
  2972. GeoPoint{90.01, 13.37},
  2973. false,
  2974. },
  2975. {
  2976. "high lng",
  2977. GeoPoint{67.21, 182},
  2978. false,
  2979. },
  2980. {
  2981. "low lng",
  2982. GeoPoint{67.21, -181},
  2983. false,
  2984. },
  2985. }
  2986. for _, tc := range testCases {
  2987. if got := tc.pt.Valid(); got != tc.want {
  2988. t.Errorf("%s: got %v, want %v", tc.desc, got, tc.want)
  2989. }
  2990. }
  2991. }
  2992. func TestPutInvalidEntity(t *testing.T) {
  2993. // Test that trying to put an invalid entity always returns the correct error
  2994. // type.
  2995. // Fake client that can pretend to start a transaction.
  2996. fakeClient := &fakeDatastoreClient{
  2997. beginTransaction: func(*pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error) {
  2998. return &pb.BeginTransactionResponse{
  2999. Transaction: []byte("deadbeef"),
  3000. }, nil
  3001. },
  3002. }
  3003. client := &Client{
  3004. client: fakeClient,
  3005. }
  3006. ctx := context.Background()
  3007. key := IncompleteKey("kind", nil)
  3008. _, err := client.Put(ctx, key, "invalid entity")
  3009. if err != ErrInvalidEntityType {
  3010. t.Errorf("client.Put returned err %v, want %v", err, ErrInvalidEntityType)
  3011. }
  3012. _, err = client.PutMulti(ctx, []*Key{key}, []interface{}{"invalid entity"})
  3013. if me, ok := err.(MultiError); !ok {
  3014. t.Errorf("client.PutMulti returned err %v, want MultiError type", err)
  3015. } else if len(me) != 1 || me[0] != ErrInvalidEntityType {
  3016. t.Errorf("client.PutMulti returned err %v, want MulitError{ErrInvalidEntityType}", err)
  3017. }
  3018. client.RunInTransaction(ctx, func(tx *Transaction) error {
  3019. _, err := tx.Put(key, "invalid entity")
  3020. if err != ErrInvalidEntityType {
  3021. t.Errorf("tx.Put returned err %v, want %v", err, ErrInvalidEntityType)
  3022. }
  3023. _, err = tx.PutMulti([]*Key{key}, []interface{}{"invalid entity"})
  3024. if me, ok := err.(MultiError); !ok {
  3025. t.Errorf("tx.PutMulti returned err %v, want MultiError type", err)
  3026. } else if len(me) != 1 || me[0] != ErrInvalidEntityType {
  3027. t.Errorf("tx.PutMulti returned err %v, want MulitError{ErrInvalidEntityType}", err)
  3028. }
  3029. return errors.New("bang") // Return error: we don't actually want to commit.
  3030. })
  3031. }
  3032. func TestDeferred(t *testing.T) {
  3033. type Ent struct {
  3034. A int
  3035. B string
  3036. }
  3037. keys := []*Key{
  3038. NameKey("testKind", "first", nil),
  3039. NameKey("testKind", "second", nil),
  3040. }
  3041. entity1 := &pb.Entity{
  3042. Key: keyToProto(keys[0]),
  3043. Properties: map[string]*pb.Value{
  3044. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  3045. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  3046. },
  3047. }
  3048. entity2 := &pb.Entity{
  3049. Key: keyToProto(keys[1]),
  3050. Properties: map[string]*pb.Value{
  3051. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  3052. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  3053. },
  3054. }
  3055. // count keeps track of the number of times fakeClient.lookup has been
  3056. // called.
  3057. var count int
  3058. // Fake client that will return Deferred keys in resp on the first call.
  3059. fakeClient := &fakeDatastoreClient{
  3060. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3061. count++
  3062. // On the first call, we return deferred keys.
  3063. if count == 1 {
  3064. return &pb.LookupResponse{
  3065. Found: []*pb.EntityResult{
  3066. {
  3067. Entity: entity1,
  3068. Version: 1,
  3069. },
  3070. },
  3071. Deferred: []*pb.Key{
  3072. keyToProto(keys[1]),
  3073. },
  3074. }, nil
  3075. }
  3076. // On the second call, we do not return any more deferred keys.
  3077. return &pb.LookupResponse{
  3078. Found: []*pb.EntityResult{
  3079. {
  3080. Entity: entity2,
  3081. Version: 1,
  3082. },
  3083. },
  3084. }, nil
  3085. },
  3086. }
  3087. client := &Client{
  3088. client: fakeClient,
  3089. }
  3090. ctx := context.Background()
  3091. dst := make([]Ent, len(keys))
  3092. err := client.GetMulti(ctx, keys, dst)
  3093. if err != nil {
  3094. t.Fatalf("client.Get: %v", err)
  3095. }
  3096. if count != 2 {
  3097. t.Fatalf("expected client.lookup to be called 2 times. Got %d", count)
  3098. }
  3099. if len(dst) != 2 {
  3100. t.Fatalf("expected 2 entities returned, got %d", len(dst))
  3101. }
  3102. for _, e := range dst {
  3103. if e.A == 1 {
  3104. if e.B != "one" {
  3105. t.Fatalf("unexpected entity %+v", e)
  3106. }
  3107. } else if e.A == 2 {
  3108. if e.B != "two" {
  3109. t.Fatalf("unexpected entity %+v", e)
  3110. }
  3111. } else {
  3112. t.Fatalf("unexpected entity %+v", e)
  3113. }
  3114. }
  3115. }
  3116. type KeyLoaderEnt struct {
  3117. A int
  3118. K *Key
  3119. }
  3120. func (e *KeyLoaderEnt) Load(p []Property) error {
  3121. e.A = 2
  3122. return nil
  3123. }
  3124. func (e *KeyLoaderEnt) LoadKey(k *Key) error {
  3125. e.K = k
  3126. return nil
  3127. }
  3128. func (e *KeyLoaderEnt) Save() ([]Property, error) {
  3129. return []Property{{Name: "A", Value: int64(3)}}, nil
  3130. }
  3131. func TestKeyLoaderEndToEnd(t *testing.T) {
  3132. keys := []*Key{
  3133. NameKey("testKind", "first", nil),
  3134. NameKey("testKind", "second", nil),
  3135. }
  3136. entity1 := &pb.Entity{
  3137. Key: keyToProto(keys[0]),
  3138. Properties: map[string]*pb.Value{
  3139. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  3140. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  3141. },
  3142. }
  3143. entity2 := &pb.Entity{
  3144. Key: keyToProto(keys[1]),
  3145. Properties: map[string]*pb.Value{
  3146. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  3147. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  3148. },
  3149. }
  3150. fakeClient := &fakeDatastoreClient{
  3151. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3152. return &pb.LookupResponse{
  3153. Found: []*pb.EntityResult{
  3154. {
  3155. Entity: entity1,
  3156. Version: 1,
  3157. },
  3158. {
  3159. Entity: entity2,
  3160. Version: 1,
  3161. },
  3162. },
  3163. }, nil
  3164. },
  3165. }
  3166. client := &Client{
  3167. client: fakeClient,
  3168. }
  3169. ctx := context.Background()
  3170. dst := make([]*KeyLoaderEnt, len(keys))
  3171. err := client.GetMulti(ctx, keys, dst)
  3172. if err != nil {
  3173. t.Fatalf("client.Get: %v", err)
  3174. }
  3175. for i := range dst {
  3176. if !testutil.Equal(dst[i].K, keys[i]) {
  3177. t.Fatalf("unexpected entity %d to have key %+v, got %+v", i, keys[i], dst[i].K)
  3178. }
  3179. }
  3180. }
  3181. func TestDeferredMissing(t *testing.T) {
  3182. type Ent struct {
  3183. A int
  3184. B string
  3185. }
  3186. keys := []*Key{
  3187. NameKey("testKind", "first", nil),
  3188. NameKey("testKind", "second", nil),
  3189. }
  3190. entity1 := &pb.Entity{
  3191. Key: keyToProto(keys[0]),
  3192. }
  3193. entity2 := &pb.Entity{
  3194. Key: keyToProto(keys[1]),
  3195. }
  3196. var count int
  3197. fakeClient := &fakeDatastoreClient{
  3198. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3199. count++
  3200. if count == 1 {
  3201. return &pb.LookupResponse{
  3202. Missing: []*pb.EntityResult{
  3203. {
  3204. Entity: entity1,
  3205. Version: 1,
  3206. },
  3207. },
  3208. Deferred: []*pb.Key{
  3209. keyToProto(keys[1]),
  3210. },
  3211. }, nil
  3212. }
  3213. return &pb.LookupResponse{
  3214. Missing: []*pb.EntityResult{
  3215. {
  3216. Entity: entity2,
  3217. Version: 1,
  3218. },
  3219. },
  3220. }, nil
  3221. },
  3222. }
  3223. client := &Client{
  3224. client: fakeClient,
  3225. }
  3226. ctx := context.Background()
  3227. dst := make([]Ent, len(keys))
  3228. err := client.GetMulti(ctx, keys, dst)
  3229. errs, ok := err.(MultiError)
  3230. if !ok {
  3231. t.Fatalf("expected error returns to be MultiError; got %v", err)
  3232. }
  3233. if len(errs) != 2 {
  3234. t.Fatalf("expected 2 errors returns, got %d", len(errs))
  3235. }
  3236. if errs[0] != ErrNoSuchEntity {
  3237. t.Fatalf("expected error to be ErrNoSuchEntity; got %v", errs[0])
  3238. }
  3239. if errs[1] != ErrNoSuchEntity {
  3240. t.Fatalf("expected error to be ErrNoSuchEntity; got %v", errs[1])
  3241. }
  3242. if count != 2 {
  3243. t.Fatalf("expected client.lookup to be called 2 times. Got %d", count)
  3244. }
  3245. if len(dst) != 2 {
  3246. t.Fatalf("expected 2 entities returned, got %d", len(dst))
  3247. }
  3248. for _, e := range dst {
  3249. if e.A != 0 || e.B != "" {
  3250. t.Fatalf("unexpected entity %+v", e)
  3251. }
  3252. }
  3253. }
  3254. func TestGetWithNilKey(t *testing.T) {
  3255. client := &Client{}
  3256. err := client.Get(context.Background(), nil, []Property{})
  3257. if err != ErrInvalidKey {
  3258. t.Fatalf("want ErrInvalidKey, got %v", err)
  3259. }
  3260. }
  3261. func TestGetMultiWithNilKey(t *testing.T) {
  3262. client := &Client{}
  3263. dest := make([]PropertyList, 1)
  3264. err := client.GetMulti(context.Background(), []*Key{nil}, dest)
  3265. if me, ok := err.(MultiError); !ok {
  3266. t.Fatalf("want MultiError, got %v", err)
  3267. } else if len(me) != 1 || me[0] != ErrInvalidKey {
  3268. t.Fatalf("want MultiError{ErrInvalidKey}, got %v", me)
  3269. }
  3270. }
  3271. func TestGetWithIncompleteKey(t *testing.T) {
  3272. client := &Client{}
  3273. err := client.Get(context.Background(), &Key{Kind: "testKind"}, []Property{})
  3274. if err == nil {
  3275. t.Fatalf("want err, got nil")
  3276. }
  3277. }
  3278. func TestGetMultiWithIncompleteKey(t *testing.T) {
  3279. client := &Client{}
  3280. dest := make([]PropertyList, 1)
  3281. err := client.GetMulti(context.Background(), []*Key{{Kind: "testKind"}}, dest)
  3282. if me, ok := err.(MultiError); !ok {
  3283. t.Fatalf("want MultiError, got %v", err)
  3284. } else if len(me) != 1 || me[0] == nil {
  3285. t.Fatalf("want MultiError{err}, got %v", me)
  3286. }
  3287. }
  3288. func TestDeleteWithNilKey(t *testing.T) {
  3289. client := &Client{}
  3290. err := client.Delete(context.Background(), nil)
  3291. if err != ErrInvalidKey {
  3292. t.Fatalf("want ErrInvalidKey, got %v", err)
  3293. }
  3294. }
  3295. func TestDeleteMultiWithNilKey(t *testing.T) {
  3296. client := &Client{}
  3297. err := client.DeleteMulti(context.Background(), []*Key{nil})
  3298. if me, ok := err.(MultiError); !ok {
  3299. t.Fatalf("want MultiError, got %v", err)
  3300. } else if len(me) != 1 || me[0] != ErrInvalidKey {
  3301. t.Fatalf("want MultiError{ErrInvalidKey}, got %v", me)
  3302. }
  3303. }
  3304. func TestDeleteWithIncompleteKey(t *testing.T) {
  3305. client := &Client{}
  3306. err := client.Delete(context.Background(), &Key{Kind: "testKind"})
  3307. if err == nil {
  3308. t.Fatalf("want err, got nil")
  3309. }
  3310. }
  3311. func TestDeleteMultiWithIncompleteKey(t *testing.T) {
  3312. client := &Client{}
  3313. err := client.DeleteMulti(context.Background(), []*Key{{Kind: "testKind"}})
  3314. if me, ok := err.(MultiError); !ok {
  3315. t.Fatalf("want MultiError, got %v", err)
  3316. } else if len(me) != 1 || me[0] == nil {
  3317. t.Fatalf("want MultiError{err}, got %v", me)
  3318. }
  3319. }
  3320. type fakeDatastoreClient struct {
  3321. pb.DatastoreClient
  3322. // Optional handlers for the datastore methods.
  3323. // Any handlers left undefined will return an error.
  3324. lookup func(*pb.LookupRequest) (*pb.LookupResponse, error)
  3325. runQuery func(*pb.RunQueryRequest) (*pb.RunQueryResponse, error)
  3326. beginTransaction func(*pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error)
  3327. commit func(*pb.CommitRequest) (*pb.CommitResponse, error)
  3328. rollback func(*pb.RollbackRequest) (*pb.RollbackResponse, error)
  3329. allocateIds func(*pb.AllocateIdsRequest) (*pb.AllocateIdsResponse, error)
  3330. }
  3331. func (c *fakeDatastoreClient) Lookup(ctx context.Context, in *pb.LookupRequest, opts ...grpc.CallOption) (*pb.LookupResponse, error) {
  3332. if c.lookup == nil {
  3333. return nil, errors.New("no lookup handler defined")
  3334. }
  3335. return c.lookup(in)
  3336. }
  3337. func (c *fakeDatastoreClient) RunQuery(ctx context.Context, in *pb.RunQueryRequest, opts ...grpc.CallOption) (*pb.RunQueryResponse, error) {
  3338. if c.runQuery == nil {
  3339. return nil, errors.New("no runQuery handler defined")
  3340. }
  3341. return c.runQuery(in)
  3342. }
  3343. func (c *fakeDatastoreClient) BeginTransaction(ctx context.Context, in *pb.BeginTransactionRequest, opts ...grpc.CallOption) (*pb.BeginTransactionResponse, error) {
  3344. if c.beginTransaction == nil {
  3345. return nil, errors.New("no beginTransaction handler defined")
  3346. }
  3347. return c.beginTransaction(in)
  3348. }
  3349. func (c *fakeDatastoreClient) Commit(ctx context.Context, in *pb.CommitRequest, opts ...grpc.CallOption) (*pb.CommitResponse, error) {
  3350. if c.commit == nil {
  3351. return nil, errors.New("no commit handler defined")
  3352. }
  3353. return c.commit(in)
  3354. }
  3355. func (c *fakeDatastoreClient) Rollback(ctx context.Context, in *pb.RollbackRequest, opts ...grpc.CallOption) (*pb.RollbackResponse, error) {
  3356. if c.rollback == nil {
  3357. return nil, errors.New("no rollback handler defined")
  3358. }
  3359. return c.rollback(in)
  3360. }
  3361. func (c *fakeDatastoreClient) AllocateIds(ctx context.Context, in *pb.AllocateIdsRequest, opts ...grpc.CallOption) (*pb.AllocateIdsResponse, error) {
  3362. if c.allocateIds == nil {
  3363. return nil, errors.New("no allocateIds handler defined")
  3364. }
  3365. return c.allocateIds(in)
  3366. }