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

Side by Side Diff: service/datastore/size_test.go

Issue 1363063002: Add ability to estimate the size of a PropertyMap (Closed) Base URL: https://github.com/luci/gae.git@minor_tweak
Patch Set: fix errcheck madness Created 5 years, 2 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
« no previous file with comments | « service/datastore/properties.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package datastore
6
7 import (
8 "bytes"
9 "fmt"
10 "sort"
11 "strings"
12 "testing"
13
14 "github.com/luci/gae/service/blobstore"
15 . "github.com/smartystreets/goconvey/convey"
16 )
17
18 func mps(vals ...interface{}) PropertySlice {
19 ret := make(PropertySlice, len(vals))
20 for i, val := range vals {
21 ret[i] = mp(val)
22 }
23 return ret
24 }
25
26 var estimateSizeTests = []struct {
27 pm PropertyMap
28 expect int
29 }{
30 {PropertyMap{"Something": {}}, 9},
31 {PropertyMap{"Something": mps(100)}, 18},
32 {PropertyMap{"Something": mps(100.1, "sup")}, 22},
33 {PropertyMap{
34 "Something": mps(100, "sup"),
35 "Keys": mps(MakeKey("aid", "ns", "parent", "something", "ki nd", int64(20))),
36 }, 59},
37 {PropertyMap{
38 "Null": mps(nil),
39 "Bool": mps(true, false),
40 "GP": mps(GeoPoint{23.2, 122.1}),
41 "bskey": mps(blobstore.Key("hello")),
42 "[]byte": mps([]byte("sup")),
43 }, 59},
44 }
45
46 func stablePmString(pm PropertyMap) string {
47 keys := make([]string, 0, len(pm))
48 for k := range pm {
49 keys = append(keys, k)
50 }
51 sort.Strings(keys)
52
53 buf := &bytes.Buffer{}
54 _, _ = buf.WriteString("map[")
55 for i, k := range keys {
56 if i != 0 {
57 _, _ = buf.WriteString(" ")
58 }
59 vals := pm[k]
60 strs := make([]string, len(vals))
61 for i, v := range vals {
62 strs[i] = v.GQL()
63 }
64 fmt.Fprintf(buf, "%s:[%s]", k, strings.Join(strs, ", "))
65 }
66 _, _ = buf.WriteRune(']')
67 return buf.String()
68 }
69
70 func TestEstimateSizes(t *testing.T) {
71 t.Parallel()
72
73 Convey("Test EstimateSize", t, func() {
74 for _, tc := range estimateSizeTests {
75 Convey(stablePmString(tc.pm), func() {
76 So(tc.pm.EstimateSize(), ShouldEqual, tc.expect)
77 })
78 }
79 })
80 }
OLDNEW
« no previous file with comments | « service/datastore/properties.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698