123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368 |
- // Copyright 2014 Google Inc. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package datastore
- import (
- "encoding/json"
- "errors"
- "fmt"
- "reflect"
- "strings"
- "testing"
- "time"
- )
- type (
- myBlob []byte
- myByte byte
- myString string
- )
- func makeMyByteSlice(n int) []myByte {
- b := make([]myByte, n)
- for i := range b {
- b[i] = myByte(i)
- }
- return b
- }
- func makeInt8Slice(n int) []int8 {
- b := make([]int8, n)
- for i := range b {
- b[i] = int8(i)
- }
- return b
- }
- func makeUint8Slice(n int) []uint8 {
- b := make([]uint8, n)
- for i := range b {
- b[i] = uint8(i)
- }
- return b
- }
- func newKey(stringID string, parent *Key) *Key {
- return &Key{
- kind: "kind",
- name: stringID,
- id: 0,
- parent: parent,
- }
- }
- var (
- testKey0 = newKey("name0", nil)
- testKey1a = newKey("name1", nil)
- testKey1b = newKey("name1", nil)
- testKey2a = newKey("name2", testKey0)
- testKey2b = newKey("name2", testKey0)
- )
- type B0 struct {
- B []byte `datastore:",noindex"`
- }
- type B1 struct {
- B []int8
- }
- type B2 struct {
- B myBlob `datastore:",noindex"`
- }
- type B3 struct {
- B []myByte `datastore:",noindex"`
- }
- type B4 struct {
- B [][]byte
- }
- type C0 struct {
- I int
- C chan int
- }
- type C1 struct {
- I int
- C *chan int
- }
- type C2 struct {
- I int
- C []chan int
- }
- type C3 struct {
- C string
- }
- type E struct{}
- type K0 struct {
- K *Key
- }
- type K1 struct {
- K []*Key
- }
- type N0 struct {
- X0
- Nonymous X0
- Ignore string `datastore:"-"`
- Other string
- }
- type N1 struct {
- X0
- Nonymous []X0
- Ignore string `datastore:"-"`
- Other string
- }
- type N2 struct {
- N1 `datastore:"red"`
- Green N1 `datastore:"green"`
- Blue N1
- White N1 `datastore:"-"`
- }
- type O0 struct {
- I int64
- }
- type O1 struct {
- I int32
- }
- type U0 struct {
- U uint
- }
- type U1 struct {
- U string
- }
- type T struct {
- T time.Time
- }
- type X0 struct {
- S string
- I int
- i int
- }
- type X1 struct {
- S myString
- I int32
- J int64
- }
- type X2 struct {
- Z string
- i int
- }
- type X3 struct {
- S bool
- I int
- }
- type Y0 struct {
- B bool
- F []float64
- G []float64
- }
- type Y1 struct {
- B bool
- F float64
- }
- type Y2 struct {
- B bool
- F []int64
- }
- type Tagged struct {
- A int `datastore:"a,noindex"`
- B []int `datastore:"b"`
- C int `datastore:",noindex"`
- D int `datastore:""`
- E int
- I int `datastore:"-"`
- J int `datastore:",noindex" json:"j"`
- Y0 `datastore:"-"`
- Z chan int `datastore:"-,"`
- }
- type InvalidTagged1 struct {
- I int `datastore:"\t"`
- }
- type InvalidTagged2 struct {
- I int
- J int `datastore:"I"`
- }
- type Inner1 struct {
- W int32
- X string
- }
- type Inner2 struct {
- Y float64
- }
- type Inner3 struct {
- Z bool
- }
- type Outer struct {
- A int16
- I []Inner1
- J Inner2
- Inner3
- }
- type OuterEquivalent struct {
- A int16
- IDotW []int32 `datastore:"I.W"`
- IDotX []string `datastore:"I.X"`
- JDotY float64 `datastore:"J.Y"`
- Z bool
- }
- type Dotted struct {
- A DottedA `datastore:"A0.A1.A2"`
- }
- type DottedA struct {
- B DottedB `datastore:"B3"`
- }
- type DottedB struct {
- C int `datastore:"C4.C5"`
- }
- type SliceOfSlices struct {
- I int
- S []struct {
- J int
- F []float64
- }
- }
- type Recursive struct {
- I int
- R []Recursive
- }
- type MutuallyRecursive0 struct {
- I int
- R []MutuallyRecursive1
- }
- type MutuallyRecursive1 struct {
- I int
- R []MutuallyRecursive0
- }
- type Doubler struct {
- S string
- I int64
- B bool
- }
- func (d *Doubler) Load(props []Property) error {
- return LoadStruct(d, props)
- }
- func (d *Doubler) Save() ([]Property, error) {
- // Save the default Property slice to an in-memory buffer (a PropertyList).
- props, err := SaveStruct(d)
- if err != nil {
- return nil, err
- }
- var list PropertyList
- if err := list.Load(props); err != nil {
- return nil, err
- }
- // Edit that PropertyList, and send it on.
- for i := range list {
- switch v := list[i].Value.(type) {
- case string:
- // + means string concatenation.
- list[i].Value = v + v
- case int64:
- // + means integer addition.
- list[i].Value = v + v
- }
- }
- return list.Save()
- }
- var _ PropertyLoadSaver = (*Doubler)(nil)
- type Deriver struct {
- S, Derived, Ignored string
- }
- func (e *Deriver) Load(props []Property) error {
- for _, p := range props {
- if p.Name != "S" {
- continue
- }
- e.S = p.Value.(string)
- e.Derived = "derived+" + e.S
- }
- return nil
- }
- func (e *Deriver) Save() ([]Property, error) {
- return []Property{
- {
- Name: "S",
- Value: e.S,
- },
- }, nil
- }
- var _ PropertyLoadSaver = (*Deriver)(nil)
- type BadMultiPropEntity struct{}
- func (e *BadMultiPropEntity) Load(props []Property) error {
- return errors.New("unimplemented")
- }
- func (e *BadMultiPropEntity) Save() ([]Property, error) {
- // Write multiple properties with the same name "I", but Multiple is false.
- var props []Property
- for i := 0; i < 3; i++ {
- props = append(props, Property{
- Name: "I",
- Value: int64(i),
- })
- }
- return props, nil
- }
- var _ PropertyLoadSaver = (*BadMultiPropEntity)(nil)
- type testCase struct {
- desc string
- src interface{}
- want interface{}
- putErr string
- getErr string
- }
- var testCases = []testCase{
- {
- "chan save fails",
- &C0{I: -1},
- &E{},
- "unsupported struct field",
- "",
- },
- {
- "*chan save fails",
- &C1{I: -1},
- &E{},
- "unsupported struct field",
- "",
- },
- {
- "[]chan save fails",
- &C2{I: -1, C: make([]chan int, 8)},
- &E{},
- "unsupported struct field",
- "",
- },
- {
- "chan load fails",
- &C3{C: "not a chan"},
- &C0{},
- "",
- "type mismatch",
- },
- {
- "*chan load fails",
- &C3{C: "not a *chan"},
- &C1{},
- "",
- "type mismatch",
- },
- {
- "[]chan load fails",
- &C3{C: "not a []chan"},
- &C2{},
- "",
- "type mismatch",
- },
- {
- "empty struct",
- &E{},
- &E{},
- "",
- "",
- },
- {
- "key",
- &K0{K: testKey1a},
- &K0{K: testKey1b},
- "",
- "",
- },
- {
- "key with parent",
- &K0{K: testKey2a},
- &K0{K: testKey2b},
- "",
- "",
- },
- {
- "nil key",
- &K0{},
- &K0{},
- "",
- "",
- },
- {
- "all nil keys in slice",
- &K1{[]*Key{nil, nil}},
- &K1{[]*Key{nil, nil}},
- "",
- "",
- },
- {
- "some nil keys in slice",
- &K1{[]*Key{testKey1a, nil, testKey2a}},
- &K1{[]*Key{testKey1b, nil, testKey2b}},
- "",
- "",
- },
- {
- "overflow",
- &O0{I: 1 << 48},
- &O1{},
- "",
- "overflow",
- },
- {
- "time",
- &T{T: time.Unix(1e9, 0)},
- &T{T: time.Unix(1e9, 0)},
- "",
- "",
- },
- {
- "time as props",
- &T{T: time.Unix(1e9, 0)},
- &PropertyList{
- Property{Name: "T", Value: time.Unix(1e9, 0), NoIndex: false},
- },
- "",
- "",
- },
- {
- "uint save",
- &U0{U: 1},
- &U0{},
- "unsupported struct field",
- "",
- },
- {
- "uint load",
- &U1{U: "not a uint"},
- &U0{},
- "",
- "type mismatch",
- },
- {
- "zero",
- &X0{},
- &X0{},
- "",
- "",
- },
- {
- "basic",
- &X0{S: "one", I: 2, i: 3},
- &X0{S: "one", I: 2},
- "",
- "",
- },
- {
- "save string/int load myString/int32",
- &X0{S: "one", I: 2, i: 3},
- &X1{S: "one", I: 2},
- "",
- "",
- },
- {
- "missing fields",
- &X0{S: "one", I: 2, i: 3},
- &X2{},
- "",
- "no such struct field",
- },
- {
- "save string load bool",
- &X0{S: "one", I: 2, i: 3},
- &X3{I: 2},
- "",
- "type mismatch",
- },
- {
- "basic slice",
- &Y0{B: true, F: []float64{7, 8, 9}},
- &Y0{B: true, F: []float64{7, 8, 9}},
- "",
- "",
- },
- {
- "save []float64 load float64",
- &Y0{B: true, F: []float64{7, 8, 9}},
- &Y1{B: true},
- "",
- "requires a slice",
- },
- {
- "save []float64 load []int64",
- &Y0{B: true, F: []float64{7, 8, 9}},
- &Y2{B: true},
- "",
- "type mismatch",
- },
- {
- "single slice is too long",
- &Y0{F: make([]float64, maxIndexedProperties+1)},
- &Y0{},
- "too many indexed properties",
- "",
- },
- {
- "two slices are too long",
- &Y0{F: make([]float64, maxIndexedProperties), G: make([]float64, maxIndexedProperties)},
- &Y0{},
- "too many indexed properties",
- "",
- },
- {
- "one slice and one scalar are too long",
- &Y0{F: make([]float64, maxIndexedProperties), B: true},
- &Y0{},
- "too many indexed properties",
- "",
- },
- {
- "long blob",
- &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
- &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
- "",
- "",
- },
- {
- "long []int8 is too long",
- &B1{B: makeInt8Slice(maxIndexedProperties + 1)},
- &B1{},
- "too many indexed properties",
- "",
- },
- {
- "short []int8",
- &B1{B: makeInt8Slice(3)},
- &B1{B: makeInt8Slice(3)},
- "",
- "",
- },
- {
- "long myBlob",
- &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
- &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
- "",
- "",
- },
- {
- "short myBlob",
- &B2{B: makeUint8Slice(3)},
- &B2{B: makeUint8Slice(3)},
- "",
- "",
- },
- {
- "long []myByte",
- &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
- &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
- "",
- "",
- },
- {
- "short []myByte",
- &B3{B: makeMyByteSlice(3)},
- &B3{B: makeMyByteSlice(3)},
- "",
- "",
- },
- {
- "slice of blobs",
- &B4{B: [][]byte{
- makeUint8Slice(3),
- makeUint8Slice(4),
- makeUint8Slice(5),
- }},
- &B4{B: [][]byte{
- makeUint8Slice(3),
- makeUint8Slice(4),
- makeUint8Slice(5),
- }},
- "",
- "",
- },
- {
- "[]byte must be noindex",
- &PropertyList{
- Property{Name: "B", Value: makeUint8Slice(1501), NoIndex: false},
- },
- nil,
- "is too long to index",
- "",
- },
- {
- "string must be noindex",
- &PropertyList{
- Property{Name: "B", Value: strings.Repeat("x", 1501), NoIndex: false},
- },
- nil,
- "is too long to index",
- "",
- },
- {
- "save tagged load props",
- &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
- &PropertyList{
- // A and B are renamed to a and b; A and C are noindex, I is ignored.
- // Order is alphabetical
- Property{Name: "a", Value: int64(1), NoIndex: true},
- Property{Name: "b", Value: int64(21), NoIndex: false, Multiple: true},
- Property{Name: "b", Value: int64(22), NoIndex: false, Multiple: true},
- Property{Name: "b", Value: int64(23), NoIndex: false, Multiple: true},
- Property{Name: "C", Value: int64(3), NoIndex: true},
- Property{Name: "D", Value: int64(4), NoIndex: false},
- Property{Name: "E", Value: int64(5), NoIndex: false},
- Property{Name: "J", Value: int64(7), NoIndex: true},
- },
- "",
- "",
- },
- {
- "save tagged load tagged",
- &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
- &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, J: 7},
- "",
- "",
- },
- {
- "save props load tagged",
- &PropertyList{
- Property{Name: "A", Value: int64(11), NoIndex: true},
- Property{Name: "a", Value: int64(12), NoIndex: true},
- },
- &Tagged{A: 12},
- "",
- `cannot load field "A"`,
- },
- {
- "invalid tagged1",
- &InvalidTagged1{I: 1},
- &InvalidTagged1{},
- "struct tag has invalid property name",
- "",
- },
- {
- "invalid tagged2",
- &InvalidTagged2{I: 1, J: 2},
- &InvalidTagged2{},
- "struct tag has repeated property name",
- "",
- },
- {
- "doubler",
- &Doubler{S: "s", I: 1, B: true},
- &Doubler{S: "ss", I: 2, B: true},
- "",
- "",
- },
- {
- "save struct load props",
- &X0{S: "s", I: 1},
- &PropertyList{
- Property{Name: "S", Value: "s", NoIndex: false},
- Property{Name: "I", Value: int64(1), NoIndex: false},
- },
- "",
- "",
- },
- {
- "save props load struct",
- &PropertyList{
- Property{Name: "S", Value: "s", NoIndex: false},
- Property{Name: "I", Value: int64(1), NoIndex: false},
- },
- &X0{S: "s", I: 1},
- "",
- "",
- },
- {
- "nil-value props",
- &PropertyList{
- Property{Name: "I", Value: nil, NoIndex: false},
- Property{Name: "B", Value: nil, NoIndex: false},
- Property{Name: "S", Value: nil, NoIndex: false},
- Property{Name: "F", Value: nil, NoIndex: false},
- Property{Name: "K", Value: nil, NoIndex: false},
- Property{Name: "T", Value: nil, NoIndex: false},
- Property{Name: "J", Value: nil, NoIndex: false},
- Property{Name: "J", Value: int64(7), NoIndex: false},
- Property{Name: "J", Value: nil, NoIndex: false},
- },
- &struct {
- I int64
- B bool
- S string
- F float64
- K *Key
- T time.Time
- J []int64
- }{
- J: []int64{0, 7, 0},
- },
- "",
- "",
- },
- {
- "save outer load props",
- &Outer{
- A: 1,
- I: []Inner1{
- {10, "ten"},
- {20, "twenty"},
- {30, "thirty"},
- },
- J: Inner2{
- Y: 3.14,
- },
- Inner3: Inner3{
- Z: true,
- },
- },
- &PropertyList{
- Property{Name: "A", Value: int64(1), NoIndex: false},
- Property{Name: "I.W", Value: int64(10), NoIndex: false, Multiple: true},
- Property{Name: "I.W", Value: int64(20), NoIndex: false, Multiple: true},
- Property{Name: "I.W", Value: int64(30), NoIndex: false, Multiple: true},
- Property{Name: "I.X", Value: "ten", NoIndex: false, Multiple: true},
- Property{Name: "I.X", Value: "twenty", NoIndex: false, Multiple: true},
- Property{Name: "I.X", Value: "thirty", NoIndex: false, Multiple: true},
- Property{Name: "J.Y", Value: float64(3.14), NoIndex: false},
- Property{Name: "Z", Value: true, NoIndex: false},
- },
- "",
- "",
- },
- {
- "save props load outer-equivalent",
- &PropertyList{
- Property{Name: "A", Value: int64(1), NoIndex: false},
- Property{Name: "I.W", Value: int64(10), NoIndex: false},
- Property{Name: "I.X", Value: "ten", NoIndex: false},
- Property{Name: "I.W", Value: int64(20), NoIndex: false},
- Property{Name: "I.X", Value: "twenty", NoIndex: false},
- Property{Name: "I.W", Value: int64(30), NoIndex: false},
- Property{Name: "I.X", Value: "thirty", NoIndex: false},
- Property{Name: "J.Y", Value: float64(3.14), NoIndex: false},
- Property{Name: "Z", Value: true, NoIndex: false},
- },
- &OuterEquivalent{
- A: 1,
- IDotW: []int32{10, 20, 30},
- IDotX: []string{"ten", "twenty", "thirty"},
- JDotY: 3.14,
- Z: true,
- },
- "",
- "",
- },
- {
- "save outer-equivalent load outer",
- &OuterEquivalent{
- A: 1,
- IDotW: []int32{10, 20, 30},
- IDotX: []string{"ten", "twenty", "thirty"},
- JDotY: 3.14,
- Z: true,
- },
- &Outer{
- A: 1,
- I: []Inner1{
- {10, "ten"},
- {20, "twenty"},
- {30, "thirty"},
- },
- J: Inner2{
- Y: 3.14,
- },
- Inner3: Inner3{
- Z: true,
- },
- },
- "",
- "",
- },
- {
- "dotted names save",
- &Dotted{A: DottedA{B: DottedB{C: 88}}},
- &PropertyList{
- Property{Name: "A0.A1.A2.B3.C4.C5", Value: int64(88), NoIndex: false},
- },
- "",
- "",
- },
- {
- "dotted names load",
- &PropertyList{
- Property{Name: "A0.A1.A2.B3.C4.C5", Value: int64(99), NoIndex: false},
- },
- &Dotted{A: DottedA{B: DottedB{C: 99}}},
- "",
- "",
- },
- {
- "save struct load deriver",
- &X0{S: "s", I: 1},
- &Deriver{S: "s", Derived: "derived+s"},
- "",
- "",
- },
- {
- "save deriver load struct",
- &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"},
- &X0{S: "s"},
- "",
- "",
- },
- {
- "zero time.Time",
- &T{T: time.Time{}},
- &T{T: time.Time{}},
- "",
- "",
- },
- {
- "time.Time near Unix zero time",
- &T{T: time.Unix(0, 4e3)},
- &T{T: time.Unix(0, 4e3)},
- "",
- "",
- },
- {
- "time.Time, far in the future",
- &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
- &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
- "",
- "",
- },
- {
- "time.Time, very far in the past",
- &T{T: time.Date(-300000, 1, 1, 0, 0, 0, 0, time.UTC)},
- &T{},
- "time value out of range",
- "",
- },
- {
- "time.Time, very far in the future",
- &T{T: time.Date(294248, 1, 1, 0, 0, 0, 0, time.UTC)},
- &T{},
- "time value out of range",
- "",
- },
- {
- "structs",
- &N0{
- X0: X0{S: "one", I: 2, i: 3},
- Nonymous: X0{S: "four", I: 5, i: 6},
- Ignore: "ignore",
- Other: "other",
- },
- &N0{
- X0: X0{S: "one", I: 2},
- Nonymous: X0{S: "four", I: 5},
- Other: "other",
- },
- "",
- "",
- },
- {
- "slice of structs",
- &N1{
- X0: X0{S: "one", I: 2, i: 3},
- Nonymous: []X0{
- {S: "four", I: 5, i: 6},
- {S: "seven", I: 8, i: 9},
- {S: "ten", I: 11, i: 12},
- {S: "thirteen", I: 14, i: 15},
- },
- Ignore: "ignore",
- Other: "other",
- },
- &N1{
- X0: X0{S: "one", I: 2},
- Nonymous: []X0{
- {S: "four", I: 5},
- {S: "seven", I: 8},
- {S: "ten", I: 11},
- {S: "thirteen", I: 14},
- },
- Other: "other",
- },
- "",
- "",
- },
- {
- "structs with slices of structs",
- &N2{
- N1: N1{
- X0: X0{S: "rouge"},
- Nonymous: []X0{
- {S: "rosso0"},
- {S: "rosso1"},
- },
- },
- Green: N1{
- X0: X0{S: "vert"},
- Nonymous: []X0{
- {S: "verde0"},
- {S: "verde1"},
- {S: "verde2"},
- },
- },
- Blue: N1{
- X0: X0{S: "bleu"},
- Nonymous: []X0{
- {S: "blu0"},
- {S: "blu1"},
- {S: "blu2"},
- {S: "blu3"},
- },
- },
- },
- &N2{
- N1: N1{
- X0: X0{S: "rouge"},
- Nonymous: []X0{
- {S: "rosso0"},
- {S: "rosso1"},
- },
- },
- Green: N1{
- X0: X0{S: "vert"},
- Nonymous: []X0{
- {S: "verde0"},
- {S: "verde1"},
- {S: "verde2"},
- },
- },
- Blue: N1{
- X0: X0{S: "bleu"},
- Nonymous: []X0{
- {S: "blu0"},
- {S: "blu1"},
- {S: "blu2"},
- {S: "blu3"},
- },
- },
- },
- "",
- "",
- },
- {
- "save structs load props",
- &N2{
- N1: N1{
- X0: X0{S: "rouge"},
- Nonymous: []X0{
- {S: "rosso0"},
- {S: "rosso1"},
- },
- },
- Green: N1{
- X0: X0{S: "vert"},
- Nonymous: []X0{
- {S: "verde0"},
- {S: "verde1"},
- {S: "verde2"},
- },
- },
- Blue: N1{
- X0: X0{S: "bleu"},
- Nonymous: []X0{
- {S: "blu0"},
- {S: "blu1"},
- {S: "blu2"},
- {S: "blu3"},
- },
- },
- },
- &PropertyList{
- Property{Name: "red.S", Value: "rouge", NoIndex: false},
- Property{Name: "red.I", Value: int64(0), NoIndex: false},
- Property{Name: "red.Nonymous.S", Value: "rosso0", NoIndex: false, Multiple: true},
- Property{Name: "red.Nonymous.S", Value: "rosso1", NoIndex: false, Multiple: true},
- Property{Name: "red.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "red.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "red.Other", Value: "", NoIndex: false},
- Property{Name: "green.S", Value: "vert", NoIndex: false},
- Property{Name: "green.I", Value: int64(0), NoIndex: false},
- Property{Name: "green.Nonymous.S", Value: "verde0", NoIndex: false, Multiple: true},
- Property{Name: "green.Nonymous.S", Value: "verde1", NoIndex: false, Multiple: true},
- Property{Name: "green.Nonymous.S", Value: "verde2", NoIndex: false, Multiple: true},
- Property{Name: "green.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "green.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "green.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "green.Other", Value: "", NoIndex: false},
- Property{Name: "Blue.S", Value: "bleu", NoIndex: false},
- Property{Name: "Blue.I", Value: int64(0), NoIndex: false},
- Property{Name: "Blue.Nonymous.S", Value: "blu0", NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.S", Value: "blu1", NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.S", Value: "blu2", NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.S", Value: "blu3", NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "Blue.Nonymous.I", Value: int64(0), NoIndex: false, Multiple: true},
- Property{Name: "Blue.Other", Value: "", NoIndex: false},
- },
- "",
- "",
- },
- {
- "save props load structs with ragged fields",
- &PropertyList{
- Property{Name: "red.S", Value: "rot", NoIndex: false},
- Property{Name: "green.Nonymous.I", Value: int64(10), NoIndex: false},
- Property{Name: "green.Nonymous.I", Value: int64(11), NoIndex: false},
- Property{Name: "green.Nonymous.I", Value: int64(12), NoIndex: false},
- Property{Name: "green.Nonymous.I", Value: int64(13), NoIndex: false},
- Property{Name: "Blue.Nonymous.S", Value: "blau0", NoIndex: false},
- Property{Name: "Blue.Nonymous.I", Value: int64(20), NoIndex: false},
- Property{Name: "Blue.Nonymous.S", Value: "blau1", NoIndex: false},
- Property{Name: "Blue.Nonymous.I", Value: int64(21), NoIndex: false},
- Property{Name: "Blue.Nonymous.S", Value: "blau2", NoIndex: false},
- },
- &N2{
- N1: N1{
- X0: X0{S: "rot"},
- },
- Green: N1{
- Nonymous: []X0{
- {I: 10},
- {I: 11},
- {I: 12},
- {I: 13},
- },
- },
- Blue: N1{
- Nonymous: []X0{
- {S: "blau0", I: 20},
- {S: "blau1", I: 21},
- {S: "blau2"},
- },
- },
- },
- "",
- "",
- },
- {
- "save structs with noindex tags",
- &struct {
- A struct {
- X string `datastore:",noindex"`
- Y string
- } `datastore:",noindex"`
- B struct {
- X string `datastore:",noindex"`
- Y string
- }
- }{},
- &PropertyList{
- Property{Name: "A.X", Value: "", NoIndex: true},
- Property{Name: "A.Y", Value: "", NoIndex: true},
- Property{Name: "B.X", Value: "", NoIndex: true},
- Property{Name: "B.Y", Value: "", NoIndex: false},
- },
- "",
- "",
- },
- {
- "embedded struct with name override",
- &struct {
- Inner1 `datastore:"foo"`
- }{},
- &PropertyList{
- Property{Name: "foo.W", Value: int64(0), NoIndex: false},
- Property{Name: "foo.X", Value: "", NoIndex: false},
- },
- "",
- "",
- },
- {
- "slice of slices",
- &SliceOfSlices{},
- nil,
- "flattening nested structs leads to a slice of slices",
- "",
- },
- {
- "recursive struct",
- &Recursive{},
- nil,
- "recursive struct",
- "",
- },
- {
- "mutually recursive struct",
- &MutuallyRecursive0{},
- nil,
- "recursive struct",
- "",
- },
- {
- "non-exported struct fields",
- &struct {
- i, J int64
- }{i: 1, J: 2},
- &PropertyList{
- Property{Name: "J", Value: int64(2), NoIndex: false},
- },
- "",
- "",
- },
- {
- "json.RawMessage",
- &struct {
- J json.RawMessage
- }{
- J: json.RawMessage("rawr"),
- },
- &PropertyList{
- Property{Name: "J", Value: []byte("rawr"), NoIndex: false},
- },
- "",
- "",
- },
- {
- "json.RawMessage to myBlob",
- &struct {
- B json.RawMessage
- }{
- B: json.RawMessage("rawr"),
- },
- &B2{B: myBlob("rawr")},
- "",
- "",
- },
- }
- // checkErr returns the empty string if either both want and err are zero,
- // or if want is a non-empty substring of err's string representation.
- func checkErr(want string, err error) string {
- if err != nil {
- got := err.Error()
- if want == "" || strings.Index(got, want) == -1 {
- return got
- }
- } else if want != "" {
- return fmt.Sprintf("want error %q", want)
- }
- return ""
- }
- func TestRoundTrip(t *testing.T) {
- for _, tc := range testCases {
- p, err := saveEntity(testKey0, tc.src)
- if s := checkErr(tc.putErr, err); s != "" {
- t.Errorf("%s: save: %s", tc.desc, s)
- continue
- }
- if p == nil {
- continue
- }
- var got interface{}
- if _, ok := tc.want.(*PropertyList); ok {
- got = new(PropertyList)
- } else {
- got = reflect.New(reflect.TypeOf(tc.want).Elem()).Interface()
- }
- err = loadEntity(got, p)
- if s := checkErr(tc.getErr, err); s != "" {
- t.Errorf("%s: load: %s", tc.desc, s)
- continue
- }
- equal := false
- if gotT, ok := got.(*T); ok {
- // Round tripping a time.Time can result in a different time.Location: Local instead of UTC.
- // We therefore test equality explicitly, instead of relying on reflect.DeepEqual.
- equal = gotT.T.Equal(tc.want.(*T).T)
- } else {
- equal = reflect.DeepEqual(got, tc.want)
- }
- if !equal {
- t.Errorf("%s: compare: got %v want %v", tc.desc, got, tc.want)
- continue
- }
- }
- }
- func TestQueryConstruction(t *testing.T) {
- tests := []struct {
- q, exp *Query
- err string
- }{
- {
- q: NewQuery("Foo"),
- exp: &Query{
- kind: "Foo",
- limit: -1,
- },
- },
- {
- // Regular filtered query with standard spacing.
- q: NewQuery("Foo").Filter("foo >", 7),
- exp: &Query{
- kind: "Foo",
- filter: []filter{
- {
- FieldName: "foo",
- Op: greaterThan,
- Value: 7,
- },
- },
- limit: -1,
- },
- },
- {
- // Filtered query with no spacing.
- q: NewQuery("Foo").Filter("foo=", 6),
- exp: &Query{
- kind: "Foo",
- filter: []filter{
- {
- FieldName: "foo",
- Op: equal,
- Value: 6,
- },
- },
- limit: -1,
- },
- },
- {
- // Filtered query with funky spacing.
- q: NewQuery("Foo").Filter(" foo< ", 8),
- exp: &Query{
- kind: "Foo",
- filter: []filter{
- {
- FieldName: "foo",
- Op: lessThan,
- Value: 8,
- },
- },
- limit: -1,
- },
- },
- {
- // Filtered query with multicharacter op.
- q: NewQuery("Foo").Filter("foo >=", 9),
- exp: &Query{
- kind: "Foo",
- filter: []filter{
- {
- FieldName: "foo",
- Op: greaterEq,
- Value: 9,
- },
- },
- limit: -1,
- },
- },
- {
- // Query with ordering.
- q: NewQuery("Foo").Order("bar"),
- exp: &Query{
- kind: "Foo",
- order: []order{
- {
- FieldName: "bar",
- Direction: ascending,
- },
- },
- limit: -1,
- },
- },
- {
- // Query with reverse ordering, and funky spacing.
- q: NewQuery("Foo").Order(" - bar"),
- exp: &Query{
- kind: "Foo",
- order: []order{
- {
- FieldName: "bar",
- Direction: descending,
- },
- },
- limit: -1,
- },
- },
- {
- // Query with an empty ordering.
- q: NewQuery("Foo").Order(""),
- err: "empty order",
- },
- {
- // Query with a + ordering.
- q: NewQuery("Foo").Order("+bar"),
- err: "invalid order",
- },
- }
- for i, test := range tests {
- if test.q.err != nil {
- got := test.q.err.Error()
- if !strings.Contains(got, test.err) {
- t.Errorf("%d: error mismatch: got %q want something containing %q", i, got, test.err)
- }
- continue
- }
- if !reflect.DeepEqual(test.q, test.exp) {
- t.Errorf("%d: mismatch: got %v want %v", i, test.q, test.exp)
- }
- }
- }
|