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

Unified 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, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « service/datastore/properties.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/datastore/size_test.go
diff --git a/service/datastore/size_test.go b/service/datastore/size_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..3279948977a27889d68a2f53ac24f8262eefcbdf
--- /dev/null
+++ b/service/datastore/size_test.go
@@ -0,0 +1,80 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package datastore
+
+import (
+ "bytes"
+ "fmt"
+ "sort"
+ "strings"
+ "testing"
+
+ "github.com/luci/gae/service/blobstore"
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func mps(vals ...interface{}) PropertySlice {
+ ret := make(PropertySlice, len(vals))
+ for i, val := range vals {
+ ret[i] = mp(val)
+ }
+ return ret
+}
+
+var estimateSizeTests = []struct {
+ pm PropertyMap
+ expect int
+}{
+ {PropertyMap{"Something": {}}, 9},
+ {PropertyMap{"Something": mps(100)}, 18},
+ {PropertyMap{"Something": mps(100.1, "sup")}, 22},
+ {PropertyMap{
+ "Something": mps(100, "sup"),
+ "Keys": mps(MakeKey("aid", "ns", "parent", "something", "kind", int64(20))),
+ }, 59},
+ {PropertyMap{
+ "Null": mps(nil),
+ "Bool": mps(true, false),
+ "GP": mps(GeoPoint{23.2, 122.1}),
+ "bskey": mps(blobstore.Key("hello")),
+ "[]byte": mps([]byte("sup")),
+ }, 59},
+}
+
+func stablePmString(pm PropertyMap) string {
+ keys := make([]string, 0, len(pm))
+ for k := range pm {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+
+ buf := &bytes.Buffer{}
+ _, _ = buf.WriteString("map[")
+ for i, k := range keys {
+ if i != 0 {
+ _, _ = buf.WriteString(" ")
+ }
+ vals := pm[k]
+ strs := make([]string, len(vals))
+ for i, v := range vals {
+ strs[i] = v.GQL()
+ }
+ fmt.Fprintf(buf, "%s:[%s]", k, strings.Join(strs, ", "))
+ }
+ _, _ = buf.WriteRune(']')
+ return buf.String()
+}
+
+func TestEstimateSizes(t *testing.T) {
+ t.Parallel()
+
+ Convey("Test EstimateSize", t, func() {
+ for _, tc := range estimateSizeTests {
+ Convey(stablePmString(tc.pm), func() {
+ So(tc.pm.EstimateSize(), ShouldEqual, tc.expect)
+ })
+ }
+ })
+}
« 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