| OLD | NEW |
| 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 bigtable | 5 package bigtable |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "strings" |
| 9 "testing" | 10 "testing" |
| 10 | 11 |
| 11 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
| 12 ) | 13 ) |
| 13 | 14 |
| 14 func TestRowKey(t *testing.T) { | 15 func TestRowKey(t *testing.T) { |
| 15 t.Parallel() | 16 t.Parallel() |
| 16 | 17 |
| 17 Convey(`A row key, constructed from "a/b/+/c/d"`, t, func() { | 18 Convey(`A row key, constructed from "a/b/+/c/d"`, t, func() { |
| 18 path := "a/b/+/c/d" | 19 path := "a/b/+/c/d" |
| 19 » » rk := newRowKey(path, 1337) | 20 » » rk := newRowKey(path, 1337, 42) |
| 20 | 21 |
| 21 Convey(`Shares a path with a row key from the same Path.`, func(
) { | 22 Convey(`Shares a path with a row key from the same Path.`, func(
) { |
| 22 » » » So(rk.sharesPathWith(newRowKey(path, 2468)), ShouldBeTru
e) | 23 » » » So(rk.sharesPathWith(newRowKey(path, 2468, 0)), ShouldBe
True) |
| 23 }) | 24 }) |
| 24 | 25 |
| 25 for _, v := range []string{ | 26 for _, v := range []string{ |
| 26 "a/b/+/c", | 27 "a/b/+/c", |
| 27 "asdf", | 28 "asdf", |
| 28 "", | 29 "", |
| 29 } { | 30 } { |
| 30 Convey(fmt.Sprintf(`Does not share a path with: %q`, v),
func() { | 31 Convey(fmt.Sprintf(`Does not share a path with: %q`, v),
func() { |
| 31 » » » » So(rk.sharesPathWith(newRowKey(v, 0)), ShouldBeF
alse) | 32 » » » » So(rk.sharesPathWith(newRowKey(v, 0, 0)), Should
BeFalse) |
| 32 }) | 33 }) |
| 33 } | 34 } |
| 34 | 35 |
| 35 » » Convey(`Can be encoded, then decoded into its hash and index.`,
func() { | 36 » » Convey(`Can be encoded, then decoded into its fields.`, func() { |
| 36 enc := rk.encode() | 37 enc := rk.encode() |
| 37 So(len(enc), ShouldBeLessThanOrEqualTo, maxEncodedKeySiz
e) | 38 So(len(enc), ShouldBeLessThanOrEqualTo, maxEncodedKeySiz
e) |
| 38 | 39 |
| 39 drk, err := decodeRowKey(enc) | 40 drk, err := decodeRowKey(enc) |
| 40 So(err, ShouldBeNil) | 41 So(err, ShouldBeNil) |
| 41 | 42 |
| 42 So(drk.pathHash, ShouldResemble, rk.pathHash) | 43 So(drk.pathHash, ShouldResemble, rk.pathHash) |
| 43 So(drk.index, ShouldEqual, rk.index) | 44 So(drk.index, ShouldEqual, rk.index) |
| 45 So(drk.count, ShouldEqual, rk.count) |
| 44 }) | 46 }) |
| 45 }) | 47 }) |
| 46 | 48 |
| 47 » Convey(`A series of row keys should be ascendingly sorted and parsable.`
, t, func() { | 49 » Convey(`A series of ordered row keys`, t, func() { |
| 48 prev := "" | 50 prev := "" |
| 49 for _, i := range []int64{ | 51 for _, i := range []int64{ |
| 50 -1, /* Why not? */ | 52 -1, /* Why not? */ |
| 51 0, | 53 0, |
| 52 7, | 54 7, |
| 53 8, | 55 8, |
| 54 257, | 56 257, |
| 55 1029, | 57 1029, |
| 56 1337, | 58 1337, |
| 57 } { | 59 } { |
| 58 » » » rk := newRowKey("test", i) | 60 » » » Convey(fmt.Sprintf(`Row key %d should be ascendingly sor
ted and parsable.`, i), func() { |
| 61 » » » » rk := newRowKey("test", i, i) |
| 59 | 62 |
| 60 » » » // Test that it encodes/decodes back to identity. | 63 » » » » // Test that it encodes/decodes back to identity
. |
| 61 » » » enc := rk.encode() | 64 » » » » enc := rk.encode() |
| 62 » » » drk, err := decodeRowKey(enc) | 65 » » » » drk, err := decodeRowKey(enc) |
| 63 » » » So(err, ShouldBeNil) | 66 » » » » So(err, ShouldBeNil) |
| 64 » » » So(drk.index, ShouldEqual, i) | 67 » » » » So(drk.index, ShouldEqual, i) |
| 65 | 68 |
| 66 » » » // Assert that it is ordered. | 69 » » » » // Assert that it is ordered. |
| 67 » » » if prev != "" { | 70 » » » » if prev != "" { |
| 68 » » » » So(prev, ShouldBeLessThan, enc) | 71 » » » » » So(prev, ShouldBeLessThan, enc) |
| 69 | 72 |
| 70 » » » » prevp, err := decodeRowKey(prev) | 73 » » » » » prevp, err := decodeRowKey(prev) |
| 71 » » » » So(err, ShouldBeNil) | 74 » » » » » So(err, ShouldBeNil) |
| 72 » » » » So(prevp.sharesPathWith(rk), ShouldBeTrue) | 75 » » » » » So(prevp.sharesPathWith(rk), ShouldBeTru
e) |
| 73 » » » » So(prevp.index, ShouldBeLessThan, drk.index) | 76 » » » » » So(prevp.index, ShouldBeLessThan, drk.in
dex) |
| 74 » » » } | 77 » » » » » So(prevp.count, ShouldBeLessThan, drk.co
unt) |
| 78 » » » » } |
| 79 |
| 80 » » » » Convey(`Legacy row key value will parse with cou
nt 0.`, func() { |
| 81 » » » » » if rk.count > 0 { |
| 82 » » » » » » enc = enc[:strings.LastIndex(enc
, "~")] |
| 83 » » » » » } |
| 84 |
| 85 » » » » » drk, err = decodeRowKey(enc) |
| 86 » » » » » So(err, ShouldBeNil) |
| 87 » » » » » So(drk.index, ShouldEqual, i) |
| 88 » » » » » So(drk.count, ShouldEqual, 0) |
| 89 |
| 90 » » » » » // Assert that it is ordered. |
| 91 » » » » » if prev != "" { |
| 92 » » » » » » So(prev, ShouldBeLessThan, enc) |
| 93 |
| 94 » » » » » » prevp, err := decodeRowKey(prev) |
| 95 » » » » » » So(err, ShouldBeNil) |
| 96 » » » » » » So(prevp.sharesPathWith(rk), Sho
uldBeTrue) |
| 97 » » » » » » So(prevp.index, ShouldBeLessThan
, drk.index) |
| 98 » » » » » } |
| 99 » » » » }) |
| 100 » » » }) |
| 75 } | 101 } |
| 76 }) | 102 }) |
| 77 | 103 |
| 78 Convey(`Invalid row keys will fail to decode with "errMalformedRowKey".`
, t, func() { | 104 Convey(`Invalid row keys will fail to decode with "errMalformedRowKey".`
, t, func() { |
| 79 for _, t := range []struct { | 105 for _, t := range []struct { |
| 80 name string | 106 name string |
| 81 v string | 107 v string |
| 82 }{ | 108 }{ |
| 83 {"No tilde", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd380
80"}, | 109 {"No tilde", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd380
80"}, |
| 84 {"No path hash", "~8080"}, | 110 {"No path hash", "~8080"}, |
| 85 {"Bad hex path hash", "badhex~8080"}, | 111 {"Bad hex path hash", "badhex~8080"}, |
| 86 {"Path has too short", "4a54700540127~8080"}, | 112 {"Path has too short", "4a54700540127~8080"}, |
| 87 {"Bad hex index", "a94a8fe5ccb19ba61c4c0873d391e987982fb
bd3~badhex"}, | 113 {"Bad hex index", "a94a8fe5ccb19ba61c4c0873d391e987982fb
bd3~badhex"}, |
| 88 {"Missing index.", "a94a8fe5ccb19ba61c4c0873d391e987982f
bbd3~"}, | 114 {"Missing index.", "a94a8fe5ccb19ba61c4c0873d391e987982f
bbd3~"}, |
| 89 {"Varint overflow", "a94a8fe5ccb19ba61c4c0873d391e987982
fbbd3~ffffffffffff"}, | 115 {"Varint overflow", "a94a8fe5ccb19ba61c4c0873d391e987982
fbbd3~ffffffffffff"}, |
| 90 {"Trailing data", "a94a8fe5ccb19ba61c4c0873d391e987982fb
bd3~8080badd06"}, | 116 {"Trailing data", "a94a8fe5ccb19ba61c4c0873d391e987982fb
bd3~8080badd06"}, |
| 91 } { | 117 } { |
| 92 Convey(fmt.Sprintf(`Row key fails to decode [%s]: %q`, t
.name, t.v), func() { | 118 Convey(fmt.Sprintf(`Row key fails to decode [%s]: %q`, t
.name, t.v), func() { |
| 93 _, err := decodeRowKey(t.v) | 119 _, err := decodeRowKey(t.v) |
| 94 So(err, ShouldEqual, errMalformedRowKey) | 120 So(err, ShouldEqual, errMalformedRowKey) |
| 95 }) | 121 }) |
| 96 } | 122 } |
| 97 }) | 123 }) |
| 98 } | 124 } |
| OLD | NEW |