|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + UnmarshallDynamoDBAttributeError, |
| 4 | + unmarshallDynamoDB, |
| 5 | +} from '../../src/unmarshallDynamoDB.js'; |
| 6 | + |
| 7 | +describe('Function: unmarshallDynamoDB', () => { |
| 8 | + it('unmarshalls a DynamoDB string attribute', () => { |
| 9 | + // Prepare |
| 10 | + const value = { Message: { S: 'test string' } }; |
| 11 | + |
| 12 | + // Act |
| 13 | + const result = unmarshallDynamoDB(value); |
| 14 | + |
| 15 | + // Assess |
| 16 | + expect(result).toStrictEqual({ Message: 'test string' }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('unmarshalls a DynamoDB number attribute', () => { |
| 20 | + // Prepare |
| 21 | + const value = { Id: { N: '123' } }; |
| 22 | + |
| 23 | + // Act |
| 24 | + const result = unmarshallDynamoDB(value); |
| 25 | + |
| 26 | + // Assess |
| 27 | + expect(result).toStrictEqual({ Id: 123 }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('unmarshalls a DynamoDB boolean attribute', () => { |
| 31 | + // Prepare |
| 32 | + const value = { Message: { BOOL: true } }; |
| 33 | + |
| 34 | + // Act |
| 35 | + const result = unmarshallDynamoDB(value); |
| 36 | + |
| 37 | + // Assess |
| 38 | + expect(result).toStrictEqual({ Message: true }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('unmarshalls a DynamoDB null attribute', () => { |
| 42 | + // Prepare |
| 43 | + const value = { Message: { NULL: true } }; |
| 44 | + |
| 45 | + // Act |
| 46 | + const result = unmarshallDynamoDB(value); |
| 47 | + |
| 48 | + // Assess |
| 49 | + expect(result).toStrictEqual({ Message: null }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('unmarshalls a DynamoDB list attribute', () => { |
| 53 | + // Prepare |
| 54 | + const value = { |
| 55 | + Messages: { |
| 56 | + L: [{ S: 'string' }, { N: '123' }, { BOOL: true }, { NULL: true }], |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + // Act |
| 61 | + const result = unmarshallDynamoDB(value); |
| 62 | + |
| 63 | + // Assess |
| 64 | + expect(result).toStrictEqual({ Messages: ['string', 123, true, null] }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('unmarshalls a DynamoDB map attribute', () => { |
| 68 | + // Prepare |
| 69 | + const value = { |
| 70 | + Settings: { |
| 71 | + M: { |
| 72 | + string: { S: 'test' }, |
| 73 | + number: { N: '123' }, |
| 74 | + boolean: { BOOL: true }, |
| 75 | + null: { NULL: true }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + // Act |
| 81 | + const result = unmarshallDynamoDB(value); |
| 82 | + |
| 83 | + // Assess |
| 84 | + expect(result).toStrictEqual({ |
| 85 | + Settings: { |
| 86 | + string: 'test', |
| 87 | + number: 123, |
| 88 | + boolean: true, |
| 89 | + null: null, |
| 90 | + }, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('unmarshalls a DynamoDB string set attribute', () => { |
| 95 | + // Prepare |
| 96 | + const value = { Messages: { SS: ['a', 'b', 'c'] } }; |
| 97 | + |
| 98 | + // Act |
| 99 | + const result = unmarshallDynamoDB(value); |
| 100 | + |
| 101 | + // Assess |
| 102 | + expect(result).toStrictEqual({ Messages: new Set(['a', 'b', 'c']) }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('unmarshalls a DynamoDB number set attribute', () => { |
| 106 | + // Prepare |
| 107 | + const value = { Ids: { NS: ['1', '2', '3'] } }; |
| 108 | + |
| 109 | + // Act |
| 110 | + const result = unmarshallDynamoDB(value); |
| 111 | + |
| 112 | + // Assess |
| 113 | + expect(result).toStrictEqual({ Ids: new Set([1, 2, 3]) }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('unmarshalls nested DynamoDB structures', () => { |
| 117 | + // Prepare |
| 118 | + const value = { |
| 119 | + Messages: { |
| 120 | + M: { |
| 121 | + nested: { |
| 122 | + M: { |
| 123 | + list: { |
| 124 | + L: [ |
| 125 | + { S: 'string' }, |
| 126 | + { |
| 127 | + M: { |
| 128 | + key: { S: 'value' }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + // Act |
| 140 | + const result = unmarshallDynamoDB(value); |
| 141 | + |
| 142 | + // Assess |
| 143 | + expect(result).toStrictEqual({ |
| 144 | + Messages: { |
| 145 | + nested: { |
| 146 | + list: ['string', { key: 'value' }], |
| 147 | + }, |
| 148 | + }, |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('unmarshalls a DynamoDB binary attribute', () => { |
| 153 | + // Prepare |
| 154 | + const value = { |
| 155 | + Data: { |
| 156 | + B: new Uint8Array( |
| 157 | + Buffer.from('dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk', 'base64') |
| 158 | + ), |
| 159 | + }, |
| 160 | + }; |
| 161 | + |
| 162 | + // Act |
| 163 | + const result = unmarshallDynamoDB(value); |
| 164 | + |
| 165 | + // Assess |
| 166 | + expect(result).toStrictEqual({ Data: expect.any(Uint8Array) }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('unmarshalls a DynamoDB binary set attribute', () => { |
| 170 | + // Prepare |
| 171 | + const value = { |
| 172 | + Data: { |
| 173 | + BS: [ |
| 174 | + new Uint8Array( |
| 175 | + Buffer.from('dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk', 'base64') |
| 176 | + ), |
| 177 | + new Uint8Array( |
| 178 | + Buffer.from('dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk', 'base64') |
| 179 | + ), |
| 180 | + ], |
| 181 | + }, |
| 182 | + }; |
| 183 | + |
| 184 | + // Act |
| 185 | + const result = unmarshallDynamoDB(value); |
| 186 | + |
| 187 | + // Assess |
| 188 | + expect(result).toStrictEqual({ Data: expect.any(Set) }); |
| 189 | + }); |
| 190 | + |
| 191 | + it('throws if an unsupported type is passed', () => { |
| 192 | + // Prepare |
| 193 | + const value = { Message: { NNN: '123' } }; |
| 194 | + |
| 195 | + // Act & Assess |
| 196 | + // @ts-expect-error - Intentionally invalid value |
| 197 | + expect(() => unmarshallDynamoDB(value)).toThrow( |
| 198 | + UnmarshallDynamoDBAttributeError |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it('unmarshalls a DynamoDB large number attribute to BigInt', () => { |
| 203 | + // Prepare |
| 204 | + const value = { Balance: { N: '9007199254740992' } }; // Number.MAX_SAFE_INTEGER + 1 |
| 205 | + |
| 206 | + // Act |
| 207 | + const result = unmarshallDynamoDB(value); |
| 208 | + |
| 209 | + // Assess |
| 210 | + expect(result).toStrictEqual({ Balance: BigInt('9007199254740992') }); |
| 211 | + }); |
| 212 | + |
| 213 | + it('unmarshalls a DynamoDB negative large number attribute to BigInt', () => { |
| 214 | + // Prepare |
| 215 | + const value = { Balance: { N: '-9007199254740992' } }; // Number.MIN_SAFE_INTEGER - 1 |
| 216 | + |
| 217 | + // Act |
| 218 | + const result = unmarshallDynamoDB(value); |
| 219 | + |
| 220 | + // Assess |
| 221 | + expect(result).toStrictEqual({ Balance: BigInt('-9007199254740992') }); |
| 222 | + }); |
| 223 | + |
| 224 | + it('throws when trying to convert an invalid number string to BigInt', () => { |
| 225 | + // Prepare |
| 226 | + const value = { Balance: { N: '9007199254740992.5' } }; // Invalid BigInt string (decimals not allowed) |
| 227 | + |
| 228 | + // Act & Assess |
| 229 | + expect(() => unmarshallDynamoDB(value)).toThrow( |
| 230 | + new UnmarshallDynamoDBAttributeError( |
| 231 | + "9007199254740992.5 can't be converted to BigInt" |
| 232 | + ) |
| 233 | + ); |
| 234 | + }); |
| 235 | + |
| 236 | + it('throws when no data is found', () => { |
| 237 | + // Prepare |
| 238 | + const value = undefined; |
| 239 | + |
| 240 | + // Act & Assess |
| 241 | + // @ts-expect-error - Intentionally invalid value |
| 242 | + expect(() => unmarshallDynamoDB(value)).toThrow( |
| 243 | + UnmarshallDynamoDBAttributeError |
| 244 | + ); |
| 245 | + }); |
| 246 | +}); |
0 commit comments