Skip to content

Commit fd41801

Browse files
committed
add tests for entity actions from table instance
1 parent 5ec6f1f commit fd41801

File tree

3 files changed

+186
-5
lines changed

3 files changed

+186
-5
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const { DocumentClient } = require('./bootstrap-tests')
2+
3+
// Require Table and Entity classes
4+
const Table = require('../classes/Table')
5+
const Entity = require('../classes/Entity')
6+
7+
// Create basic entity
8+
const TestEntity = new Entity(require('./entities/test-entity.js'))
9+
const SimpleEntity = new Entity(require('./entities/simple-entity.js'))
10+
11+
// Create basic table
12+
const TestTable = new Table({
13+
name: 'test-table',
14+
partitionKey: 'pk',
15+
sortKey: 'sk',
16+
entities: [TestEntity,SimpleEntity],
17+
DocumentClient
18+
})
19+
20+
21+
describe('parse',()=>{
22+
23+
it('parses single item', ()=>{
24+
let item = TestEntity.parse({ pk: 'test@test.com', sk: 'email', test_string: 'test', _et: 'TestEntity' })
25+
expect(item).toEqual({
26+
email: 'test@test.com',
27+
test_type: 'email',
28+
test_string: 'test',
29+
entity: 'TestEntity'
30+
})
31+
})
32+
33+
it('parses single item and includes certain fields', ()=>{
34+
let item = TestEntity.parse({ pk: 'test@test.com', sk: 'email', test_string: 'test', _et: 'TestEntity' }, ['email','sk'])
35+
expect(item).toEqual({
36+
email: 'test@test.com',
37+
test_type: 'email'
38+
})
39+
})
40+
41+
it('parses multiple items', ()=>{
42+
let items = TestEntity.parse([
43+
{ pk: 'test@test.com', sk: 'email', test_string: 'test' },
44+
{ pk: 'test2@test.com', sk: 'email2', test_string: 'test2' }
45+
])
46+
expect(items).toEqual([
47+
{
48+
email: 'test@test.com',
49+
test_type: 'email',
50+
test_string: 'test'
51+
},
52+
{
53+
email: 'test2@test.com',
54+
test_type: 'email2',
55+
test_string: 'test2'
56+
}
57+
])
58+
})
59+
60+
it('parses multiple items and incudes certain field', ()=>{
61+
let items = TestEntity.parse([
62+
{ pk: 'test@test.com', sk: 'email', test_string: 'test' },
63+
{ pk: 'test2@test.com', sk: 'email2', test_string: 'test2' }
64+
],['pk','test_string'])
65+
expect(items).toEqual([
66+
{
67+
email: 'test@test.com',
68+
test_string: 'test'
69+
},
70+
{
71+
email: 'test2@test.com',
72+
test_string: 'test2'
73+
}
74+
])
75+
})
76+
77+
it('parses composite field', ()=>{
78+
let item = SimpleEntity.parse({ pk: 'test@test.com', sk: 'active#email', test_composite: 'test' })
79+
expect(item).toEqual({
80+
pk: 'test@test.com',
81+
test_composite: 'test',
82+
test_composite2: 'email',
83+
})
84+
})
85+
86+
87+
}) // end parse
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const { Table, Entity } = require('../index')
2+
const { DocumentClient } = require('./bootstrap-tests')
3+
4+
const TestTable = new Table({
5+
name: 'test-table',
6+
partitionKey: 'pk',
7+
sortKey: 'sk',
8+
indexes: { GSI1: { partitionKey: 'GSI1pk', sortKey: 'GSIsk1' } },
9+
DocumentClient,
10+
autoExecute: false
11+
})
12+
13+
const TestEntity = new Entity({
14+
name: 'TestEntity',
15+
autoExecute: false,
16+
attributes: {
17+
email: { type: 'string', partitionKey: true },
18+
sort: { type: 'string', sortKey: true },
19+
test: 'string'
20+
},
21+
table: TestTable
22+
})
23+
24+
describe('table.get',()=>{
25+
26+
it('gets an entity from the table', async () => {
27+
let result = await TestTable.get('TestEntity',{ email: 'val1', sort: 'val2' })
28+
expect(result).toEqual({ TableName: 'test-table', Key: { pk: 'val1', sk: 'val2' } })
29+
})
30+
31+
it('fails on invalid entity when performing a get', () => {
32+
expect(TestTable.get('TestEntityX')).rejects.toThrow(`'TestEntityX' is not a valid Entity`)
33+
})
34+
})
35+
36+
describe('table.delete',()=>{
37+
38+
it('deletes an entity from the table', async () => {
39+
let result = await TestTable.delete('TestEntity',{ email: 'val1', sort: 'val2' })
40+
expect(result).toEqual({ TableName: 'test-table', Key: { pk: 'val1', sk: 'val2' } })
41+
})
42+
43+
it('fails on invalid entity when performing a delete', () => {
44+
expect(TestTable.delete('TestEntityX')).rejects.toThrow(`'TestEntityX' is not a valid Entity`)
45+
})
46+
})
47+
48+
describe('table.update',()=>{
49+
50+
it('updates an entity from the table', async () => {
51+
let result = await TestTable.update('TestEntity',{ email: 'val1', sort: 'val2', test: 'testing' })
52+
expect(result.TableName).toBe('test-table')
53+
expect(result.Key).toEqual({ pk: 'val1', sk: 'val2' })
54+
expect(result.UpdateExpression).toBe('SET #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test = :test')
55+
})
56+
57+
it('fails on invalid entity when performing an update', () => {
58+
expect(TestTable.update('TestEntityX')).rejects.toThrow(`'TestEntityX' is not a valid Entity`)
59+
})
60+
})
61+
62+
describe('table.put',()=>{
63+
64+
it('puts an entity to the table', async () => {
65+
let result = await TestTable.put('TestEntity',{ email: 'val1', sort: 'val2', test: 'testing' })
66+
expect(result.TableName).toBe('test-table')
67+
expect(result.Item.pk).toBe('val1')
68+
expect(result.Item.sk).toBe('val2')
69+
expect(result.Item.test).toBe('testing')
70+
})
71+
72+
it('fails on invalid entity when performing a put', () => {
73+
expect(TestTable.put('TestEntityX')).rejects.toThrow(`'TestEntityX' is not a valid Entity`)
74+
})
75+
})
76+
77+
78+
describe('table.parse',()=>{
79+
80+
it('parses single item', async ()=>{
81+
let item = await TestTable.parse('TestEntity',{ pk: 'test@test.com', sk: 'email', test: 'testing', _et: 'TestEntity' })
82+
expect(item).toEqual({
83+
email: 'test@test.com',
84+
sort: 'email',
85+
test: 'testing',
86+
entity: 'TestEntity'
87+
})
88+
})
89+
90+
it('fails on invalid entity when performing a parse', () => {
91+
expect(TestTable.parse('TestEntityX')).rejects.toThrow(`'TestEntityX' is not a valid Entity`)
92+
})
93+
94+
}) // end table.parse

