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

Unified Diff: service/datastore/serialize/serialize_test.go

Issue 2302743002: Interface update, per-method Contexts. (Closed)
Patch Set: WithoutTransaction, comments, fixes, cleanup. Created 4 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
Index: service/datastore/serialize/serialize_test.go
diff --git a/service/datastore/serialize/serialize_test.go b/service/datastore/serialize/serialize_test.go
index d6c9b9f65453e9030f7eecb5803414cc64f22309..74d8e0a1cd91912c87276b0aa03f4aea7c2061a1 100644
--- a/service/datastore/serialize/serialize_test.go
+++ b/service/datastore/serialize/serialize_test.go
@@ -14,6 +14,7 @@ import (
"github.com/luci/gae/service/blobstore"
ds "github.com/luci/gae/service/datastore"
"github.com/luci/luci-go/common/data/cmpbin"
+
. "github.com/luci/luci-go/common/testing/assertions"
. "github.com/smartystreets/goconvey/convey"
)
@@ -32,7 +33,9 @@ type dspmapTC struct {
props ds.PropertyMap
}
-var mkKey = ds.MakeKey
+func mkKey(appID, namespace string, elems ...interface{}) *ds.Key {
+ return ds.KeyContext{appID, namespace}.MakeKey(elems...)
+}
func mkBuf(data []byte) Buffer {
return Invertible(bytes.NewBuffer(data))
@@ -111,7 +114,7 @@ func TestPropertyMapSerialization(t *testing.T) {
tc := tc
Convey(tc.name, func() {
data := ToBytesWithContext(tc.props)
- dec, err := ReadPropertyMap(mkBuf(data), WithContext, "", "")
+ dec, err := ReadPropertyMap(mkBuf(data), WithContext, ds.KeyContext{})
So(err, ShouldBeNil)
So(dec, ShouldResemble, tc.props)
})
@@ -204,28 +207,28 @@ func TestSerializationReadMisc(t *testing.T) {
Convey("w/ ctx decodes normally w/ ctx", func() {
k := mkKey("aid", "ns", "knd", "yo", "other", 10)
data := ToBytesWithContext(k)
- dk, err := ReadKey(mkBuf(data), WithContext, "", "")
+ dk, err := ReadKey(mkBuf(data), WithContext, ds.KeyContext{})
So(err, ShouldBeNil)
So(dk, ShouldEqualKey, k)
})
Convey("w/ ctx decodes normally w/o ctx", func() {
k := mkKey("aid", "ns", "knd", "yo", "other", 10)
data := ToBytesWithContext(k)
- dk, err := ReadKey(mkBuf(data), WithoutContext, "spam", "nerd")
+ dk, err := ReadKey(mkBuf(data), WithoutContext, ds.KeyContext{"spam", "nerd"})
So(err, ShouldBeNil)
So(dk, ShouldEqualKey, mkKey("spam", "nerd", "knd", "yo", "other", 10))
})
Convey("w/o ctx decodes normally w/ ctx", func() {
k := mkKey("aid", "ns", "knd", "yo", "other", 10)
data := ToBytes(k)
- dk, err := ReadKey(mkBuf(data), WithContext, "spam", "nerd")
+ dk, err := ReadKey(mkBuf(data), WithContext, ds.KeyContext{"spam", "nerd"})
So(err, ShouldBeNil)
So(dk, ShouldEqualKey, mkKey("", "", "knd", "yo", "other", 10))
})
Convey("w/o ctx decodes normally w/o ctx", func() {
k := mkKey("aid", "ns", "knd", "yo", "other", 10)
data := ToBytes(k)
- dk, err := ReadKey(mkBuf(data), WithoutContext, "spam", "nerd")
+ dk, err := ReadKey(mkBuf(data), WithoutContext, ds.KeyContext{"spam", "nerd"})
So(err, ShouldBeNil)
So(dk, ShouldEqualKey, mkKey("spam", "nerd", "knd", "yo", "other", 10))
})
@@ -245,31 +248,31 @@ func TestSerializationReadMisc(t *testing.T) {
Convey("err cases", func() {
buf := mkBuf(nil)
Convey("nil", func() {
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("str", func() {
_, err := buf.WriteString("sup")
die(err)
- _, err = ReadKey(buf, WithContext, "", "")
+ _, err = ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "expected actualCtx")
})
Convey("truncated 1", func() {
die(buf.WriteByte(1)) // actualCtx == 1
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("truncated 2", func() {
die(buf.WriteByte(1)) // actualCtx == 1
ws(buf, "aid")
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("truncated 3", func() {
die(buf.WriteByte(1)) // actualCtx == 1
ws(buf, "aid")
ws(buf, "ns")
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("huge key", func() {
@@ -281,7 +284,7 @@ func TestSerializationReadMisc(t *testing.T) {
die(WriteKeyTok(buf, ds.KeyTok{Kind: "sup", IntID: int64(i)}))
}
die(buf.WriteByte(0))
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "huge key")
})
Convey("insufficient tokens", func() {
@@ -289,7 +292,7 @@ func TestSerializationReadMisc(t *testing.T) {
ws(buf, "aid")
ws(buf, "ns")
wui(buf, 2)
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("partial token 1", func() {
@@ -298,7 +301,7 @@ func TestSerializationReadMisc(t *testing.T) {
ws(buf, "ns")
die(buf.WriteByte(1))
ws(buf, "hi")
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("partial token 2", func() {
@@ -308,7 +311,7 @@ func TestSerializationReadMisc(t *testing.T) {
die(buf.WriteByte(1))
ws(buf, "hi")
die(buf.WriteByte(byte(ds.PTString)))
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("bad token (invalid type)", func() {
@@ -318,7 +321,7 @@ func TestSerializationReadMisc(t *testing.T) {
die(buf.WriteByte(1))
ws(buf, "hi")
die(buf.WriteByte(byte(ds.PTBlobKey)))
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "invalid type PTBlobKey")
})
Convey("bad token (invalid IntID)", func() {
@@ -329,7 +332,7 @@ func TestSerializationReadMisc(t *testing.T) {
ws(buf, "hi")
die(buf.WriteByte(byte(ds.PTInt)))
wi(buf, -2)
- _, err := ReadKey(buf, WithContext, "", "")
+ _, err := ReadKey(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "zero/negative")
})
})
@@ -374,24 +377,24 @@ func TestSerializationReadMisc(t *testing.T) {
Convey("ReadProperty", func() {
buf := mkBuf(nil)
Convey("trunc 1", func() {
- p, err := ReadProperty(buf, WithContext, "", "")
+ p, err := ReadProperty(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
So(p.Type(), ShouldEqual, ds.PTNull)
So(p.Value(), ShouldBeNil)
})
Convey("trunc (PTBytes)", func() {
die(buf.WriteByte(byte(ds.PTBytes)))
- _, err := ReadProperty(buf, WithContext, "", "")
+ _, err := ReadProperty(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("trunc (PTBlobKey)", func() {
die(buf.WriteByte(byte(ds.PTBlobKey)))
- _, err := ReadProperty(buf, WithContext, "", "")
+ _, err := ReadProperty(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("invalid type", func() {
die(buf.WriteByte(byte(ds.PTUnknown + 1)))
- _, err := ReadProperty(buf, WithContext, "", "")
+ _, err := ReadProperty(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "unknown type!")
})
})
@@ -399,37 +402,37 @@ func TestSerializationReadMisc(t *testing.T) {
Convey("ReadPropertyMap", func() {
buf := mkBuf(nil)
Convey("trunc 1", func() {
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("too many rows", func() {
wui(buf, 1000000)
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "huge number of rows")
})
Convey("trunc 2", func() {
wui(buf, 10)
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("trunc 3", func() {
wui(buf, 10)
ws(buf, "ohai")
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
Convey("too many values", func() {
wui(buf, 10)
ws(buf, "ohai")
wui(buf, 100000)
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldErrLike, "huge number of properties")
})
Convey("trunc 4", func() {
wui(buf, 10)
ws(buf, "ohai")
wui(buf, 10)
- _, err := ReadPropertyMap(buf, WithContext, "", "")
+ _, err := ReadPropertyMap(buf, WithContext, ds.KeyContext{})
So(err, ShouldEqual, io.EOF)
})
})

Powered by Google App Engine
This is Rietveld 408576698