forked from aws/aws-sdk-js-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertToNative.spec.ts
265 lines (240 loc) · 9.59 KB
/
convertToNative.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { AttributeValue } from "@aws-sdk/client-dynamodb";
import { convertToNative } from "./convertToNative";
import { NativeAttributeValue } from "./models";
describe("convertToNative", () => {
describe("null", () => {
it(`returns for null`, () => {
expect(convertToNative({ NULL: true })).toEqual(null);
});
});
describe("boolean", () => {
[true, false].forEach((bool) => {
it(`returns for boolean: ${bool}`, () => {
expect(convertToNative({ BOOL: bool })).toEqual(bool);
});
});
});
describe("number", () => {
const wrapNumbers = true;
[1, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER]
.map((num) => num.toString())
.forEach((numString) => {
it(`returns for number (integer): ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(Number(numString));
});
it(`returns NumberValue for number (integer) with options.wrapNumbers=true: ${numString}`, () => {
expect(convertToNative({ N: numString }, { wrapNumbers })).toEqual({ value: numString });
});
});
[1.01, Math.PI, Math.E, Number.MIN_VALUE, Number.EPSILON]
.map((num) => num.toString())
.forEach((numString) => {
it(`returns for number (floating point): ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(Number(numString));
});
it(`returns NumberValue for number (floating point) with options.wrapNumbers=true: ${numString}`, () => {
expect(convertToNative({ N: numString }, { wrapNumbers })).toEqual({ value: numString });
});
});
[Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
.map((num) => num.toString())
.forEach((numString) => {
it(`returns for number (special numeric value): ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(Number(numString));
});
});
[Number.MAX_SAFE_INTEGER + 1, Number.MAX_VALUE, Number.MIN_SAFE_INTEGER - 1]
.map((num) => BigInt(num).toString())
.forEach((numString) => {
it(`returns bigint for numbers outside SAFE_INTEGER range: ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(BigInt(numString));
});
it(`throws error for numbers outside SAFE_INTEGER range when BigInt is not defined: ${numString}`, () => {
const BigIntConstructor = BigInt;
(BigInt as any) = undefined;
expect(() => {
convertToNative({ N: numString });
}).toThrowError(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`);
BigInt = BigIntConstructor;
});
it(`returns NumberValue for numbers outside SAFE_INTEGER range with options.wrapNumbers=true: ${numString}`, () => {
expect(convertToNative({ N: numString }, { wrapNumbers })).toEqual({ value: numString });
});
});
[
`${Number.MAX_SAFE_INTEGER}.1`,
`${Number.MIN_SAFE_INTEGER}.1`,
`${Number.MIN_VALUE}1`,
`-${Number.MIN_VALUE}1`,
].forEach((numString) => {
it(`throws if number is outside IEEE 754 Floating-Point Arithmetic: ${numString}`, () => {
expect(() => {
convertToNative({ N: numString });
}).toThrowError(
`Value ${numString} is outside IEEE 754 Floating-Point Arithmetic. Set options.wrapNumbers to get string value.`
);
});
});
[`${Number.MAX_SAFE_INTEGER + 2}.1`, `${Number.MIN_SAFE_INTEGER - 2}.1`].forEach((numString) => {
it(`throws if number cannot be converted into BigInt: ${numString}`, () => {
expect(() => {
convertToNative({ N: numString });
}).toThrowError(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
});
});
});
describe("binary", () => {
it(`returns for Uint8Array`, () => {
const data = new Uint8Array([...Array(64).keys()]);
expect(convertToNative({ B: data })).toEqual(data);
});
});
describe("string", () => {
["", "string", "'single-quote'", '"double-quote"'].forEach((str) => {
it(`returns for string: ${str}`, () => {
expect(convertToNative({ S: str })).toEqual(str);
});
});
});
describe("list", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
([
{
input: [{ NULL: true }, { BOOL: false }],
output: [null, false],
},
{
input: [{ S: "one" }, { N: "1.01" }, { N: "9007199254740996" }],
output: ["one", 1.01, BigInt(9007199254740996)],
},
{
input: [{ B: uint8Arr1 }, { B: uint8Arr2 }],
output: [uint8Arr1, uint8Arr2],
},
{
input: [
{ M: { nullKey: { NULL: true }, boolKey: { BOOL: false } } },
{ M: { stringKey: { S: "one" }, numberKey: { N: "1.01" }, bigintKey: { N: "9007199254740996" } } },
],
output: [
{ nullKey: null, boolKey: false },
{ stringKey: "one", numberKey: 1.01, bigintKey: BigInt(9007199254740996) },
],
},
{
input: [
{ NS: ["1", "2", "3"] },
{ NS: ["9007199254740996", "-9007199254740996"] },
{ BS: [uint8Arr1, uint8Arr2] },
{ SS: ["one", "two", "three"] },
],
output: [
new Set([1, 2, 3]),
new Set([BigInt(9007199254740996), BigInt(-9007199254740996)]),
new Set([uint8Arr1, uint8Arr2]),
new Set(["one", "two", "three"]),
],
},
] as { input: AttributeValue[]; output: NativeAttributeValue[] }[]).forEach(({ input, output }) => {
it(`testing list: ${JSON.stringify(input)}`, () => {
expect(convertToNative({ L: input })).toEqual(output);
});
});
it(`testing list with options.wrapNumbers=true`, () => {
const input = [{ N: "1.01" }, { N: "9007199254740996" }];
expect(convertToNative({ L: input as AttributeValue[] }, { wrapNumbers: true })).toEqual(
input.map((item) => ({ value: item.N }))
);
});
});
describe("map", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
([
{
input: { nullKey: { NULL: true }, boolKey: { BOOL: false } },
output: { nullKey: null, boolKey: false },
},
{
input: { stringKey: { S: "one" }, numberKey: { N: "1.01" }, bigintKey: { N: "9007199254740996" } },
output: { stringKey: "one", numberKey: 1.01, bigintKey: BigInt(9007199254740996) },
},
{
input: { uint8Arr1Key: { B: uint8Arr1 }, uint8Arr2Key: { B: uint8Arr2 } },
output: { uint8Arr1Key: uint8Arr1, uint8Arr2Key: uint8Arr2 },
},
{
input: {
list1: { L: [{ NULL: true }, { BOOL: false }] },
list2: { L: [{ S: "one" }, { N: "1.01" }, { N: "9007199254740996" }] },
},
output: { list1: [null, false], list2: ["one", 1.01, BigInt(9007199254740996)] },
},
{
input: {
numberSet: { NS: ["1", "2", "3"] },
bigintSet: { NS: ["9007199254740996", "-9007199254740996"] },
binarySet: { BS: [uint8Arr1, uint8Arr2] },
stringSet: { SS: ["one", "two", "three"] },
},
output: {
numberSet: new Set([1, 2, 3]),
bigintSet: new Set([BigInt(9007199254740996), BigInt(-9007199254740996)]),
binarySet: new Set([uint8Arr1, uint8Arr2]),
stringSet: new Set(["one", "two", "three"]),
},
},
] as { input: { [key: string]: AttributeValue }; output: { [key: string]: NativeAttributeValue } }[]).forEach(
({ input, output }) => {
it(`testing map: ${input}`, () => {
expect(convertToNative({ M: input })).toEqual(output);
});
}
);
it(`testing map with options.wrapNumbers=true`, () => {
const input = { numberKey: { N: "1.01" }, bigintKey: { N: "9007199254740996" } };
const output = { numberKey: { value: "1.01" }, bigintKey: { value: "9007199254740996" } };
expect(convertToNative({ M: input }, { wrapNumbers: true })).toEqual(output);
});
});
describe("set", () => {
describe("number set", () => {
const input = ["1", "2", "9007199254740996"];
it("without options.wrapNumbers", () => {
expect(convertToNative({ NS: input })).toEqual(new Set([1, 2, BigInt(9007199254740996)]));
});
it("with options.wrapNumbers=true", () => {
expect(convertToNative({ NS: input }, { wrapNumbers: true })).toEqual(
new Set(input.map((numString) => ({ value: numString })))
);
});
});
it("binary set", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
const input = [uint8Arr1, uint8Arr2];
expect(convertToNative({ BS: input })).toEqual(new Set(input));
});
it("string set", () => {
const input = ["one", "two", "three"];
expect(convertToNative({ SS: input })).toEqual(new Set(input));
});
});
describe(`unsupported type`, () => {
["A", "P", "LS"].forEach((type) => {
it(`throws for unsupported type: ${type}`, () => {
expect(() => {
// @ts-ignore Property '$unknown' is missing in type.
convertToNative({ [type]: "data" });
}).toThrowError(`Unsupported type passed: ${type}`);
});
});
});
it(`no value defined`, () => {
expect(() => {
// @ts-ignore Property '$unknown' is missing in type.
convertToNative({});
}).toThrowError(`No value defined: {}`);
});
});