-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamodb-mongostyle.test.js
More file actions
125 lines (116 loc) · 3.72 KB
/
Copy pathdynamodb-mongostyle.test.js
File metadata and controls
125 lines (116 loc) · 3.72 KB
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
require('./dynamodb-mongostyle');
test('getSchemaHash', () => {
expect(getSchemaHash( [ { AttributeName: 'id', KeyType: 'HASH' } ] )).toEqual('id');
});
test('getSchemaRange', () => {
expect(getSchemaRange( [ { AttributeName: 'date', KeyType: 'RANGE' } ] )).toEqual('date');
});
test('jsonToDynamo', () => {
expect(jsonToDynamo(2)).toEqual( { 'N': '2' });
expect(jsonToDynamo('string')).toEqual( { 'S': 'string' });
expect(jsonToDynamo([1,2,3]))
.toEqual( { 'L': [ { 'N': '1' }, { 'N': '2' }, { 'N': '3'}] });
expect(jsonToDynamo( { test: 1 }))
.toEqual( { 'test': { 'N': '1' }})
expect(jsonToDynamo({ list: [1,2,3] }))
.toEqual( { 'list': { 'L': [ { 'N': '1' }, { 'N': '2' }, { 'N': '3'}] } });
});
test('queryValueToExpression', () => {
expect(queryValueToExpression('foo', 'bar'))
.toEqual( { expression: '#foo = :foo', value: 'bar' });
expect(queryValueToExpression('foo', null))
.toEqual( { expression: 'attribute_not_exists(#foo)', value: null });
expect(queryValueToExpression('foo', { $gt: 0 } ))
.toEqual( { expression: '#foo > :foo', value: 0 });
expect(queryValueToExpression('foo', { $lt: 3 } ))
.toEqual( { expression: '#foo < :foo', value: 3 });
expect(queryValueToExpression('foo', { $gte: 5 } ))
.toEqual( { expression: '#foo >= :foo', value: 5 });
expect(queryValueToExpression('foo', { $lte: 5 } ))
.toEqual( { expression: '#foo <= :foo', value: 5 });
expect(queryValueToExpression('foo', { $ne: 'bar' } ))
.toEqual( { expression: '#foo <> :foo', value: 'bar' });
});
test('getBestQuery', () => {
let tableDesc = {
Table: {
KeySchema: [ { KeyType: 'HASH', AttributeName: 'id' }]
}
};
expect(getBestQuery({ id: 'myid' }, 'tableName', tableDesc))
.toEqual( {
type: 'query',
options: {
TableName: 'tableName',
Limit: 10,
ExpressionAttributeNames: { '#id': 'id' },
ExpressionAttributeValues: { ':id': { 'S': 'myid' } },
KeyConditionExpression: '#id = :id'
}
});
expect(getBestQuery({ foo: { $gt: 0 } }, 'tableName', tableDesc))
.toEqual( {
type: 'scan',
options: {
TableName: 'tableName',
Limit: 10,
ExpressionAttributeNames: { '#foo': 'foo' },
ExpressionAttributeValues: { ':foo': { 'N': '0' } },
FilterExpression: '#foo > :foo'
}
});
})
test('getBestIndex', () => {
let indexes = [
{
IndexName: 'status-updateAt-index',
KeySchema: [
{ AttributeName: 'status', KeyType: 'HASH' },
{ AttributeName: 'updateAt', KeyType: 'RANGE' }
]
}
];
expect(getBestIndex( { status: 'ACTIVE', updateAt: 1000 }, indexes))
.toEqual(
{
IndexName: 'status-updateAt-index',
KeySchema: [
{ AttributeName: 'status', KeyType: 'HASH' },
{ AttributeName: 'updateAt', KeyType: 'RANGE' }
]
}
);
let index = getBestIndex( { status: 'ACTIVE', updateAt: 1000 }, indexes);
expect(getSchemaHash(index.KeySchema))
.toEqual('status');
expect(getSchemaRange(index.KeySchema))
.toEqual('updateAt');
})
test('getBestQuery with range index', () => {
let tableDesc = {
Table: {
KeySchema: [ { KeyType: 'HASH', AttributeName: 'id' }],
GlobalSecondaryIndexes: [
{
IndexName: 'status-updateAt-index',
KeySchema: [
{ AttributeName: 'status', KeyType: 'HASH' },
{ AttributeName: 'updateAt', KeyType: 'RANGE' }
]
}
]
},
};
expect(getBestQuery( { status: 'ACTIVE', updateAt: { $gte: 1000 } }, 'tableName', tableDesc))
.toEqual( {
type: 'query',
options: {
TableName: 'tableName',
Limit: 10,
ExpressionAttributeNames: { '#status': 'status', '#updateAt': 'updateAt' },
ExpressionAttributeValues: { ':status': { 'S': 'ACTIVE' }, ':updateAt': { 'N': '1000' } },
IndexName: 'status-updateAt-index',
KeyConditionExpression: '#status = :status AND #updateAt >= :updateAt'
}
});
});