classes/Table.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,27 +1030,27 @@ class Table {
10301030

10311031

10321032
// Entity operation references
1033-
parse(entity,input,include=[]) {
1033+
async parse(entity,input,include=[]) {
10341034
if (!this[entity]) error(`'${entity}' is not a valid Entity`)
10351035
return this[entity].parse(input,include)
10361036
}
10371037

1038-
get(entity,item={},options={},params={}) {
1038+
async get(entity,item={},options={},params={}) {
10391039
if (!this[entity]) error(`'${entity}' is not a valid Entity`)
10401040
return this[entity].get(item,options,params)
10411041
}
10421042

1043-
delete(entity,item={},options={},params={}) {
1043+
async delete(entity,item={},options={},params={}) {
10441044
if (!this[entity]) error(`'${entity}' is not a valid Entity`)
10451045
return this[entity].delete(item,options,params)
10461046
}
10471047

1048-
update(entity,item={},options={},params={}) {
1048+
async update(entity,item={},options={},params={}) {
10491049
if (!this[entity]) error(`'${entity}' is not a valid Entity`)
10501050
return this[entity].update(item,options,params)
10511051
}
10521052

1053-
put(entity,item={},options={},params={}) {
1053+
async put(entity,item={},options={},params={}) {
10541054
if (!this[entity]) error(`'${entity}' is not a valid Entity`)
10551055
return this[entity].put(item,options,params)
10561056
}

0 commit comments

Comments
 (0)