Example #1
0
func TestDatasets(t *testing.T) {
	service := &listDatasetsFake{projectID: "p"}
	client := &Client{service: service}
	datasets := []*Dataset{
		{"p", "a", client},
		{"p", "b", client},
		{"p", "hidden", client},
		{"p", "c", client},
	}
	service.datasets = datasets
	service.hidden = map[*Dataset]bool{datasets[2]: true}
	c := &Client{
		projectID: "p",
		service:   service,
	}
	msg, ok := itest.TestIterator(datasets,
		func() interface{} { it := c.Datasets(context.Background()); it.ListHidden = true; return it },
		func(it interface{}) (interface{}, error) { return it.(*DatasetIterator).Next() })
	if !ok {
		t.Fatalf("ListHidden=true: %s", msg)
	}

	msg, ok = itest.TestIterator([]*Dataset{datasets[0], datasets[1], datasets[3]},
		func() interface{} { it := c.Datasets(context.Background()); it.ListHidden = false; return it },
		func(it interface{}) (interface{}, error) { return it.(*DatasetIterator).Next() })
	if !ok {
		t.Fatalf("ListHidden=false: %s", msg)
	}
}
Example #2
0
func TestListMetrics(t *testing.T) {
	ctx := context.Background()

	var metrics []*Metric
	for i := 0; i < 10; i++ {
		metrics = append(metrics, &Metric{
			ID:          ltesting.UniqueID(testMetricIDPrefix),
			Description: "DESC",
			Filter:      "FILTER",
		})
	}
	for _, m := range metrics {
		if err := client.CreateMetric(ctx, m); err != nil {
			t.Fatalf("Create(%q): %v", m.ID, err)
		}
		defer client.DeleteMetric(ctx, m.ID)
	}

	msg, ok := itesting.TestIterator(metrics,
		func() interface{} { return client.Metrics(ctx) },
		func(it interface{}) (interface{}, error) { return it.(*MetricIterator).Next() })
	if !ok {
		t.Fatal(msg)
	}
}
Example #3
0
func TestTables(t *testing.T) {
	t1 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t1"}
	t2 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t2"}
	t3 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t3"}
	allTables := []*Table{t1, t2, t3}
	c := &Client{
		service: &listTablesServiceStub{
			expectedProject: "x",
			expectedDataset: "y",
			tables:          allTables,
		},
		projectID: "x",
	}
	msg, ok := itest.TestIterator(allTables,
		func() interface{} { return c.Dataset("y").Tables(context.Background()) },
		func(it interface{}) (interface{}, error) { return it.(*TableIterator).Next() })
	if !ok {
		t.Error(msg)
	}
}
Example #4
0
func TestNext(t *testing.T) {
	// Test the iterator's Next method with a variety of different service behaviors.
	// This is primarily a test of PageInfo.next.
	for _, svc := range []service{
		{end: 0},
		{end: 5},
		{end: 5, max: 1},
		{end: 5, max: 2},
		{end: 5, zeroes: true},
		{end: 5, max: 2, zeroes: true},
	} {
		client := &Client{&svc}

		msg, ok := itest.TestIterator(
			seq(0, svc.end),
			func() interface{} { return client.Items(ctx) },
			func(it interface{}) (interface{}, error) { return it.(*ItemIterator).Next() })
		if !ok {
			t.Errorf("%+v: %s", svc, msg)
		}
	}
}
Example #5
0
func testObjectIterator(t *testing.T, bkt *BucketHandle, objects []string) {
	ctx := context.Background()
	// Collect the list of items we expect: ObjectAttrs in lexical order by name.
	names := make([]string, len(objects))
	copy(names, objects)
	sort.Strings(names)
	var attrs []*ObjectAttrs
	for _, name := range names {
		attr, err := bkt.Object(name).Attrs(ctx)
		if err != nil {
			t.Errorf("Object(%q).Attrs: %v", name, err)
			return
		}
		attrs = append(attrs, attr)
	}
	// The following iterator test fails occasionally, probably because the
	// underlying Objects.List operation is eventually consistent. So we retry
	// it.
	tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
	defer cancel()
	var msg string
	var ok bool
	err := internal.Retry(tctx, gax.Backoff{}, func() (stop bool, err error) {
		msg, ok = itesting.TestIterator(attrs,
			func() interface{} { return bkt.Objects(ctx, &Query{Prefix: "obj"}) },
			func(it interface{}) (interface{}, error) { return it.(*ObjectIterator).Next() })
		if ok {
			return true, nil
		} else {
			t.Logf("TestIterator failed, trying again: %s", msg)
			return false, nil
		}
	})
	if !ok {
		t.Errorf("ObjectIterator.Next: %s (err=%v)", msg, err)
	}
	// TODO(jba): test query.Delimiter != ""
}
Example #6
0
func TestListSinks(t *testing.T) {
	ctx := context.Background()
	var sinks []*Sink
	for i := 0; i < 4; i++ {
		sinks = append(sinks, &Sink{
			ID:          ltesting.UniqueID(testSinkIDPrefix),
			Destination: testSinkDestination,
			Filter:      testFilter,
		})
	}
	for _, s := range sinks {
		if _, err := client.CreateSink(ctx, s); err != nil {
			t.Fatalf("Create(%q): %v", s.ID, err)
		}
		defer client.DeleteSink(ctx, s.ID)
	}

	msg, ok := itesting.TestIterator(sinks,
		func() interface{} { return client.Sinks(ctx) },
		func(it interface{}) (interface{}, error) { return it.(*SinkIterator).Next() })
	if !ok {
		t.Fatal(msg)
	}
}