Skip to content

Commit

Permalink
add parseTable tests and misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed May 1, 2020
1 parent 8dc69fa commit c8912a8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
111 changes: 111 additions & 0 deletions __tests__/parseTable.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const parseTable = require('../lib/parseTable')

// Bootstrap testing
const { DocumentClient } = require('./bootstrap-tests')

// Require Table and Entity classes
const Table = require('../classes/Table')

let table = {
name: 'test-table',
alias: 'test-table-alias',
partitionKey: 'pk',
sortKey: 'sk',
entityField: 'entity',
attributes: { pk: 'string', sk: 'string' },
indexes: { GSI: { partitionKey: 'GSIpk', sortKey: 'GSIsk' } },
autoExecute: false,
autoParse: false
}

describe('parseTable', () => {

it('parses simple mapping', async () => {
let tbl = parseTable(table)
expect(tbl.name).toBe('test-table')
expect(tbl.alias).toBe('test-table-alias')
expect(tbl.Table.attributes.entity).toEqual({ type: 'string' })
expect(tbl.Table.attributes.pk).toEqual({ type: 'string' })
expect(tbl.Table.attributes.sk).toEqual({ type: 'string' })
expect(tbl.Table.entityField).toBe('entity')
expect(tbl._entities).toEqual([])
expect(tbl.autoExecute).toBe(false)
expect(tbl.autoParse).toBe(false)
})

it('fails on extra config option', async () => {
expect(() => {
parseTable(Object.assign({},table,{ invalidConfig: true }))
}).toThrow(`Invalid Table configuration options: invalidConfig`)
})

it('fails if missing name', async () => {
expect(() => {
parseTable(Object.assign({},table,{ name: undefined }))
}).toThrow(`'name' must be defined`)
})

it('fails if alias is not a string', async () => {
expect(() => {
parseTable(Object.assign({},table,{ alias: 123 }))
}).toThrow(`'alias' must be a string value`)
})

it('fails if missing partitionKey', async () => {
expect(() => {
parseTable(Object.assign({},table,{ partitionKey: undefined }))
}).toThrow(`'partitionKey' must be defined`)
})

it('fails if sortKey is not a strin', async () => {
expect(() => {
parseTable(Object.assign({},table,{ sortKey: 123 }))
}).toThrow(`'sortKey' must be a string value`)
})

it('fails if attributes is not an object', async () => {
expect(() => {
parseTable(Object.assign({},table,{ attributes: 'string' }))
}).toThrow(`Please provide a valid 'attributes' object`)
})

it('passes if attributes is null', async () => {
expect(parseTable(Object.assign({},table,{ attributes: null }))).toHaveProperty('Table')
})

it('passes if attributes is undefined', async () => {
expect(parseTable(Object.assign({},table,{ attributes: undefined }))).toHaveProperty('Table')
})

it('fails if indexes is not an object', async () => {
expect(() => {
parseTable(Object.assign({},table,{ indexes: 'string' }))
}).toThrow(`Please provide a valid 'indexes' object`)
})

it('fails if index contain extra arguments', async () => {
expect(() => {
parseTable(Object.assign({},table,{ indexes: { GSI: { partitionKey: 'pk', invalidParam: true }} }))
}).toThrow(`Invalid index options: invalidParam`)
})

it('fails if index partitionKey is not a string', async () => {
expect(() => {
parseTable(Object.assign({},table,{ indexes: { GSI: { partitionKey: 123 }} }))
}).toThrow(`'partitionKey' for GSI must be a string`)
})

it('fails if index sortKey is not a string', async () => {
expect(() => {
parseTable(Object.assign({},table,{ indexes: { GSI: { sortKey: 123 }} }))
}).toThrow(`'sortKey' for GSI must be a string`)
})

it('fails if index does not contain partitionKey or sortKey', async () => {
expect(() => {
parseTable(Object.assign({},table,{ indexes: { GSI: { }} }))
}).toThrow(`A 'partitionKey', 'sortKey' or both, must be provided for GSI`)
})


})
12 changes: 6 additions & 6 deletions lib/parseTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

const parseAttributes = require('./parseTableAttributes')
const { error } = require('./utils')
const { error, hasValue } = require('./utils')

// Parse table
module.exports = table => {
Expand Down Expand Up @@ -36,10 +36,10 @@ module.exports = table => {
name = typeof name === 'string' && name.trim().length > 0 ? name.trim()
: error(`'name' must be defined`)

// Verify sortKey
// Verify alias
alias = typeof alias === 'string'
&& alias.trim().length > 0 ? alias.trim()
: alias ? error(`'alias' must be string value`)
: alias ? error(`'alias' must be a string value`)
: null

// Specify partitionKey attribute
Expand All @@ -50,7 +50,7 @@ module.exports = table => {
// Specify sortKey attribute (optional)
sortKey = typeof sortKey === 'string'
&& sortKey.trim().length > 0 ? sortKey.trim()
: sortKey ? error(`'sortKey' must be string value`)
: sortKey ? error(`'sortKey' must be a string value`)
: null

// Disable, or rename field for entity tracking
Expand All @@ -60,7 +60,7 @@ module.exports = table => {
: '_tp'

// Parse table attributes
attributes = typeof attributes === 'object' && !Array.isArray(attributes) ?
attributes = hasValue(attributes) && typeof attributes === 'object' && !Array.isArray(attributes) ?
attributes
: attributes ? error(`Please provide a valid 'attributes' object`)
: {}
Expand All @@ -69,7 +69,7 @@ module.exports = table => {
if (entityField) attributes[entityField] = 'string'

// Parse indexes (optional)
indexes = typeof indexes === 'object' && !Array.isArray(indexes) ?
indexes = hasValue(indexes) && typeof indexes === 'object' && !Array.isArray(indexes) ?
parseIndexes(indexes,partitionKey)
: indexes ? error(`Please provide a valid 'indexes' object`)
: {}
Expand Down

0 comments on commit c8912a8

Please sign in to comment.