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

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: 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
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 expectWith int
29 expectWithout int
30 }{
31 {PropertyMap{"Something": {}}, 9, 9},
32 {PropertyMap{"Something": mps(100)}, 18, 18},
33 {PropertyMap{"Something": mps(100.1, "sup")}, 22, 22},
34 {PropertyMap{
35 "Something": mps(100, "sup"),
36 "Keys": mps(MakeKey("aid", "ns", "parent", "something", "ki nd", int64(20))),
37 }, 59, 54},
38 {PropertyMap{
39 "Null": mps(nil),
40 "Bool": mps(true, false),
41 "GP": mps(GeoPoint{23.2, 122.1}),
42 "bskey": mps(blobstore.Key("hello")),
43 "[]byte": mps([]byte("sup")),
44 }, 59, 59},
45 }
46
47 func stablePmString(pm PropertyMap) string {
48 keys := make([]string, 0, len(pm))
49 for k := range pm {
50 keys = append(keys, k)
51 }
52 sort.Strings(keys)
53
54 buf := &bytes.Buffer{}
55 buf.WriteString("map[")
56 for i, k := range keys {
57 if i != 0 {
58 buf.WriteString(" ")
59 }
60 vals := pm[k]
61 strs := make([]string, len(vals))
62 for i, v := range vals {
63 strs[i] = v.GQL()
64 }
65 fmt.Fprintf(buf, "%s:[%s]", k, strings.Join(strs, ", "))
66 }
67 buf.WriteRune(']')
68 return buf.String()
69 }
70
71 func TestEstimateSizes(t *testing.T) {
72 t.Parallel()
73
74 Convey("Test EstimateSize", t, func() {
75 for _, tc := range estimateSizeTests {
76 Convey(stablePmString(tc.pm), func() {
77 So(tc.pm.EstimateSize(true), ShouldEqual, tc.exp ectWith)
78 So(tc.pm.EstimateSize(false), ShouldEqual, tc.ex pectWithout)
79 })
80 }
81 })
82 }
OLDNEW
« service/datastore/properties.go ('K') | « service/datastore/properties.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698