-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
105 lines (87 loc) · 2.08 KB
/
test.js
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
const test = require("tape")
const merge = require("./index")
test("SET", (t) => {
t.plan(2)
t.deepEqual(
merge({ x: 42 }),
{
UpdateExpression: "SET x = :x",
ExpressionAttributeValues: { ':x': 42 }
}
)
t.deepEqual(
merge({ x: { y: "foo", z: { w: 42 } }, q: 0 }),
{
UpdateExpression: "SET x.y = :x_y, x.z.w = :x_z_w, q = :q",
ExpressionAttributeValues: { ":x_y": "foo", ":x_z_w": 42, ":q": 0 }
}
)
})
test("Combination", (t) => {
t.plan(1)
t.deepEqual(
merge({ x: { y: [1, 2], z: "x" }, q: 10 }),
{
UpdateExpression: "SET x.z = :x_z, q = :q ADD x.y :x_y0, x.y :x_y1",
ExpressionAttributeValues: { ":x_z": "x", ":q": 10, ":x_y0": 1, ":x_y1": 2 }
}
)
})
test("Functions", (t) => {
t.plan(1)
t.deepEqual(
merge({ x: { y: () => "foo", z: "x" }, q: 10 }),
{
UpdateExpression: "SET x.y = :x_y, x.z = :x_z, q = :q",
ExpressionAttributeValues: { ":x_y": "foo", ":q": 10, ":x_z": "x" }
}
)
})
test("Null values should be deleted", (t) => {
t.plan(3)
t.deepEqual(
merge({ x: null }),
{
UpdateExpression: "DELETE x",
ExpressionAttributeValues: {}
}
)
t.deepEqual(
merge({ x: null, y: "foo" }),
{
UpdateExpression: "SET y = :y DELETE x",
ExpressionAttributeValues: { ":y": "foo" }
}
)
t.deepEqual(
merge({ x: { y: null, z: { w: 42 } }, q: 0 }),
{
UpdateExpression: "SET x.z.w = :x_z_w, q = :q DELETE x.y",
ExpressionAttributeValues: { ":x_z_w": 42, ":q": 0 }
}
)
})
test("Undefineds should be left alone", (t) => {
t.plan(2)
t.deepEqual(
merge({ x: undefined }),
{ UpdateExpression: "", ExpressionAttributeValues: {} }
)
t.deepEqual(
merge({ x: { y: undefined, z: { w: 42 } }, q: 0 }),
{
UpdateExpression: "SET x.z.w = :x_z_w, q = :q",
ExpressionAttributeValues: { ":x_z_w": 42, ":q": 0 }
}
)
})
test("Booleans", (t) => {
t.plan(1)
t.deepEqual(
merge({ x: false }),
{
UpdateExpression: "SET x = :x",
ExpressionAttributeValues: { ":x": false }
}
)
})