|
| 1 | +import { marshallInput } from "../../../lib/lib-dynamodb/src/commands/utils"; |
| 2 | +import { marshall } from "./marshall"; |
| 3 | +import { convertToAttr } from "./convertToAttr"; |
| 4 | + |
| 5 | +describe("marshall integration", () => { |
| 6 | + describe("behaves as convertToAttr for non-collection values or Sets", () => { |
| 7 | + it("marshals string", () => { |
| 8 | + const value = "hello"; |
| 9 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 10 | + }); |
| 11 | + it("marshals number", () => { |
| 12 | + const value = 1578; |
| 13 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 14 | + }); |
| 15 | + it("marshals binary", () => { |
| 16 | + const value = new Uint8Array([0, 1, 0, 1]); |
| 17 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 18 | + }); |
| 19 | + it("marshals boolean", () => { |
| 20 | + let value = false; |
| 21 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 22 | + value = true; |
| 23 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 24 | + }); |
| 25 | + it("marshals null", () => { |
| 26 | + const value = null; |
| 27 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 28 | + }); |
| 29 | + it("marshals string set", () => { |
| 30 | + const value = new Set(["a", "b"]); |
| 31 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 32 | + }); |
| 33 | + it("marshals number set", () => { |
| 34 | + const value = new Set([1, 2]); |
| 35 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 36 | + }); |
| 37 | + it("marshals binary set", () => { |
| 38 | + const value = new Set([new Uint8Array([1, 0]), new Uint8Array([0, 1])]); |
| 39 | + expect(marshall(value)).toEqual(convertToAttr(value)); |
| 40 | + }); |
| 41 | + }); |
| 42 | + describe("unwraps one level for input data which are lists or maps", () => { |
| 43 | + it("marshals and unwraps map", () => { |
| 44 | + expect(marshall({ a: 1, b: { a: 2, b: [1, 2, 3] } })).toEqual({ |
| 45 | + a: { N: "1" }, |
| 46 | + b: { |
| 47 | + M: { |
| 48 | + a: { N: "2" }, |
| 49 | + b: { |
| 50 | + L: [{ N: "1" }, { N: "2" }, { N: "3" }], |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + it("marshals and unwraps list", () => { |
| 57 | + expect(marshall(["test", 2, null])).toEqual([{ S: "test" }, { N: "2" }, { NULL: true }]); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("marshallInput command integration", () => { |
| 63 | + it("marshals QueryCommand input", () => { |
| 64 | + const input = { |
| 65 | + TableName: "TestTable", |
| 66 | + KeyConditions: { |
| 67 | + id: { |
| 68 | + AttributeValueList: ["test"], |
| 69 | + ComparisonOperator: "EQ", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }; |
| 73 | + const inputKeyNodes = [ |
| 74 | + { |
| 75 | + key: "KeyConditions", |
| 76 | + children: { |
| 77 | + children: [{ key: "AttributeValueList" }], |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + key: "QueryFilter", |
| 82 | + children: { |
| 83 | + children: [{ key: "AttributeValueList" }], |
| 84 | + }, |
| 85 | + }, |
| 86 | + { key: "ExclusiveStartKey" }, |
| 87 | + { key: "ExpressionAttributeValues" }, |
| 88 | + ]; |
| 89 | + const output = { |
| 90 | + TableName: "TestTable", |
| 91 | + KeyConditions: { id: { AttributeValueList: [{ S: "test" }], ComparisonOperator: "EQ" } }, |
| 92 | + QueryFilter: undefined, |
| 93 | + ExclusiveStartKey: undefined, |
| 94 | + ExpressionAttributeValues: undefined, |
| 95 | + }; |
| 96 | + expect(marshallInput(input, inputKeyNodes)).toEqual(output); |
| 97 | + }); |
| 98 | + it("marshals ExecuteStatementCommand input", () => { |
| 99 | + const input = { |
| 100 | + Statement: `SELECT col_1 |
| 101 | + FROM some_table |
| 102 | + WHERE contains("col_1", ?)`, |
| 103 | + Parameters: ["some_param"], |
| 104 | + }; |
| 105 | + const inputKeyNodes = [{ key: "Parameters" }]; |
| 106 | + const output = { |
| 107 | + Statement: input.Statement, |
| 108 | + Parameters: [{ S: "some_param" }], |
| 109 | + }; |
| 110 | + expect(marshallInput(input, inputKeyNodes)).toEqual(output); |
| 111 | + }); |
| 112 | + it("marshals BatchExecuteStatementCommand input", () => { |
| 113 | + const input = { |
| 114 | + Statements: [ |
| 115 | + { |
| 116 | + Statement: ` |
| 117 | + UPDATE "table" |
| 118 | + SET field1=? |
| 119 | + WHERE field2=? |
| 120 | + AND field3=? |
| 121 | + `, |
| 122 | + Parameters: [false, "field 2 value", 1234], |
| 123 | + }, |
| 124 | + ], |
| 125 | + }; |
| 126 | + const inputKeyNodes = [{ key: "Statements", children: [{ key: "Parameters" }] }]; |
| 127 | + const output = { |
| 128 | + Statements: [ |
| 129 | + { |
| 130 | + Statement: input.Statements[0].Statement, |
| 131 | + Parameters: [ |
| 132 | + { |
| 133 | + BOOL: false, |
| 134 | + }, |
| 135 | + { S: "field 2 value" }, |
| 136 | + { N: "1234" }, |
| 137 | + ], |
| 138 | + }, |
| 139 | + ], |
| 140 | + }; |
| 141 | + expect(marshallInput(input, inputKeyNodes)).toEqual(output); |
| 142 | + }); |
| 143 | +}); |
0 commit comments