Skip to content

Commit db215dd

Browse files
authored
tests(parameters): additional integration tests to DynamoDBProvider (#1285)
1 parent 6b08fb6 commit db215dd

File tree

2 files changed

+102
-117
lines changed

2 files changed

+102
-117
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Context } from 'aws-lambda';
22
import { DynamoDBProvider } from '../../src/dynamodb';
3+
import {
4+
DynamoDBGetOptionsInterface,
5+
DynamoDBGetMultipleOptionsInterface,
6+
} from '../../src/types';
37
import { TinyLogger } from '../helpers/tinyLogger';
4-
// # TODO: Uncomment code below once #1222 is fixed
5-
/*
68
import { middleware } from '../helpers/sdkMiddlewareRequestCounter';
79
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
8-
*/
10+
11+
// We use a custom logger to log pure JSON objects to stdout
12+
const logger = new TinyLogger();
913

1014
const tableGet = process.env.TABLE_GET ?? 'my-table';
1115
const tableGetMultiple = process.env.TABLE_GET_MULTIPLE ?? 'my-table';
@@ -15,9 +19,6 @@ const keyAttr = process.env.KEY_ATTR ?? 'id';
1519
const sortAttr = process.env.SORT_ATTR ?? 'sk';
1620
const valueAttr = process.env.VALUE_ATTR ?? 'value';
1721

