Skip to content

Commit

Permalink
test: add basic integration tests for scalar and sorted set (#14)
Browse files Browse the repository at this point in the history
Adds integration tests for scalar set and sorted set put elements.
  • Loading branch information
malandis authored Dec 20, 2024
1 parent 72ff47d commit 1a9f2a1
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
32 changes: 24 additions & 8 deletions test/integration-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Configurations,
CreateCache,
CredentialProvider,
DeleteCache,
DeleteCacheResponse,
ICacheClient,
MomentoErrorCode,
} from '@gomomento/sdk';
import {
Expand All @@ -32,7 +33,8 @@ function regionalMomentoClientForTesting(regionEnvVarName: string) {
}

export function SetupIntegrationTest(): {
client: IMultiRegionCacheWriterClient;
multiRegionClient: IMultiRegionCacheWriterClient;
regionalClients: Record<string, ICacheClient>;
cacheName: string;
} {
const cacheName = testCacheName();
Expand Down Expand Up @@ -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<string, CredentialProvider>
);
const multiRegionClient = new MultiRegionCacheWriterClient({
credentialProviders,
configuration: Configurations.Laptop.latest(),
defaultTtlSeconds: 60,
});

const regionalClients: Record<string, ICacheClient> = {};
for (const regionEnvVarName of regionEnvVarNames) {
const regionalClient = regionalMomentoClientForTesting(regionEnvVarName);
regionalClients[regionEnvVarName] = regionalClient;
}

return {
client,
multiRegionClient,
regionalClients,
cacheName,
};
}
44 changes: 44 additions & 0 deletions test/scalar.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});
51 changes: 51 additions & 0 deletions test/sorted-set.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});

0 comments on commit 1a9f2a1

Please sign in to comment.