-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master: Some improvements and fixes on query generator and parsing
- Loading branch information
Showing
16 changed files
with
176 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { cleanUp, parse, generateInsert } from './query'; | ||
|
||
describe('query cleanUp', () => { | ||
it('returns empty string for empty inputs', () => { | ||
expect(cleanUp('')).toEqual(''); | ||
expect(cleanUp()).toEqual(''); | ||
expect(cleanUp(null)).toEqual(''); | ||
expect(cleanUp(<any>false)).toEqual(''); | ||
}) | ||
|
||
it('removes line breaks from simple select query', () => { | ||
const query = ` | ||
select | ||
* | ||
from | ||
table | ||
`; | ||
expect(cleanUp(query)).toEqual('select * from table') | ||
}); | ||
|
||
it('removes comments from queries', () => { | ||
const query = ` | ||
select | ||
* -- here is a comment | ||
/** | ||
* multiline comment | ||
*/ | ||
from | ||
table | ||
`; | ||
expect(cleanUp(query)).toEqual('select * from table') | ||
}); | ||
|
||
it('don`t change inline queries', () => { | ||
const query = `udpate tablename set value = 2, value2 = 'string' where id = 1`; | ||
expect(cleanUp(query)).toEqual(query); | ||
}); | ||
}); | ||
|
||
describe('query parse', () => { | ||
it('parses single query string to array', () => { | ||
let query = `select | ||
* -- here is a comment | ||
/** | ||
* multiline comment | ||
*/ | ||
from | ||
table`; | ||
expect(parse(query)).toEqual([query]); | ||
expect(parse(query, 'mysql')).toEqual([query]); | ||
expect(parse(query, 'pg')).toEqual([query]); | ||
const mssqlQuery = `${query}; | ||
GO` | ||
expect(parse(mssqlQuery, 'mssql')).toEqual([`${query};`]); | ||
}); | ||
|
||
it('parses muliple query string to array of queries', () => { | ||
let query = `select | ||
* | ||
from | ||
table;`; | ||
expect(parse(`${query}\n${query}`)).toEqual([query, query]); | ||
expect(parse(`${query}\n${query}`, 'mysql')).toEqual([query, query]); | ||
expect(parse(`${query}\n${query}`, 'pg')).toEqual([query, query]); | ||
expect(parse(`${query}\n${query}`, 'mssql')).toEqual([query, query]); | ||
const mssqlQuery = `${query} | ||
GO | ||
${query} | ||
GO` | ||
expect(parse(mssqlQuery, 'mssql')).toEqual([query, query]); | ||
}); | ||
}); | ||
|
||
describe('generateInsert query', () => { | ||
const generated = generateInsert('tablename', [ | ||
{ type: 'integer', tableName: 'tablename', columnName: 'col1', isNullable: false }, | ||
]); | ||
const expected = `INSERT INTO | ||
tablename (col1) | ||
VALUES | ||
(\${1:col1:integer});$0`; | ||
expect(generated).toBe(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.