• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang testutil.Context函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中google/golang.org/cloud/internal/testutil.Context函数的典型用法代码示例。如果您正苦于以下问题:Golang Context函数的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Context函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestGetAllWithFieldMismatch

func TestGetAllWithFieldMismatch(t *testing.T) {
	type Fat struct {
		X, Y int
	}
	type Thin struct {
		X int
	}

	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	putKeys := make([]*Key, 3)
	for i := range putKeys {
		putKeys[i] = NewKey(c, "GetAllThing", "", int64(10+i), nil)
		_, err := Put(c, putKeys[i], &Fat{X: 20 + i, Y: 30 + i})
		if err != nil {
			t.Fatalf("Put: %v", err)
		}
	}

	var got []Thin
	want := []Thin{
		{X: 20},
		{X: 21},
		{X: 22},
	}
	getKeys, err := NewQuery("GetAllThing").GetAll(c, &got)
	if len(getKeys) != 3 && !reflect.DeepEqual(getKeys, putKeys) {
		t.Errorf("GetAll: keys differ\ngetKeys=%v\nputKeys=%v", getKeys, putKeys)
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("GetAll: entities differ\ngot =%v\nwant=%v", got, want)
	}
	if _, ok := err.(*ErrFieldMismatch); !ok {
		t.Errorf("GetAll: got err=%v, want ErrFieldMismatch", err)
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:35,代码来源:integration_test.go


示例2: TestProjection

func TestProjection(t *testing.T) {
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	parent := NewKey(c, "SQParent", "TestProjection", 0, nil)
	now := time.Now().Truncate(time.Millisecond).Unix()
	children := []*SQChild{
		{I: 1 << 0, J: 100, T: now, U: now},
		{I: 1 << 1, J: 100, T: now, U: now},
		{I: 1 << 2, J: 200, T: now, U: now},
		{I: 1 << 3, J: 300, T: now, U: now},
		{I: 1 << 4, J: 300, T: now, U: now},
	}
	baseQuery := NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Filter("J>", 150)
	testSmallQueries(t, c, parent, children, []SQTestCase{
		{
			"project",
			baseQuery.Project("J"),
			3,
			200 + 300 + 300,
		},
		{
			"distinct",
			baseQuery.Project("J").Distinct(),
			2,
			200 + 300,
		},
		{
			"project on meaningful (GD_WHEN) field",
			baseQuery.Project("U"),
			3,
			0,
		},
	})
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:33,代码来源:integration_test.go


示例3: TestBasics

func TestBasics(t *testing.T) {
	type X struct {
		I int
		S string
		T time.Time
	}
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	x0 := X{66, "99", time.Now().Truncate(time.Millisecond)}
	k, err := Put(c, NewIncompleteKey(c, "BasicsX", nil), &x0)
	if err != nil {
		t.Fatalf("Put: %v", err)
	}
	x1 := X{}
	err = Get(c, k, &x1)
	if err != nil {
		t.Errorf("Get: %v", err)
	}
	err = Delete(c, k)
	if err != nil {
		t.Errorf("Delete: %v", err)
	}
	if !reflect.DeepEqual(x0, x1) {
		t.Errorf("compare: x0=%v, x1=%v", x0, x1)
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:25,代码来源:integration_test.go


示例4: TestACL

func TestACL(t *testing.T) {
	ctx := testutil.Context(ScopeFullControl)
	cleanup(t, "acl")
	entity := ACLEntity("domain-google.com")
	if err := PutDefaultACLRule(ctx, bucket, entity, RoleReader); err != nil {
		t.Errorf("Can't put default ACL rule for the bucket, errored with %v", err)
	}
	for _, obj := range aclObjects {
		t.Logf("Writing %v", obj)
		wc := NewWriter(ctx, bucket, obj)
		c := randomContents()
		if _, err := wc.Write(c); err != nil {
			t.Errorf("Write for %v failed with %v", obj, err)
		}
		if err := wc.Close(); err != nil {
			t.Errorf("Close for %v failed with %v", obj, err)
		}
	}
	name := aclObjects[0]
	acl, err := ACL(ctx, bucket, name)
	if err != nil {
		t.Errorf("Can't retrieve ACL of %v", name)
	}
	aclFound := false
	for _, rule := range acl {
		if rule.Entity == entity && rule.Role == RoleReader {
			aclFound = true
		}
	}
	if !aclFound {
		t.Error("Expected to find an ACL rule for google.com domain users, but not found")
	}
	if err := DeleteACLRule(ctx, bucket, name, entity); err != nil {
		t.Errorf("Can't delete the ACL rule for the entity: %v", entity)
	}
	/* TODO(gmlewis): uncomment this section when Cloud Storage Bucket ACL APIs are working again.
	if err := PutBucketACLRule(ctx, bucket, "[email protected]", RoleReader); err != nil {
		t.Errorf("Error while putting bucket ACL rule: %v", err)
	}
	bACL, err := BucketACL(ctx, bucket)
	if err != nil {
		t.Errorf("Error while getting the ACL of the bucket: %v", err)
	}
	bACLFound := false
	for _, rule := range bACL {
		if rule.Entity == "[email protected]" && rule.Role == RoleReader {
			bACLFound = true
		}
	}
	if !bACLFound {
		t.Error("Expected to find an ACL rule for [email protected] user, but not found")
	}
	if err := DeleteBucketACLRule(ctx, bucket, "[email protected]"); err != nil {
		t.Errorf("Error while deleting bucket ACL rule: %v", err)
	}
	*/
}
开发者ID:virgillo,项目名称:gcloud-golang,代码行数:57,代码来源:integration_test.go


示例5: TestGetMulti

func TestGetMulti(t *testing.T) {
	type X struct {
		I int
	}
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	p := NewKey(c, "X", "", time.Now().Unix(), nil)

	cases := []struct {
		key *Key
		put bool
	}{
		{key: NewKey(c, "X", "item1", 0, p), put: true},
		{key: NewKey(c, "X", "item2", 0, p), put: false},
		{key: NewKey(c, "X", "item3", 0, p), put: false},
		{key: NewKey(c, "X", "item4", 0, p), put: true},
	}

	var src, dst []*X
	var srcKeys, dstKeys []*Key
	for _, c := range cases {
		dst = append(dst, &X{})
		dstKeys = append(dstKeys, c.key)
		if c.put {
			src = append(src, &X{})
			srcKeys = append(srcKeys, c.key)
		}
	}
	if _, err := PutMulti(c, srcKeys, src); err != nil {
		t.Error(err)
	}
	err := GetMulti(c, dstKeys, dst)
	if err == nil {
		t.Errorf("GetMulti got %v, expected error", err)
	}
	e, ok := err.(MultiError)
	if !ok {
		t.Errorf("GetMulti got %t, expected MultiError", err)
	}
	for i, err := range e {
		got, want := err, (error)(nil)
		if !cases[i].put {
			got, want = err, ErrNoSuchEntity
		}
		if got != want {
			t.Errorf("MultiError[%d] == %v, want %v", i, got, want)
		}
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:48,代码来源:integration_test.go


示例6: TestAllocateIDs

func TestAllocateIDs(t *testing.T) {
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	keys := make([]*Key, 5)
	for i := range keys {
		keys[i] = NewIncompleteKey(c, "AllocID", nil)
	}
	keys, err := AllocateIDs(c, keys)
	if err != nil {
		t.Errorf("AllocID #0 failed: %v", err)
	}
	if want := len(keys); want != 5 {
		t.Errorf("Expected to allocate 5 keys, %d keys are found", want)
	}
	for _, k := range keys {
		if k.Incomplete() {
			t.Errorf("Unexpeceted incomplete key found: %v", k)
		}
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:19,代码来源:integration_test.go


示例7: TestEventualConsistency

func TestEventualConsistency(t *testing.T) {
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	parent := NewKey(c, "SQParent", "TestEventualConsistency", 0, nil)
	now := time.Now().Truncate(time.Millisecond).Unix()
	children := []*SQChild{
		{I: 0, T: now, U: now},
		{I: 1, T: now, U: now},
		{I: 2, T: now, U: now},
	}
	query := NewQuery("SQChild").Ancestor(parent).Filter("T =", now).EventualConsistency()
	testSmallQueries(t, c, parent, children, nil, func() {
		got, err := query.Count(c)
		if err != nil {
			t.Fatalf("Count: %v", err)
		}
		if got < 0 || 3 < got {
			t.Errorf("Count: got %d, want [0,3]", got)
		}
	})
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:20,代码来源:integration_test.go


示例8: TestGetAllWithFieldMismatch

func TestGetAllWithFieldMismatch(t *testing.T) {
	type Fat struct {
		X, Y int
	}
	type Thin struct {
		X int
	}

	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	// Ancestor queries (those within an entity group) are strongly consistent
	// by default, which prevents a test from being flaky.
	// See https://cloud.google.com/appengine/docs/go/datastore/queries#Go_Data_consistency
	// for more information.
	parent := NewKey(c, "SQParent", "TestGetAllWithFieldMismatch", 0, nil)
	putKeys := make([]*Key, 3)
	for i := range putKeys {
		putKeys[i] = NewKey(c, "GetAllThing", "", int64(10+i), parent)
		_, err := Put(c, putKeys[i], &Fat{X: 20 + i, Y: 30 + i})
		if err != nil {
			t.Fatalf("Put: %v", err)
		}
	}

	var got []Thin
	want := []Thin{
		{X: 20},
		{X: 21},
		{X: 22},
	}
	getKeys, err := NewQuery("GetAllThing").Ancestor(parent).GetAll(c, &got)
	if len(getKeys) != 3 && !reflect.DeepEqual(getKeys, putKeys) {
		t.Errorf("GetAll: keys differ\ngetKeys=%v\nputKeys=%v", getKeys, putKeys)
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("GetAll: entities differ\ngot =%v\nwant=%v", got, want)
	}
	if _, ok := err.(*ErrFieldMismatch); !ok {
		t.Errorf("GetAll: got err=%v, want ErrFieldMismatch", err)
	}
}
开发者ID:christopherhesse,项目名称:disgusting-appengine-context-hack,代码行数:40,代码来源:integration_test.go


示例9: cleanup

func cleanup(t *testing.T, prefix string) {
	ctx := testutil.Context(ScopeFullControl)
	var q *Query = &Query{
		Prefix: prefix,
	}
	for {
		o, err := ListObjects(ctx, bucket, q)
		if err != nil {
			t.Fatalf("Cleanup List for bucket %v failed with error: %v", bucket, err)
		}
		for _, obj := range o.Results {
			t.Logf("Cleanup deletion of %v", obj.Name)
			if err = DeleteObject(ctx, bucket, obj.Name); err != nil {
				t.Fatalf("Cleanup Delete for object %v failed with %v", obj.Name, err)
			}
		}
		if o.Next == nil {
			break
		}
		q = o.Next
	}
}
开发者ID:herberteuler,项目名称:kythe,代码行数:22,代码来源:integration_test.go


示例10: TestListValues

func TestListValues(t *testing.T) {
	p0 := PropertyList{
		{Name: "L", Value: int64(12), Multiple: true},
		{Name: "L", Value: "string", Multiple: true},
		{Name: "L", Value: true, Multiple: true},
	}
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	k, err := Put(c, NewIncompleteKey(c, "ListValue", nil), &p0)
	if err != nil {
		t.Fatalf("Put: %v", err)
	}
	var p1 PropertyList
	if err := Get(c, k, &p1); err != nil {
		t.Errorf("Get: %v", err)
	}
	if !reflect.DeepEqual(p0, p1) {
		t.Errorf("compare:\np0=%v\np1=%#v", p0, p1)
	}
	if err = Delete(c, k); err != nil {
		t.Errorf("Delete: %v", err)
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:22,代码来源:integration_test.go


示例11: TestUnindexableValues

func TestUnindexableValues(t *testing.T) {
	x1500 := strings.Repeat("x", 1500)
	x1501 := strings.Repeat("x", 1501)
	testCases := []struct {
		in      Z
		wantErr bool
	}{
		{in: Z{S: x1500}, wantErr: false},
		{in: Z{S: x1501}, wantErr: true},
		{in: Z{T: x1500}, wantErr: false},
		{in: Z{T: x1501}, wantErr: false},
		{in: Z{P: []byte(x1500)}, wantErr: false},
		{in: Z{P: []byte(x1501)}, wantErr: true},
		{in: Z{K: []byte(x1500)}, wantErr: false},
		{in: Z{K: []byte(x1501)}, wantErr: false},
	}
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	for _, tt := range testCases {
		_, err := Put(c, NewIncompleteKey(c, "BasicsZ", nil), &tt.in)
		if (err != nil) != tt.wantErr {
			t.Errorf("Put %s got err %v, want err %t", tt.in, err, tt.wantErr)
		}
	}
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:24,代码来源:integration_test.go


示例12: TestAll

func TestAll(t *testing.T) {
	ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
	now := time.Now()
	topic := fmt.Sprintf("topic-%d", now.Unix())
	subscription := fmt.Sprintf("subscription-%d", now.Unix())

	if err := CreateTopic(ctx, topic); err != nil {
		t.Errorf("CreateTopic error: %v", err)
	}

	if err := CreateSub(ctx, subscription, topic, time.Duration(0), ""); err != nil {
		t.Errorf("CreateSub error: %v", err)
	}

	exists, err := TopicExists(ctx, topic)
	if err != nil {
		t.Fatalf("TopicExists error: %v", err)
	}
	if !exists {
		t.Errorf("topic %s should exist, but it doesn't", topic)
	}

	exists, err = SubExists(ctx, subscription)
	if err != nil {
		t.Fatalf("SubExists error: %v", err)
	}
	if !exists {
		t.Errorf("subscription %s should exist, but it doesn't", subscription)
	}

	max := 10
	msgs := make([]*Message, max)
	expectedMsgs := make(map[string]bool, max)
	for i := 0; i < max; i++ {
		text := fmt.Sprintf("a message with an index %d", i)
		attrs := make(map[string]string)
		attrs["foo"] = "bar"
		msgs[i] = &Message{
			Data:       []byte(text),
			Attributes: attrs,
		}
		expectedMsgs[text] = false
	}

	ids, err := Publish(ctx, topic, msgs...)
	if err != nil {
		t.Fatalf("Publish (1) error: %v", err)
	}

	if len(ids) != max {
		t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
	}

	expectedIDs := make(map[string]bool, max)
	for _, id := range ids {
		expectedIDs[id] = false
	}

	received, err := PullWait(ctx, subscription, max)
	if err != nil {
		t.Fatalf("PullWait error: %v", err)
	}

	if len(received) != max {
		t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
	}

	for _, msg := range received {
		expectedMsgs[string(msg.Data)] = true
		expectedIDs[msg.ID] = true
		if msg.Attributes["foo"] != "bar" {
			t.Errorf("message attribute foo is expected to be 'bar', found '%s'", msg.Attributes["foo"])
		}
	}

	for msg, found := range expectedMsgs {
		if !found {
			t.Errorf("message '%s' should be received", msg)
		}
	}

	for id, found := range expectedIDs {
		if !found {
			t.Errorf("message with the message id '%s' should be received", id)
		}
	}

	// base64 test
	data := "[email protected]~"
	msg := &Message{
		Data: []byte(data),
	}
	_, err = Publish(ctx, topic, msg)
	if err != nil {
		t.Fatalf("Publish (2) error: %v", err)
	}

	received, err = PullWait(ctx, subscription, 1)
	if err != nil {
		t.Fatalf("PullWait error: %v", err)
//.........这里部分代码省略.........
开发者ID:rkazak,项目名称:distribution,代码行数:101,代码来源:integration_test.go


示例13: TestAll

func TestAll(t *testing.T) {
	if testing.Short() {
		t.Skip("Integration tests skipped in short mode")
	}
	// TODO(djd): Replace this ctx with context.Background() when the new API is complete.
	ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
	if ctx == nil {
		t.Skip("Integration tests skipped. See CONTRIBUTING.md for details")
	}
	ts := testutil.TokenSource(ctx, ScopePubSub, ScopeCloudPlatform)
	if ts == nil {
		t.Skip("Integration tests skipped. See CONTRIBUTING.md for details")
	}

	now := time.Now()
	topicName := fmt.Sprintf("topic-%d", now.Unix())
	subName := fmt.Sprintf("subscription-%d", now.Unix())

	client, err := NewClient(ctx, testutil.ProjID(), cloud.WithTokenSource(ts))
	if err != nil {
		t.Fatalf("Creating client error: %v", err)
	}

	var topic *TopicHandle
	if topic, err = client.NewTopic(ctx, topicName); err != nil {
		t.Errorf("CreateTopic error: %v", err)
	}

	var sub *SubscriptionHandle
	if sub, err = topic.Subscribe(ctx, subName, nil); err != nil {
		t.Errorf("CreateSub error: %v", err)
	}

	exists, err := topic.Exists(ctx)
	if err != nil {
		t.Fatalf("TopicExists error: %v", err)
	}
	if !exists {
		t.Errorf("topic %s should exist, but it doesn't", topic)
	}

	exists, err = sub.Exists(ctx)
	if err != nil {
		t.Fatalf("SubExists error: %v", err)
	}
	if !exists {
		t.Errorf("subscription %s should exist, but it doesn't", subName)
	}

	max := 10
	msgs := make([]*Message, max)
	expectedMsgs := make(map[string]bool, max)
	for i := 0; i < max; i++ {
		text := fmt.Sprintf("a message with an index %d", i)
		attrs := make(map[string]string)
		attrs["foo"] = "bar"
		msgs[i] = &Message{
			Data:       []byte(text),
			Attributes: attrs,
		}
		expectedMsgs[text] = false
	}

	ids, err := Publish(ctx, topicName, msgs...)
	if err != nil {
		t.Fatalf("Publish (1) error: %v", err)
	}

	if len(ids) != max {
		t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
	}

	expectedIDs := make(map[string]bool, max)
	for _, id := range ids {
		expectedIDs[id] = false
	}

	received, err := PullWait(ctx, subName, max)
	if err != nil {
		t.Fatalf("PullWait error: %v", err)
	}

	if len(received) != max {
		t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
	}

	for _, msg := range received {
		expectedMsgs[string(msg.Data)] = true
		expectedIDs[msg.ID] = true
		if msg.Attributes["foo"] != "bar" {
			t.Errorf("message attribute foo is expected to be 'bar', found '%s'", msg.Attributes["foo"])
		}
	}

	for msg, found := range expectedMsgs {
		if !found {
			t.Errorf("message '%s' should be received", msg)
		}
	}

//.........这里部分代码省略.........
开发者ID:gonyi,项目名称:gcloud-golang,代码行数:101,代码来源:integration_test.go


示例14: TestObjects

func TestObjects(t *testing.T) {
	ctx := testutil.Context(ScopeFullControl)
	bucket = os.Getenv(envBucket)

	// Cleanup.
	cleanup(t, "obj")

	const defaultType = "text/plain"

	// Test Writer.
	for _, obj := range objects {
		t.Logf("Writing %v", obj)
		wc := NewWriter(ctx, bucket, obj)
		wc.ContentType = defaultType
		c := randomContents()
		if _, err := wc.Write(c); err != nil {
			t.Errorf("Write for %v failed with %v", obj, err)
		}
		if err := wc.Close(); err != nil {
			t.Errorf("Close for %v failed with %v", obj, err)
		}
		contents[obj] = c
	}

	// Test Reader.
	for _, obj := range objects {
		t.Logf("Creating a reader to read %v", obj)
		rc, err := NewReader(ctx, bucket, obj)
		if err != nil {
			t.Errorf("Can't create a reader for %v, errored with %v", obj, err)
		}
		slurp, err := ioutil.ReadAll(rc)
		if err != nil {
			t.Errorf("Can't ReadAll object %v, errored with %v", obj, err)
		}
		if got, want := slurp, contents[obj]; !bytes.Equal(got, want) {
			t.Errorf("Contents (%v) = %q; want %q", obj, got, want)
		}
		rc.Close()
	}

	// Test NotFound.
	_, err := NewReader(ctx, bucket, "obj-not-exists")
	if err != ErrObjectNotExist {
		t.Errorf("Object should not exist, err found to be %v", err)
	}

	name := objects[0]

	// Test StatObject.
	o, err := StatObject(ctx, bucket, name)
	if err != nil {
		t.Error(err)
	}
	if got, want := o.Name, name; got != want {
		t.Errorf("Name (%v) = %q; want %q", name, got, want)
	}
	if got, want := o.ContentType, defaultType; got != want {
		t.Errorf("ContentType (%v) = %q; want %q", name, got, want)
	}

	// Test object copy.
	copy, err := CopyObject(ctx, bucket, name, bucket, copyObj, nil)
	if err != nil {
		t.Errorf("CopyObject failed with %v", err)
	}
	if copy.Name != copyObj {
		t.Errorf("Copy object's name = %q; want %q", copy.Name, copyObj)
	}
	if copy.Bucket != bucket {
		t.Errorf("Copy object's bucket = %q; want %q", copy.Bucket, bucket)
	}

	// Test UpdateAttrs.
	updated, err := UpdateAttrs(ctx, bucket, name, ObjectAttrs{
		ContentType: "text/html",
		ACL:         []ACLRule{{Entity: "domain-google.com", Role: RoleReader}},
	})
	if err != nil {
		t.Errorf("UpdateAttrs failed with %v", err)
	}
	if want := "text/html"; updated.ContentType != want {
		t.Errorf("updated.ContentType == %q; want %q", updated.ContentType, want)
	}

	// Test checksums.
	checksumCases := []struct {
		name     string
		contents [][]byte
		size     int64
		md5      string
		crc32c   uint32
	}{
		{
			name:     "checksum-object",
			contents: [][]byte{[]byte("hello"), []byte("world")},
			size:     10,
			md5:      "fc5e038d38a57032085441e7fe7010b0",
			crc32c:   1456190592,
		},
//.........这里部分代码省略.........
开发者ID:herberteuler,项目名称:kythe,代码行数:101,代码来源:integration_test.go


示例15: TestKindlessQueries

func TestKindlessQueries(t *testing.T) {
	type Dee struct {
		I   int
		Why string
	}
	type Dum struct {
		I     int
		Pling string
	}

	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	parent := NewKey(c, "Tweedle", "tweedle", 0, nil)

	keys := []*Key{
		NewKey(c, "Dee", "dee0", 0, parent),
		NewKey(c, "Dum", "dum1", 0, parent),
		NewKey(c, "Dum", "dum2", 0, parent),
		NewKey(c, "Dum", "dum3", 0, parent),
	}
	src := []interface{}{
		&Dee{1, "binary0001"},
		&Dum{2, "binary0010"},
		&Dum{4, "binary0100"},
		&Dum{8, "binary1000"},
	}
	keys, err := PutMulti(c, keys, src)
	if err != nil {
		t.Fatalf("put: %v", err)
	}

	testCases := []struct {
		desc    string
		query   *Query
		want    []int
		wantErr string
	}{
		{
			desc:  "Dee",
			query: NewQuery("Dee"),
			want:  []int{1},
		},
		{
			desc:  "Doh",
			query: NewQuery("Doh"),
			want:  nil},
		{
			desc:  "Dum",
			query: NewQuery("Dum"),
			want:  []int{2, 4, 8},
		},
		{
			desc:  "",
			query: NewQuery(""),
			want:  []int{1, 2, 4, 8},
		},
		{
			desc:  "Kindless filter",
			query: NewQuery("").Filter("__key__ =", keys[2]),
			want:  []int{4},
		},
		{
			desc:  "Kindless order",
			query: NewQuery("").Order("__key__"),
			want:  []int{1, 2, 4, 8},
		},
		{
			desc:    "Kindless bad filter",
			query:   NewQuery("").Filter("I =", 4),
			wantErr: "kind is required for filter: I",
		},
		{
			desc:    "Kindless bad order",
			query:   NewQuery("").Order("-__key__"),
			wantErr: "kind is required for all orders except __key__ ascending",
		},
	}
loop:
	for _, tc := range testCases {
		q := tc.query.Ancestor(parent)
		gotCount, err := q.Count(c)
		if err != nil {
			if tc.wantErr == "" || !strings.Contains(err.Error(), tc.wantErr) {
				t.Errorf("count %q: err %v, want err %q", tc.desc, err, tc.wantErr)
			}
			continue
		}
		if tc.wantErr != "" {
			t.Errorf("count %q: want err %q", tc.desc, tc.wantErr)
			continue
		}
		if gotCount != len(tc.want) {
			t.Errorf("count %q: got %d want %d", tc.desc, gotCount, len(tc.want))
			continue
		}
		var got []int
		for iter := q.Run(c); ; {
			var dst struct {
				I          int
				Why, Pling string
			}
//.........这里部分代码省略.........
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:101,代码来源:integration_test.go


示例16: TestFilters

func TestFilters(t *testing.T) {
	c := testutil.Context(ScopeDatastore, ScopeUserEmail)
	parent := NewKey(c, "SQParent", "TestFilters", 0, nil)
	now := time.Now().Truncate(time.Millisecond).Unix()
	children := []*SQChild{
		{I: 0, T: now, U: now},
		{I: 1, T: now, U: now},
		{I: 2, T: now, U: now},
		{I: 3, T: now, U: now},
		{I: 4, T: now, U: now},
		{I: 5, T: now, U: now},
		{I: 6, T: now, U: now},
		{I: 7, T: now, U: now},
	}
	baseQuery := NewQuery("SQChild").Ancestor(parent).Filter("T=", now)
	testSmallQueries(t, c, parent, children, []SQTestCase{
		{
			"I>1",
			baseQuery.Filter("I>", 1),
			6,
			2 + 3 + 4 + 5 + 6 + 7,
		},
		{
			"I>2 AND I<=5",
			baseQuery.Filter("I>", 2).Filter("I<=", 5),
			3,
			3 + 4 + 5,
		},
		{
			"I>=3 AND I<3",
			baseQuery.Filter("I>=", 3).Filter("I<", 3),
			0,
			0,
		},
		{
			"I=4",
			baseQuery.Filter("I=", 4),
			1,
			4,
		},
	}, func() {
		got := []*SQChild{}
		want := []*SQChild{
			{I: 0, T: now, U: now},
			{I: 1, T: now, U: now},
			{I: 2, T: now, U: now},
			{I: 3, T: now, U: now},
			{I: 4, T: now, U: now},
			{I: 5, T: now, U: now},
			{I: 6, T: now, U: now},
			{I: 7, T: now, U: now},
		}
		_, err := baseQuery.Order("I").GetAll(c, &got)
		if err != nil {
			t.Errorf("GetAll: %v", err)
		}
		if !reflect.DeepEqual(got, want) {
			t.Errorf("compare: got=%v, want=%v", got, want)
		}
	}, func() {
		got := []*SQChild{}
		want := []*SQChild{
			{I: 7, T: now, U: now},
			{I: 6, T: now, U: now},
			{I: 5, T: now, U: now},
			{I: 4, T: now, U: now},
			{I: 3, T: now, U: now},
			{I: 2, T: now, U: now},
			{I: 1, T: now, U: now},
			{I: 0, T: now, U: now},
		}
		_, err := baseQuery.Order("-I").GetAll(c, &got)
		if err != nil {
			t.Errorf("GetAll: %v", err)
		}
		if !reflect.DeepEqual(got, want) {
			t.Errorf("compare: got=%v, want=%v", got, want)
		}
	})
}
开发者ID:AbhiAgarwal,项目名称:abelana,代码行数:80,代码来源:integration_test.go



注:本文中的google/golang.org/cloud/internal/testutil.Context函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang testutil.ProjID函数代码示例发布时间:2022-05-28
下一篇:
Golang opts.DialOpt类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap