From 1a9f2a16b2f0de8dfefed9e98636b6ce689dade5 Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Fri, 20 Dec 2024 11:25:04 -0800 Subject: [PATCH] test: add basic integration tests for scalar and sorted set (#14) Adds integration tests for scalar set and sorted set put elements. --- test/integration-setup.ts | 32 ++++++++++++++++++------ test/scalar.test.ts | 44 +++++++++++++++++++++++++++++++++ test/sorted-set.test.ts | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 test/scalar.test.ts create mode 100644 test/sorted-set.test.ts diff --git a/test/integration-setup.ts b/test/integration-setup.ts index 2fd267e..c36e502 100644 --- a/test/integration-setup.ts +++ b/test/integration-setup.ts @@ -6,7 +6,8 @@ import { Configurations, CreateCache, CredentialProvider, - DeleteCache, + DeleteCacheResponse, + ICacheClient, MomentoErrorCode, } from '@gomomento/sdk'; import { @@ -32,7 +33,8 @@ function regionalMomentoClientForTesting(regionEnvVarName: string) { } export function SetupIntegrationTest(): { - client: IMultiRegionCacheWriterClient; + multiRegionClient: IMultiRegionCacheWriterClient; + regionalClients: Record; cacheName: string; } { const cacheName = testCacheName(); @@ -60,23 +62,37 @@ export function SetupIntegrationTest(): { for (const regionEnvVarName of regionEnvVarNames) { const momento = regionalMomentoClientForTesting(regionEnvVarName); const deleteResponse = await momento.deleteCache(cacheName); - if (deleteResponse instanceof DeleteCache.Error) { + if ( + deleteResponse.type === DeleteCacheResponse.Error && + deleteResponse.errorCode() !== MomentoErrorCode.CACHE_NOT_FOUND_ERROR + ) { throw deleteResponse.innerException(); } } }); - const client = new MultiRegionCacheWriterClient({ - credentialProviders: { - 'region-1': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_1'), - 'region-2': CredentialProvider.fromEnvVar('MOMENTO_API_KEY_REGION_2'), + const credentialProviders = regionEnvVarNames.reduce( + (acc, envVarName, index) => { + acc[`region-${index + 1}`] = CredentialProvider.fromEnvVar(envVarName); + return acc; }, + {} as Record + ); + const multiRegionClient = new MultiRegionCacheWriterClient({ + credentialProviders, configuration: Configurations.Laptop.latest(), defaultTtlSeconds: 60, }); + const regionalClients: Record = {}; + for (const regionEnvVarName of regionEnvVarNames) { + const regionalClient = regionalMomentoClientForTesting(regionEnvVarName); + regionalClients[regionEnvVarName] = regionalClient; + } + return { - client, + multiRegionClient, + regionalClients, cacheName, }; } diff --git a/test/scalar.test.ts b/test/scalar.test.ts new file mode 100644 index 0000000..4eda9b2 --- /dev/null +++ b/test/scalar.test.ts @@ -0,0 +1,44 @@ +import {v4} from 'uuid'; +import {SetupIntegrationTest} from './integration-setup'; +import {MultiRegionCacheSetResponse} from '../src'; +import {CacheGetResponse} from '@gomomento/sdk'; + +const {multiRegionClient, regionalClients, cacheName} = SetupIntegrationTest(); + +describe('simple get and set', () => { + it('happy path set get', async () => { + const key = v4(); + const value = v4(); + + const multiRegionSetResponse = await multiRegionClient.set( + cacheName, + key, + value + ); + + expect(multiRegionSetResponse.type).toEqual( + MultiRegionCacheSetResponse.Success + ); + + for (const [_, client] of Object.entries(regionalClients)) { + const getResponse = await client.get(cacheName, key); + expect(getResponse.type).toEqual(CacheGetResponse.Hit); + expect(getResponse.value()).toEqual(value); + } + }); + it("has an error when writing to a cache that doesn't exist", async () => { + const key = v4(); + const value = v4(); + const nonExistentCacheName = v4(); + + const multiRegionSetResponse = await multiRegionClient.set( + nonExistentCacheName, + key, + value + ); + + expect(multiRegionSetResponse.type).toEqual( + MultiRegionCacheSetResponse.Error + ); + }); +}); diff --git a/test/sorted-set.test.ts b/test/sorted-set.test.ts new file mode 100644 index 0000000..acb927d --- /dev/null +++ b/test/sorted-set.test.ts @@ -0,0 +1,51 @@ +import {v4} from 'uuid'; +import {SetupIntegrationTest} from './integration-setup'; +import {MultiRegionCacheSortedSetPutElementsResponse} from '../src'; +import {CacheSortedSetFetchResponse} from '@gomomento/sdk'; + +const {multiRegionClient, regionalClients, cacheName} = SetupIntegrationTest(); + +describe('sorted set put elements', () => { + it('happy path put elements', async () => { + const sortedSetName = v4(); + + const items = {element1: 1, element2: 2}; + const multiRegionSetResponse = await multiRegionClient.sortedSetPutElements( + cacheName, + sortedSetName, + items + ); + + expect(multiRegionSetResponse.type).toEqual( + MultiRegionCacheSortedSetPutElementsResponse.Success + ); + + const expectedItems = Object.entries(items).map(([value, score]) => ({ + value, + score, + })); + for (const [_, client] of Object.entries(regionalClients)) { + const getResponse = await client.sortedSetFetchByRank( + cacheName, + sortedSetName + ); + expect(getResponse.type).toEqual(CacheSortedSetFetchResponse.Hit); + expect(getResponse.value()).toEqual(expectedItems); + } + }); + it("has an error when writing to a cache that doesn't exist", async () => { + const sortedSetName = v4(); + const items = {element1: 1, element2: 2}; + const nonExistentCacheName = v4(); + + const multiRegionSetResponse = await multiRegionClient.sortedSetPutElements( + nonExistentCacheName, + sortedSetName, + items + ); + + expect(multiRegionSetResponse.type).toEqual( + MultiRegionCacheSortedSetPutElementsResponse.Error + ); + }); +});