Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions __tests__/DbColumn.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
const {DbColumn, DbReference, DbLang} = require ('..')

const newCol = src => {

if (typeof src === 'string') src = (new DbLang ()).parseColumn (src)

return new DbColumn (src)

}

const tst = (src, dst) => expect (newCol (src)).toEqual ({src, ...dst})

test ('Object', () => {

const o = {name: 'id', type: 'int'}
const od = {name: 'id', type: 'int', default: 1}
const on = {name: 'id', type: 'int', nullable: false}
const ond = {name: 'id', type: 'int', default: 1, nullable: true}

expect (new DbColumn (o)).toEqual ({...o, nullable: true})
expect (new DbColumn (od)).toEqual ({...od, nullable: false})
expect (new DbColumn (on)).toEqual (on)
expect (new DbColumn (ond)).toEqual (ond)
expect (newCol (o)).toEqual ({...o, nullable: true})
expect (newCol (od)).toEqual ({...od, nullable: false})
expect (newCol (on)).toEqual (on)
expect (newCol (ond)).toEqual (ond)
expect (new DbReference (o)).toEqual ({...o, on: {}})

})

const tst = (src, dst) => expect (new DbColumn (src)).toEqual ({src, ...dst})

test ('comment', () => {

tst (' int', {type: 'int', nullable: true})
Expand All @@ -38,21 +46,21 @@ test ('pattern', () => {
tst ('text /^[0-9\\/]+$/', {type: 'text', pattern: '^[0-9\\/]+$', nullable: true})
tst ('\t text = OK /^(OK|ERROR)$/ // \t\t\t status', {type: 'text', pattern: '^(OK|ERROR)$', default: 'OK', comment: 'status', nullable: false})

expect (() => new DbColumn ('text=/^/')).toThrow ()
expect (() => new DbColumn ('text / // ?')).toThrow ()
expect (() => newCol ('text=/^/')).toThrow ()
expect (() => newCol ('text / // ?')).toThrow ()

})

test ('range', () => {

tst ('int {0..10}', {type: 'int', min: '0', max: '10', nullable: true})
tst ('int {0..}', {type: 'int', min: '0', nullable: true})
tst ('int {..10}', {type: 'int', max: '10', nullable: true})
tst ('int [0..10]', {type: 'int', min: '0', max: '10', nullable: true})
tst ('int [0..]', {type: 'int', min: '0', nullable: true})
tst ('int [..10]', {type: 'int', max: '10', nullable: true})

tst ('date = 1980-01-01 { 1970-01-01 .. NOW } /-01$/ // created ', {type: 'date', min: '1970-01-01', default: '1980-01-01', max: 'NOW', pattern: '-01$', comment: 'created', nullable: false})
tst ('date = 1980-01-01 [ 1970-01-01 .. NOW ] /-01$/ // created ', {type: 'date', min: '1970-01-01', default: '1980-01-01', max: 'NOW', pattern: '-01$', comment: 'created', nullable: false})

expect (() => new DbColumn ('date }')).toThrow ()
expect (() => new DbColumn ('date {}')).toThrow ()
expect (() => newCol ('date ]')).toThrow ()
expect (() => newCol ('date []')).toThrow ()

})

Expand All @@ -61,7 +69,7 @@ test ('dimension', () => {
tst ('char(1)', {type: 'char', size: 1, nullable: true})
tst ('decimal (10, 2)', {type: 'decimal', size: 10, scale: 2, nullable: true})

tst (' \t \t decimal (10, 2) = 0 { 0.00 .. 1000.00 } /00$/ // \t\t\t salary ', {
tst (' \t \t decimal (10, 2) = 0 [ 0.00 .. 1000.00 ] /00$/ // \t\t\t salary ', {
type: 'decimal',
size: 10,
scale: 2,
Expand All @@ -73,9 +81,9 @@ test ('dimension', () => {
nullable: false,
})

expect (() => new DbColumn ('date )')).toThrow ()
expect (() => new DbColumn ('decimal (10,zz)')).toThrow ()
expect (() => new DbColumn ('decimal (+,1)')).toThrow ()
expect (() => newCol ('date )')).toThrow ()
expect (() => newCol ('decimal (10,zz)')).toThrow ()
expect (() => newCol ('decimal (+,1)')).toThrow ()

})

Expand All @@ -84,7 +92,7 @@ test ('nullable', () => {
tst ('int!', {type: 'int', nullable: false})
tst ('int?=0', {type: 'int', default: '0', nullable: true})

tst (' \t \t decimal (10, 2) ?= 0 { 0.00 .. 1000.00 } /00$/ // \t\t\t salary ', {
tst (' \t \t decimal (10, 2) ?= 0 [ 0.00 .. 1000.00 ] /00$/ // \t\t\t salary ', {
type: 'decimal',
size: 10,
scale: 2,
Expand All @@ -96,7 +104,7 @@ test ('nullable', () => {
nullable: true,
})

tst (' \t \t decimal (10, 2) ! { 0.00 .. 1000.00 } /00$/ // \t\t\t salary ', {
tst (` \t \t decimal (10, 2) ! [ 0.00 .. 1000.00 ] /00$/ {compression: '{gz}', ttl: 600} // \t\t\t salary `, {
type: 'decimal',
size: 10,
scale: 2,
Expand All @@ -105,15 +113,18 @@ test ('nullable', () => {
pattern: '00$',
comment: 'salary',
nullable: false,
compression: '{gz}',
ttl: 600,
})

expect (() => new DbColumn ('=0')).toThrow ()
expect (() => newCol ('=0')).toThrow ()
expect (() => newCol ('int=0}')).toThrow ()

})

const tst_ref = (src, dst) => {

const col = new DbColumn (src)
const col = newCol (src)

expect (col.reference.column).toBe (col)

Expand All @@ -135,7 +146,7 @@ test ('reference', () => {
tst_ref ('(~users)', {type: undefined, nullable: true, reference: {targetRelationName: 'users', on: {DELETE: 'SET NULL'}}})
tst_ref ('(~users)=current_user()', {type: undefined, nullable: false, default: 'current_user()', reference: {targetRelationName: 'users', on: {DELETE: 'SET DEFAULT'}}})

expect (() => new DbColumn ('()')).toThrow ()
expect (() => newCol ('()')).toThrow ()
expect (() => new DbReference ('(((')).toThrow ()
expect (() => new DbReference (')))')).toThrow ()

Expand All @@ -147,37 +158,37 @@ test ('typeDim', () => {
const lang = new DbLang ()

{
const col = new DbColumn ({name: 'parent', ref: 'deps'})
const col = newCol ({name: 'parent', ref: 'deps'})
col.setLang (lang)
expect (col.typeDim).toBeUndefined
}

{
const col = new DbColumn ({name: 'dt', type: 'date'})
const col = newCol ({name: 'dt', type: 'date'})
col.setLang (lang)
expect (col.typeDim).toBe ('DATE')
}

{
const col = new DbColumn ({name: 'cc', type: 'char', size: 2})
const col = newCol ({name: 'cc', type: 'char', size: 2})
col.setLang (lang)
expect (col.typeDim).toBe ('CHAR(2)')
}

{
const col = new DbColumn ({name: 'amount', type: 'decimal', size: 10, scale: 2})
const col = newCol ({name: 'amount', type: 'decimal', size: 10, scale: 2})
col.setLang (lang)
expect (col.typeDim).toBe ('NUMERIC(10,2)')
}

{
const col = new DbColumn ({name: 'amount', type: 'char', size: 10, scale: undefined})
const col = newCol ({name: 'amount', type: 'char', size: 10, scale: undefined})
col.setLang (lang)
expect (col.typeDim).toBe ('CHAR(10)')
}

{
const col = new DbColumn ({name: 'amount', type: 'bool', size: undefined, scale: undefined})
const col = newCol ({name: 'amount', type: 'bool', size: undefined, scale: undefined})
col.setLang (lang)
expect (col.typeDim).toBe ('BOOL')
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/DbCsvPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test ('infty', async () => {

const p = new DbCsvPrinter ({
lang: new DbLang (),
columns: {id: 'int'},
columns: {id: {type: 'int'}},
})

await new Promise ((ok, fail) => {
Expand Down
Loading