Skip to content

Commit

Permalink
addl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed May 1, 2020
1 parent c8912a8 commit c351362
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 116 deletions.
75 changes: 75 additions & 0 deletions __tests__/parseTableAttributes.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const parseTableAttributes = require('../lib/parseTableAttributes')

let attrs = {
pk: 'string',
sk: 'string'
}


describe('parseTableAttributes', () => {

it('parse simple attributes', async () => {
expect(parseTableAttributes(attrs,'pk','sk')).toEqual({ pk: { type: 'string' }, sk: { type: 'string' } })
})

it('fails when attribute is missing type', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: {} }),'pk','sk')
}).toThrow(`Invalid or missing type for 'test'. Valid types are 'string', 'boolean', 'number', 'list', 'map', 'binary', and 'set'.`)
})

it('fails when partitionKey is an invalid type', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ pk: 'map' }),'pk','sk')
}).toThrow(`Invalid or missing type for 'pk'. Valid types for partitionKey and sortKey are 'string','number' and 'binary'`)
})

it('fails when sortKey is an invalid type', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ sk: 'map' }),'pk','sk')
}).toThrow(`Invalid or missing type for 'sk'. Valid types for partitionKey and sortKey are 'string','number' and 'binary'`)
})

it('fails when attribute is an invalid type', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: 'not-a-type' }),'pk','sk')
}).toThrow(`Invalid or missing type for 'test'. Valid types are 'string', 'boolean', 'number', 'list', 'map', 'binary', and 'set'.`)
})

it('fails when partitionKey is an invalid type (in object config)', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ pk: { type: 'map' } }),'pk','sk')
}).toThrow(`Invalid or missing type for 'pk'. Valid types for partitionKey and sortKey are 'string','number' and 'binary'`)
})

it('fails when sortKey is an invalid type (in object config)', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ sk: { type: 'map' } }),'pk','sk')
}).toThrow(`Invalid or missing type for 'sk'. Valid types for partitionKey and sortKey are 'string','number' and 'binary'`)
})

it('fails when attribute is an invalid type (in an object config)', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: { type: 'not-a-type' } }),'pk','sk')
}).toThrow(`Invalid or missing type for 'test'. Valid types are 'string', 'boolean', 'number', 'list', 'map', 'binary', and 'set'.`)
})

it(`fails when attribute contains setType but isn't a set`, async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: { type: 'string', setType: 'string' } }),'pk','sk')
}).toThrow(`'setType' is only valid for type 'set'`)
})

it(`fails when attribute contains invalid setType`, async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: { type: 'set', setType: 'invalid' } }),'pk','sk')
}).toThrow(`Invalid 'setType', must be 'string', 'number', or 'binary'`)
})

it('fails when attribute has an invalid config option', async () => {
expect(() => {
parseTableAttributes(Object.assign({},attrs,{ test: { type: 'string', invalid: 'invalid' } }),'pk','sk')
}).toThrow(`'invalid' is not a valid property type`)
})

})
114 changes: 0 additions & 114 deletions __tests__/utility.unit.test.js

This file was deleted.

30 changes: 30 additions & 0 deletions __tests__/utils.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const utils = require('../lib/utils')

describe('utility functions',()=>{

test('toBool', ()=>{
expect(utils.toBool('true')).toBe(true)
expect(utils.toBool(1)).toBe(true)
expect(utils.toBool(true)).toBe(true)
expect(utils.toBool('any')).toBe(true)
expect(utils.toBool(false)).toBe(false)
expect(utils.toBool('false')).toBe(false)
expect(utils.toBool(0)).toBe(false)
expect(utils.toBool('no')).toBe(false)
})

test('hasValue', ()=>{
expect(utils.hasValue('string')).toBe(true)
expect(utils.hasValue(1)).toBe(true)
expect(utils.hasValue(true)).toBe(true)
expect(utils.hasValue({})).toBe(true)
expect(utils.hasValue([])).toBe(true)
expect(utils.hasValue(false)).toBe(true)
expect(utils.hasValue(undefined)).toBe(false)
expect(utils.hasValue(null)).toBe(false)
})

test('error', () => {
expect(() => { utils.error('test error') }).toThrow('test error')
})
})
4 changes: 2 additions & 2 deletions lib/parseTableAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const parseAttributeConfig = (field,config) => {
}
})

// Default the type
if (!config.type) config.type = 'string'
// // Default the type // Table attributes must specify type?
// if (!config.type) config.type = 'string'

return {
[field]: config
Expand Down

0 comments on commit c351362

Please sign in to comment.