Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to aws-sdk-js-v3 document client #174

Prev Previous commit
Next Next commit
test: cover utilitites and set cases
  • Loading branch information
deniszatsepin committed Jun 7, 2021
commit 374faa2b8d9a7ab82af6079fe76ffaa90e5104c2
2 changes: 1 addition & 1 deletion src/__tests__/entity-creation.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Table from '../classes/Table'
import Entity, { EntityConstructor} from '../classes/Entity'

// import { DocumentClient } from './bootstrap-tests'
import { ddbDocClient as DocumentClient } from './bootstrap-tests'

describe('Entity creation', ()=> {

Expand Down
132 changes: 108 additions & 24 deletions src/__tests__/validateTypes.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,156 @@
import validateTypes from '../lib/validateTypes'
import validateTypes, { typeOf, hasSameTypes } from '../lib/validateTypes'

import { ddbDocClient as DocumentClient } from './bootstrap-tests'

describe('validateTypes', () => {
describe('typeOf', () => {
it('detects null type', () => {
expect(typeOf(null)).toBe('null')
})

it('detects binary type', () => {
expect(typeOf(new Int32Array())).toBe('Binary')
})

it('detects string type', () => {
expect(typeOf('str')).toBe('String')
})

it('detects object type', () => {
expect(typeOf(Object.create(null))).toBe('Object')
})

it('detects undefined type', () => {
expect(typeOf()).toBe('undefined')
})
})

describe('hasSameType', () => {
it('should have same types for empty array', () => {
expect(hasSameTypes([])).toBeTruthy()
})

it('should have same types for one element array', () => {
expect(hasSameTypes([''])).toBeTruthy()
})

it('should have same types for all numbers array', () => {
expect(hasSameTypes([1, 2])).toBeTruthy()
})

it('should have different types for string/numbers array', () => {
expect(hasSameTypes([1, '2'])).toBeFalsy()
})
})

it('validates string', async () => {
let result = validateTypes(DocumentClient)({ type: 'string' },'attr','string value')
let result = validateTypes(DocumentClient)(
{ type: 'string' },
'attr',
'string value'
)
expect(result).toBe('string value')
})

it('fails with invalid string', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'string' },'attr',[]) })
.toThrow(`'attr' must be of type string`)
expect(() => {
validateTypes(DocumentClient)({ type: 'string' }, 'attr', [])
}).toThrow(`'attr' must be of type string`)
})

it('validates boolean', async () => {
let result = validateTypes(DocumentClient)({ type: 'boolean' },'attr',false)
let result = validateTypes(DocumentClient)(
{ type: 'boolean' },
'attr',
false
)
expect(result).toBe(false)
})

it('fails with invalid boolean', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'boolean' },'attr','string') })
.toThrow(`'attr' must be of type boolean`)
expect(() => {
validateTypes(DocumentClient)({ type: 'boolean' }, 'attr', 'string')
}).toThrow(`'attr' must be of type boolean`)
})

it('validates number (int)', async () => {
let result = validateTypes(DocumentClient)({ type: 'number' },'attr',123)
let result = validateTypes(DocumentClient)({ type: 'number' }, 'attr', 123)
expect(result).toBe(123)
})

it('validates number (float)', async () => {
let result = validateTypes(DocumentClient)({ type: 'number' },'attr',1.23)
let result = validateTypes(DocumentClient)({ type: 'number' }, 'attr', 1.23)
expect(result).toBe(1.23)
})

it('fails with invalid number', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'number' },'attr','string') })
.toThrow(`'attr' must be of type number`)
expect(() => {
validateTypes(DocumentClient)({ type: 'number' }, 'attr', 'string')
}).toThrow(`'attr' must be of type number`)
})

it('validates list', async () => {
let result = validateTypes(DocumentClient)({ type: 'list' },'attr',[1,2,3])
expect(result).toEqual([1,2,3])
let result = validateTypes(DocumentClient)({ type: 'list' }, 'attr', [
1,
2,
3,
])
expect(result).toEqual([1, 2, 3])
})

it('fails with invalid list', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'list' },'attr',false) })
.toThrow(`'attr' must be a list (array)`)
expect(() => {
validateTypes(DocumentClient)({ type: 'list' }, 'attr', false)
}).toThrow(`'attr' must be a list (array)`)
})

it('validates map', async () => {
let result = validateTypes(DocumentClient)({ type: 'map' },'attr',{ test: true })
let result = validateTypes(DocumentClient)({ type: 'map' }, 'attr', {
test: true,
})
expect(result).toEqual({ test: true })
})

it('fails with invalid map', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'map' },'attr',false) })
.toThrow(`'attr' must be a map (object)`)
expect(() => {
validateTypes(DocumentClient)({ type: 'map' }, 'attr', false)
}).toThrow(`'attr' must be a map (object)`)
})

it('validates set', async () => {
let result = validateTypes(DocumentClient)({ type: 'set', setType: 'number' },'attr',[1,2,3])
expect(result).toEqual(new Set([1,2,3]))
it('validates set from array', async () => {
let result = validateTypes(DocumentClient)(
{ type: 'set', setType: 'number' },
'attr',
[1, 2, 3]
)
expect(result).toEqual(new Set([1, 2, 3]))
})

it('fails with invalid set', async () => {
expect(() => { validateTypes(DocumentClient)({ type: 'set', setType: 'string' },'attr',false) })
.toThrow(`'attr' must be a valid set (array)`)
it('validates set from well formed string', async () => {
let result = validateTypes(DocumentClient)(
{ type: 'set', setType: 'string', coerce: true },
'attr',
'a,b,c'
)
expect(result).toEqual(new Set(['a', 'b', 'c']))
})

it('validates set from set', async () => {
let result = validateTypes(DocumentClient)(
{ type: 'set', setType: 'string', coerce: true },
'attr',
new Set(['a', 'b', 'c'])
)
expect(result).toEqual(new Set(['a', 'b', 'c']))
})

it('fails with invalid set', async () => {
expect(() => {
validateTypes(DocumentClient)(
{ type: 'set', setType: 'string' },
'attr',
false
)
}).toThrow(`'attr' must be a valid set (array)`)
})
})