OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | |
9 "fmt" | 8 "fmt" |
10 "testing" | 9 "testing" |
11 | 10 |
12 dsS "github.com/luci/gae/service/datastore" | 11 dsS "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/gae/service/datastore/dskey" |
| 13 "github.com/luci/gae/service/datastore/serialize" |
13 infoS "github.com/luci/gae/service/info" | 14 infoS "github.com/luci/gae/service/info" |
14 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
15 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
16 ) | 17 ) |
17 | 18 |
18 func TestDatastoreKinder(t *testing.T) { | 19 func TestDatastoreKinder(t *testing.T) { |
19 t.Parallel() | 20 t.Parallel() |
20 | 21 |
21 Convey("Datastore keys", t, func() { | 22 Convey("Datastore keys", t, func() { |
22 c := Use(context.Background()) | 23 c := Use(context.Background()) |
23 ds := dsS.Get(c) | 24 ds := dsS.Get(c) |
24 So(ds, ShouldNotBeNil) | 25 So(ds, ShouldNotBeNil) |
25 | 26 |
26 Convey("implements DSNewKeyer", func() { | 27 Convey("implements DSNewKeyer", func() { |
27 Convey("NewKey", func() { | 28 Convey("NewKey", func() { |
28 key := ds.NewKey("nerd", "stringID", 0, nil) | 29 key := ds.NewKey("nerd", "stringID", 0, nil) |
29 So(key, ShouldNotBeNil) | 30 So(key, ShouldNotBeNil) |
30 So(key.Kind(), ShouldEqual, "nerd") | 31 So(key.Kind(), ShouldEqual, "nerd") |
31 So(key.StringID(), ShouldEqual, "stringID") | 32 So(key.StringID(), ShouldEqual, "stringID") |
32 So(key.IntID(), ShouldEqual, 0) | 33 So(key.IntID(), ShouldEqual, 0) |
33 So(key.Parent(), ShouldBeNil) | 34 So(key.Parent(), ShouldBeNil) |
34 So(key.AppID(), ShouldEqual, "dev~app") | 35 So(key.AppID(), ShouldEqual, "dev~app") |
35 So(key.Namespace(), ShouldEqual, "") | 36 So(key.Namespace(), ShouldEqual, "") |
36 So(key.String(), ShouldEqual, "/nerd,stringID") | 37 So(key.String(), ShouldEqual, "/nerd,stringID") |
37 » » » » So(dsS.KeyIncomplete(key), ShouldBeFalse) | 38 » » » » So(key.Incomplete(), ShouldBeFalse) |
38 » » » » So(dsS.KeyValid(key, false, "dev~app", ""), Shou
ldBeTrue) | 39 » » » » So(key.Valid(false, "dev~app", ""), ShouldBeTrue
) |
39 }) | 40 }) |
40 }) | 41 }) |
41 | 42 |
42 }) | 43 }) |
43 } | 44 } |
44 | 45 |
45 type MetaGroup struct { | 46 type MetaGroup struct { |
46 _id int64 `gae:"$id,1"` | 47 _id int64 `gae:"$id,1"` |
47 _kind string `gae:"$kind,__entity_group__"` | 48 _kind string `gae:"$kind,__entity_group__"` |
48 Parent dsS.Key `gae:"$parent"` | 49 Parent dsS.Key `gae:"$parent"` |
49 | 50 |
50 Version int64 `gae:"__version__"` | 51 Version int64 `gae:"__version__"` |
51 } | 52 } |
52 | 53 |
53 func testGetMeta(c context.Context, k dsS.Key) int64 { | 54 func testGetMeta(c context.Context, k dsS.Key) int64 { |
54 ds := dsS.Get(c) | 55 ds := dsS.Get(c) |
55 » mg := &MetaGroup{Parent: dsS.KeyRoot(k)} | 56 » mg := &MetaGroup{Parent: dskey.Root(k)} |
56 if err := ds.Get(mg); err != nil { | 57 if err := ds.Get(mg); err != nil { |
57 panic(err) | 58 panic(err) |
58 } | 59 } |
59 return mg.Version | 60 return mg.Version |
60 } | 61 } |
61 | 62 |
62 var pls = dsS.GetPLS | 63 var pls = dsS.GetPLS |
63 | 64 |
64 type Foo struct { | 65 type Foo struct { |
65 Id int64 `gae:"$id"` | 66 Id int64 `gae:"$id"` |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 }) | 419 }) |
419 }) | 420 }) |
420 }) | 421 }) |
421 | 422 |
422 }) | 423 }) |
423 } | 424 } |
424 | 425 |
425 func TestCompoundIndexes(t *testing.T) { | 426 func TestCompoundIndexes(t *testing.T) { |
426 t.Parallel() | 427 t.Parallel() |
427 | 428 |
428 » idxKey := func(def *dsS.IndexDefinition) string { | 429 » idxKey := func(def dsS.IndexDefinition) string { |
429 » » buf := &bytes.Buffer{} | 430 » » So(def, ShouldNotBeNil) |
430 » » buf.WriteString("idx::") | 431 » » return "idx::" + string(serialize.ToBytes(def)) |
431 » » So(def.Write(buf), ShouldBeNil) | |
432 » » return buf.String() | |
433 } | 432 } |
434 | 433 |
435 numItms := func(c *memCollection) uint64 { | 434 numItms := func(c *memCollection) uint64 { |
436 ret, _ := c.GetTotals() | 435 ret, _ := c.GetTotals() |
437 return ret | 436 return ret |
438 } | 437 } |
439 | 438 |
440 Convey("Test Compound indexes", t, func() { | 439 Convey("Test Compound indexes", t, func() { |
441 type Model struct { | 440 type Model struct { |
442 ID int64 `gae:"$id"` | 441 ID int64 `gae:"$id"` |
443 | 442 |
444 Field1 []string | 443 Field1 []string |
445 Field2 []int64 | 444 Field2 []int64 |
446 } | 445 } |
447 | 446 |
448 c := Use(context.Background()) | 447 c := Use(context.Background()) |
449 ds := dsS.Get(c) | 448 ds := dsS.Get(c) |
450 t := ds.Raw().Testable().(*dsImpl) | 449 t := ds.Raw().Testable().(*dsImpl) |
451 store := t.data.store | 450 store := t.data.store |
452 | 451 |
453 So(ds.Put(&Model{1, []string{"hello", "world"}, []int64{10, 11}}
), ShouldBeNil) | 452 So(ds.Put(&Model{1, []string{"hello", "world"}, []int64{10, 11}}
), ShouldBeNil) |
454 | 453 |
455 » » idx := &dsS.IndexDefinition{ | 454 » » idx := dsS.IndexDefinition{ |
456 Kind: "Model", | 455 Kind: "Model", |
457 SortBy: []dsS.IndexColumn{ | 456 SortBy: []dsS.IndexColumn{ |
458 {Property: "Field2"}, | 457 {Property: "Field2"}, |
459 }, | 458 }, |
460 } | 459 } |
461 | 460 |
462 coll := store.GetCollection(idxKey(idx)) | 461 coll := store.GetCollection(idxKey(idx)) |
463 So(coll, ShouldNotBeNil) | 462 So(coll, ShouldNotBeNil) |
464 So(numItms(coll), ShouldEqual, 2) | 463 So(numItms(coll), ShouldEqual, 2) |
465 | 464 |
466 idx.SortBy[0].Property = "Field1" | 465 idx.SortBy[0].Property = "Field1" |
467 coll = store.GetCollection(idxKey(idx)) | 466 coll = store.GetCollection(idxKey(idx)) |
468 So(coll, ShouldNotBeNil) | 467 So(coll, ShouldNotBeNil) |
469 So(numItms(coll), ShouldEqual, 2) | 468 So(numItms(coll), ShouldEqual, 2) |
470 | 469 |
471 idx.SortBy = append(idx.SortBy, dsS.IndexColumn{Property: "Field
1"}) | 470 idx.SortBy = append(idx.SortBy, dsS.IndexColumn{Property: "Field
1"}) |
472 So(store.GetCollection(idxKey(idx)), ShouldBeNil) | 471 So(store.GetCollection(idxKey(idx)), ShouldBeNil) |
473 | 472 |
474 » » t.AddIndexes(idx) | 473 » » t.AddIndexes(&idx) |
475 coll = store.GetCollection(idxKey(idx)) | 474 coll = store.GetCollection(idxKey(idx)) |
476 So(coll, ShouldNotBeNil) | 475 So(coll, ShouldNotBeNil) |
477 So(numItms(coll), ShouldEqual, 4) | 476 So(numItms(coll), ShouldEqual, 4) |
478 }) | 477 }) |
479 } | 478 } |
OLD | NEW |