From 8307bb676db3990bb9f1ba84cf5cacfd61c2d619 Mon Sep 17 00:00:00 2001 From: Jan Kamieth Date: Fri, 22 Mar 2019 22:20:34 +0100 Subject: [PATCH] integrate index into key interface --- dynamo_repository.go | 30 ++-- dynamo_repository_interface.go | 3 - examples/example.go | 4 +- global_index.go | 81 --------- go.mod | 10 ++ go.sum | 46 ++++++ key.go | 12 ++ key_interface.go | 2 + mock/log_interface.go | 2 +- mock/metrics_interface.go | 2 +- reflect_helper_test.go | 2 +- repository_delete_test.go | 4 +- repository_get_test.go | 4 +- repository_global_index_test.go | 280 -------------------------------- repository_save_test.go | 4 +- repository_update_test.go | 4 +- 16 files changed, 100 insertions(+), 390 deletions(-) delete mode 100644 global_index.go create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 repository_global_index_test.go diff --git a/dynamo_repository.go b/dynamo_repository.go index 8901409..13a8be0 100644 --- a/dynamo_repository.go +++ b/dynamo_repository.go @@ -48,6 +48,11 @@ func (repository *Repository) GetItemWithContext(ctx context.Context, key KeyInt query = query.Range(*key.RangeKeyName(), dynamo.Equal, key.RangeKey()) } + // index + if key.Index() != nil { + query = query.Index(*key.Index()) + } + err := query.OneWithContext(ctx, item) if err != nil { if err == dynamo.ErrNotFound { @@ -144,14 +149,14 @@ func (repository *Repository) DeleteItemWithContext(ctx context.Context, key Key return err } // by hash - delete := repository.table(key.TableName()).Delete(*key.HashKeyName(), key.HashKey()) + del := repository.table(key.TableName()).Delete(*key.HashKeyName(), key.HashKey()) // by range if key.RangeKeyName() != nil && key.RangeKey() != nil { - delete = delete.Range(*key.RangeKeyName(), key.RangeKey()) + del = del.Range(*key.RangeKeyName(), key.RangeKey()) } - err := delete.RunWithContext(ctx) + err := del.RunWithContext(ctx) if err != nil { repository.log.error(ctx, key.TableName(), err.Error()) return err @@ -249,7 +254,15 @@ func (repository *Repository) GetItemsWithContext(ctx context.Context, key KeyIn return false, err } - err := repository.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()).AllWithContext(ctx, items) + query := repository.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()) + + // index + if key.Index() != nil { + query = query.Index(*key.Index()) + } + + err := query.AllWithContext(ctx, items) + if err != nil { if err == dynamo.ErrNotFound { repository.log.info(ctx, key.TableName(), ErrNoItemFound.Error()) @@ -303,15 +316,6 @@ func (repository Repository) GetItems(key KeyInterface, items interface{}) (bool return repository.GetItemsWithContext(context.TODO(), key, items) } -// GIndex creates an index repository by name -func (repository Repository) GIndex(name string) GlobalIndexInterface { - return GlobalIndex{ - name: name, - log: repository.log, - dynamoClient: repository.dynamoClient, - } -} - func (repository Repository) table(tableName string) dynamo.Table { return repository.dynamoClient.Table(tableName) } diff --git a/dynamo_repository_interface.go b/dynamo_repository_interface.go index 4029359..deb8fa1 100644 --- a/dynamo_repository_interface.go +++ b/dynamo_repository_interface.go @@ -67,7 +67,4 @@ type RepositoryInterface interface { // GetItems by key; it accepts key of item to get it; context which used to enable log with context // returns true if items are found, returns false and nil if no items found, returns false and error in case of error GetItemsWithContext(ctx context.Context, key KeyInterface, out interface{}) (bool, error) - - // GIndex returns index repository - GIndex(name string) GlobalIndexInterface } diff --git a/examples/example.go b/examples/example.go index 64fd018..596fb90 100644 --- a/examples/example.go +++ b/examples/example.go @@ -3,8 +3,8 @@ package examples import ( "context" "fmt" - "github.com/djoemo" - "github.com/djoemo/mock" + "github.com/adjoeio/djoemo" + "github.com/adjoeio/djoemo/mock" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" "time" diff --git a/global_index.go b/global_index.go deleted file mode 100644 index a012b95..0000000 --- a/global_index.go +++ /dev/null @@ -1,81 +0,0 @@ -package djoemo - -import ( - "context" - "github.com/guregu/dynamo" -) - -// GlobalIndex models a global secondary index used in a query -type GlobalIndex struct { - name string - dynamoClient *dynamo.DB - log logger -} - -// GetItems by key; it accepts a key interface that is used to get the table name, hash key and range key if it exists; the output will be given in items -// returns true if items are found, returns false and nil if no items found, returns false and error in case of error -func (gi GlobalIndex) GetItems(key KeyInterface, items interface{}) (bool, error) { - return gi.GetItemsWithContext(context.TODO(), key, items) -} - -// GetItem get item; it accepts a key interface that is used to get the table name, hash key and range key if it exists; the output will be given in item -// returns true if item is found, returns false and nil if no item found, returns false and an error in case of error -func (gi GlobalIndex) GetItem(key KeyInterface, item interface{}) (bool, error) { - return gi.GetItemWithContext(context.TODO(), key, item) -} - -// GetItem item; it needs a key interface that is used to get the table name, hash key, and the range key if it exists; output will be contained in item; context is optional param, which used to enable log with context -func (gi GlobalIndex) GetItemWithContext(ctx context.Context, key KeyInterface, item interface{}) (bool, error) { - - if err := isValidKey(key); err != nil { - gi.log.error(ctx, key.TableName(), err.Error()) - return false, err - } - - // by hash - query := gi.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()) - - // by range - if key.RangeKeyName() != nil && key.RangeKey() != nil { - query = query.Range(*key.RangeKeyName(), dynamo.Equal, key.RangeKey()) - } - - err := query.Index(gi.name).OneWithContext(ctx, item) - if err != nil { - if err == dynamo.ErrNotFound { - gi.log.info(ctx, key.TableName(), ErrNoItemFound.Error()) - return false, nil - } - - gi.log.error(ctx, key.TableName(), err.Error()) - return false, err - } - - return true, nil - -} - -// GetItems queries multiple items by key (hash key) and returns it in the slice of items items -func (gi GlobalIndex) GetItemsWithContext(ctx context.Context, key KeyInterface, items interface{}) (bool, error) { - if err := isValidKey(key); err != nil { - gi.log.error(ctx, key.TableName(), err.Error()) - return false, err - } - - err := gi.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()).Index(gi.name).AllWithContext(ctx, items) - if err != nil { - if err == dynamo.ErrNotFound { - gi.log.info(ctx, key.TableName(), ErrNoItemFound.Error()) - return false, nil - } - - gi.log.error(ctx, key.TableName(), err.Error()) - return false, err - } - - return true, nil -} - -func (gi GlobalIndex) table(tableName string) dynamo.Table { - return gi.dynamoClient.Table(tableName) -} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6a50f0c --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/adjoeio/djoemo + +require ( + github.com/aws/aws-sdk-go v1.19.1 + github.com/golang/mock v1.2.0 + github.com/guregu/dynamo v1.2.1 + github.com/onsi/ginkgo v1.8.0 + github.com/onsi/gomega v1.5.0 + github.com/pkg/errors v0.8.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3b8fff5 --- /dev/null +++ b/go.sum @@ -0,0 +1,46 @@ +github.com/aws/aws-sdk-go v1.18.5/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/aws/aws-sdk-go v1.19.1 h1:8kOP0/XGJwXIFlYoD1DAtA39cAjc15Iv/QiDMKitD9U= +github.com/aws/aws-sdk-go v1.19.1/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY= +github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= +github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/guregu/dynamo v1.2.1 h1:1jKHg3GSTo4/JpmnlaLawqhh8XoYCrTCD5IrWs4ONp8= +github.com/guregu/dynamo v1.2.1/go.mod h1:ZS3tuE64ykQlCnuGfOnAi+ztGZlq0Wo/z5EVQA1fwFY= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w= +github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190318221613-d196dffd7c2b h1:ZWpVMTsK0ey5WJCu+vVdfMldWq7/ezaOcjnKWIHWVkE= +golang.org/x/net v0.0.0-20190318221613-d196dffd7c2b/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/key.go b/key.go index 143d37b..bad7412 100644 --- a/key.go +++ b/key.go @@ -2,6 +2,7 @@ package djoemo type key struct { tableName string + index *string hashKeyName *string rangeKeyName *string hashKey interface{} @@ -19,6 +20,12 @@ func (k *key) WithTableName(tableName string) *key { return k } +// WithIndex set djoemo index +func (k *key) WithIndex(index *string) *key { + k.index = index + return k +} + // WithHashKeyName set djoemo key hash key name func (k *key) WithHashKeyName(hashKeyName string) *key { k.hashKeyName = &hashKeyName @@ -48,6 +55,11 @@ func (k *key) TableName() string { return k.tableName } +// Index returns the djoemo index +func (k *key) Index() *string { + return k.index +} + // HashKeyName returns the name of hash key if exists func (k *key) HashKeyName() *string { return k.hashKeyName diff --git a/key_interface.go b/key_interface.go index 7717bc0..84b8f5c 100644 --- a/key_interface.go +++ b/key_interface.go @@ -4,6 +4,8 @@ package djoemo type KeyInterface interface { // TableName returns the djoemo table name TableName() string + // Index returns the djoemo index to use + Index() *string // HashKeyName returns the name of hash key if exists HashKeyName() *string // RangeKeyName returns the name of range key if exists diff --git a/mock/log_interface.go b/mock/log_interface.go index 25dc612..1365989 100644 --- a/mock/log_interface.go +++ b/mock/log_interface.go @@ -6,7 +6,7 @@ package mock import ( context "context" - djoemo "github.com/djoemo" + djoemo "github.com/adjoeio/djoemo" gomock "github.com/golang/mock/gomock" reflect "reflect" ) diff --git a/mock/metrics_interface.go b/mock/metrics_interface.go index 0b7efa6..583bf88 100644 --- a/mock/metrics_interface.go +++ b/mock/metrics_interface.go @@ -6,7 +6,7 @@ package mock import ( context "context" - djoemo "github.com/djoemo" + djoemo "github.com/adjoeio/djoemo" gomock "github.com/golang/mock/gomock" reflect "reflect" ) diff --git a/reflect_helper_test.go b/reflect_helper_test.go index 8d125ea..528ce3b 100644 --- a/reflect_helper_test.go +++ b/reflect_helper_test.go @@ -1,7 +1,7 @@ package djoemo_test import ( - . "djoemo" + . "github.com/adjoeio/djoemo" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) diff --git a/repository_delete_test.go b/repository_delete_test.go index 7380579..5a56d42 100644 --- a/repository_delete_test.go +++ b/repository_delete_test.go @@ -3,8 +3,8 @@ package djoemo_test import ( "context" "errors" - . "github.com/djoemo" - "github.com/djoemo/mock" + . "github.com/adjoeio/djoemo" + "github.com/adjoeio/djoemo/mock" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/repository_get_test.go b/repository_get_test.go index 4e9b561..ec3ab39 100644 --- a/repository_get_test.go +++ b/repository_get_test.go @@ -2,8 +2,8 @@ package djoemo_test import ( "errors" - . "github.com/djoemo" - "github.com/djoemo/mock" + . "github.com/adjoeio/djoemo" + "github.com/adjoeio/djoemo/mock" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/repository_global_index_test.go b/repository_global_index_test.go deleted file mode 100644 index 8e41996..0000000 --- a/repository_global_index_test.go +++ /dev/null @@ -1,280 +0,0 @@ -package djoemo_test - -import ( - "errors" - . "github.com/djoemo" - "github.com/djoemo/mock" - "github.com/golang/mock/gomock" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Global Index", func() { - const ( - UserTableName = "UserTable" - ProfileTableName = "ProfileTable" - IndexName = "gindex" - ) - - var ( - dMock mock.DynamoMock - repository RepositoryInterface - ) - - BeforeEach(func() { - mockCtrl := gomock.NewController(GinkgoT()) - dAPIMock := mock.NewMockDynamoDBAPI(mockCtrl) - dMock = mock.NewDynamoMock(dAPIMock) - repository = NewRepository(dAPIMock) - }) - - Describe("GetItem", func() { - Describe("GetItem Invalid key ", func() { - It("should fail with table name is nil", func() { - key := Key().WithHashKeyName("UUID").WithHashKey("uuid") - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(Equal(ErrInvalidTableName)) - Expect(found).To(BeFalse()) - }) - It("should fail with hash key name is nil", func() { - key := Key().WithTableName(UserTableName).WithHashKey("uuid") - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(Equal(ErrInvalidHashKeyName)) - Expect(found).To(BeFalse()) - }) - It("should fail with hash key value is nil", func() { - key := Key().WithTableName(UserTableName).WithHashKeyName("UUID") - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(Equal(ErrInvalidHashKeyValue)) - Expect(found).To(BeFalse()) - }) - }) - - Describe("GetItems Invalid key ", func() { - It("should fail with table name is nil", func() { - key := Key().WithHashKeyName("UUID").WithHashKey("uuid") - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItem(key, users) - - Expect(err).To(Equal(ErrInvalidTableName)) - Expect(found).To(BeFalse()) - }) - It("should fail with hash key name is nil", func() { - key := Key().WithTableName(UserTableName).WithHashKey("uuid") - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItem(key, users) - - Expect(err).To(Equal(ErrInvalidHashKeyName)) - Expect(found).To(BeFalse()) - }) - It("should fail with hash key value is nil", func() { - key := Key().WithTableName(UserTableName).WithHashKeyName("UUID") - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItem(key, users) - - Expect(err).To(Equal(ErrInvalidHashKeyValue)) - Expect(found).To(BeFalse()) - }) - }) - Describe("GetItem", func() { - It("should get item with Hash", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - - userDBOutput := map[string]interface{}{ - "UUID": "uuid", - } - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithQueryOutput(userDBOutput), - ).Exec() - - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(BeNil()) - Expect(found).To(BeTrue()) - Expect(user.UUID).To(Equal(userDBOutput["UUID"])) - }) - - It("should get item with Hash and range", func() { - key := Key().WithTableName(ProfileTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid"). - WithRangeKeyName("Email"). - WithRangeKey("user@adjeo.io") - - profileDBOutput := map[string]interface{}{ - "UUID": "uuid", - "Email": "user@adjeo.io", - } - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithCondition(*key.RangeKeyName(), key.RangeKey(), "EQ"), - dMock.WithQueryOutput(profileDBOutput), - ).Exec() - - profile := &Profile{} - found, err := repository.GIndex(IndexName).GetItem(key, profile) - - Expect(err).To(BeNil()) - Expect(found).To(BeTrue()) - Expect(profile.UUID).To(Equal(profileDBOutput["UUID"])) - Expect(profile.Email).To(Equal(profileDBOutput["Email"])) - }) - - It("should return false and nil if item was not found", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithQueryOutput(nil), - ).Exec() - - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(BeNil()) - Expect(found).To(BeFalse()) - }) - - It("should return false and error in case of error", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - err := errors.New("invalid query") - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithError(err), - ).Exec() - - user := &User{} - found, err := repository.GIndex(IndexName).GetItem(key, user) - - Expect(err).To(BeEquivalentTo(err)) - Expect(found).To(BeFalse()) - }) - }) - Describe("GetItems", func() { - It("should get items with Hash", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - - userDBOutput := []map[string]interface{}{ - {"UUID": "uuid", "UserName": "name1"}, - {"UUID": "uuid", "UserName": "name2"}, - } - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithQueryOutput(userDBOutput), - ).Exec() - - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItems(key, users) - Expect(err).To(BeNil()) - Expect(found).To(BeTrue()) - result := *users - Expect(len(result)).To(Equal(2)) - Expect(result[0].UUID).To(Equal(userDBOutput[0]["UUID"])) - }) - - It("should get items with Hash and ignore range", func() { - key := Key().WithTableName(ProfileTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - - profileDBOutput := []map[string]interface{}{ - {"UUID": "uuid", "Email": "email1"}, - {"UUID": "uuid", "Email": "email2"}, - } - - dMock.Should(). - Query( - dMock.WithTable(key.TableName()), - dMock.WithIndex(IndexName), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithQueryOutput(profileDBOutput), - ).Exec() - - profiles := &[]Profile{} - found, err := repository.GIndex(IndexName).GetItems(key, profiles) - Expect(err).To(BeNil()) - Expect(found).To(BeTrue()) - result := *profiles - Expect(len(result)).To(Equal(2)) - Expect(result[0].UUID).To(Equal(profileDBOutput[0]["UUID"])) - - }) - - It("should return false and nil if item was not found", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - - dMock.Should(). - Query( - dMock.WithIndex(IndexName), - dMock.WithTable(key.TableName()), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithQueryOutput(nil), - ).Exec() - - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItems(key, users) - - Expect(err).To(BeNil()) - Expect(found).To(BeFalse()) - }) - - It("should return false and error in case of error", func() { - key := Key().WithTableName(UserTableName). - WithHashKeyName("UUID"). - WithHashKey("uuid") - err := errors.New("invalid query") - dMock.Should(). - Query( - dMock.WithIndex(IndexName), - dMock.WithTable(key.TableName()), - dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"), - dMock.WithError(err), - ).Exec() - - users := &[]User{} - found, err := repository.GIndex(IndexName).GetItems(key, users) - - Expect(err).To(BeEquivalentTo(err)) - Expect(found).To(BeFalse()) - }) - - }) - }) -}) diff --git a/repository_save_test.go b/repository_save_test.go index ba21417..4cc9acb 100644 --- a/repository_save_test.go +++ b/repository_save_test.go @@ -2,14 +2,14 @@ package djoemo_test import ( "context" - "github.com/djoemo/mock" + "github.com/adjoeio/djoemo/mock" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" "github.com/pkg/errors" . "github.com/onsi/gomega" - . "github.com/djoemo" + . "github.com/adjoeio/djoemo" ) var _ = Describe("Repository", func() { diff --git a/repository_update_test.go b/repository_update_test.go index 80744fd..db5415c 100644 --- a/repository_update_test.go +++ b/repository_update_test.go @@ -3,8 +3,8 @@ package djoemo_test import ( "context" "errors" - . "github.com/djoemo" - "github.com/djoemo/mock" + . "github.com/adjoeio/djoemo" + "github.com/adjoeio/djoemo/mock" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega"