|
1 | 1 | import { describe, expect, it } from 'vitest';
|
2 | 2 | import { z } from 'zod';
|
3 | 3 | import { JSONStringified } from '../../src/helpers.js';
|
| 4 | +import { DynamoDBMarshalled } from '../../src/helpers/dynamodb.js'; |
4 | 5 | import { AlbSchema } from '../../src/schemas/alb.js';
|
| 6 | +import { |
| 7 | + DynamoDBStreamRecord, |
| 8 | + DynamoDBStreamSchema, |
| 9 | +} from '../../src/schemas/dynamodb'; |
5 | 10 | import {
|
6 | 11 | SnsNotificationSchema,
|
7 | 12 | SnsRecordSchema,
|
8 | 13 | } from '../../src/schemas/sns.js';
|
9 | 14 | import { SqsRecordSchema, SqsSchema } from '../../src/schemas/sqs.js';
|
10 |
| -import type { SnsEvent, SqsEvent } from '../../src/types/schema.js'; |
| 15 | +import type { |
| 16 | + DynamoDBStreamEvent, |
| 17 | + SnsEvent, |
| 18 | + SqsEvent, |
| 19 | +} from '../../src/types/schema.js'; |
11 | 20 | import { getTestEvent } from './schema/utils.js';
|
12 | 21 |
|
13 | 22 | const bodySchema = z.object({
|
@@ -152,3 +161,145 @@ describe('JSONStringified', () => {
|
152 | 161 | });
|
153 | 162 | });
|
154 | 163 | });
|
| 164 | + |
| 165 | +describe('DynamoDBMarshalled', () => { |
| 166 | + // Prepare |
| 167 | + const schema = z.object({ |
| 168 | + Message: z.string(), |
| 169 | + Id: z.number(), |
| 170 | + }); |
| 171 | + |
| 172 | + const extendedSchema = DynamoDBStreamSchema.extend({ |
| 173 | + Records: z.array( |
| 174 | + DynamoDBStreamRecord.extend({ |
| 175 | + dynamodb: z.object({ |
| 176 | + NewImage: DynamoDBMarshalled(schema).optional(), |
| 177 | + }), |
| 178 | + }) |
| 179 | + ), |
| 180 | + }); |
| 181 | + |
| 182 | + it('should correctly unmarshall and validate a valid DynamoDB stream record', () => { |
| 183 | + // Prepare |
| 184 | + const testInput = [ |
| 185 | + { |
| 186 | + Message: { |
| 187 | + S: 'New item!', |
| 188 | + }, |
| 189 | + Id: { |
| 190 | + N: '101', |
| 191 | + }, |
| 192 | + }, |
| 193 | + { |
| 194 | + Message: { |
| 195 | + S: 'This item has changed', |
| 196 | + }, |
| 197 | + Id: { |
| 198 | + N: '101', |
| 199 | + }, |
| 200 | + }, |
| 201 | + ]; |
| 202 | + const expectedOutput = [ |
| 203 | + { |
| 204 | + Id: 101, |
| 205 | + Message: 'New item!', |
| 206 | + }, |
| 207 | + { |
| 208 | + Id: 101, |
| 209 | + Message: 'This item has changed', |
| 210 | + }, |
| 211 | + ]; |
| 212 | + |
| 213 | + const testEvent = getTestEvent<DynamoDBStreamEvent>({ |
| 214 | + eventsPath: '.', |
| 215 | + filename: 'dynamoStreamEvent', |
| 216 | + }); |
| 217 | + |
| 218 | + testEvent.Records[0].dynamodb.NewImage = testInput[0]; |
| 219 | + testEvent.Records[1].dynamodb.NewImage = testInput[1]; |
| 220 | + |
| 221 | + // Act & Assess |
| 222 | + expect(extendedSchema.parse(testEvent)).toStrictEqual({ |
| 223 | + Records: [ |
| 224 | + { |
| 225 | + ...testEvent.Records[0], |
| 226 | + dynamodb: { |
| 227 | + NewImage: expectedOutput[0], |
| 228 | + }, |
| 229 | + }, |
| 230 | + { |
| 231 | + ...testEvent.Records[1], |
| 232 | + dynamodb: { |
| 233 | + NewImage: expectedOutput[1], |
| 234 | + }, |
| 235 | + }, |
| 236 | + ], |
| 237 | + }); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should throw an error if the DynamoDB stream record cannot be unmarshalled', () => { |
| 241 | + // Prepare |
| 242 | + const testInput = [ |
| 243 | + { |
| 244 | + Message: { |
| 245 | + S: 'New item!', |
| 246 | + }, |
| 247 | + Id: { |
| 248 | + NNN: '101', //unknown type |
| 249 | + }, |
| 250 | + }, |
| 251 | + { |
| 252 | + Message: { |
| 253 | + S: 'This item has changed', |
| 254 | + }, |
| 255 | + Id: { |
| 256 | + N: '101', |
| 257 | + }, |
| 258 | + }, |
| 259 | + ]; |
| 260 | + |
| 261 | + const testEvent = getTestEvent<DynamoDBStreamEvent>({ |
| 262 | + eventsPath: '.', |
| 263 | + filename: 'dynamoStreamEvent', |
| 264 | + }); |
| 265 | + |
| 266 | + testEvent.Records[0].dynamodb.NewImage = testInput[0]; |
| 267 | + testEvent.Records[1].dynamodb.NewImage = testInput[1]; |
| 268 | + |
| 269 | + // Act & Assess |
| 270 | + expect(() => extendedSchema.parse(testEvent)).toThrow( |
| 271 | + 'Could not unmarshall DynamoDB stream record' |
| 272 | + ); |
| 273 | + }); |
| 274 | + |
| 275 | + it('should throw a validation error if the unmarshalled record does not match the schema', () => { |
| 276 | + // Prepare |
| 277 | + const testInput = [ |
| 278 | + { |
| 279 | + Message: { |
| 280 | + S: 'New item!', |
| 281 | + }, |
| 282 | + Id: { |
| 283 | + N: '101', |
| 284 | + }, |
| 285 | + }, |
| 286 | + { |
| 287 | + Message: { |
| 288 | + S: 'This item has changed', |
| 289 | + }, |
| 290 | + // Id is missing |
| 291 | + }, |
| 292 | + ]; |
| 293 | + |
| 294 | + const testEvent = getTestEvent<DynamoDBStreamEvent>({ |
| 295 | + eventsPath: '.', |
| 296 | + filename: 'dynamoStreamEvent', |
| 297 | + }); |
| 298 | + |
| 299 | + testEvent.Records[0].dynamodb.NewImage = testInput[0]; |
| 300 | + testEvent.Records[1].dynamodb.NewImage = testInput[1]; |
| 301 | + |
| 302 | + // Act & Assess |
| 303 | + expect(() => extendedSchema.parse(testEvent)).toThrow(); |
| 304 | + }); |
| 305 | +}); |
0 commit comments