-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(util-dynamodb): allow marshall function to handle more input types (
#3539) * fix(util-dynamodb): allow marshall function to handle more input types * fix(util-dynamodb): revert marshall.spec.ts and minor formatting * fix(util-dynamodb): code spacing
- Loading branch information
Showing
4 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { marshallInput } from "./utils"; | ||
|
||
describe("marshallInput and processObj", () => { | ||
it("marshallInput should not ignore falsy values", () => { | ||
expect(marshallInput({ Items: [0, false, null, ""] }, [{ key: "Items" }])).toEqual({ | ||
Items: [{ N: "0" }, { BOOL: false }, { NULL: true }, { S: "" }], | ||
}); | ||
}); | ||
}); | ||
|
||
describe("marshallInput for commands", () => { | ||
it("marshals QueryCommand input", () => { | ||
const input = { | ||
TableName: "TestTable", | ||
KeyConditions: { | ||
id: { | ||
AttributeValueList: ["test"], | ||
ComparisonOperator: "EQ", | ||
}, | ||
}, | ||
}; | ||
const inputKeyNodes = [ | ||
{ | ||
key: "KeyConditions", | ||
children: { | ||
children: [{ key: "AttributeValueList" }], | ||
}, | ||
}, | ||
{ | ||
key: "QueryFilter", | ||
children: { | ||
children: [{ key: "AttributeValueList" }], | ||
}, | ||
}, | ||
{ key: "ExclusiveStartKey" }, | ||
{ key: "ExpressionAttributeValues" }, | ||
]; | ||
const output = { | ||
TableName: "TestTable", | ||
KeyConditions: { id: { AttributeValueList: [{ S: "test" }], ComparisonOperator: "EQ" } }, | ||
QueryFilter: undefined, | ||
ExclusiveStartKey: undefined, | ||
ExpressionAttributeValues: undefined, | ||
}; | ||
expect(marshallInput(input, inputKeyNodes)).toEqual(output); | ||
}); | ||
it("marshals ExecuteStatementCommand input", () => { | ||
const input = { | ||
Statement: `SELECT col_1 | ||
FROM some_table | ||
WHERE contains("col_1", ?)`, | ||
Parameters: ["some_param"], | ||
}; | ||
const inputKeyNodes = [{ key: "Parameters" }]; | ||
const output = { | ||
Statement: input.Statement, | ||
Parameters: [{ S: "some_param" }], | ||
}; | ||
expect(marshallInput(input, inputKeyNodes)).toEqual(output); | ||
}); | ||
it("marshals BatchExecuteStatementCommand input", () => { | ||
const input = { | ||
Statements: [ | ||
{ | ||
Statement: ` | ||
UPDATE "table" | ||
SET field1=? | ||
WHERE field2 = ? | ||
AND field3 = ? | ||
`, | ||
Parameters: [false, "field 2 value", 1234], | ||
}, | ||
], | ||
}; | ||
const inputKeyNodes = [{ key: "Statements", children: [{ key: "Parameters" }] }]; | ||
const output = { | ||
Statements: [ | ||
{ | ||
Statement: input.Statements[0].Statement, | ||
Parameters: [ | ||
{ | ||
BOOL: false, | ||
}, | ||
{ S: "field 2 value" }, | ||
{ N: "1234" }, | ||
], | ||
}, | ||
], | ||
}; | ||
expect(marshallInput(input, inputKeyNodes)).toEqual(output); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { convertToAttr } from "./convertToAttr"; | ||
import { marshall } from "./marshall"; | ||
|
||
describe("marshall type discernment", () => { | ||
describe("behaves as convertToAttr for non-collection values or Sets", () => { | ||
it("marshals string", () => { | ||
const value = "hello"; | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals number", () => { | ||
const value = 1578; | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals binary", () => { | ||
const value = new Uint8Array([0, 1, 0, 1]); | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals boolean", () => { | ||
let value = false; | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
value = true; | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals null", () => { | ||
const value = null; | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
it("marshals string set", () => { | ||
const value = new Set(["a", "b"]); | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals number set", () => { | ||
const value = new Set([1, 2]); | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
|
||
it("marshals binary set", () => { | ||
const value = new Set([new Uint8Array([1, 0]), new Uint8Array([0, 1])]); | ||
expect(marshall(value)).toEqual(convertToAttr(value)); | ||
}); | ||
}); | ||
|
||
describe("unwraps one level for input data which are lists or maps", () => { | ||
it("marshals and unwraps map", () => { | ||
expect(marshall({ a: 1, b: { a: 2, b: [1, 2, 3] } })).toEqual({ | ||
a: { N: "1" }, | ||
b: { | ||
M: { | ||
a: { N: "2" }, | ||
b: { | ||
L: [{ N: "1" }, { N: "2" }, { N: "3" }], | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("marshals and unwraps list", () => { | ||
expect(marshall(["test", 2, null])).toEqual([{ S: "test" }, { N: "2" }, { NULL: true }]); | ||
}); | ||
}); | ||
}); |