-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gregory Russell
committed
Nov 20, 2019
1 parent
81978dd
commit 6f54c24
Showing
4 changed files
with
89 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tracker_test | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"sync" | ||
|
||
"cloud.google.com/go/datastore" | ||
"github.com/GoogleCloudPlatform/google-cloud-go-testing/datastore/dsiface" | ||
) | ||
|
||
func validateDatastoreEntity(e interface{}) error { | ||
v := reflect.ValueOf(e) | ||
if v.Kind() != reflect.Ptr { | ||
return datastore.ErrInvalidEntityType | ||
} | ||
if reflect.Indirect(v).Kind() != reflect.Struct { | ||
return datastore.ErrInvalidEntityType | ||
} | ||
return nil | ||
} | ||
|
||
type testClient struct { | ||
dsiface.Client // For unimplemented methods | ||
lock sync.Mutex | ||
objects map[datastore.Key]reflect.Value | ||
} | ||
|
||
func newTestClient() *testClient { | ||
return &testClient{objects: make(map[datastore.Key]reflect.Value, 10)} | ||
} | ||
|
||
func (c *testClient) Close() error { return nil } | ||
|
||
func (c *testClient) Count(ctx context.Context, q *datastore.Query) (n int, err error) { | ||
return 0, ErrNotImplemented | ||
} | ||
|
||
func (c *testClient) Delete(ctx context.Context, key *datastore.Key) error { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
_, ok := c.objects[*key] | ||
if !ok { | ||
return datastore.ErrNoSuchEntity | ||
} | ||
delete(c.objects, *key) | ||
return nil | ||
} | ||
|
||
func (c *testClient) Get(ctx context.Context, key *datastore.Key, dst interface{}) (err error) { | ||
err = validateDatastoreEntity(dst) | ||
if err != nil { | ||
return err | ||
} | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
v := reflect.ValueOf(dst) | ||
o, ok := c.objects[*key] | ||
if !ok { | ||
return datastore.ErrNoSuchEntity | ||
} | ||
v.Elem().Set(o) | ||
return nil | ||
} | ||
|
||
func (c *testClient) Put(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error) { | ||
err := validateDatastoreEntity(src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
v := reflect.ValueOf(src) | ||
c.objects[*key] = reflect.Indirect(v) | ||
return key, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters