diff --git a/batchutils/batch_operations_test.go b/batchutils/batch_operations_test.go index e11a8e36..0ddb405e 100644 --- a/batchutils/batch_operations_test.go +++ b/batchutils/batch_operations_test.go @@ -92,4 +92,15 @@ var _ = Describe("Batch operations", func() { } } }) + + It("doesn't error trying to batch delete nonexistent keys", func() { + keys := []Key{String("i"), String("don't"), String("exist")} + errors := batchutils.BatchDelete(ctx, &batchutils.BatchDeleteRequest{ + Client: client, + CacheName: cacheName, + Keys: keys, + }) + Expect(errors).To(BeNil()) + }) + }) diff --git a/momento/control_test.go b/momento/control_test.go index 841977b2..ef0043af 100644 --- a/momento/control_test.go +++ b/momento/control_test.go @@ -20,8 +20,8 @@ var _ = Describe("Control ops", func() { DeferCleanup(func() { sharedContext.Close() }) }) - Describe(`Happy Path`, func() { - It(`creates, lists, and deletes caches`, func() { + Describe("Happy Path", func() { + It("creates, lists, and deletes caches", func() { cacheNames := []string{uuid.NewString(), uuid.NewString()} defer func() { for _, cacheName := range cacheNames { @@ -80,11 +80,40 @@ var _ = Describe("Control ops", func() { sharedContext.ClientWithDefaultCacheName.DeleteCache(sharedContext.Ctx, &DeleteCacheRequest{}), ).To(BeAssignableToTypeOf(&DeleteCacheSuccess{})) }) + + }) + + Describe("cache client with default cache name", func() { + It("overrides default cache name", func() { + Expect( + sharedContext.ClientWithDefaultCacheName.CreateCache( + sharedContext.Ctx, &CreateCacheRequest{CacheName: sharedContext.CacheName}, + ), + ).To(BeAssignableToTypeOf(&CreateCacheSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.Get( + sharedContext.Ctx, &GetRequest{Key: String("hi")}, + ), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) + Expect( + sharedContext.ClientWithDefaultCacheName.Get( + sharedContext.Ctx, &GetRequest{ + CacheName: sharedContext.CacheName, + Key: String("hi"), + }, + ), + ).To(BeAssignableToTypeOf(&GetMiss{})) + Expect( + sharedContext.ClientWithDefaultCacheName.DeleteCache( + sharedContext.Ctx, &DeleteCacheRequest{CacheName: sharedContext.CacheName}, + ), + ).To(BeAssignableToTypeOf(&DeleteCacheSuccess{})) + }) }) - Describe(`Validate cache name`, func() { - It(`CreateCache and DeleteCache errors on bad cache names`, func() { - badCacheNames := []string{``, ` `} + Describe("Validate cache name", func() { + It("CreateCache and DeleteCache errors on bad cache names", func() { + badCacheNames := []string{"", " "} for _, badCacheName := range badCacheNames { createResp, err := sharedContext.Client.CreateCache(sharedContext.Ctx, &CreateCacheRequest{CacheName: badCacheName}) Expect(createResp).To(BeNil()) @@ -102,8 +131,8 @@ var _ = Describe("Control ops", func() { }) }) - Describe(`DeleteCache`, func() { - It(`succeeds even if the cache does not exist`, func() { + Describe("DeleteCache", func() { + It("succeeds even if the cache does not exist", func() { Expect( sharedContext.Client.DeleteCache(sharedContext.Ctx, &DeleteCacheRequest{CacheName: uuid.NewString()}), ).To(BeAssignableToTypeOf(&DeleteCacheSuccess{})) diff --git a/momento/dictionary_test.go b/momento/dictionary_test.go index a87b90e1..ed1e1859 100644 --- a/momento/dictionary_test.go +++ b/momento/dictionary_test.go @@ -18,7 +18,7 @@ var _ = Describe("Dictionary methods", func() { BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) @@ -99,17 +99,18 @@ var _ = Describe("Dictionary methods", func() { ) DescribeTable("add string and bytes value for single field happy path", - func(field Value, value Value, expectedFieldString string, expectedFieldBytes []byte, expectedValueString string, expectedValueBytes []byte) { + func(clientType string, field Value, value Value, expectedFieldString string, expectedFieldBytes []byte, expectedValueString string, expectedValueBytes []byte) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.DictionarySetField(sharedContext.Ctx, &DictionarySetFieldRequest{ - CacheName: sharedContext.CacheName, + client.DictionarySetField(sharedContext.Ctx, &DictionarySetFieldRequest{ + CacheName: cacheName, DictionaryName: sharedContext.CollectionName, Field: field, Value: value, }), ).Error().To(BeNil()) - getFieldResp, err := sharedContext.Client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ - CacheName: sharedContext.CacheName, + getFieldResp, err := client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ + CacheName: cacheName, DictionaryName: sharedContext.CollectionName, Field: field, }) @@ -123,10 +124,14 @@ var _ = Describe("Dictionary methods", func() { Expect(result.ValueByte()).To(Equal(expectedValueBytes)) } }, - Entry("using string value and field", String("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), - Entry("using string value and bytes field", String("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), - Entry("using bytes value and string field", Bytes("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), - Entry("using bytes value and field", Bytes("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using string value and field", DefaultClient, String("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using string value and bytes field", DefaultClient, String("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using bytes value and string field", DefaultClient, Bytes("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using bytes value and field", DefaultClient, Bytes("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using string value and field with default cache", WithDefaultCache, String("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using string value and bytes field with default cache", WithDefaultCache, String("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using bytes value and string field with default cache", WithDefaultCache, Bytes("myField"), String("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), + Entry("using bytes value and field with default cache", WithDefaultCache, Bytes("myField"), Bytes("myValue"), "myField", []byte("myField"), "myValue", []byte("myValue")), ) DescribeTable("try using empty and nil fields and values for set", @@ -159,16 +164,17 @@ var _ = Describe("Dictionary methods", func() { }) DescribeTable("add string fields and string and bytes values for set fields happy path", - func(elements []DictionaryElement, expectedItemsStringValue map[string]string, expectedItemsByteValue map[string][]byte) { + func(clientType string, elements []DictionaryElement, expectedItemsStringValue map[string]string, expectedItemsByteValue map[string][]byte) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{ - CacheName: sharedContext.CacheName, + client.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{ + CacheName: cacheName, DictionaryName: sharedContext.CollectionName, Elements: elements, }), ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) - fetchResp, err := sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ + CacheName: cacheName, DictionaryName: sharedContext.CollectionName, }) Expect(err).To(BeNil()) @@ -198,6 +204,7 @@ var _ = Describe("Dictionary methods", func() { }, Entry( "with string values", + DefaultClient, []DictionaryElement{ {Field: String("myField1"), Value: String("myValue1")}, {Field: String("myField2"), Value: String("myValue2")}, @@ -207,6 +214,7 @@ var _ = Describe("Dictionary methods", func() { ), Entry( "with byte values", + DefaultClient, []DictionaryElement{ {Field: Bytes("myField1"), Value: Bytes("myValue1")}, {Field: Bytes("myField2"), Value: Bytes("myValue2")}, @@ -216,6 +224,7 @@ var _ = Describe("Dictionary methods", func() { ), Entry( "with mixed values", + DefaultClient, []DictionaryElement{ {Field: Bytes("myField1"), Value: String("myValue1")}, {Field: String("myField2"), Value: Bytes("myValue2")}, @@ -225,6 +234,47 @@ var _ = Describe("Dictionary methods", func() { ), Entry( "with empty values", + DefaultClient, + []DictionaryElement{ + {Field: Bytes("myField1"), Value: String("")}, + {Field: String("myField2"), Value: Bytes("")}, + }, + map[string]string{"myField1": "", "myField2": ""}, + map[string][]byte{"myField1": []byte(""), "myField2": []byte("")}, + ), + Entry( + "with string values and default cache", + WithDefaultCache, + []DictionaryElement{ + {Field: String("myField1"), Value: String("myValue1")}, + {Field: String("myField2"), Value: String("myValue2")}, + }, + map[string]string{"myField1": "myValue1", "myField2": "myValue2"}, + map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")}, + ), + Entry( + "with byte values and default cache", + WithDefaultCache, + []DictionaryElement{ + {Field: Bytes("myField1"), Value: Bytes("myValue1")}, + {Field: Bytes("myField2"), Value: Bytes("myValue2")}, + }, + map[string]string{"myField1": "myValue1", "myField2": "myValue2"}, + map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")}, + ), + Entry( + "with mixed values and default cache", + WithDefaultCache, + []DictionaryElement{ + {Field: Bytes("myField1"), Value: String("myValue1")}, + {Field: String("myField2"), Value: Bytes("myValue2")}, + }, + map[string]string{"myField1": "myValue1", "myField2": "myValue2"}, + map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")}, + ), + Entry( + "with empty values and default cache", + WithDefaultCache, []DictionaryElement{ {Field: Bytes("myField1"), Value: String("")}, {Field: String("myField2"), Value: Bytes("")}, @@ -296,20 +346,25 @@ var _ = Describe("Dictionary methods", func() { Describe("dictionary increment", func() { - It("populates nonexistent field", func() { - incrResp, err := sharedContext.Client.DictionaryIncrement(sharedContext.Ctx, &DictionaryIncrementRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Field: String("myField"), - Amount: 3, - }) - Expect(err).To(BeNil()) - Expect(incrResp).To(BeAssignableToTypeOf(&DictionaryIncrementSuccess{})) - switch result := incrResp.(type) { - case *DictionaryIncrementSuccess: - Expect(result.Value()).To(Equal(int64(3))) - } - }) + DescribeTable("populates nonexistent field", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + incrResp, err := client.DictionaryIncrement(sharedContext.Ctx, &DictionaryIncrementRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Field: String("myField"), + Amount: 3, + }) + Expect(err).To(BeNil()) + Expect(incrResp).To(BeAssignableToTypeOf(&DictionaryIncrementSuccess{})) + switch result := incrResp.(type) { + case *DictionaryIncrementSuccess: + Expect(result.Value()).To(Equal(int64(3))) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error when called on a non-integer field", func() { Expect( @@ -341,32 +396,37 @@ var _ = Describe("Dictionary methods", func() { Expect(err).To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It("increments on the happy path", func() { - field := String("counter") - for i := 0; i < 10; i++ { - Expect( - sharedContext.Client.DictionaryIncrement(sharedContext.Ctx, &DictionaryIncrementRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Field: field, - Amount: 1, - }), - ).To(BeAssignableToTypeOf(&DictionaryIncrementSuccess{})) - } - fetchResp, err := sharedContext.Client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Field: field, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&DictionaryGetFieldHit{})) - switch result := fetchResp.(type) { - case *DictionaryGetFieldHit: - Expect(result.ValueString()).To(Equal("10")) - default: - Fail("expected a hit for get field but got a miss") - } - }) + DescribeTable("increments on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + field := String("counter") + for i := 0; i < 10; i++ { + Expect( + client.DictionaryIncrement(sharedContext.Ctx, &DictionaryIncrementRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Field: field, + Amount: 1, + }), + ).To(BeAssignableToTypeOf(&DictionaryIncrementSuccess{})) + } + fetchResp, err := client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Field: field, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&DictionaryGetFieldHit{})) + switch result := fetchResp.(type) { + case *DictionaryGetFieldHit: + Expect(result.ValueString()).To(Equal("10")) + default: + Fail("expected a hit for get field but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error when field is nil", func() { Expect( @@ -392,32 +452,45 @@ var _ = Describe("Dictionary methods", func() { ), }), ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{ + DictionaryName: sharedContext.CollectionName, + Elements: DictionaryElementsFromMapStringValue( + map[string]Value{"myField1": String("myValue1"), "myField2": Bytes("myValue2")}, + ), + }), + ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) }) When("getting single field", func() { - It("returns the correct string and byte values", func() { - expected := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} - - for fieldName, valueStr := range expected { - getResp, err := sharedContext.Client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Field: String(fieldName), - }) - Expect(err).To(BeNil()) - Expect(getResp).To(BeAssignableToTypeOf(&DictionaryGetFieldHit{})) - switch result := getResp.(type) { - case *DictionaryGetFieldHit: - Expect(result.FieldString()).To(Equal(fieldName)) - Expect(result.FieldByte()).To(Equal([]byte(fieldName))) - Expect(result.ValueString()).To(Equal(valueStr)) - Expect(result.ValueByte()).To(Equal([]byte(valueStr))) - default: - Fail("something really weird happened") + DescribeTable("returns the correct string and byte values", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + expected := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} + + for fieldName, valueStr := range expected { + getResp, err := client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Field: String(fieldName), + }) + Expect(err).To(BeNil()) + Expect(getResp).To(BeAssignableToTypeOf(&DictionaryGetFieldHit{})) + switch result := getResp.(type) { + case *DictionaryGetFieldHit: + Expect(result.FieldString()).To(Equal(fieldName)) + Expect(result.FieldByte()).To(Equal([]byte(fieldName))) + Expect(result.ValueString()).To(Equal(valueStr)) + Expect(result.ValueByte()).To(Equal([]byte(valueStr))) + default: + Fail("something really weird happened") + } } - } - }) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns a miss for a nonexistent field", func() { getResp, err := sharedContext.Client.DictionaryGetField(sharedContext.Ctx, &DictionaryGetFieldRequest{ @@ -453,24 +526,29 @@ var _ = Describe("Dictionary methods", func() { When("getting multiple fields", func() { - It("returns the correct string and byte values", func() { - getResp, err := sharedContext.Client.DictionaryGetFields(sharedContext.Ctx, &DictionaryGetFieldsRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Fields: []Value{String("myField1"), String("myField2")}, - }) - Expect(err).To(BeNil()) - Expect(getResp).To(BeAssignableToTypeOf(&DictionaryGetFieldsHit{})) + DescribeTable("returns the correct string and byte values", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + getResp, err := client.DictionaryGetFields(sharedContext.Ctx, &DictionaryGetFieldsRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Fields: []Value{String("myField1"), String("myField2")}, + }) + Expect(err).To(BeNil()) + Expect(getResp).To(BeAssignableToTypeOf(&DictionaryGetFieldsHit{})) - expectedStrings := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} - expectedBytes := map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")} - switch result := getResp.(type) { - case *DictionaryGetFieldsHit: - Expect(result.ValueMapStringString()).To(Equal(expectedStrings)) - Expect(result.ValueMap()).To(Equal(expectedStrings)) - Expect(result.ValueMapStringBytes()).To(Equal(expectedBytes)) - } - }) + expectedStrings := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} + expectedBytes := map[string][]byte{"myField1": []byte("myValue1"), "myField2": []byte("myValue2")} + switch result := getResp.(type) { + case *DictionaryGetFieldsHit: + Expect(result.ValueMapStringString()).To(Equal(expectedStrings)) + Expect(result.ValueMap()).To(Equal(expectedStrings)) + Expect(result.ValueMapStringBytes()).To(Equal(expectedBytes)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns a miss for nonexistent dictionary", func() { Expect( @@ -539,21 +617,34 @@ var _ = Describe("Dictionary methods", func() { ), }), ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{ + DictionaryName: sharedContext.CollectionName, + Elements: DictionaryElementsFromMapStringValue( + map[string]Value{"myField1": String("myValue1"), "myField2": Bytes("myValue2")}, + ), + }), + ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) }) - It("fetches on the happy path", func() { - expected := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} - fetchResp, err := sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&DictionaryFetchHit{})) - switch result := fetchResp.(type) { - case *DictionaryFetchHit: - Expect(result.ValueMap()).To(Equal(expected)) - } - }) + DescribeTable("fetches on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + expected := map[string]string{"myField1": "myValue1", "myField2": "myValue2"} + fetchResp, err := client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&DictionaryFetchHit{})) + switch result := fetchResp.(type) { + case *DictionaryFetchHit: + Expect(result.ValueMap()).To(Equal(expected)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns a miss for nonexistent dictionary", func() { Expect( @@ -582,34 +673,51 @@ var _ = Describe("Dictionary methods", func() { ), }), ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.DictionarySetFields(sharedContext.Ctx, &DictionarySetFieldsRequest{ + DictionaryName: sharedContext.CollectionName, + Elements: DictionaryElementsFromMapStringValue( + map[string]Value{ + "myField1": String("myValue1"), + "myField2": Bytes("myValue2"), + "myField3": String("myValue3"), + }, + ), + }), + ).To(BeAssignableToTypeOf(&DictionarySetFieldsSuccess{})) }) When("removing a single field", func() { - It("properly removes a field", func() { - removeResp, err := sharedContext.Client.DictionaryRemoveField(sharedContext.Ctx, &DictionaryRemoveFieldRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Field: String("myField1"), - }) - Expect(err).To(BeNil()) - Expect(removeResp).To(BeAssignableToTypeOf(&DictionaryRemoveFieldSuccess{})) + DescribeTable("properly removes a field", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + removeResp, err := client.DictionaryRemoveField(sharedContext.Ctx, &DictionaryRemoveFieldRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Field: String("myField1"), + }) + Expect(err).To(BeNil()) + Expect(removeResp).To(BeAssignableToTypeOf(&DictionaryRemoveFieldSuccess{})) - fetchResp, err := sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *DictionaryFetchHit: - Expect(result.ValueMap()).To(Equal(map[string]string{ - "myField2": "myValue2", - "myField3": "myValue3", - })) - default: - Fail("expected a hit from dictionary fetch but got a miss") - } - }) + fetchResp, err := client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *DictionaryFetchHit: + Expect(result.ValueMap()).To(Equal(map[string]string{ + "myField2": "myValue2", + "myField3": "myValue3", + })) + default: + Fail("expected a hit from dictionary fetch but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("no-ops when attempting to remove a nonexistent field", func() { removeResp, err := sharedContext.Client.DictionaryRemoveField(sharedContext.Ctx, &DictionaryRemoveFieldRequest{ @@ -644,29 +752,34 @@ var _ = Describe("Dictionary methods", func() { }) When("removing multiple fields", func() { - It("properly removes multiple fields", func() { - removeResp, err := sharedContext.Client.DictionaryRemoveFields(sharedContext.Ctx, &DictionaryRemoveFieldsRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - Fields: []Value{String("myField1"), Bytes("myField2")}, - }) - Expect(err).To(BeNil()) - Expect(removeResp).To(BeAssignableToTypeOf(&DictionaryRemoveFieldsSuccess{})) + DescribeTable("properly removes multiple fields", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + removeResp, err := client.DictionaryRemoveFields(sharedContext.Ctx, &DictionaryRemoveFieldsRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + Fields: []Value{String("myField1"), Bytes("myField2")}, + }) + Expect(err).To(BeNil()) + Expect(removeResp).To(BeAssignableToTypeOf(&DictionaryRemoveFieldsSuccess{})) - fetchResp, err := sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ - CacheName: sharedContext.CacheName, - DictionaryName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *DictionaryFetchHit: - Expect(result.ValueMap()).To(Equal(map[string]string{ - "myField3": "myValue3", - })) - default: - Fail("expected a hit from dictionary fetch but got a miss") - } - }) + fetchResp, err := client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{ + CacheName: cacheName, + DictionaryName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *DictionaryFetchHit: + Expect(result.ValueMap()).To(Equal(map[string]string{ + "myField3": "myValue3", + })) + default: + Fail("expected a hit from dictionary fetch but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("no-ops when attempting to remove a nonexistent field", func() { removeResp, err := sharedContext.Client.DictionaryRemoveFields(sharedContext.Ctx, &DictionaryRemoveFieldsRequest{ diff --git a/momento/list_test.go b/momento/list_test.go index 14cc7c2c..761fa67c 100644 --- a/momento/list_test.go +++ b/momento/list_test.go @@ -38,6 +38,12 @@ func populateList(sharedContext SharedContext, numItems int) []string { Values: values, }), ).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ + ListName: sharedContext.CollectionName, + Values: values, + }), + ).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) return expected } @@ -46,30 +52,31 @@ var _ = Describe("List methods", func() { BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) }) DescribeTable("try using invalid cache and list names", - func(cacheName string, listName string, expectedErrorCode string) { + func(clientType string, cacheName string, listName string, expectedErrorCode string) { + client, _ := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ CacheName: cacheName, ListName: listName, }), ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListLength(sharedContext.Ctx, &ListLengthRequest{ + client.ListLength(sharedContext.Ctx, &ListLengthRequest{ CacheName: cacheName, ListName: listName, }), ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ + client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ CacheName: cacheName, ListName: listName, Values: []Value{String("hi")}, @@ -77,7 +84,7 @@ var _ = Describe("List methods", func() { ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ + client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ CacheName: cacheName, ListName: listName, Values: []Value{String("hi")}, @@ -85,21 +92,21 @@ var _ = Describe("List methods", func() { ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListPopBack(sharedContext.Ctx, &ListPopBackRequest{ + client.ListPopBack(sharedContext.Ctx, &ListPopBackRequest{ CacheName: cacheName, ListName: listName, }), ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListPopFront(sharedContext.Ctx, &ListPopFrontRequest{ + client.ListPopFront(sharedContext.Ctx, &ListPopFrontRequest{ CacheName: cacheName, ListName: listName, }), ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ + client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ CacheName: cacheName, ListName: listName, Value: String("hi"), @@ -107,7 +114,7 @@ var _ = Describe("List methods", func() { ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ + client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ CacheName: cacheName, ListName: listName, Value: String("hi"), @@ -115,16 +122,19 @@ var _ = Describe("List methods", func() { ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) Expect( - sharedContext.Client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ + client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ CacheName: cacheName, ListName: listName, Value: String("hi"), }), ).Error().To(HaveMomentoErrorCode(expectedErrorCode)) }, - Entry("nonexistent cache name", uuid.NewString(), uuid.NewString(), NotFoundError), - Entry("empty cache name", "", sharedContext.CollectionName, InvalidArgumentError), - Entry("empty list name", sharedContext.CacheName, "", InvalidArgumentError), + Entry("nonexistent cache name", DefaultClient, uuid.NewString(), uuid.NewString(), NotFoundError), + Entry("empty cache name", DefaultClient, "", sharedContext.CollectionName, InvalidArgumentError), + Entry("empty list name", DefaultClient, sharedContext.CacheName, "", InvalidArgumentError), + Entry("nonexistent cache name", WithDefaultCache, uuid.NewString(), uuid.NewString(), NotFoundError), + Entry("empty cache name", WithDefaultCache, "", sharedContext.CollectionName, InvalidArgumentError), + Entry("empty list name", WithDefaultCache, sharedContext.CacheName, "", InvalidArgumentError), ) It("returns the correct list length", func() { @@ -147,51 +157,61 @@ var _ = Describe("List methods", func() { When("pushing to the front of the list", func() { - It("pushes strings and bytes on the happy path", func() { - numItems := 10 - values, expected := getValueAndExpectedValueLists(numItems) - sort.Sort(sort.Reverse(sort.StringSlice(expected))) - for _, value := range values { + DescribeTable("pushes strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + values, expected := getValueAndExpectedValueLists(numItems) + sort.Sort(sort.Reverse(sort.StringSlice(expected))) + for _, value := range values { + Expect( + client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: value, + }), + ).To(BeAssignableToTypeOf(&ListPushFrontSuccess{})) + } + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) + Expect(fetchResp).To(HaveListLength(numItems)) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal(expected)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) + + DescribeTable("truncates the list properly", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + truncateTo := 5 + populateList(sharedContext, numItems) Expect( - sharedContext.Client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: value, + client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: String("andherlittledogtoo"), + TruncateBackToSize: uint32(truncateTo), }), - ).To(BeAssignableToTypeOf(&ListPushFrontSuccess{})) - } - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) - Expect(fetchResp).To(HaveListLength(numItems)) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal(expected)) - } - }) - - It("truncates the list properly", func() { - numItems := 10 - truncateTo := 5 - populateList(sharedContext, numItems) - Expect( - sharedContext.Client.ListPushFront(sharedContext.Ctx, &ListPushFrontRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: String("andherlittledogtoo"), - TruncateBackToSize: uint32(truncateTo), - }), - ).Error().To(BeNil()) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(truncateTo)) - }) + ).Error().To(BeNil()) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(truncateTo)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns invalid argument for a nil value", func() { Expect( @@ -223,50 +243,60 @@ var _ = Describe("List methods", func() { When("pushing to the back of the list", func() { - It("pushes strings and bytes on the happy path", func() { - numItems := 10 - values, expected := getValueAndExpectedValueLists(numItems) - for _, value := range values { + DescribeTable("pushes strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + values, expected := getValueAndExpectedValueLists(numItems) + for _, value := range values { + Expect( + client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: value, + }), + ).To(BeAssignableToTypeOf(&ListPushBackSuccess{})) + } + + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(numItems)) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal(expected)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) + + DescribeTable("truncates the list properly", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + truncateTo := 5 + populateList(sharedContext, numItems) Expect( - sharedContext.Client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: value, + client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: String("andherlittledogtoo"), + TruncateFrontToSize: uint32(truncateTo), }), - ).To(BeAssignableToTypeOf(&ListPushBackSuccess{})) - } - - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(numItems)) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal(expected)) - } - }) - - It("truncates the list properly", func() { - numItems := 10 - truncateTo := 5 - populateList(sharedContext, numItems) - Expect( - sharedContext.Client.ListPushBack(sharedContext.Ctx, &ListPushBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: String("andherlittledogtoo"), - TruncateFrontToSize: uint32(truncateTo), - }), - ).Error().To(BeNil()) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(truncateTo)) - }) + ).Error().To(BeNil()) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(truncateTo)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns invalid argument for a nil value", func() { Expect( @@ -302,58 +332,68 @@ var _ = Describe("List methods", func() { When("concatenating to the front of the list", func() { - It("pushes strings and bytes on the happy path", func() { - numItems := 10 - expected := populateList(sharedContext, numItems) - - numConcatItems := 5 - concatValues, concatExpected := getValueAndExpectedValueLists(numConcatItems) - concatResp, err := sharedContext.Client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Values: concatValues, - }) - Expect(err).To(BeNil()) - Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) + DescribeTable("pushes strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + expected := populateList(sharedContext, numItems) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) - Expect(fetchResp).To(HaveListLength(numItems + numConcatItems)) - expected = append(concatExpected, expected...) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal(expected)) - } - }) + numConcatItems := 5 + concatValues, concatExpected := getValueAndExpectedValueLists(numConcatItems) + concatResp, err := client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Values: concatValues, + }) + Expect(err).To(BeNil()) + Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) - It("truncates the list properly", func() { - populateList(sharedContext, 5) - concatValues := []Value{String("100"), String("101"), String("102")} - concatResp, err := sharedContext.Client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Values: concatValues, - TruncateBackToSize: 3, - }) - Expect(err).To(BeNil()) - Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) + Expect(fetchResp).To(HaveListLength(numItems + numConcatItems)) + expected = append(concatExpected, expected...) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal(expected)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) + + DescribeTable("truncates the list properly", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + populateList(sharedContext, 5) + concatValues := []Value{String("100"), String("101"), String("102")} + concatResp, err := client.ListConcatenateFront(sharedContext.Ctx, &ListConcatenateFrontRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Values: concatValues, + TruncateBackToSize: 3, + }) + Expect(err).To(BeNil()) + Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateFrontSuccess{})) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) - Expect(fetchResp).To(HaveListLength(3)) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal([]string{"100", "101", "102"})) - } - }) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) + Expect(fetchResp).To(HaveListLength(3)) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal([]string{"100", "101", "102"})) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an invalid argument for a nil value", func() { populateList(sharedContext, 5) @@ -388,58 +428,68 @@ var _ = Describe("List methods", func() { When("concatenating to the back of the list", func() { - It("pushes strings and bytes on the happy path", func() { - numItems := 10 - expected := populateList(sharedContext, numItems) + DescribeTable("pushes strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 10 + expected := populateList(sharedContext, numItems) - numConcatItems := 5 - concatValues, concatExpected := getValueAndExpectedValueLists(numConcatItems) - concatResp, err := sharedContext.Client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Values: concatValues, - }) - Expect(err).To(BeNil()) - Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) - - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) - Expect(fetchResp).To(HaveListLength(numItems + numConcatItems)) - expected = append(expected, concatExpected...) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal(expected)) - } - }) + numConcatItems := 5 + concatValues, concatExpected := getValueAndExpectedValueLists(numConcatItems) + concatResp, err := client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Values: concatValues, + }) + Expect(err).To(BeNil()) + Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) - It("truncates the list properly", func() { - populateList(sharedContext, 5) - concatValues := []Value{String("100"), String("101"), String("102")} - concatResp, err := sharedContext.Client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Values: concatValues, - TruncateFrontToSize: 3, - }) - Expect(err).To(BeNil()) - Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) + Expect(fetchResp).To(HaveListLength(numItems + numConcatItems)) + expected = append(expected, concatExpected...) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal(expected)) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) + + DescribeTable("truncates the list properly", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + populateList(sharedContext, 5) + concatValues := []Value{String("100"), String("101"), String("102")} + concatResp, err := client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Values: concatValues, + TruncateFrontToSize: 3, + }) + Expect(err).To(BeNil()) + Expect(concatResp).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) - Expect(fetchResp).To(HaveListLength(3)) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal([]string{"100", "101", "102"})) - } - }) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(BeAssignableToTypeOf(&ListFetchHit{})) + Expect(fetchResp).To(HaveListLength(3)) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal([]string{"100", "101", "102"})) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an invalid argument for a nil value", func() { populateList(sharedContext, 5) @@ -477,29 +527,34 @@ var _ = Describe("List methods", func() { When("popping from the front of the list", func() { - It("pops strings and bytes on the happy path", func() { - numItems := 5 - expected := populateList(sharedContext, numItems) - - popResp, err := sharedContext.Client.ListPopFront(sharedContext.Ctx, &ListPopFrontRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := popResp.(type) { - case *ListPopFrontHit: - Expect(result.ValueString()).To(Equal(string(expected[0]))) - default: - Fail("expected a hit from list pop front but got a miss") - } + DescribeTable("pops strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 5 + expected := populateList(sharedContext, numItems) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(numItems - 1)) - }) + popResp, err := client.ListPopFront(sharedContext.Ctx, &ListPopFrontRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := popResp.(type) { + case *ListPopFrontHit: + Expect(result.ValueString()).To(Equal(string(expected[0]))) + default: + Fail("expected a hit from list pop front but got a miss") + } + + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(numItems - 1)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns a miss after popping the last item", func() { numItems := 3 @@ -524,29 +579,34 @@ var _ = Describe("List methods", func() { When("popping from the back of the list", func() { - It("pops strings and bytes on the happy path", func() { - numItems := 5 - expected := populateList(sharedContext, numItems) + DescribeTable("pops strings and bytes on the happy path", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 5 + expected := populateList(sharedContext, numItems) - popResp, err := sharedContext.Client.ListPopBack(sharedContext.Ctx, &ListPopBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := popResp.(type) { - case *ListPopBackHit: - Expect(result.ValueString()).To(Equal(string(expected[numItems-1]))) - default: - Fail("expected a hit from list pop front but got a miss") - } - - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(numItems - 1)) - }) + popResp, err := client.ListPopBack(sharedContext.Ctx, &ListPopBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := popResp.(type) { + case *ListPopBackHit: + Expect(result.ValueString()).To(Equal(string(expected[numItems-1]))) + default: + Fail("expected a hit from list pop front but got a miss") + } + + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(numItems - 1)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns a miss after popping the last item", func() { numItems := 3 @@ -575,29 +635,34 @@ var _ = Describe("List methods", func() { When("removing a value that appears once", func() { - It("removes the value", func() { - numItems := 5 - expected := populateList(sharedContext, numItems) - Expect( - sharedContext.Client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: String(expected[0]), - }), - ).Error().To(BeNil()) + DescribeTable("removes the value", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 5 + expected := populateList(sharedContext, numItems) + Expect( + client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: String(expected[0]), + }), + ).Error().To(BeNil()) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal(expected[1:])) - default: - Fail("expected a hit for list fetch but got a miss") - } - }) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal(expected[1:])) + default: + Fail("expected a hit for list fetch but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error for a nil value", func() { Expect( @@ -645,38 +710,43 @@ var _ = Describe("List methods", func() { When("removing a value that appears multiple times", func() { - It("removes all occurrences of the value", func() { - numItems := 5 - populateList(sharedContext, numItems) - toAdd := []Value{String("#4"), String("#4"), String("#4"), String("#0")} - Expect( - sharedContext.Client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Values: toAdd, - }), - ).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) + DescribeTable("removes all occurrences of the value", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 5 + populateList(sharedContext, numItems) + toAdd := []Value{String("#4"), String("#4"), String("#4"), String("#0")} + Expect( + client.ListConcatenateBack(sharedContext.Ctx, &ListConcatenateBackRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Values: toAdd, + }), + ).To(BeAssignableToTypeOf(&ListConcatenateBackSuccess{})) - Expect( - sharedContext.Client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: String("#4"), - }), - ).To(BeAssignableToTypeOf(&ListRemoveValueSuccess{})) + Expect( + client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: String("#4"), + }), + ).To(BeAssignableToTypeOf(&ListRemoveValueSuccess{})) - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *ListFetchHit: - Expect(result.ValueList()).To(Equal([]string{"#0", "#1", "#2", "#3", "#0"})) - default: - Fail("expected a hit from list fetch but got a miss") - } - }) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *ListFetchHit: + Expect(result.ValueList()).To(Equal([]string{"#0", "#1", "#2", "#3", "#0"})) + default: + Fail("expected a hit from list fetch but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error for a nil value", func() { Expect( @@ -692,25 +762,29 @@ var _ = Describe("List methods", func() { When("removing a value that isn't in the list", func() { - It("returns success", func() { - numItems := 5 - populateList(sharedContext, numItems) - Expect( - sharedContext.Client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - Value: String("iamnotinthelist"), - }), - ).To(BeAssignableToTypeOf(&ListRemoveValueSuccess{})) - - fetchResp, err := sharedContext.Client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ - CacheName: sharedContext.CacheName, - ListName: sharedContext.CollectionName, - }) - Expect(err).To(BeNil()) - Expect(fetchResp).To(HaveListLength(numItems)) - }) + DescribeTable("returns success", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + numItems := 5 + populateList(sharedContext, numItems) + Expect( + client.ListRemoveValue(sharedContext.Ctx, &ListRemoveValueRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + Value: String("iamnotinthelist"), + }), + ).To(BeAssignableToTypeOf(&ListRemoveValueSuccess{})) + fetchResp, err := client.ListFetch(sharedContext.Ctx, &ListFetchRequest{ + CacheName: cacheName, + ListName: sharedContext.CollectionName, + }) + Expect(err).To(BeNil()) + Expect(fetchResp).To(HaveListLength(numItems)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) }) When("removing from a nonexistent list", func() { diff --git a/momento/ping_test.go b/momento/ping_test.go index 5f962838..a70a3e98 100644 --- a/momento/ping_test.go +++ b/momento/ping_test.go @@ -12,7 +12,7 @@ var _ = Describe("ping", func() { BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) diff --git a/momento/scalar_test.go b/momento/scalar_test.go index 927a0cbf..b91c52a0 100644 --- a/momento/scalar_test.go +++ b/momento/scalar_test.go @@ -17,23 +17,24 @@ var _ = Describe("Scalar methods", func() { var sharedContext SharedContext BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) }) - DescribeTable(`Gets, Sets, and Deletes`, - func(key Key, value Value, expectedString string, expectedBytes []byte) { + DescribeTable("Gets, Sets, and Deletes", + func(clientType string, key Key, value Value, expectedString string, expectedBytes []byte) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: sharedContext.CacheName, + client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, Key: key, Value: value, }), ).To(BeAssignableToTypeOf(&SetSuccess{})) - getResp, err := sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, + getResp, err := client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, Key: key, }) Expect(err).To(BeNil()) @@ -47,93 +48,113 @@ var _ = Describe("Scalar methods", func() { } Expect( - sharedContext.Client.Delete(sharedContext.Ctx, &DeleteRequest{ - CacheName: sharedContext.CacheName, + client.Delete(sharedContext.Ctx, &DeleteRequest{ + CacheName: cacheName, Key: key, }), ).To(BeAssignableToTypeOf(&DeleteSuccess{})) Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, Key: key, }), ).To(BeAssignableToTypeOf(&GetMiss{})) }, - Entry("when the key and value are strings", String("key"), String("value"), "value", []byte("value")), - Entry("when the key and value are bytes", Bytes([]byte{1, 2, 3}), Bytes([]byte("string")), "string", []byte("string")), - Entry("when the value is empty", String("key"), String(""), "", []byte("")), - Entry("when the value is blank", String("key"), String(" "), " ", []byte(" ")), + Entry("when the key and value are strings", DefaultClient, String("key"), String("value"), "value", []byte("value")), + Entry("when the key and value are bytes", DefaultClient, Bytes([]byte{1, 2, 3}), Bytes("string"), "string", []byte("string")), + Entry("when the value is empty", DefaultClient, String("key"), String(""), "", []byte("")), + Entry("when the value is blank", DefaultClient, String("key"), String(" "), " ", []byte(" ")), + Entry("with default cache name when the key and value are strings", WithDefaultCache, String("key"), String("value"), "value", []byte("value")), + Entry("with default cache name when the key and value are bytes", WithDefaultCache, Bytes([]byte{1, 2, 3}), Bytes("string"), "string", []byte("string")), + Entry("with default cache name when the value is empty", WithDefaultCache, String("key"), String(""), "", []byte("")), + Entry("with default cache name when the value is blank", WithDefaultCache, String("key"), String(" "), " ", []byte(" ")), ) - It(`errors when the cache is missing`, func() { - cacheName := uuid.NewString() - key := String("key") - value := String("value") + DescribeTable("errors when the cache is missing", + func(clientType string) { + client, _ := sharedContext.GetClientPrereqsForType(clientType) + cacheName := uuid.NewString() + key := String("key") + value := String("value") - getResp, err := sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: cacheName, - Key: key, - }) - Expect(getResp).To(BeNil()) - Expect(err).To(HaveMomentoErrorCode(NotFoundError)) + getResp, err := client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: key, + }) + Expect(getResp).To(BeNil()) + Expect(err).To(HaveMomentoErrorCode(NotFoundError)) - setResp, err := sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: cacheName, - Key: key, - Value: value, - }) - Expect(setResp).To(BeNil()) - Expect(err).To(HaveMomentoErrorCode(NotFoundError)) + setResp, err := client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, + Key: key, + Value: value, + }) + Expect(setResp).To(BeNil()) + Expect(err).To(HaveMomentoErrorCode(NotFoundError)) - deleteResp, err := sharedContext.Client.Delete(sharedContext.Ctx, &DeleteRequest{ - CacheName: cacheName, - Key: key, - }) - Expect(deleteResp).To(BeNil()) - Expect(err).To(HaveMomentoErrorCode(NotFoundError)) - }) + deleteResp, err := client.Delete(sharedContext.Ctx, &DeleteRequest{ + CacheName: cacheName, + Key: key, + }) + Expect(deleteResp).To(BeNil()) + Expect(err).To(HaveMomentoErrorCode(NotFoundError)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`errors when the key is nil`, func() { - Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, - Key: nil, - }), - ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) - Expect( - sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: sharedContext.CacheName, - Key: nil, - }), - ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) - Expect( - sharedContext.Client.Delete(sharedContext.Ctx, &DeleteRequest{ - CacheName: sharedContext.CacheName, - Key: nil, - }), - ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) - }) + DescribeTable("errors when the key is nil", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + Expect( + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: nil, + }), + ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) + Expect( + client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, + Key: nil, + }), + ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) + Expect( + client.Delete(sharedContext.Ctx, &DeleteRequest{ + CacheName: cacheName, + Key: nil, + }), + ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It("returns a miss when the key doesn't exist", func() { - Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, - Key: String(uuid.NewString()), - }), - ).To(BeAssignableToTypeOf(&GetMiss{})) - }) + DescribeTable("returns a miss when the key doesn't exist", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + Expect( + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: String(uuid.NewString()), + }), + ).To(BeAssignableToTypeOf(&GetMiss{})) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - DescribeTable(`invalid cache names and keys`, - func(cacheName string, key Key, value Key) { - getResp, err := sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ + DescribeTable("invalid cache names and keys", + func(clientType string, cacheName string, key Key, value Key) { + client, _ := sharedContext.GetClientPrereqsForType(clientType) + getResp, err := client.Get(sharedContext.Ctx, &GetRequest{ CacheName: cacheName, Key: key, }) Expect(getResp).To(BeNil()) Expect(err).To(HaveMomentoErrorCode(InvalidArgumentError)) - setResp, err := sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ + setResp, err := client.Set(sharedContext.Ctx, &SetRequest{ CacheName: cacheName, Key: key, Value: value, @@ -141,20 +162,22 @@ var _ = Describe("Scalar methods", func() { Expect(setResp).To(BeNil()) Expect(err).To(HaveMomentoErrorCode(InvalidArgumentError)) - deleteResp, err := sharedContext.Client.Delete(sharedContext.Ctx, &DeleteRequest{ + deleteResp, err := client.Delete(sharedContext.Ctx, &DeleteRequest{ CacheName: cacheName, Key: key, }) Expect(deleteResp).To(BeNil()) Expect(err).To(HaveMomentoErrorCode(InvalidArgumentError)) }, - Entry(`With an empty cache name`, "", String("key"), String("value")), - Entry(`With an bank cache name`, " ", String("key"), String("value")), - Entry(`With an empty key`, uuid.NewString(), String(""), String("value")), + Entry("With default client and an empty cache name", DefaultClient, "", String("key"), String("value")), + Entry("With default client and an bank cache name", DefaultClient, " ", String("key"), String("value")), + Entry("With default client and an empty key", DefaultClient, uuid.NewString(), String(""), String("value")), + Entry("With client with default cache and an bank cache name", WithDefaultCache, " ", String("key"), String("value")), + Entry("With client with default cache and an empty key", WithDefaultCache, uuid.NewString(), String(""), String("value")), ) - Describe(`Set`, func() { - It(`Uses the default TTL`, func() { + Describe("Set", func() { + It("Uses the default TTL", func() { key := String("key") value := String("value") @@ -185,7 +208,7 @@ var _ = Describe("Scalar methods", func() { ).To(BeAssignableToTypeOf(&GetMiss{})) }) - It(`Overrides the default TTL`, func() { + It("Overrides the default TTL", func() { key := String("key") value := String("value") @@ -250,13 +273,20 @@ var _ = Describe("Scalar methods", func() { Value: String(strVal), }), ).To(BeAssignableToTypeOf(&SetSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.Set(sharedContext.Ctx, &SetRequest{ + Key: String(strKey), + Value: String(strVal), + }), + ).To(BeAssignableToTypeOf(&SetSuccess{})) } }) DescribeTable("check for valid keys exist results", - func(toCheck []Key, expected []bool) { - resp, err := sharedContext.Client.KeysExist(sharedContext.Ctx, &KeysExistRequest{ - CacheName: sharedContext.CacheName, + func(clientType string, toCheck []Key, expected []bool) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + resp, err := client.KeysExist(sharedContext.Ctx, &KeysExistRequest{ + CacheName: cacheName, Keys: toCheck, }) Expect(err).To(BeNil()) @@ -267,105 +297,124 @@ var _ = Describe("Scalar methods", func() { Fail(fmt.Sprintf("expected keys exist success but got %s", result)) } }, - Entry("all hits", []Key{String("#1"), String("#2")}, []bool{true, true}), - Entry("all misses", []Key{String("nope"), String("stillnope")}, []bool{false, false}), - Entry("mixed", []Key{String("nope"), String("#1")}, []bool{false, true}), + Entry("all hits", DefaultClient, []Key{String("#1"), String("#2")}, []bool{true, true}), + Entry("all misses", DefaultClient, []Key{String("nope"), String("stillnope")}, []bool{false, false}), + Entry("mixed", DefaultClient, []Key{String("nope"), String("#1")}, []bool{false, true}), + Entry("all hits with default cache", WithDefaultCache, []Key{String("#1"), String("#2")}, []bool{true, true}), + Entry("all misses with default cache", WithDefaultCache, []Key{String("nope"), String("stillnope")}, []bool{false, false}), + Entry("mixed with default cache", WithDefaultCache, []Key{String("nope"), String("#1")}, []bool{false, true}), ) }) - Describe(`UpdateTtl`, func() { - It(`Overwrites Ttl`, func() { - key := String("key") - value := String("value") + Describe("UpdateTtl", func() { + DescribeTable("Overwrites Ttl", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + key := String("key") + value := String("value") - Expect( - sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Value: value, - }), - ).To(BeAssignableToTypeOf(&SetSuccess{})) + Expect( + client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, + Key: key, + Value: value, + }), + ).To(BeAssignableToTypeOf(&SetSuccess{})) - Expect( - sharedContext.Client.UpdateTtl(sharedContext.Ctx, &UpdateTtlRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Ttl: 6 * time.Second, - }), - ).To(BeAssignableToTypeOf(&UpdateTtlSet{})) + Expect( + client.UpdateTtl(sharedContext.Ctx, &UpdateTtlRequest{ + CacheName: cacheName, + Key: key, + Ttl: 6 * time.Second, + }), + ).To(BeAssignableToTypeOf(&UpdateTtlSet{})) - time.Sleep(sharedContext.DefaultTtl) + time.Sleep(sharedContext.DefaultTtl) - Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - }), - ).To(BeAssignableToTypeOf(&GetHit{})) - }) + Expect( + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: key, + }), + ).To(BeAssignableToTypeOf(&GetHit{})) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`Increases Ttl`, func() { - key := String("key") - value := String("value") + DescribeTable("Increases Ttl", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + key := String("key") + value := String("value") - Expect( - sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Value: value, - Ttl: 2 * time.Second, - }), - ).To(BeAssignableToTypeOf(&SetSuccess{})) + Expect( + client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, + Key: key, + Value: value, + Ttl: 2 * time.Second, + }), + ).To(BeAssignableToTypeOf(&SetSuccess{})) - Expect( - sharedContext.Client.IncreaseTtl(sharedContext.Ctx, &IncreaseTtlRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Ttl: 3 * time.Second, - }), - ).To(BeAssignableToTypeOf(&IncreaseTtlSet{})) + Expect( + client.IncreaseTtl(sharedContext.Ctx, &IncreaseTtlRequest{ + CacheName: cacheName, + Key: key, + Ttl: 3 * time.Second, + }), + ).To(BeAssignableToTypeOf(&IncreaseTtlSet{})) - time.Sleep(2 * time.Second) + time.Sleep(2 * time.Second) - Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - }), - ).To(BeAssignableToTypeOf(&GetHit{})) - }) + Expect( + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: key, + }), + ).To(BeAssignableToTypeOf(&GetHit{})) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`Decreases Ttl`, func() { - key := String("key") - value := String("value") + DescribeTable("Decreases Ttl", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) - Expect( - sharedContext.Client.Set(sharedContext.Ctx, &SetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Value: value, - }), - ).To(BeAssignableToTypeOf(&SetSuccess{})) + key := String("key") + value := String("value") - Expect( - sharedContext.Client.DecreaseTtl(sharedContext.Ctx, &DecreaseTtlRequest{ - CacheName: sharedContext.CacheName, - Key: key, - Ttl: 2 * time.Second, - }), - ).To(BeAssignableToTypeOf(&DecreaseTtlSet{})) + Expect( + client.Set(sharedContext.Ctx, &SetRequest{ + CacheName: cacheName, + Key: key, + Value: value, + }), + ).To(BeAssignableToTypeOf(&SetSuccess{})) - time.Sleep(2 * time.Second) + Expect( + client.DecreaseTtl(sharedContext.Ctx, &DecreaseTtlRequest{ + CacheName: cacheName, + Key: key, + Ttl: 2 * time.Second, + }), + ).To(BeAssignableToTypeOf(&DecreaseTtlSet{})) - Expect( - sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{ - CacheName: sharedContext.CacheName, - Key: key, - }), - ).To(BeAssignableToTypeOf(&GetMiss{})) - }) + time.Sleep(2 * time.Second) + + Expect( + client.Get(sharedContext.Ctx, &GetRequest{ + CacheName: cacheName, + Key: key, + }), + ).To(BeAssignableToTypeOf(&GetMiss{})) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`Returns InvalidArgumentError with negative or zero Ttl value for UpdateTtl`, func() { + It("Returns InvalidArgumentError with negative or zero Ttl value for UpdateTtl", func() { key := String("key") value := String("value") @@ -394,7 +443,7 @@ var _ = Describe("Scalar methods", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`Returns InvalidArgumentError with negative or zero Ttl value for IncreaseTtl`, func() { + It("Returns InvalidArgumentError with negative or zero Ttl value for IncreaseTtl", func() { key := String("key") value := String("value") @@ -423,7 +472,7 @@ var _ = Describe("Scalar methods", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`Returns InvalidArgumentError with negative or zero Ttl value for DecreaseTtl`, func() { + It("Returns InvalidArgumentError with negative or zero Ttl value for DecreaseTtl", func() { key := String("key") value := String("value") @@ -452,7 +501,7 @@ var _ = Describe("Scalar methods", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`Returns UpdateTtlMiss when a key doesn't exist`, func() { + It("Returns UpdateTtlMiss when a key doesn't exist", func() { Expect( sharedContext.Client.UpdateTtl(sharedContext.Ctx, &UpdateTtlRequest{ CacheName: sharedContext.CacheName, @@ -462,7 +511,7 @@ var _ = Describe("Scalar methods", func() { ).To(BeAssignableToTypeOf(&UpdateTtlMiss{})) }) - It(`Returns IncreaseTtlMiss when a key doesn't exist`, func() { + It("Returns IncreaseTtlMiss when a key doesn't exist", func() { Expect( sharedContext.Client.IncreaseTtl(sharedContext.Ctx, &IncreaseTtlRequest{ CacheName: sharedContext.CacheName, @@ -472,7 +521,7 @@ var _ = Describe("Scalar methods", func() { ).To(BeAssignableToTypeOf(&IncreaseTtlMiss{})) }) - It(`Returns DecreaseTtlMiss when a key doesn't exist`, func() { + It("Returns DecreaseTtlMiss when a key doesn't exist", func() { Expect( sharedContext.Client.DecreaseTtl(sharedContext.Ctx, &DecreaseTtlRequest{ CacheName: sharedContext.CacheName, diff --git a/momento/set_test.go b/momento/set_test.go index e5ca7a5b..51434f54 100644 --- a/momento/set_test.go +++ b/momento/set_test.go @@ -27,55 +27,60 @@ var _ = Describe("Set methods", func() { BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) }) - It("errors when the cache is missing", func() { - cacheName := uuid.NewString() - setName := uuid.NewString() + DescribeTable("errors when the cache is missing", + func(clientType string) { + client, _ := sharedContext.GetClientPrereqsForType(clientType) + cacheName := uuid.NewString() + setName := uuid.NewString() - Expect( - sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ - CacheName: cacheName, - SetName: setName, - }), - ).Error().To(HaveMomentoErrorCode(NotFoundError)) + Expect( + client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ + CacheName: cacheName, + SetName: setName, + }), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) - Expect( - sharedContext.Client.SetAddElement(sharedContext.Ctx, &SetAddElementRequest{ - CacheName: cacheName, - SetName: setName, - Element: String("astring"), - }), - ).Error().To(HaveMomentoErrorCode(NotFoundError)) + Expect( + client.SetAddElement(sharedContext.Ctx, &SetAddElementRequest{ + CacheName: cacheName, + SetName: setName, + Element: String("astring"), + }), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) - Expect( - sharedContext.Client.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ - CacheName: cacheName, - SetName: setName, - Elements: []Value{String("astring"), String("bstring")}, - }), - ).Error().To(HaveMomentoErrorCode(NotFoundError)) + Expect( + client.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ + CacheName: cacheName, + SetName: setName, + Elements: []Value{String("astring"), String("bstring")}, + }), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) - Expect( - sharedContext.Client.SetRemoveElement(sharedContext.Ctx, &SetRemoveElementRequest{ - CacheName: cacheName, - SetName: setName, - Element: String("astring"), - }), - ).Error().To(HaveMomentoErrorCode(NotFoundError)) + Expect( + client.SetRemoveElement(sharedContext.Ctx, &SetRemoveElementRequest{ + CacheName: cacheName, + SetName: setName, + Element: String("astring"), + }), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) - Expect( - sharedContext.Client.SetContainsElements(sharedContext.Ctx, &SetContainsElementsRequest{ - CacheName: cacheName, - SetName: setName, - Elements: []Value{String("hi")}, - }), - ).Error().To(HaveMomentoErrorCode(NotFoundError)) - }) + Expect( + client.SetContainsElements(sharedContext.Ctx, &SetContainsElementsRequest{ + CacheName: cacheName, + SetName: setName, + Elements: []Value{String("hi")}, + }), + ).Error().To(HaveMomentoErrorCode(NotFoundError)) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("errors on invalid set name", func() { setName := "" @@ -139,17 +144,18 @@ var _ = Describe("Set methods", func() { Describe("add", func() { DescribeTable("add string and byte single elements happy path", - func(element Value, expectedStrings []string, expectedBytes [][]byte) { + func(clientType string, element Value, expectedStrings []string, expectedBytes [][]byte) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.SetAddElement(sharedContext.Ctx, &SetAddElementRequest{ - CacheName: sharedContext.CacheName, + client.SetAddElement(sharedContext.Ctx, &SetAddElementRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, Element: element, }), ).To(BeAssignableToTypeOf(&SetAddElementSuccess{})) - fetchResp, err := sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, }) Expect(err).To(BeNil()) @@ -161,22 +167,26 @@ var _ = Describe("Set methods", func() { Fail("Unexpected result for Set Fetch") } }, - Entry("when element is a string", String("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), - Entry("when element is bytes", Bytes("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), - Entry("when element is a empty", String(""), []string{""}, [][]byte{[]byte("")}), + Entry("when element is a string", DefaultClient, String("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), + Entry("when element is bytes", DefaultClient, Bytes("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), + Entry("when element is a empty", DefaultClient, String(""), []string{""}, [][]byte{[]byte("")}), + Entry("when element is a string", WithDefaultCache, String("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), + Entry("when element is bytes", WithDefaultCache, Bytes("hello"), []string{"hello"}, [][]byte{[]byte("hello")}), + Entry("when element is a empty", WithDefaultCache, String(""), []string{""}, [][]byte{[]byte("")}), ) DescribeTable("add string and byte multiple elements happy path", - func(elements []Value, expectedStrings []string, expectedBytes [][]byte) { + func(clientType string, elements []Value, expectedStrings []string, expectedBytes [][]byte) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ - CacheName: sharedContext.CacheName, + client.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, Elements: elements, }), ).To(BeAssignableToTypeOf(&SetAddElementsSuccess{})) - fetchResp, err := sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, }) Expect(err).To(BeNil()) @@ -189,25 +199,57 @@ var _ = Describe("Set methods", func() { } }, Entry( - "when elements are strings", + "with default client when elements are strings", + DefaultClient, + []Value{String("hello"), String("world"), String("!"), String("␆")}, + []string{"hello", "world", "!", "␆"}, + [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, + ), + Entry( + "with default client when elements are bytes", + DefaultClient, + []Value{Bytes([]byte("hello")), Bytes([]byte("world")), Bytes([]byte("!")), Bytes([]byte("␆"))}, + []string{"hello", "world", "!", "␆"}, + [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, + ), + Entry( + "with default client when elements are mixed", + DefaultClient, + []Value{Bytes([]byte("hello")), String([]byte("world")), Bytes([]byte("!")), String([]byte("␆"))}, + []string{"hello", "world", "!", "␆"}, + [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, + ), + Entry( + "with default client when elements are empty", + DefaultClient, + []Value{Bytes([]byte("")), Bytes([]byte(""))}, + []string{""}, + [][]byte{[]byte("")}, + ), + Entry( + "with client with default cache when elements are strings", + WithDefaultCache, []Value{String("hello"), String("world"), String("!"), String("␆")}, []string{"hello", "world", "!", "␆"}, [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, ), Entry( - "when elements are bytes", + "with client with default cache when elements are bytes", + WithDefaultCache, []Value{Bytes([]byte("hello")), Bytes([]byte("world")), Bytes([]byte("!")), Bytes([]byte("␆"))}, []string{"hello", "world", "!", "␆"}, [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, ), Entry( - "when elements are mixed", + "with client with default cache when elements are mixed", + WithDefaultCache, []Value{Bytes([]byte("hello")), String([]byte("world")), Bytes([]byte("!")), String([]byte("␆"))}, []string{"hello", "world", "!", "␆"}, [][]byte{[]byte("hello"), []byte("world"), []byte("!"), []byte("␆")}, ), Entry( - "when elements are empty", + "with client with default cache when elements are empty", + WithDefaultCache, []Value{Bytes([]byte("")), Bytes([]byte(""))}, []string{""}, [][]byte{[]byte("")}, @@ -253,20 +295,27 @@ var _ = Describe("Set methods", func() { Elements: elements, }), ).Error().To(BeNil()) + Expect( + sharedContext.ClientWithDefaultCacheName.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ + SetName: sharedContext.CollectionName, + Elements: elements, + }), + ).Error().To(BeNil()) }) DescribeTable("single elements as strings and as bytes", - func(toRemove Value, expectedLength int) { + func(clientType string, toRemove Value, expectedLength int) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.SetRemoveElement(sharedContext.Ctx, &SetRemoveElementRequest{ - CacheName: sharedContext.CacheName, + client.SetRemoveElement(sharedContext.Ctx, &SetRemoveElementRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, Element: toRemove, }), ).Error().To(BeNil()) - fetchResp, err := sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, }) Expect(err).To(BeNil()) @@ -278,23 +327,27 @@ var _ = Describe("Set methods", func() { Fail("something really weird happened") } }, - Entry("as string", String("#5"), 9), - Entry("as bytes", Bytes([]byte("#5")), 9), - Entry("unmatched", String("notvalid"), 10), + Entry("with default client as string", DefaultClient, String("#5"), 9), + Entry("with default client as bytes", DefaultClient, Bytes([]byte("#5")), 9), + Entry("with default client unmatched", DefaultClient, String("notvalid"), 10), + Entry("with client with default cache as string", WithDefaultCache, String("#5"), 9), + Entry("with client with default cache as bytes", WithDefaultCache, Bytes([]byte("#5")), 9), + Entry("with client with default cache unmatched", WithDefaultCache, String("notvalid"), 10), ) DescribeTable("multiple elements as strings and bytes", - func(toRemove []Value, expectedLength int) { + func(clientType string, toRemove []Value, expectedLength int) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) Expect( - sharedContext.Client.SetRemoveElements(sharedContext.Ctx, &SetRemoveElementsRequest{ - CacheName: sharedContext.CacheName, + client.SetRemoveElements(sharedContext.Ctx, &SetRemoveElementsRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, Elements: toRemove, }), ).Error().To(BeNil()) - fetchResp, err := sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.SetFetch(sharedContext.Ctx, &SetFetchRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, }) Expect(err).To(BeNil()) @@ -306,9 +359,12 @@ var _ = Describe("Set methods", func() { Fail("something really weird happened") } }, - Entry("as strings", getElements(5), 5), - Entry("as bytes", []Value{Bytes("#3"), Bytes("#4")}, 8), - Entry("unmatched", []Value{String("notvalid")}, 10), + Entry("with default client as strings", DefaultClient, getElements(5), 5), + Entry("with default client as bytes", DefaultClient, []Value{Bytes("#3"), Bytes("#4")}, 8), + Entry("with default client unmatched", DefaultClient, []Value{String("notvalid")}, 10), + Entry("with client with default cache as strings", WithDefaultCache, getElements(5), 5), + Entry("with client with default cache as bytes", WithDefaultCache, []Value{Bytes("#3"), Bytes("#4")}, 8), + Entry("with client with default cache unmatched", WithDefaultCache, []Value{String("notvalid")}, 10), ) It("returns an error when trying to remove nil elements", func() { @@ -350,12 +406,19 @@ var _ = Describe("Set methods", func() { Elements: elements, }), ).Error().To(BeNil()) + Expect( + sharedContext.ClientWithDefaultCacheName.SetAddElements(sharedContext.Ctx, &SetAddElementsRequest{ + SetName: sharedContext.CollectionName, + Elements: elements, + }), + ).Error().To(BeNil()) }) DescribeTable("check for various mixes of hits and misses", - func(toCheck []Value, expected []bool) { - containsResp, err := sharedContext.Client.SetContainsElements(sharedContext.Ctx, &SetContainsElementsRequest{ - CacheName: sharedContext.CacheName, + func(clientType string, toCheck []Value, expected []bool) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + containsResp, err := client.SetContainsElements(sharedContext.Ctx, &SetContainsElementsRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, Elements: toCheck, }) @@ -366,9 +429,12 @@ var _ = Describe("Set methods", func() { Expect(result.ContainsElements()).To(Equal(expected)) } }, - Entry("all hits", []Value{String("#1"), String("#2"), String("#3")}, []bool{true, true, true}), - Entry("all misses", []Value{String("not"), String("this"), String("time")}, []bool{false, false, false}), - Entry("a mixture", []Value{String("not"), String("#2"), String("time")}, []bool{false, true, false}), + Entry("with default client all hits", DefaultClient, []Value{String("#1"), String("#2"), String("#3")}, []bool{true, true, true}), + Entry("with default client all misses", DefaultClient, []Value{String("not"), String("this"), String("time")}, []bool{false, false, false}), + Entry("with default client a mixture", DefaultClient, []Value{String("not"), String("#2"), String("time")}, []bool{false, true, false}), + Entry("with client with default cache all hits", WithDefaultCache, []Value{String("#1"), String("#2"), String("#3")}, []bool{true, true, true}), + Entry("with client with default cache all misses", WithDefaultCache, []Value{String("not"), String("this"), String("time")}, []bool{false, false, false}), + Entry("with client with default cache a mixture", WithDefaultCache, []Value{String("not"), String("#2"), String("time")}, []bool{false, true, false}), ) It("gets a miss on a nonexistent set", func() { diff --git a/momento/sorted_set_test.go b/momento/sorted_set_test.go index 9605d786..a0dec5f0 100644 --- a/momento/sorted_set_test.go +++ b/momento/sorted_set_test.go @@ -18,7 +18,7 @@ var _ = Describe("SortedSet", func() { var sharedContext SharedContext BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close() }) }) @@ -35,6 +35,15 @@ var _ = Describe("SortedSet", func() { }, ), ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) + Expect( + sharedContext.ClientWithDefaultCacheName.SortedSetPutElements( + sharedContext.Ctx, + &SortedSetPutElementsRequest{ + SetName: sharedContext.CollectionName, + Elements: elements, + }, + ), + ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) } // Convenience for fetching elements. @@ -48,9 +57,9 @@ var _ = Describe("SortedSet", func() { ) } - DescribeTable(`Validates the names`, - func(cacheName string, collectionName string, expectedError string) { - client := sharedContext.Client + DescribeTable("Validates the names", + func(clientType string, cacheName string, collectionName string, expectedError string) { + client, _ := sharedContext.GetClientPrereqsForType(clientType) ctx := sharedContext.Ctx value := String(uuid.NewString()) @@ -119,14 +128,19 @@ var _ = Describe("SortedSet", func() { }), ).Error().To(HaveMomentoErrorCode(expectedError)) }, - Entry("Empty cache name", "", sharedContext.CollectionName, InvalidArgumentError), - Entry("Blank cache name", " ", sharedContext.CollectionName, InvalidArgumentError), - Entry("Empty collection name", sharedContext.CacheName, "", InvalidArgumentError), - Entry("Blank collection name", sharedContext.CacheName, " ", InvalidArgumentError), - Entry("Non-existent cache", uuid.NewString(), uuid.NewString(), NotFoundError), + Entry("Empty cache name with default client", DefaultClient, "", sharedContext.CollectionName, InvalidArgumentError), + Entry("Blank cache name with default client", DefaultClient, " ", sharedContext.CollectionName, InvalidArgumentError), + Entry("Empty collection name with default client", DefaultClient, sharedContext.CacheName, "", InvalidArgumentError), + Entry("Blank collection name with default client", DefaultClient, sharedContext.CacheName, " ", InvalidArgumentError), + Entry("Non-existent cache with default client", DefaultClient, uuid.NewString(), uuid.NewString(), NotFoundError), + Entry("Empty cache name with client with default cache", WithDefaultCache, "", sharedContext.CollectionName, InvalidArgumentError), + Entry("Blank cache name with client with default cache", WithDefaultCache, " ", sharedContext.CollectionName, InvalidArgumentError), + Entry("Empty collection name with client with default cache", WithDefaultCache, sharedContext.CacheName, "", InvalidArgumentError), + Entry("Blank collection name with client with default cache", WithDefaultCache, sharedContext.CacheName, " ", InvalidArgumentError), + Entry("Non-existent cache with client with default cache", WithDefaultCache, uuid.NewString(), uuid.NewString(), NotFoundError), ) - DescribeTable(`Honors CollectionTtl `, + DescribeTable("Honors CollectionTtl ", func( changer func(SortedSetElement, *utils.CollectionTtl), ) { @@ -187,7 +201,7 @@ var _ = Describe("SortedSet", func() { Expect(fetch()).To(Equal(&SortedSetFetchMiss{})) }, Entry( - `SortedSetIncrementScore`, + "SortedSetIncrementScore", func(element SortedSetElement, ttl *utils.CollectionTtl) { request := &SortedSetIncrementScoreRequest{ CacheName: sharedContext.CacheName, @@ -204,7 +218,7 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(0))) }, ), - Entry(`SortedSetPutElement`, + Entry("SortedSetPutElement", func(element SortedSetElement, ttl *utils.CollectionTtl) { request := &SortedSetPutElementRequest{ CacheName: sharedContext.CacheName, @@ -221,7 +235,7 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(&SortedSetPutElementSuccess{})) }, ), - Entry(`SortedSetPutElements`, + Entry("SortedSetPutElements", func(element SortedSetElement, ttl *utils.CollectionTtl) { request := &SortedSetPutElementsRequest{ CacheName: sharedContext.CacheName, @@ -239,8 +253,8 @@ var _ = Describe("SortedSet", func() { ), ) - Describe(`SortedSetFetchByRank`, func() { - It(`Misses if the set does not exist`, func() { + Describe("SortedSetFetchByRank", func() { + It("Misses if the set does not exist", func() { Expect( sharedContext.Client.SortedSetFetchByRank( sharedContext.Ctx, @@ -252,7 +266,7 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(&SortedSetFetchMiss{})) }) - Context(`With a populated SortedSet`, func() { + Context("With a populated SortedSet", func() { // We'll populate the SortedSet with these elements. sortedSetElements := []SortedSetElement{ {Value: String("one"), Score: 9999}, @@ -267,38 +281,43 @@ var _ = Describe("SortedSet", func() { putElements(sortedSetElements) }) - It(`With no extra args it fetches everything in ascending order`, func() { - resp, err := sharedContext.Client.SortedSetFetchByRank( - sharedContext.Ctx, - &SortedSetFetchByRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - }, - ) - Expect(err).To(BeNil()) - Expect(resp).To(HaveSortedSetElements( - []SortedSetBytesElement{ - {Value: []byte("six"), Score: -1000}, - {Value: []byte("five"), Score: -500}, - {Value: []byte("four"), Score: -50}, - {Value: []byte("three"), Score: 0}, - {Value: []byte("two"), Score: 50}, - {Value: []byte("one"), Score: 9999}, - }, - )) - Expect(resp).To(HaveSortedSetStringElements( - []SortedSetStringElement{ - {Value: "six", Score: -1000}, - {Value: "five", Score: -500}, - {Value: "four", Score: -50}, - {Value: "three", Score: 0}, - {Value: "two", Score: 50}, - {Value: "one", Score: 9999}, - }, - )) - }) + DescribeTable("With no extra args it fetches everything in ascending order", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + resp, err := client.SortedSetFetchByRank( + sharedContext.Ctx, + &SortedSetFetchByRankRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + }, + ) + Expect(err).To(BeNil()) + Expect(resp).To(HaveSortedSetElements( + []SortedSetBytesElement{ + {Value: []byte("six"), Score: -1000}, + {Value: []byte("five"), Score: -500}, + {Value: []byte("four"), Score: -50}, + {Value: []byte("three"), Score: 0}, + {Value: []byte("two"), Score: 50}, + {Value: []byte("one"), Score: 9999}, + }, + )) + Expect(resp).To(HaveSortedSetStringElements( + []SortedSetStringElement{ + {Value: "six", Score: -1000}, + {Value: "five", Score: -500}, + {Value: "four", Score: -50}, + {Value: "three", Score: 0}, + {Value: "two", Score: 50}, + {Value: "one", Score: 9999}, + }, + )) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`Orders`, func() { + It("Orders", func() { Expect( sharedContext.Client.SortedSetFetchByRank( sharedContext.Ctx, @@ -320,7 +339,7 @@ var _ = Describe("SortedSet", func() { )) }) - It(`Constrains by start/end rank`, func() { + It("Constrains by start/end rank", func() { start := int32(1) end := int32(4) Expect( @@ -343,7 +362,7 @@ var _ = Describe("SortedSet", func() { )) }) - It(`Counts negative start rank inclusive from the end`, func() { + It("Counts negative start rank inclusive from the end", func() { start := int32(-3) Expect( sharedContext.Client.SortedSetFetchByRank( @@ -385,7 +404,7 @@ var _ = Describe("SortedSet", func() { } }) - It(`Counts negative end rank exclusively from the end`, func() { + It("Counts negative end rank exclusively from the end", func() { end := int32(-3) Expect( sharedContext.Client.SortedSetFetchByRank( @@ -432,8 +451,8 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetFetchByScore`, func() { - It(`Misses if the set does not exist`, func() { + Describe("SortedSetFetchByScore", func() { + It("Misses if the set does not exist", func() { Expect( sharedContext.Client.SortedSetFetchByScore( sharedContext.Ctx, @@ -445,7 +464,7 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(&SortedSetFetchMiss{})) }) - Context(`With a populated SortedSet`, func() { + Context("With a populated SortedSet", func() { // We'll populate the SortedSet with these elements. sortedSetElements := []SortedSetElement{ {Value: String("one"), Score: 9999}, @@ -460,38 +479,43 @@ var _ = Describe("SortedSet", func() { putElements(sortedSetElements) }) - It(`With no extra args it fetches everything in ascending order`, func() { - resp, err := sharedContext.Client.SortedSetFetchByScore( - sharedContext.Ctx, - &SortedSetFetchByScoreRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - }, - ) - Expect(err).To(BeNil()) - Expect(resp).To(HaveSortedSetElements( - []SortedSetBytesElement{ - {Value: []byte("six"), Score: -1000}, - {Value: []byte("five"), Score: -500}, - {Value: []byte("four"), Score: -50}, - {Value: []byte("three"), Score: 0}, - {Value: []byte("two"), Score: 50}, - {Value: []byte("one"), Score: 9999}, - }, - )) - Expect(resp).To(HaveSortedSetStringElements( - []SortedSetStringElement{ - {Value: "six", Score: -1000}, - {Value: "five", Score: -500}, - {Value: "four", Score: -50}, - {Value: "three", Score: 0}, - {Value: "two", Score: 50}, - {Value: "one", Score: 9999}, - }, - )) - }) + DescribeTable("With no extra args it fetches everything in ascending order", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + resp, err := client.SortedSetFetchByScore( + sharedContext.Ctx, + &SortedSetFetchByScoreRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + }, + ) + Expect(err).To(BeNil()) + Expect(resp).To(HaveSortedSetElements( + []SortedSetBytesElement{ + {Value: []byte("six"), Score: -1000}, + {Value: []byte("five"), Score: -500}, + {Value: []byte("four"), Score: -50}, + {Value: []byte("three"), Score: 0}, + {Value: []byte("two"), Score: 50}, + {Value: []byte("one"), Score: 9999}, + }, + )) + Expect(resp).To(HaveSortedSetStringElements( + []SortedSetStringElement{ + {Value: "six", Score: -1000}, + {Value: "five", Score: -500}, + {Value: "four", Score: -50}, + {Value: "three", Score: 0}, + {Value: "two", Score: 50}, + {Value: "one", Score: 9999}, + }, + )) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cacha", WithDefaultCache), + ) - It(`Orders`, func() { + It("Orders", func() { Expect( sharedContext.Client.SortedSetFetchByScore( sharedContext.Ctx, @@ -513,7 +537,7 @@ var _ = Describe("SortedSet", func() { )) }) - It(`Constrains by score inclusive`, func() { + It("Constrains by score inclusive", func() { minScore := float64(0) maxScore := float64(50) Expect( @@ -535,7 +559,7 @@ var _ = Describe("SortedSet", func() { )) }) - It(`Limits and offsets`, func() { + It("Limits and offsets", func() { minScore := float64(-750) maxScore := float64(51) offset := uint32(1) @@ -563,8 +587,8 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetGetRank`, func() { - It(`Misses when the element does not exist`, func() { + Describe("SortedSetGetRank", func() { + It("Misses when the element does not exist", func() { Expect( sharedContext.Client.SortedSetGetRank( sharedContext.Ctx, @@ -589,57 +613,62 @@ var _ = Describe("SortedSet", func() { Expect(getResp).To(BeAssignableToTypeOf(&SortedSetGetRankMiss{})) }) - It(`Gets the rank`, func() { - putElements( - []SortedSetElement{ - {Value: String("first"), Score: 9999}, - {Value: String("last"), Score: -9999}, - {Value: String("middle"), Score: 50}, - }, - ) - - resp, err := sharedContext.Client.SortedSetGetRank( - sharedContext.Ctx, - &SortedSetGetRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Value: String("first"), - }, - ) - Expect(err).To(BeNil()) - Expect(resp).To(Equal(SortedSetGetRankHit(2))) - switch r := resp.(type) { - case SortedSetGetRankHit: - Expect(r.Rank()).To(Equal(uint64(2))) - default: - Fail(fmt.Sprintf("Wrong type: %T", r)) - } - - Expect( - sharedContext.Client.SortedSetGetRank( - sharedContext.Ctx, - &SortedSetGetRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Value: String("last"), + DescribeTable("Gets the rank", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + putElements( + []SortedSetElement{ + {Value: String("first"), Score: 9999}, + {Value: String("last"), Score: -9999}, + {Value: String("middle"), Score: 50}, }, - ), - ).To(Equal(SortedSetGetRankHit(0))) + ) - Expect( - sharedContext.Client.SortedSetGetRank( + resp, err := client.SortedSetGetRank( sharedContext.Ctx, &SortedSetGetRankRequest{ - CacheName: sharedContext.CacheName, + CacheName: cacheName, SetName: sharedContext.CollectionName, - Order: DESCENDING, - Value: String("last"), + Value: String("first"), }, - ), - ).To(Equal(SortedSetGetRankHit(2))) - }) + ) + Expect(err).To(BeNil()) + Expect(resp).To(Equal(SortedSetGetRankHit(2))) + switch r := resp.(type) { + case SortedSetGetRankHit: + Expect(r.Rank()).To(Equal(uint64(2))) + default: + Fail(fmt.Sprintf("Wrong type: %T", r)) + } - It(`returns an error for a nil element value`, func() { + Expect( + client.SortedSetGetRank( + sharedContext.Ctx, + &SortedSetGetRankRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Value: String("last"), + }, + ), + ).To(Equal(SortedSetGetRankHit(0))) + + Expect( + client.SortedSetGetRank( + sharedContext.Ctx, + &SortedSetGetRankRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Order: DESCENDING, + Value: String("last"), + }, + ), + ).To(Equal(SortedSetGetRankHit(2))) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) + + It("returns an error for a nil element value", func() { Expect( sharedContext.Client.SortedSetGetRank( sharedContext.Ctx, @@ -652,7 +681,7 @@ var _ = Describe("SortedSet", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`accepts an empty value`, func() { + It("accepts an empty value", func() { putElements([]SortedSetElement{ {Value: String(""), Score: 0}, }) @@ -670,8 +699,8 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetGetScores`, func() { - It(`Misses when the element does not exist`, func() { + Describe("SortedSetGetScores", func() { + It("Misses when the element does not exist", func() { Expect( sharedContext.Client.SortedSetGetScores( sharedContext.Ctx, @@ -696,43 +725,48 @@ var _ = Describe("SortedSet", func() { Expect(getResp).To(BeAssignableToTypeOf(&SortedSetGetScoresMiss{})) }) - It(`Gets the correct set of scores`, func() { - putElements( - []SortedSetElement{ - {Value: String("first"), Score: 9999}, - {Value: String("last"), Score: -9999}, - {Value: String("middle"), Score: 50}, - }, - ) - - getResp, err := sharedContext.Client.SortedSetGetScores( - sharedContext.Ctx, - &SortedSetGetScoresRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Values: []Value{ - String("first"), String("last"), String("dne"), + DescribeTable("Gets the correct set of scores", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + putElements( + []SortedSetElement{ + {Value: String("first"), Score: 9999}, + {Value: String("last"), Score: -9999}, + {Value: String("middle"), Score: 50}, }, - }, - ) - Expect(err).To(BeNil()) - switch resp := getResp.(type) { - case *SortedSetGetScoresHit: - Expect(resp.Responses()).To(Equal( - []SortedSetGetScoreResponse{ - NewSortedSetGetScoreHit(9999), - NewSortedSetGetScoreHit(-9999), - &SortedSetGetScoreMiss{}, + ) + + getResp, err := client.SortedSetGetScores( + sharedContext.Ctx, + &SortedSetGetScoresRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Values: []Value{ + String("first"), String("last"), String("dne"), + }, }, - )) + ) + Expect(err).To(BeNil()) + switch resp := getResp.(type) { + case *SortedSetGetScoresHit: + Expect(resp.Responses()).To(Equal( + []SortedSetGetScoreResponse{ + NewSortedSetGetScoreHit(9999), + NewSortedSetGetScoreHit(-9999), + &SortedSetGetScoreMiss{}, + }, + )) - Expect(resp.ScoresArray()).To(Equal([]float64{9999, -9999})) + Expect(resp.ScoresArray()).To(Equal([]float64{9999, -9999})) - Expect(resp.ScoresMap()).To(Equal(map[string]float64{"first": 9999, "last": -9999})) - } - }) + Expect(resp.ScoresMap()).To(Equal(map[string]float64{"first": 9999, "last": -9999})) + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) - It(`returns an error when element values are nil`, func() { + It("returns an error when element values are nil", func() { Expect( sharedContext.Client.SortedSetGetScores( sharedContext.Ctx, @@ -758,30 +792,35 @@ var _ = Describe("SortedSet", func() { }) Describe("sorted set get score", func() { - It("succeeds on the happy path", func() { - putElements( - []SortedSetElement{ - {Value: String("first"), Score: 9999}, - {Value: String("last"), Score: -9999}, - {Value: String("middle"), Score: 50}, - }, - ) - getResp, err := sharedContext.Client.SortedSetGetScore( - sharedContext.Ctx, &SortedSetGetScoreRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Value: String("first"), - }, - ) - Expect(err).To(BeNil()) - switch result := getResp.(type) { - case *SortedSetGetScoreHit: - score := result.Score() - Expect(score).To(Equal(9999.0)) - default: - Fail("expected a sorted set get score hit but got a miss") - } - }) + DescribeTable("succeeds on the happy path", + func(clientTYpe string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientTYpe) + putElements( + []SortedSetElement{ + {Value: String("first"), Score: 9999}, + {Value: String("last"), Score: -9999}, + {Value: String("middle"), Score: 50}, + }, + ) + getResp, err := client.SortedSetGetScore( + sharedContext.Ctx, &SortedSetGetScoreRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Value: String("first"), + }, + ) + Expect(err).To(BeNil()) + switch result := getResp.(type) { + case *SortedSetGetScoreHit: + score := result.Score() + Expect(score).To(Equal(9999.0)) + default: + Fail("expected a sorted set get score hit but got a miss") + } + }, + Entry("with default cache", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("misses when the element doesn't exist", func() { putElements([]SortedSetElement{ @@ -823,8 +862,8 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetIncrementScore`, func() { - It(`Increments if it does not exist`, func() { + Describe("SortedSetIncrementScore", func() { + It("Increments if it does not exist", func() { Expect( sharedContext.Client.SortedSetIncrementScore( sharedContext.Ctx, @@ -838,7 +877,7 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(99))) }) - It(`Is invalid to increment by 0`, func() { + It("Is invalid to increment by 0", func() { Expect( sharedContext.Client.SortedSetIncrementScore( sharedContext.Ctx, @@ -852,7 +891,7 @@ var _ = Describe("SortedSet", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`Is invalid to not include the Amount`, func() { + It("Is invalid to not include the Amount", func() { Expect( sharedContext.Client.SortedSetIncrementScore( sharedContext.Ctx, @@ -865,45 +904,50 @@ var _ = Describe("SortedSet", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`Increments the score`, func() { - putElements( - []SortedSetElement{ - {Value: String("first"), Score: 9999}, - {Value: String("last"), Score: -9999}, - {Value: String("middle"), Score: 50}, - }, - ) - - resp, err := sharedContext.Client.SortedSetIncrementScore( - sharedContext.Ctx, - &SortedSetIncrementScoreRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Value: String("middle"), - Amount: 42, - }, - ) - Expect(err).To(BeNil()) - Expect(resp).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(92))) - switch r := resp.(type) { - case SortedSetIncrementScoreSuccess: - Expect(r.Score()).To(Equal(float64(92))) - default: - Fail(fmt.Sprintf("Unexpected response type %T", r)) - } + DescribeTable("Increments the score", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + putElements( + []SortedSetElement{ + {Value: String("first"), Score: 9999}, + {Value: String("last"), Score: -9999}, + {Value: String("middle"), Score: 50}, + }, + ) - Expect( - sharedContext.Client.SortedSetIncrementScore( + resp, err := client.SortedSetIncrementScore( sharedContext.Ctx, &SortedSetIncrementScoreRequest{ - CacheName: sharedContext.CacheName, + CacheName: cacheName, SetName: sharedContext.CollectionName, Value: String("middle"), - Amount: -42, + Amount: 42, }, - ), - ).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(50))) - }) + ) + Expect(err).To(BeNil()) + Expect(resp).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(92))) + switch r := resp.(type) { + case SortedSetIncrementScoreSuccess: + Expect(r.Score()).To(Equal(float64(92))) + default: + Fail(fmt.Sprintf("Unexpected response type %T", r)) + } + + Expect( + client.SortedSetIncrementScore( + sharedContext.Ctx, + &SortedSetIncrementScoreRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Value: String("middle"), + Amount: -42, + }, + ), + ).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(50))) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error when element value is nil", func() { Expect( @@ -919,7 +963,7 @@ var _ = Describe("SortedSet", func() { ).Error().To(HaveMomentoErrorCode(InvalidArgumentError)) }) - It(`accepts an empty value`, func() { + It("accepts an empty value", func() { putElements([]SortedSetElement{ {Value: String(""), Score: 50}, }) @@ -941,11 +985,12 @@ var _ = Describe("SortedSet", func() { Describe("SortedSetPutElement", func() { DescribeTable("put an element with each of string and byte values", - func(inputValue Value, inputScore float64, expected []SortedSetBytesElement) { - resp, err := sharedContext.Client.SortedSetPutElement( + func(clientType string, inputValue Value, inputScore float64, expected []SortedSetBytesElement) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + resp, err := client.SortedSetPutElement( sharedContext.Ctx, &SortedSetPutElementRequest{ - CacheName: sharedContext.CacheName, + CacheName: cacheName, SetName: sharedContext.CollectionName, Value: inputValue, Score: inputScore, @@ -953,10 +998,10 @@ var _ = Describe("SortedSet", func() { Expect(err).To(BeNil()) Expect(resp).To(BeAssignableToTypeOf(&SortedSetPutElementSuccess{})) - fetchResp, fetchErr := sharedContext.Client.SortedSetFetchByRank( + fetchResp, fetchErr := client.SortedSetFetchByRank( sharedContext.Ctx, &SortedSetFetchByRankRequest{ - CacheName: sharedContext.CacheName, + CacheName: cacheName, SetName: sharedContext.CollectionName, }, ) @@ -966,8 +1011,10 @@ var _ = Describe("SortedSet", func() { Expect(fetchResp.ValueBytesElements()).To(Equal(expected)) } }, - Entry("string value", String("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), - Entry("bytes value", Bytes("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), + Entry("string value with default client", DefaultClient, String("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), + Entry("bytes value with default client", DefaultClient, Bytes("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), + Entry("string value with client with default cache", WithDefaultCache, String("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), + Entry("bytes value with client with default cache", WithDefaultCache, Bytes("aString"), 42.0, []SortedSetBytesElement{{Value: []byte("aString"), Score: 42}}), ) It("overwrites an existing element's score", func() { @@ -1025,38 +1072,43 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetPutElements`, func() { - It("puts multiple elements", func() { - elems := []SortedSetElement{{Value: String("val1"), Score: 0}, {Value: Bytes("val2"), Score: 10}} - Expect( - sharedContext.Client.SortedSetPutElements( - sharedContext.Ctx, - &SortedSetPutElementsRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Elements: elems, - }, - ), - ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) - - fetchResp, err := sharedContext.Client.SortedSetFetchByRank(sharedContext.Ctx, &SortedSetFetchByRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Order: ASCENDING, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *SortedSetFetchHit: - Expect( - result.ValueStringElements(), - ).To(Equal([]SortedSetStringElement{{Value: "val1", Score: 0}, {Value: "val2", Score: 10}})) + Describe("SortedSetPutElements", func() { + DescribeTable("puts multiple elements", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + elems := []SortedSetElement{{Value: String("val1"), Score: 0}, {Value: Bytes("val2"), Score: 10}} Expect( - result.ValueBytesElements(), - ).To(Equal([]SortedSetBytesElement{{Value: []byte("val1"), Score: 0}, {Value: []byte("val2"), Score: 10}})) - default: - Fail("expected a hit for sorted set fetch but got a miss") - } - }) + client.SortedSetPutElements( + sharedContext.Ctx, + &SortedSetPutElementsRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Elements: elems, + }, + ), + ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) + + fetchResp, err := client.SortedSetFetchByRank(sharedContext.Ctx, &SortedSetFetchByRankRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Order: ASCENDING, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *SortedSetFetchHit: + Expect( + result.ValueStringElements(), + ).To(Equal([]SortedSetStringElement{{Value: "val1", Score: 0}, {Value: "val2", Score: 10}})) + Expect( + result.ValueBytesElements(), + ).To(Equal([]SortedSetBytesElement{{Value: []byte("val1"), Score: 0}, {Value: []byte("val2"), Score: 10}})) + default: + Fail("expected a hit for sorted set fetch but got a miss") + } + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("overwrites multiple elements", func() { elems := []SortedSetElement{{Value: String("val1"), Score: 0}, {Value: Bytes("val2"), Score: 10}} @@ -1094,43 +1146,48 @@ var _ = Describe("SortedSet", func() { }) Describe("Sorted set remove element", func() { - It("removes an element", func() { - elems := []SortedSetElement{{Value: String("val1"), Score: 0}, {Value: String("val2"), Score: 10}} - Expect( - sharedContext.Client.SortedSetPutElements( - sharedContext.Ctx, - &SortedSetPutElementsRequest{ - CacheName: sharedContext.CacheName, + DescribeTable("removes an element", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + elems := []SortedSetElement{{Value: String("val1"), Score: 0}, {Value: String("val2"), Score: 10}} + Expect( + client.SortedSetPutElements( + sharedContext.Ctx, + &SortedSetPutElementsRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Elements: elems, + }, + ), + ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) + + Expect( + client.SortedSetRemoveElement(sharedContext.Ctx, &SortedSetRemoveElementRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, - Elements: elems, - }, - ), - ).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{})) + Value: String("val1"), + }), + ).To(BeAssignableToTypeOf(&SortedSetRemoveElementSuccess{})) - Expect( - sharedContext.Client.SortedSetRemoveElement(sharedContext.Ctx, &SortedSetRemoveElementRequest{ - CacheName: sharedContext.CacheName, + fetchResp, err := client.SortedSetFetchByRank(sharedContext.Ctx, &SortedSetFetchByRankRequest{ + CacheName: cacheName, SetName: sharedContext.CollectionName, - Value: String("val1"), - }), - ).To(BeAssignableToTypeOf(&SortedSetRemoveElementSuccess{})) - - fetchResp, err := sharedContext.Client.SortedSetFetchByRank(sharedContext.Ctx, &SortedSetFetchByRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Order: ASCENDING, - }) - Expect(err).To(BeNil()) - switch result := fetchResp.(type) { - case *SortedSetFetchHit: - Expect( - result.ValueStringElements(), - ).To(Equal([]SortedSetStringElement{{Value: "val2", Score: 10}})) - default: - Fail("expected a hit for sorted set fetch but got a miss") - } + Order: ASCENDING, + }) + Expect(err).To(BeNil()) + switch result := fetchResp.(type) { + case *SortedSetFetchHit: + Expect( + result.ValueStringElements(), + ).To(Equal([]SortedSetStringElement{{Value: "val2", Score: 10}})) + default: + Fail("expected a hit for sorted set fetch but got a miss") + } - }) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("succeeds when the element doesn't exist", func() { Expect( @@ -1160,8 +1217,8 @@ var _ = Describe("SortedSet", func() { }) }) - Describe(`SortedSetRemoveElements`, func() { - It(`Succeeds when the element does not exist`, func() { + Describe("SortedSetRemoveElements", func() { + It("Succeeds when the element does not exist", func() { Expect( sharedContext.Client.SortedSetRemoveElements( sharedContext.Ctx, @@ -1174,43 +1231,48 @@ var _ = Describe("SortedSet", func() { ).To(BeAssignableToTypeOf(&SortedSetRemoveElementsSuccess{})) }) - It(`Removes elements`, func() { - putElements( - []SortedSetElement{ - {Value: String("first"), Score: 9999}, - {Value: String("last"), Score: -9999}, - {Value: String("middle"), Score: 50}, - }, - ) + DescribeTable("Removes elements", + func(clientType string) { + client, cacheName := sharedContext.GetClientPrereqsForType(clientType) + putElements( + []SortedSetElement{ + {Value: String("first"), Score: 9999}, + {Value: String("last"), Score: -9999}, + {Value: String("middle"), Score: 50}, + }, + ) - Expect( - sharedContext.Client.SortedSetRemoveElements( - sharedContext.Ctx, - &SortedSetRemoveElementsRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, - Values: []Value{ - String("first"), String("dne"), + Expect( + client.SortedSetRemoveElements( + sharedContext.Ctx, + &SortedSetRemoveElementsRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + Values: []Value{ + String("first"), String("dne"), + }, }, - }, - ), - ).To(BeAssignableToTypeOf(&SortedSetRemoveElementsSuccess{})) + ), + ).To(BeAssignableToTypeOf(&SortedSetRemoveElementsSuccess{})) - Expect( - sharedContext.Client.SortedSetFetchByRank( - sharedContext.Ctx, - &SortedSetFetchByRankRequest{ - CacheName: sharedContext.CacheName, - SetName: sharedContext.CollectionName, + Expect( + client.SortedSetFetchByRank( + sharedContext.Ctx, + &SortedSetFetchByRankRequest{ + CacheName: cacheName, + SetName: sharedContext.CollectionName, + }, + ), + ).To(HaveSortedSetElements( + []SortedSetBytesElement{ + {Value: []byte("last"), Score: -9999}, + {Value: []byte("middle"), Score: 50}, }, - ), - ).To(HaveSortedSetElements( - []SortedSetBytesElement{ - {Value: []byte("last"), Score: -9999}, - {Value: []byte("middle"), Score: 50}, - }, - )) - }) + )) + }, + Entry("with default client", DefaultClient), + Entry("with client with default cache", WithDefaultCache), + ) It("returns an error when elements are nil", func() { Expect( diff --git a/momento/test_helpers/shared_context.go b/momento/test_helpers/shared_context.go index 79759a3f..3c0d1b5e 100644 --- a/momento/test_helpers/shared_context.go +++ b/momento/test_helpers/shared_context.go @@ -12,6 +12,11 @@ import ( "github.com/momentohq/client-sdk-go/momento" ) +const ( + DefaultClient = "defaultClient" + WithDefaultCache = "withDefaultCache" +) + type SharedContext struct { Client momento.CacheClient ClientWithDefaultCacheName momento.CacheClient @@ -66,11 +71,30 @@ func NewSharedContext() SharedContext { return shared } -func (shared SharedContext) CreateDefaultCache() { +func (shared SharedContext) GetClientPrereqsForType(clientType string) (momento.CacheClient, string) { + var client momento.CacheClient + var cacheName string + if clientType == WithDefaultCache { + client = shared.ClientWithDefaultCacheName + cacheName = "" + } else if clientType == DefaultClient { + client = shared.Client + cacheName = shared.CacheName + } else { + panic("invalid client type") + } + return client, cacheName +} + +func (shared SharedContext) CreateDefaultCaches() { _, err := shared.Client.CreateCache(shared.Ctx, &momento.CreateCacheRequest{CacheName: shared.CacheName}) if err != nil { panic(err) } + _, err = shared.Client.CreateCache(shared.Ctx, &momento.CreateCacheRequest{CacheName: shared.DefaultCacheName}) + if err != nil { + panic(err) + } } func (shared SharedContext) Close() { @@ -78,6 +102,10 @@ func (shared SharedContext) Close() { if err != nil { panic(err) } + _, err = shared.Client.DeleteCache(shared.Ctx, &momento.DeleteCacheRequest{CacheName: shared.DefaultCacheName}) + if err != nil { + panic(err) + } shared.Client.Close() } diff --git a/momento/topic_client_test.go b/momento/topic_client_test.go index eac4c338..c76fa4e2 100644 --- a/momento/topic_client_test.go +++ b/momento/topic_client_test.go @@ -17,7 +17,7 @@ var _ = Describe("Pubsub", func() { BeforeEach(func() { sharedContext = NewSharedContext() - sharedContext.CreateDefaultCache() + sharedContext.CreateDefaultCaches() DeferCleanup(func() { sharedContext.Close()