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 serialize | 5 package serialize |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
11 "testing" | 11 "testing" |
12 "time" | 12 "time" |
13 | 13 |
14 "github.com/luci/gae/service/blobstore" | 14 "github.com/luci/gae/service/blobstore" |
15 ds "github.com/luci/gae/service/datastore" | 15 ds "github.com/luci/gae/service/datastore" |
16 "github.com/luci/gae/service/datastore/dskey" | |
17 "github.com/luci/luci-go/common/cmpbin" | 16 "github.com/luci/luci-go/common/cmpbin" |
18 . "github.com/luci/luci-go/common/testing/assertions" | 17 . "github.com/luci/luci-go/common/testing/assertions" |
19 . "github.com/smartystreets/goconvey/convey" | 18 . "github.com/smartystreets/goconvey/convey" |
20 ) | 19 ) |
21 | 20 |
22 func init() { | 21 func init() { |
23 WritePropertyMapDeterministic = true | 22 WritePropertyMapDeterministic = true |
24 } | 23 } |
25 | 24 |
26 var ( | 25 var ( |
27 mp = ds.MkProperty | 26 mp = ds.MkProperty |
28 mpNI = ds.MkPropertyNI | 27 mpNI = ds.MkPropertyNI |
29 ) | 28 ) |
30 | 29 |
31 type dspmapTC struct { | 30 type dspmapTC struct { |
32 name string | 31 name string |
33 props ds.PropertyMap | 32 props ds.PropertyMap |
34 } | 33 } |
35 | 34 |
36 // TODO(riannucci): dedup with datastore/key testing file. | 35 var mkKey = ds.MakeKey |
37 func mkKey(aid, ns string, elems ...interface{}) ds.Key { | |
38 » if len(elems)%2 != 0 { | |
39 » » panic("odd number of tokens") | |
40 » } | |
41 » toks := make([]ds.KeyTok, len(elems)/2) | |
42 » for i := 0; i < len(elems); i += 2 { | |
43 » » toks[i/2].Kind = elems[i].(string) | |
44 » » switch x := elems[i+1].(type) { | |
45 » » case string: | |
46 » » » toks[i/2].StringID = x | |
47 » » case int: | |
48 » » » toks[i/2].IntID = int64(x) | |
49 » » default: | |
50 » » » panic("bad token id") | |
51 » » } | |
52 » } | |
53 » return dskey.NewToks(aid, ns, toks) | |
54 } | |
55 | 36 |
56 func mkBuf(data []byte) Buffer { | 37 func mkBuf(data []byte) Buffer { |
57 return Invertible(bytes.NewBuffer(data)) | 38 return Invertible(bytes.NewBuffer(data)) |
58 } | 39 } |
59 | 40 |
60 // TODO(riannucci): dedup with datastore/key testing file | 41 // TODO(riannucci): dedup with datastore/key testing file |
61 func ShouldEqualKey(actual interface{}, expected ...interface{}) string { | 42 func ShouldEqualKey(actual interface{}, expected ...interface{}) string { |
62 if len(expected) != 1 { | 43 if len(expected) != 1 { |
63 return fmt.Sprintf("Assertion requires 1 expected value, got %d"
, len(expected)) | 44 return fmt.Sprintf("Assertion requires 1 expected value, got %d"
, len(expected)) |
64 } | 45 } |
65 » if dskey.Equal(actual.(ds.Key), expected[0].(ds.Key)) { | 46 » if actual.(*ds.Key).Equal(expected[0].(*ds.Key)) { |
66 return "" | 47 return "" |
67 } | 48 } |
68 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0]) | 49 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0]) |
69 } | 50 } |
70 | 51 |
71 func TestPropertyMapSerialization(t *testing.T) { | 52 func TestPropertyMapSerialization(t *testing.T) { |
72 t.Parallel() | 53 t.Parallel() |
73 | 54 |
74 tests := []dspmapTC{ | 55 tests := []dspmapTC{ |
75 { | 56 { |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 for i := 0; i < MaxIndexColumns+1; i++ { | 434 for i := 0; i < MaxIndexColumns+1; i++ { |
454 id.SortBy = append(id.SortBy, ds.IndexCo
lumn{Property: "Hi", Direction: ds.ASCENDING}) | 435 id.SortBy = append(id.SortBy, ds.IndexCo
lumn{Property: "Hi", Direction: ds.ASCENDING}) |
455 } | 436 } |
456 data := ToBytes(*id.PrepForIdxTable()) | 437 data := ToBytes(*id.PrepForIdxTable()) |
457 newID, err = ReadIndexDefinition(mkBuf(data)) | 438 newID, err = ReadIndexDefinition(mkBuf(data)) |
458 So(err, ShouldErrLike, "over 64 sort orders") | 439 So(err, ShouldErrLike, "over 64 sort orders") |
459 }) | 440 }) |
460 }) | 441 }) |
461 }) | 442 }) |
462 } | 443 } |
OLD | NEW |