Abacn commented on a change in pull request #17173:
URL: https://github.com/apache/beam/pull/17173#discussion_r836586135



##########
File path: sdks/go/pkg/beam/io/datastoreio/datastore_test.go
##########
@@ -16,11 +16,164 @@
 package datastoreio
 
 import (
+       "context"
+       "errors"
+       "reflect"
+       "strings"
        "testing"
 
        "cloud.google.com/go/datastore"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest"
+       "google.golang.org/api/option"
 )
 
+// fake client type implements datastoreio.clientType
+type fakeClient struct {
+       runFn   func()
+       closeFn func()
+}
+
+func (client *fakeClient) Run(context.Context, *datastore.Query) 
*datastore.Iterator {
+       client.runFn()
+       // return an empty iterator
+       return new(datastore.Iterator)
+}
+
+func (client *fakeClient) Close() error {
+       client.closeFn()
+       return nil
+}
+
+// mock type for query
+type Foo struct {
+}
+
+type Bar struct {
+}
+
+func TestRead(t *testing.T) {
+       runCounter := 0
+       closeCounter := 0
+
+       // setup a fake newClient caller
+       originalClient := newClient
+       newClient = func(ctx context.Context,
+               projectID string,
+               opts ...option.ClientOption) (clientType, error) {
+               client := fakeClient{
+                       runFn: func() {
+                               runCounter += 1
+                       },
+                       closeFn: func() {
+                               closeCounter += 1
+                       },
+               }
+               return &client, nil
+       }
+
+       testCases := []struct {
+               v           interface{}
+               shard       int
+               expectRun   int
+               expectClose int
+       }{
+               // case 1: shard=1, without split query
+               {Foo{}, 1, 1, 1},
+               // case 2: shard=2 (>1), with split query
+               {Bar{}, 2, 2, 2},
+       }
+       for _, tc := range testCases {
+               itemType := reflect.TypeOf(tc.v)
+               itemKey := runtime.RegisterType(itemType)
+
+               p, s := beam.NewPipelineWithRoot()
+               Read(s, "project", "Item", tc.shard, itemType, itemKey)
+
+               ptest.RunAndValidate(t, p)
+
+               if runCounter != tc.expectRun {
+                       t.Errorf("got number of datastore.Client.Run call: %v, 
wanted %v",
+                               runCounter, tc.expectRun)
+               }
+               if runCounter != tc.expectRun {
+                       t.Errorf("got number of datastore.Client.Run call: %v, 
wanted %v",
+                               closeCounter, tc.expectClose)
+               }
+
+               // reset counter
+               runCounter = 0
+               closeCounter = 0
+       }
+
+       // tear down: recover original newClient caller
+       newClient = originalClient

Review comment:
       Adapted the function field approach in the last comment. Cleanup no 
longer needed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to