Skip to content

Commit 7c91705

Browse files
author
Tony Joblin
committed
Tests for getKey
Added tests for getKey function. Some of these tests will fail on existing code, for example - number types with value of 0 This is a bug in getKey and will be fixed in the next commit.
1 parent 2f79d3a commit 7c91705

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

__tests__/lib/getKey.unit.test.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
const getKey = require('../../lib/getKey')
2+
3+
describe('getKey', () => {
4+
describe('pk', () => {
5+
describe('is a string', () => {
6+
it('returns the pk data in an object', () => {
7+
const data = { pk: 'id1' }
8+
const schema = {
9+
pk: { partitionKey: true, type: 'string', coerce: true },
10+
}
11+
const dummyDocumentClient = {}
12+
13+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
14+
15+
expect(result).toEqual({ pk: 'id1' })
16+
})
17+
18+
it('can not be empty, its an AWS Dynamodb thing', () => {
19+
const data = { pk: '' }
20+
const schema = {
21+
pk: { partitionKey: true, type: 'string', coerce: true },
22+
}
23+
const dummyDocumentClient = {}
24+
25+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk')).toThrow(new Error('\'pk\' is required'))
26+
})
27+
})
28+
29+
describe('can be a number', () => {
30+
it('returns the pk data', () => {
31+
const data = { pk: 123 }
32+
const schema = {
33+
pk: { partitionKey: true, type: 'number', coerce: true },
34+
}
35+
const dummyDocumentClient = {}
36+
37+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
38+
39+
expect(result).toEqual({ pk: 123 })
40+
})
41+
42+
it('can be 0', () => {
43+
const data = { pk: 0 }
44+
const schema = {
45+
pk: { partitionKey: true, type: 'number', coerce: true },
46+
}
47+
const dummyDocumentClient = {}
48+
49+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
50+
51+
expect(result).toEqual({ pk: 0 })
52+
})
53+
54+
it('can coerce from string', () => {
55+
const data = { pk: '123' }
56+
const schema = {
57+
pk: { partitionKey: true, type: 'number', coerce: true },
58+
}
59+
const dummyDocumentClient = {}
60+
61+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
62+
63+
expect(result).toEqual({ pk: 123 })
64+
})
65+
})
66+
67+
it('if no partitionKey in data will error', () => {
68+
const data = {}
69+
const schema = {
70+
pk: { partitionKey: true, type: 'string', coerce: true },
71+
}
72+
const dummyDocumentClient = {}
73+
74+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'pk\' is required'))
75+
})
76+
})
77+
78+
describe('sk', () => {
79+
it('returned object does not contain sort key if not required', () => {
80+
const data = { pk: 'id1' }
81+
const schema = {
82+
pk: { partitionKey: true, type: 'string', coerce: true },
83+
}
84+
const dummyDocumentClient = {}
85+
86+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
87+
88+
expect(result).toEqual({
89+
pk: 'id1'
90+
})
91+
})
92+
93+
describe('can be a string', () => {
94+
it('returns the sk data', () => {
95+
const data = { pk: 'id1', sk: 'sk1' }
96+
const schema = {
97+
pk: { partitionKey: true, type: 'string', coerce: true },
98+
sk: { sortKey: true, type: 'string', coerce: true },
99+
}
100+
const dummyDocumentClient = {}
101+
102+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
103+
104+
expect(result).toEqual({ pk: 'id1', sk: 'sk1' })
105+
})
106+
107+
it('can not be empty, its an AWS Dynamodb thing', () => {
108+
const data = { pk: 'id1', sk: '' }
109+
const schema = {
110+
pk: { partitionKey: true, type: 'string', coerce: true },
111+
sk: { sortKey: true, type: 'string', coerce: true },
112+
}
113+
const dummyDocumentClient = {}
114+
115+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'sk\' is required'))
116+
})
117+
})
118+
119+
describe('can be a number', () => {
120+
it('returns the sk data', () => {
121+
const data = { pk: 'id1', sk: 5 }
122+
const schema = {
123+
pk: { partitionKey: true, type: 'string', coerce: true },
124+
sk: { sortKey: true, type: 'number', coerce: true },
125+
}
126+
const dummyDocumentClient = {}
127+
128+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
129+
130+
expect(result).toEqual({ pk: 'id1', sk: 5 })
131+
})
132+
133+
it('can coerce sortKey string to number', () => {
134+
const data = { pk: 'id1', sk: '5' }
135+
const schema = {
136+
pk: { partitionKey: true, type: 'string', coerce: true },
137+
sk: { sortKey: true, type: 'number', coerce: true },
138+
}
139+
const dummyDocumentClient = {}
140+
141+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
142+
143+
expect(result).toEqual({
144+
pk: 'id1',
145+
sk: 5
146+
})
147+
})
148+
149+
it('can be 0', () => {
150+
const data = { pk: 'id1', sk: 0 }
151+
const schema = {
152+
pk: { partitionKey: true, type: 'string', coerce: true },
153+
sk: { sortKey: true, type: 'number', coerce: true },
154+
}
155+
const dummyDocumentClient = {}
156+
157+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
158+
159+
expect(result).toEqual({
160+
pk: 'id1',
161+
sk: 0
162+
})
163+
})
164+
})
165+
166+
it('if no sortKey in data will error', () => {
167+
const data = { pk: 'id1' }
168+
const schema = {
169+
pk: { partitionKey: true, type: 'string', coerce: true },
170+
sk: { sortKey: true, type: 'number', coerce: true },
171+
}
172+
const dummyDocumentClient = {}
173+
174+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'sk\' is required'))
175+
})
176+
})
177+
})

0 commit comments

Comments
 (0)