OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "sort" | 10 "sort" |
11 "strings" | 11 "strings" |
12 "testing" | 12 "testing" |
13 | 13 |
14 "github.com/luci/gae/service/blobstore" | 14 "github.com/luci/gae/service/blobstore" |
15 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
16 ) | 16 ) |
17 | 17 |
18 func mps(vals ...interface{}) PropertySlice { | 18 func mps(vals ...interface{}) PropertySlice { |
19 ret := make(PropertySlice, len(vals)) | 19 ret := make(PropertySlice, len(vals)) |
20 for i, val := range vals { | 20 for i, val := range vals { |
21 ret[i] = mp(val) | 21 ret[i] = mp(val) |
22 } | 22 } |
23 return ret | 23 return ret |
24 } | 24 } |
25 | 25 |
26 var estimateSizeTests = []struct { | 26 var estimateSizeTests = []struct { |
27 pm PropertyMap | 27 pm PropertyMap |
28 expect int | 28 expect int |
29 }{ | 29 }{ |
30 » {PropertyMap{"Something": {}}, 9}, | 30 » {PropertyMap{"Something": mps()}, 9}, |
31 {PropertyMap{"Something": mps(100)}, 18}, | 31 {PropertyMap{"Something": mps(100)}, 18}, |
32 {PropertyMap{"Something": mps(100.1, "sup")}, 22}, | 32 {PropertyMap{"Something": mps(100.1, "sup")}, 22}, |
33 {PropertyMap{ | 33 {PropertyMap{ |
34 "Something": mps(100, "sup"), | 34 "Something": mps(100, "sup"), |
35 "Keys": mps(MakeKey("aid", "ns", "parent", "something", "ki
nd", int64(20))), | 35 "Keys": mps(MakeKey("aid", "ns", "parent", "something", "ki
nd", int64(20))), |
36 }, 59}, | 36 }, 59}, |
37 {PropertyMap{ | 37 {PropertyMap{ |
38 "Null": mps(nil), | 38 "Null": mps(nil), |
39 "Bool": mps(true, false), | 39 "Bool": mps(true, false), |
40 "GP": mps(GeoPoint{23.2, 122.1}), | 40 "GP": mps(GeoPoint{23.2, 122.1}), |
41 "bskey": mps(blobstore.Key("hello")), | 41 "bskey": mps(blobstore.Key("hello")), |
42 "[]byte": mps([]byte("sup")), | 42 "[]byte": mps([]byte("sup")), |
43 }, 59}, | 43 }, 59}, |
44 } | 44 } |
45 | 45 |
46 func stablePmString(pm PropertyMap) string { | 46 func stablePmString(pm PropertyMap) string { |
47 keys := make([]string, 0, len(pm)) | 47 keys := make([]string, 0, len(pm)) |
48 for k := range pm { | 48 for k := range pm { |
49 keys = append(keys, k) | 49 keys = append(keys, k) |
50 } | 50 } |
51 sort.Strings(keys) | 51 sort.Strings(keys) |
52 | 52 |
53 buf := &bytes.Buffer{} | 53 buf := &bytes.Buffer{} |
54 _, _ = buf.WriteString("map[") | 54 _, _ = buf.WriteString("map[") |
55 for i, k := range keys { | 55 for i, k := range keys { |
56 if i != 0 { | 56 if i != 0 { |
57 _, _ = buf.WriteString(" ") | 57 _, _ = buf.WriteString(" ") |
58 } | 58 } |
59 » » vals := pm[k] | 59 » » vals := pm.Slice(k) |
60 strs := make([]string, len(vals)) | 60 strs := make([]string, len(vals)) |
61 for i, v := range vals { | 61 for i, v := range vals { |
62 strs[i] = v.GQL() | 62 strs[i] = v.GQL() |
63 } | 63 } |
64 fmt.Fprintf(buf, "%s:[%s]", k, strings.Join(strs, ", ")) | 64 fmt.Fprintf(buf, "%s:[%s]", k, strings.Join(strs, ", ")) |
65 } | 65 } |
66 _, _ = buf.WriteRune(']') | 66 _, _ = buf.WriteRune(']') |
67 return buf.String() | 67 return buf.String() |
68 } | 68 } |
69 | 69 |
70 func TestEstimateSizes(t *testing.T) { | 70 func TestEstimateSizes(t *testing.T) { |
71 t.Parallel() | 71 t.Parallel() |
72 | 72 |
73 Convey("Test EstimateSize", t, func() { | 73 Convey("Test EstimateSize", t, func() { |
74 for _, tc := range estimateSizeTests { | 74 for _, tc := range estimateSizeTests { |
75 Convey(stablePmString(tc.pm), func() { | 75 Convey(stablePmString(tc.pm), func() { |
76 So(tc.pm.EstimateSize(), ShouldEqual, tc.expect) | 76 So(tc.pm.EstimateSize(), ShouldEqual, tc.expect) |
77 }) | 77 }) |
78 } | 78 } |
79 }) | 79 }) |
80 } | 80 } |
OLD | NEW |