Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(707)

Side by Side Diff: service/datastore/serialize/serialize_test.go

Issue 1550903002: impl/memory: Fix time serialization encoding. (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 12 matching lines...) Expand all
23 } 23 }
24 24
25 var ( 25 var (
26 mp = ds.MkProperty 26 mp = ds.MkProperty
27 mpNI = ds.MkPropertyNI 27 mpNI = ds.MkPropertyNI
28 ) 28 )
29 29
30 type dspmapTC struct { 30 type dspmapTC struct {
31 name string 31 name string
32 props ds.PropertyMap 32 props ds.PropertyMap
33 exp ds.PropertyMap
33 } 34 }
34 35
35 var mkKey = ds.MakeKey 36 var mkKey = ds.MakeKey
36 37
37 func mkBuf(data []byte) Buffer { 38 func mkBuf(data []byte) Buffer {
38 return Invertible(bytes.NewBuffer(data)) 39 return Invertible(bytes.NewBuffer(data))
39 } 40 }
40 41
41 // TODO(riannucci): dedup with datastore/key testing file 42 // TODO(riannucci): dedup with datastore/key testing file
42 func ShouldEqualKey(actual interface{}, expected ...interface{}) string { 43 func ShouldEqualKey(actual interface{}, expected ...interface{}) string {
43 if len(expected) != 1 { 44 if len(expected) != 1 {
44 return fmt.Sprintf("Assertion requires 1 expected value, got %d" , len(expected)) 45 return fmt.Sprintf("Assertion requires 1 expected value, got %d" , len(expected))
45 } 46 }
46 if actual.(*ds.Key).Equal(expected[0].(*ds.Key)) { 47 if actual.(*ds.Key).Equal(expected[0].(*ds.Key)) {
47 return "" 48 return ""
48 } 49 }
49 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0]) 50 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0])
50 } 51 }
51 52
52 func TestPropertyMapSerialization(t *testing.T) { 53 func TestPropertyMapSerialization(t *testing.T) {
53 t.Parallel() 54 t.Parallel()
54 55
56 now := time.Now().UTC()
55 tests := []dspmapTC{ 57 tests := []dspmapTC{
56 { 58 {
57 » » » "basic", 59 » » » name: "basic",
58 » » » ds.PropertyMap{ 60 » » » props: ds.PropertyMap{
59 "R": {mp(false), mp(2.1), mpNI(3)}, 61 "R": {mp(false), mp(2.1), mpNI(3)},
60 "S": {mp("hello"), mp("world")}, 62 "S": {mp("hello"), mp("world")},
61 }, 63 },
62 }, 64 },
63 { 65 {
64 » » » "keys", 66 » » » name: "keys",
65 » » » ds.PropertyMap{ 67 » » » props: ds.PropertyMap{
66 "DS": {mp(mkKey("appy", "ns", "Foo", 7)), mp(mkKey("other", "", "Yot", "wheeep"))}, 68 "DS": {mp(mkKey("appy", "ns", "Foo", 7)), mp(mkKey("other", "", "Yot", "wheeep"))},
67 "blobstore": {mp(blobstore.Key("sup")), mp(blobs tore.Key("nerds"))}, 69 "blobstore": {mp(blobstore.Key("sup")), mp(blobs tore.Key("nerds"))},
68 }, 70 },
69 }, 71 },
70 { 72 {
71 » » » "geo", 73 » » » name: "geo",
72 » » » ds.PropertyMap{ 74 » » » props: ds.PropertyMap{
73 "G": {mp(ds.GeoPoint{Lat: 1, Lng: 2})}, 75 "G": {mp(ds.GeoPoint{Lat: 1, Lng: 2})},
74 }, 76 },
75 }, 77 },
76 { 78 {
77 » » » "data", 79 » » » name: "data",
78 » » » ds.PropertyMap{ 80 » » » props: ds.PropertyMap{
79 "S": {mp("sup"), mp("fool"), mp("nerd") }, 81 "S": {mp("sup"), mp("fool"), mp("nerd") },
80 "D.Foo.Nerd": {mp([]byte("sup")), mp([]byte("foo l"))}, 82 "D.Foo.Nerd": {mp([]byte("sup")), mp([]byte("foo l"))},
81 }, 83 },
82 }, 84 },
83 { 85 {
84 » » » "time", 86 » » » name: "time",
85 » » » ds.PropertyMap{ 87 » » » props: ds.PropertyMap{
86 "T": { 88 "T": {
87 » » » » » mp(time.Now().UTC()), 89 » » » » » mp(now),
88 » » » » » mp(time.Now().Add(time.Second).UTC())}, 90 » » » » » mp(now),
91 » » » » },
92 » » » },
93 » » » exp: ds.PropertyMap{
94 » » » » "T": {
95 » » » » » mp(timeToInt(now)),
96 » » » » » mp(timeToInt(now)),
97 » » » » },
89 }, 98 },
90 }, 99 },
91 { 100 {
92 » » » "empty vals", 101 » » » name: "empty vals",
93 » » » ds.PropertyMap{ 102 » » » props: ds.PropertyMap{
94 "T": {mp(true), mp(true)}, 103 "T": {mp(true), mp(true)},
95 "F": {mp(false), mp(false)}, 104 "F": {mp(false), mp(false)},
96 "N": {mp(nil), mp(nil)}, 105 "N": {mp(nil), mp(nil)},
97 "E": {}, 106 "E": {},
98 }, 107 },
99 }, 108 },
100 } 109 }
101 110
102 Convey("PropertyMap serialization", t, func() { 111 Convey("PropertyMap serialization", t, func() {
103 Convey("round trip", func() { 112 Convey("round trip", func() {
104 for _, tc := range tests { 113 for _, tc := range tests {
105 tc := tc 114 tc := tc
106 Convey(tc.name, func() { 115 Convey(tc.name, func() {
107 data := ToBytesWithContext(tc.props) 116 data := ToBytesWithContext(tc.props)
108 dec, err := ReadPropertyMap(mkBuf(data), WithContext, "", "") 117 dec, err := ReadPropertyMap(mkBuf(data), WithContext, "", "")
109 So(err, ShouldBeNil) 118 So(err, ShouldBeNil)
110 » » » » » So(dec, ShouldResemble, tc.props) 119
120 » » » » » exp := tc.exp
121 » » » » » if exp == nil {
122 » » » » » » exp = tc.props
123 » » » » » }
124 » » » » » So(dec, ShouldResemble, exp)
111 }) 125 })
112 } 126 }
113 }) 127 })
114 }) 128 })
115 } 129 }
116 130
117 func die(err error) { 131 func die(err error) {
118 if err != nil { 132 if err != nil {
119 panic(err) 133 panic(err)
120 } 134 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 "__ancestor__": { 519 "__ancestor__": {
506 ToBytes(mp(fakeKey)), 520 ToBytes(mp(fakeKey)),
507 ToBytes(mp(fakeKey.Paren t())), 521 ToBytes(mp(fakeKey.Paren t())),
508 }, 522 },
509 }) 523 })
510 }) 524 })
511 }) 525 })
512 }) 526 })
513 }) 527 })
514 } 528 }
OLDNEW
« service/datastore/serialize/serialize.go ('K') | « service/datastore/serialize/serialize.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698