本文整理汇总了Golang中google/golang.org/appengine/internal/aetesting.FakeSingleContext函数的典型用法代码示例。如果您正苦于以下问题:Golang FakeSingleContext函数的具体用法?Golang FakeSingleContext怎么用?Golang FakeSingleContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FakeSingleContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetPresenceMultiInvalid
func TestGetPresenceMultiInvalid(t *testing.T) {
c := aetesting.FakeSingleContext(t, "xmpp", "BulkGetPresence", func(in *pb.BulkPresenceRequest, out *pb.BulkPresenceResponse) error {
if !reflect.DeepEqual(in.Jid, []string{"[email protected]", "[email protected]"}) {
return fmt.Errorf("bad request jids %#v", in.Jid)
}
out.PresenceResponse = []*pb.PresenceResponse{
newPresenceResponse(true, pb.PresenceResponse_EXTENDED_AWAY, true),
newPresenceResponse(true, pb.PresenceResponse_CHAT, false),
}
return nil
})
jids := []string{"[email protected]", "[email protected]"}
presence, err := GetPresenceMulti(c, jids, "")
wantErr := appengine.MultiError{nil, ErrInvalidJID}
if !reflect.DeepEqual(err, wantErr) {
t.Fatalf("GetPresenceMulti: got %#v, want %#v", err, wantErr)
}
want := []string{"xa", ""}
if !reflect.DeepEqual(presence, want) {
t.Errorf("GetPresenceMulti: got %#v, want %#v", presence, want)
}
}
示例2: TestGetRequest
func TestGetRequest(t *testing.T) {
serviceCalled := false
apiKey := "lyric"
c := aetesting.FakeSingleContext(t, "memcache", "Get", func(req *pb.MemcacheGetRequest, _ *pb.MemcacheGetResponse) error {
// Test request.
if n := len(req.Key); n != 1 {
t.Errorf("got %d want 1", n)
return nil
}
if k := string(req.Key[0]); k != apiKey {
t.Errorf("got %q want %q", k, apiKey)
}
serviceCalled = true
return nil
})
// Test the "forward" path from the API call parameters to the
// protobuf request object. (The "backward" path from the
// protobuf response object to the API call response,
// including the error response, are handled in the next few
// tests).
Get(c, apiKey)
if !serviceCalled {
t.Error("Service was not called as expected")
}
}
示例3: TestGetPresenceMultiFromJID
func TestGetPresenceMultiFromJID(t *testing.T) {
c := aetesting.FakeSingleContext(t, "xmpp", "BulkGetPresence", func(in *pb.BulkPresenceRequest, out *pb.BulkPresenceResponse) error {
if !reflect.DeepEqual(in.Jid, []string{"[email protected]", "[email protected]"}) {
return fmt.Errorf("bad request jids %#v", in.Jid)
}
if jid := in.GetFromJid(); jid != "[email protected]" {
return fmt.Errorf("bad from jid %q", jid)
}
out.PresenceResponse = []*pb.PresenceResponse{
newPresenceResponse(true, pb.PresenceResponse_NORMAL, true),
newPresenceResponse(true, pb.PresenceResponse_CHAT, true),
}
return nil
})
jids := []string{"[email protected]", "[email protected]"}
presence, err := GetPresenceMulti(c, jids, "[email protected]")
if err != nil {
t.Fatalf("GetPresenceMulti: %v", err)
}
want := []string{"", "chat"}
if !reflect.DeepEqual(presence, want) {
t.Errorf("GetPresenceMulti: got %v, want %v", presence, want)
}
}
示例4: TestNamespaceResetting
func TestNamespaceResetting(t *testing.T) {
namec := make(chan *string, 1)
c0 := aetesting.FakeSingleContext(t, "memcache", "Get", func(req *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
namec <- req.NameSpace
return errRPC
})
// Check that wrapping c0 in a namespace twice works correctly.
c1, err := appengine.Namespace(c0, "A")
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
c2, err := appengine.Namespace(c1, "") // should act as the original context
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
Get(c0, "key")
if ns := <-namec; ns != nil {
t.Errorf(`Get with c0: ns = %q, want nil`, *ns)
}
Get(c1, "key")
if ns := <-namec; ns == nil {
t.Error(`Get with c1: ns = nil, want "A"`)
} else if *ns != "A" {
t.Errorf(`Get with c1: ns = %q, want "A"`, *ns)
}
Get(c2, "key")
if ns := <-namec; ns != nil {
t.Errorf(`Get with c2: ns = %q, want nil`, *ns)
}
}
示例5: TestNamespaceResetting
func TestNamespaceResetting(t *testing.T) {
var nsField *string
c := aetesting.FakeSingleContext(t, "memcache", "Get", func(req *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
nsField = req.NameSpace
return errRPC
})
// Check that wrapping c in a namespace twice works correctly.
nc, err := appengine.Namespace(c, "A")
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
c0, err := appengine.Namespace(nc, "") // should act as the original context
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
Get(c, "key")
if nsField != nil {
t.Fatalf("Get with c yielded %q", *nsField)
}
Get(nc, "key")
if nsField == nil || *nsField != "A" {
t.Fatalf("Get with nc yielded %v", nsField)
}
Get(c0, "key")
if nsField != nil && *nsField != "" {
t.Fatalf("Get with c0 yielded %q", *nsField)
}
}
示例6: TestAddRequest
func TestAddRequest(t *testing.T) {
var apiItem = &Item{
Key: "lyric",
Value: []byte("Oh, give me a home"),
}
serviceCalled := false
c := aetesting.FakeSingleContext(t, "memcache", "Set", func(req *pb.MemcacheSetRequest, _ *pb.MemcacheSetResponse) error {
// Test request.
pbItem := req.Item[0]
if k := string(pbItem.Key); k != apiItem.Key {
t.Errorf("got %q want %q", k, apiItem.Key)
}
if v := string(apiItem.Value); v != string(pbItem.Value) {
t.Errorf("got %q want %q", v, string(pbItem.Value))
}
if p := *pbItem.SetPolicy; p != pb.MemcacheSetRequest_ADD {
t.Errorf("got %v want %v", p, pb.MemcacheSetRequest_ADD)
}
serviceCalled = true
return nil
})
Add(c, apiItem)
if !serviceCalled {
t.Error("Service was not called as expected")
}
}
示例7: TestNamespaceResetting
func TestNamespaceResetting(t *testing.T) {
// These environment variables are necessary because *Query.Run will
// call internal.FullyQualifiedAppID which checks these variables or falls
// back to the Metadata service that is not available in tests.
environ := []struct {
key, value string
}{
{"GAE_LONG_APP_ID", "my-app-id"},
{"GAE_PARTITION", "1"},
}
for _, v := range environ {
old := os.Getenv(v.key)
os.Setenv(v.key, v.value)
v.value = old
}
defer func() { // Restore old environment after the test completes.
for _, v := range environ {
if v.value == "" {
os.Unsetenv(v.key)
continue
}
os.Setenv(v.key, v.value)
}
}()
namec := make(chan *string, 1)
c0 := aetesting.FakeSingleContext(t, "datastore_v3", "RunQuery", func(req *pb.Query, res *pb.QueryResult) error {
namec <- req.NameSpace
return fmt.Errorf("RPC error")
})
// Check that wrapping c0 in a namespace twice works correctly.
c1, err := appengine.Namespace(c0, "A")
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
c2, err := appengine.Namespace(c1, "") // should act as the original context
if err != nil {
t.Fatalf("appengine.Namespace: %v", err)
}
q := NewQuery("SomeKind")
q.Run(c0)
if ns := <-namec; ns != nil {
t.Errorf(`RunQuery with c0: ns = %q, want nil`, *ns)
}
q.Run(c1)
if ns := <-namec; ns == nil {
t.Error(`RunQuery with c1: ns = nil, want "A"`)
} else if *ns != "A" {
t.Errorf(`RunQuery with c1: ns = %q, want "A"`, *ns)
}
q.Run(c2)
if ns := <-namec; ns != nil {
t.Errorf(`RunQuery with c2: ns = %q, want nil`, *ns)
}
}
示例8: TestSetRequest
func TestSetRequest(t *testing.T) {
var apiItem = &Item{
Key: "lyric",
Value: []byte("Where the buffalo roam"),
}
serviceCalled := false
c := aetesting.FakeSingleContext(t, "memcache", "Set", func(req *pb.MemcacheSetRequest, _ *pb.MemcacheSetResponse) error {
// Test request.
if n := len(req.Item); n != 1 {
t.Errorf("got %d want 1", n)
return nil
}
pbItem := req.Item[0]
if k := string(pbItem.Key); k != apiItem.Key {
t.Errorf("got %q want %q", k, apiItem.Key)
}
if v := string(pbItem.Value); v != string(apiItem.Value) {
t.Errorf("got %q want %q", v, string(apiItem.Value))
}
if p := *pbItem.SetPolicy; p != pb.MemcacheSetRequest_SET {
t.Errorf("got %v want %v", p, pb.MemcacheSetRequest_SET)
}
serviceCalled = true
return nil
})
Set(c, apiItem)
if !serviceCalled {
t.Error("Service was not called as expected")
}
}
示例9: TestPutAutoOrderID
func TestPutAutoOrderID(t *testing.T) {
index, err := Open("Doc")
if err != nil {
t.Fatalf("err from Open: %v", err)
}
c := aetesting.FakeSingleContext(t, "search", "IndexDocument", func(in *pb.IndexDocumentRequest, out *pb.IndexDocumentResponse) error {
if len(in.Params.GetDocument()) < 1 {
return fmt.Errorf("expected at least one Document, got %v", in)
}
got, want := in.Params.Document[0].GetOrderId(), int32(time.Since(orderIDEpoch).Seconds())
if d := got - want; -5 > d || d > 5 {
return fmt.Errorf("got OrderId %d, want near %d", got, want)
}
*out = pb.IndexDocumentResponse{
Status: []*pb.RequestStatus{
{Code: pb.SearchServiceError_OK.Enum()},
},
DocId: []string{
"doc_id",
},
}
return nil
})
if _, err := index.Put(c, "", &searchFields); err != nil {
t.Fatal(err)
}
}
示例10: TestGetPresenceMultiUnavailable
func TestGetPresenceMultiUnavailable(t *testing.T) {
c := aetesting.FakeSingleContext(t, "xmpp", "BulkGetPresence", func(in *pb.BulkPresenceRequest, out *pb.BulkPresenceResponse) error {
if !reflect.DeepEqual(in.Jid, []string{"[email protected]", "[email protected]"}) {
return fmt.Errorf("bad request jids %#v", in.Jid)
}
out.PresenceResponse = []*pb.PresenceResponse{
newPresenceResponse(false, pb.PresenceResponse_AWAY, true),
newPresenceResponse(false, pb.PresenceResponse_DO_NOT_DISTURB, true),
}
return nil
})
jids := []string{"[email protected]", "[email protected]"}
presence, err := GetPresenceMulti(c, jids, "")
wantErr := appengine.MultiError{
ErrPresenceUnavailable,
ErrPresenceUnavailable,
}
if !reflect.DeepEqual(err, wantErr) {
t.Fatalf("GetPresenceMulti: got %#v, want %#v", err, wantErr)
}
want := []string{"", ""}
if !reflect.DeepEqual(presence, want) {
t.Errorf("GetPresenceMulti: got %#v, want %#v", presence, want)
}
}
示例11: TestFieldSpec
func TestFieldSpec(t *testing.T) {
index, err := Open("Doc")
if err != nil {
t.Fatalf("err from Open: %v", err)
}
errFoo := errors.New("foo") // sentinel error when there isn't one.
testCases := []struct {
desc string
opts *SearchOptions
want *pb.FieldSpec
}{
{
desc: "No options",
want: &pb.FieldSpec{},
},
{
desc: "Fields",
opts: &SearchOptions{
Fields: []string{"one", "two"},
},
want: &pb.FieldSpec{
Name: []string{"one", "two"},
},
},
{
desc: "Expressions",
opts: &SearchOptions{
Expressions: []FieldExpression{
{Name: "one", Expr: "price * quantity"},
{Name: "two", Expr: "min(daily_use, 10) * rate"},
},
},
want: &pb.FieldSpec{
Expression: []*pb.FieldSpec_Expression{
{Name: proto.String("one"), Expression: proto.String("price * quantity")},
{Name: proto.String("two"), Expression: proto.String("min(daily_use, 10) * rate")},
},
},
},
}
for _, tt := range testCases {
c := aetesting.FakeSingleContext(t, "search", "Search", func(req *pb.SearchRequest, _ *pb.SearchResponse) error {
params := req.Params
if !reflect.DeepEqual(params.FieldSpec, tt.want) {
t.Errorf("%s: params.FieldSpec=%v; want %v", tt.desc, params.FieldSpec, tt.want)
}
return errFoo // Always return some error to prevent response parsing.
})
it := index.Search(c, "gopher", tt.opts)
if _, err := it.Next(nil); err != errFoo {
t.Fatalf("%s: got error %v; want %v", tt.desc, err, errFoo)
}
}
}
示例12: TestAddResponseRPCError
func TestAddResponseRPCError(t *testing.T) {
c := aetesting.FakeSingleContext(t, "memcache", "Set", func(_ *pb.MemcacheSetRequest, res *pb.MemcacheSetResponse) error {
return errRPC
})
if err := Add(c, &Item{}); err != errRPC {
t.Errorf("got %v want errRPC", err)
}
}
示例13: TestGetResponseRPCError
func TestGetResponseRPCError(t *testing.T) {
c := aetesting.FakeSingleContext(t, "memcache", "Get", func(_ *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
return errRPC
})
if _, err := Get(c, "something"); err != errRPC {
t.Errorf("got %v want errRPC", err)
}
}
示例14: TestAddResponseNotStored
func TestAddResponseNotStored(t *testing.T) {
c := aetesting.FakeSingleContext(t, "memcache", "Set", func(_ *pb.MemcacheSetRequest, res *pb.MemcacheSetResponse) error {
res.SetStatus = []pb.MemcacheSetResponse_SetStatusCode{pb.MemcacheSetResponse_NOT_STORED}
return nil
})
if err := Add(c, &Item{}); err != ErrNotStored {
t.Errorf("got %v want ErrNotStored", err)
}
}
示例15: TestGetResponseMiss
func TestGetResponseMiss(t *testing.T) {
c := aetesting.FakeSingleContext(t, "memcache", "Get", func(_ *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
// don't fill in any of the response
return nil
})
_, err := Get(c, "something")
if err != ErrCacheMiss {
t.Errorf("got %v want ErrCacheMiss", err)
}
}