18-
// We use a custom logger to log pure JSON objects to stdout
19-
const logger = new TinyLogger();
20-
2122
// Provider test 1, 5, 6
2223
const providerGet = new DynamoDBProvider({
2324
tableName: tableGet,
@@ -39,163 +40,122 @@ const providerGetMultipleCustomKeys = new DynamoDBProvider({
3940
sortAttr,
4041
valueAttr,
4142
});
42-
// # TODO: Uncomment code below once #1222 is fixed
43-
/*
43+
4444
// Provider test 8, 9
4545
const customClient = new DynamoDBClient({});
46-
providerWithMiddleware.middlewareStack.use(middleware);
46+
customClient.middlewareStack.use(middleware);
4747
const providerWithMiddleware = new DynamoDBProvider({
48-
awsSdkV3Client: customClient
48+
awsSdkV3Client: customClient,
49+
tableName: tableGet,
4950
});
50-
*/
5151

52-
export const handler = async (_event: unknown, _context: Context): Promise<void> => {
53-
// Test 1 - get a single parameter with default options (keyAttr: 'id', valueAttr: 'value')
52+
// Helper function to call get() and log the result
53+
const _call_get = async (
54+
paramName: string,
55+
testName: string,
56+
provider: DynamoDBProvider,
57+
options?: DynamoDBGetOptionsInterface,
58+
): Promise<void> => {
5459
try {
55-
const parameterValue = await providerGet.get('my-param');
60+
const parameterValue = await provider.get(paramName, options);
5661
logger.log({
57-
test: 'get',
62+
test: testName,
5863
value: parameterValue
5964
});
6065
} catch (err) {
6166
logger.log({
62-
test: 'get',
67+
test: testName,
6368
error: err.message
6469
});
6570
}
71+
};
6672

67-
// Test 2 - get multiple parameters with default options (keyAttr: 'id', sortAttr: 'sk', valueAttr: 'value')
73+
// Helper function to call getMultiple() and log the result
74+
const _call_get_multiple = async (
75+
paramPath: string,
76+
testName: string,
77+
provider: DynamoDBProvider,
78+
options?: DynamoDBGetMultipleOptionsInterface,
79+
): Promise<void> => {
6880
try {
69-
const parametersValues = await providerGetMultiple.getMultiple('my-params');
81+
const parameterValues = await provider.getMultiple(
82+
paramPath,
83+
options
84+
);
7085
logger.log({
71-
test: 'get-multiple',
72-
value: parametersValues
86+
test: testName,
87+
value: parameterValues
7388
});
7489
} catch (err) {
7590
logger.log({
76-
test: 'get-multiple',
91+
test: testName,
7792
error: err.message
7893
});
7994
}
95+
};
96+
97+
export const handler = async (_event: unknown, _context: Context): Promise<void> => {
98+
// Test 1 - get a single parameter with default options (keyAttr: 'id', valueAttr: 'value')
99+
await _call_get('my-param', 'get', providerGet);
100+
101+
// Test 2 - get multiple parameters with default options (keyAttr: 'id', sortAttr: 'sk', valueAttr: 'value')
102+
await _call_get_multiple('my-params', 'get-multiple', providerGetMultiple);
80103

81104
// Test 3 - get a single parameter with custom options (keyAttr: 'key', valueAttr: 'val')
82-
try {
83-
const parameterValueCustom = await providerGetCustomKeys.get('my-param');
84-
logger.log({
85-
test: 'get-custom',
86-
value: parameterValueCustom
87-
});
88-
} catch (err) {
89-
logger.log({
90-
test: 'get-custom',
91-
error: err.message
92-
});
93-
}
105+
await _call_get('my-param', 'get-custom', providerGetCustomKeys);
94106

95107
// Test 4 - get multiple parameters with custom options (keyAttr: 'key', sortAttr: 'sort', valueAttr: 'val')
96-
try {
97-
const parametersValuesCustom = await providerGetMultipleCustomKeys.getMultiple('my-params');
98-
logger.log({
99-
test: 'get-multiple-custom',
100-
value: parametersValuesCustom
101-
});
102-
} catch (err) {
103-
logger.log({
104-
test: 'get-multiple-custom',
105-
error: err.message
106-
});
107-
}
108+
await _call_get_multiple('my-params', 'get-multiple-custom', providerGetMultipleCustomKeys);
108109

109110
// Test 5 - get a single parameter with json transform
110-
try {
111-
const parameterValueJson = await providerGet.get('my-param-json', {
112-
transform: 'json'
113-
});
114-
logger.log({
115-
test: 'get-json-transform',
116-
value: typeof parameterValueJson // should be object
117-
});
118-
} catch (err) {
119-
logger.log({
120-
test: 'get-json-transform',
121-
error: err.message
122-
});
123-
}
111+
await _call_get('my-param-json', 'get-json-transform', providerGet, {
112+
transform: 'json'
113+
});
124114

125115
// Test 6 - get a single parameter with binary transform
126-
try {
127-
const parameterValueBinary = await providerGet.get('my-param-binary', {
128-
transform: 'binary'
129-
});
130-
logger.log({
131-
test: 'get-binary-transform',
132-
value: typeof parameterValueBinary // should be string
133-
});
134-
} catch (err) {
135-
logger.log({
136-
test: 'get-binary-transform',
137-
error: err.message
138-
});
139-
}
116+
await _call_get('my-param-binary', 'get-binary-transform', providerGet, {
117+
transform: 'binary'
118+
});
140119

141120
// Test 7 - get multiple parameters with auto transform
142-
try {
143-
const parametersValuesAuto = await providerGetMultiple.getMultiple('my-encoded-params', {
144-
transform: 'auto'
145-
});
146-
if (!parametersValuesAuto) throw new Error('parametersValuesAuto is undefined');
147-
148-
logger.log({
149-
test: 'get-multiple-auto-transform',
150-
value:
151-
`${typeof parametersValuesAuto['config.json']},${typeof parametersValuesAuto['key.binary']}` // should be object,string
152-
});
153-
} catch (err) {
154-
logger.log({
155-
test: 'get-multiple-auto-transform',
156-
error: err.message
157-
});
158-
}
121+
await _call_get_multiple('my-encoded-params', 'get-multiple-auto-transform', providerGetMultiple, {
122+
transform: 'auto'
123+
});
159124

160-
// # TODO: Uncomment code below once #1222 is fixed
161-
/**
162-
* Test 8 - get a parameter twice, second time should be cached
163-
*
164-
* Should only make 1 request, we use middleware to count requests
165-
*/
166-
/*
125+
// Test 8
126+
// get parameter twice with middleware, which counts the number of requests, we check later if we only called DynamoDB once
167127
try {
128+
providerWithMiddleware.clearCache();
129+
middleware.counter = 0;
168130
await providerWithMiddleware.get('my-param');
169131
await providerWithMiddleware.get('my-param');
170132
logger.log({
171-
test: 'get-cache-request-count',
172-
value: middleware.requestCount
133+
test: 'get-cached',
134+
value: middleware.counter // should be 1
173135
});
174136
} catch (err) {
175137
logger.log({
176-
test: 'get-cache-request-count',
138+
test: 'get-cached',
177139
error: err.message
178140
});
179141
}
180-
*/
181142

182-
/**
183-
* Test 9 - get a parameter once more but with forceFetch = true
184-
*
185-
* Request count should increase to 2, we use middleware to count requests
186-
*/
187-
/*
143+
// Test 9
144+
// get parameter twice, but force fetch 2nd time, we count number of SDK requests and check that we made two API calls
188145
try {
146+
providerWithMiddleware.clearCache();
147+
middleware.counter = 0;
148+
await providerWithMiddleware.get('my-param');
189149
await providerWithMiddleware.get('my-param', { forceFetch: true });
190150
logger.log({
191-
test: 'get-force-fetch-request-count',
192-
value: middleware.requestCount
151+
test: 'get-forced',
152+
value: middleware.counter // should be 2
193153
});
194154
} catch (err) {
195155
logger.log({
196-
test: 'get-force-fetch-request-count',
156+
test: 'get-forced',
197157
error: err.message
198158
});
199159
}
200-
*/
160+
201161
};

Diff for: packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe(`parameters E2E tests (dynamoDBProvider) for runtime: ${runtime}`, () =
374374

375375
expect(testLog).toStrictEqual({
376376
test: 'get-json-transform',
377-
value: 'object',
377+
value: { foo: 'bar' },
378378
});
379379

380380
});
@@ -387,7 +387,7 @@ describe(`parameters E2E tests (dynamoDBProvider) for runtime: ${runtime}`, () =
387387

388388
expect(testLog).toStrictEqual({
389389
test: 'get-binary-transform',
390-
value: 'string', // as opposed to Uint8Array
390+
value: 'baz',
391391
});
392392

393393
});
@@ -400,15 +400,40 @@ describe(`parameters E2E tests (dynamoDBProvider) for runtime: ${runtime}`, () =
400400

401401
expect(testLog).toStrictEqual({
402402
test: 'get-multiple-auto-transform',
403-
value: 'object,string',
403+
value: {
404+
'config.json': { foo: 'bar' },
405+
'key.binary': 'baz',
406+
},
407+
});
408+
409+
});
410+
411+
// Test 8 - Get a parameter twice and check that the value is cached.
412+
it('should retrieve multiple parameters with auto transforms', async () => {
413+
414+
const logs = invocationLogs[0].getFunctionLogs();
415+
const testLog = InvocationLogs.parseFunctionLog(logs[7]);
416+
417+
expect(testLog).toStrictEqual({
418+
test: 'get-cached',
419+
value: 1,
404420
});
405421

406422
});
407423

408-
// TODO: implement tests for the following cases once #1222 is merged:
409-
// Test 8 - get a parameter twice, second time should be cached
410-
// Test 9 - get a parameter once more but with forceFetch = true
424+
// Test 9 - Get a cached parameter and force retrieval.
425+
it('should retrieve multiple parameters with auto transforms', async () => {
411426

427+
const logs = invocationLogs[0].getFunctionLogs();
428+
const testLog = InvocationLogs.parseFunctionLog(logs[8]);
429+
430+
expect(testLog).toStrictEqual({
431+
test: 'get-forced',
432+
value: 2,
433+
});
434+
435+
});
436+
412437
});
413438

414439
afterAll(async () => {

0 commit comments

Comments
 (0)