2021-05-03 19:29:22 +02:00
import Model from "../src/db/Model.js" ;
import ModelFactory from "../src/db/ModelFactory.js" ;
import ModelQuery , { SelectFieldValue , WhereOperator } from "../src/db/ModelQuery.js" ;
2020-09-04 22:15:55 +02:00
describe ( 'Test ModelQuery' , ( ) = > {
2020-09-06 15:38:54 +02:00
test ( 'select' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , 'f1' , '"Test" as f2' )
2020-10-02 12:06:18 +02:00
. where ( 'f4' , 'v4' )
. where ( 'f5' , true )
. where ( 'f6' , null )
. where ( 'f7' , undefined ) ;
expect ( query . toString ( true ) ) . toBe ( 'SELECT `model`.`f1`,"Test" as f2 FROM `model` WHERE `f4`=? AND `f5`=true AND `f6` IS null AND `f7` IS null' ) ;
2020-09-06 15:38:54 +02:00
expect ( query . variables ) . toStrictEqual ( [ 'v4' ] ) ;
} ) ;
2020-09-05 16:09:30 +02:00
test ( 'order by' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > )
. sortBy ( 'model.f2' , 'ASC' ) ;
expect ( query . toString ( true ) ) . toBe ( 'SELECT `model`.* FROM `model` ORDER BY `model`.`f2` ASC' ) ;
2020-09-06 12:24:58 +02:00
const queryRaw = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > )
. sortBy ( 'coalesce(model.f1, model.f2)' , 'ASC' , true ) ;
expect ( queryRaw . toString ( true ) ) . toBe ( 'SELECT `model`.* FROM `model` ORDER BY coalesce(model.f1, model.f2) ASC' ) ;
2020-09-05 16:09:30 +02:00
} ) ;
2020-10-02 12:06:18 +02:00
test ( 'create (insert into)' , ( ) = > {
const date = new Date ( ) ;
const query = ModelQuery . insert (
{ table : 'model' } as unknown as ModelFactory < Model > ,
{
'boolean' : true ,
'null' : null ,
'undefined' : undefined ,
'string' : 'string' ,
'date' : date ,
'sensitive' : 'sensitive' , // Reserved word
} ,
) ;
expect ( query . toString ( true ) ) . toBe ( 'INSERT INTO `model` (`boolean`,`null`,`undefined`,`string`,`date`,`sensitive`) VALUES(true,null,null,?,?,?)' ) ;
expect ( query . variables ) . toStrictEqual ( [
'string' ,
date ,
'sensitive' ,
] ) ;
} ) ;
2020-09-05 15:33:51 +02:00
test ( 'update' , ( ) = > {
2020-10-02 12:06:18 +02:00
const date = new Date ( ) ;
2020-09-05 15:33:51 +02:00
const query = ModelQuery . update ( { table : 'model' } as unknown as ModelFactory < Model > , {
2020-10-02 12:06:18 +02:00
'boolean' : true ,
'null' : null ,
'undefined' : undefined ,
'string' : 'string' ,
'date' : date ,
'sensitive' : 'sensitive' , // Reserved word
} ) . where ( 'f4' , 'v4' )
. where ( 'f5' , true )
. where ( 'f6' , null )
. where ( 'f7' , undefined ) ;
expect ( query . toString ( true ) ) . toBe ( 'UPDATE `model` SET `model`.`boolean`=true,`model`.`null`=null,`model`.`undefined`=null,`model`.`string`=?,`model`.`date`=?,`model`.`sensitive`=? WHERE `f4`=? AND `f5`=true AND `f6` IS null AND `f7` IS null' ) ;
expect ( query . variables ) . toStrictEqual ( [
'string' ,
date ,
'sensitive' ,
'v4' ,
] ) ;
2020-09-05 15:33:51 +02:00
} ) ;
test ( 'function select' , ( ) = > {
2020-09-25 23:42:15 +02:00
const query = ModelQuery . select (
{ table : 'model' } as unknown as ModelFactory < Model > ,
'f1' ,
new SelectFieldValue ( '_count' , 'COUNT(*)' , true ) ,
) ;
2020-09-05 16:09:30 +02:00
expect ( query . toString ( true ) ) . toBe ( 'SELECT `model`.`f1`,(COUNT(*)) AS `_count` FROM `model`' ) ;
2020-09-05 15:33:51 +02:00
expect ( query . variables ) . toStrictEqual ( [ ] ) ;
} ) ;
test ( 'pivot' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , 'f1' ) ;
query . pivot ( 'pivot.f2' , 'f3' ) ;
2020-09-05 16:09:30 +02:00
expect ( query . toString ( true ) ) . toBe ( 'SELECT `model`.`f1`,`pivot`.`f2`,`model`.`f3` FROM `model`' ) ;
2020-09-05 15:33:51 +02:00
expect ( query . variables ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-09-04 22:15:55 +02:00
test ( 'groupWhere generates proper query' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , '*' ) ;
query . where ( 'f1' , 'v1' ) ;
query . groupWhere ( q = > q . where ( 'f2' , 'v2' ) . where ( 'f3' , 'v3' )
. groupWhere ( q = > q . where ( 'f4' , 'v4' ) , WhereOperator . OR ) )
. where ( 'f5' , 'v5' ) ;
2020-09-05 16:09:30 +02:00
expect ( query . toString ( true ) ) . toBe ( 'SELECT `model`.* FROM `model` WHERE `f1`=? AND (`f2`=? AND `f3`=? OR (`f4`=?)) AND `f5`=?' ) ;
2020-09-07 14:38:30 +02:00
expect ( query . variables ) . toStrictEqual ( [ 'v1' , 'v2' , 'v3' , 'v4' , 'v5' ] ) ;
2020-09-04 22:15:55 +02:00
} ) ;
2020-09-05 14:55:19 +02:00
test ( 'recursive queries' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , '*' ) ;
query . where ( 'f1' , 'v1' ) ;
query . leftJoin ( 'test' ) . on ( 'model.j1' , 'test.j2' ) ;
2020-09-08 18:12:39 +02:00
query . recursive ( { localKey : 'local' , foreignKey : 'foreign' } , false ) ;
query . limit ( 8 ) ;
2020-09-05 14:55:19 +02:00
2020-09-08 18:12:39 +02:00
expect ( query . toString ( true ) ) . toBe ( "WITH RECURSIVE cte AS (SELECT `model`.*,1 AS __depth, CONCAT(`local`) AS __path FROM `model` WHERE `f1`=? UNION SELECT o.*,c.__depth + 1,CONCAT(c.__path,'/',o.`local`) AS __path FROM `model` AS o, cte AS c WHERE o.`foreign`=c.`local`) SELECT * FROM cte LEFT JOIN `test` ON `model`.`j1`=`test`.`j2` ORDER BY __path ASC LIMIT 8" ) ;
2020-09-05 15:33:51 +02:00
expect ( query . variables ) . toStrictEqual ( [ 'v1' ] ) ;
2020-09-08 18:12:39 +02:00
const reversedQuery = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , '*' ) ;
reversedQuery . where ( 'f1' , 'v1' ) ;
reversedQuery . leftJoin ( 'test' ) . on ( 'model.j1' , 'test.j2' ) ;
reversedQuery . recursive ( { localKey : 'local' , foreignKey : 'foreign' } , true ) ;
expect ( reversedQuery . toString ( true ) ) . toBe ( "WITH RECURSIVE cte AS (SELECT `model`.*,1 AS __depth, CONCAT(`foreign`) AS __path FROM `model` WHERE `f1`=? UNION SELECT o.*,c.__depth + 1,CONCAT(c.__path,'/',o.`foreign`) AS __path FROM `model` AS o, cte AS c WHERE o.`foreign`=c.`local`) SELECT * FROM cte LEFT JOIN `test` ON `model`.`j1`=`test`.`j2` ORDER BY __path DESC" ) ;
expect ( reversedQuery . variables ) . toStrictEqual ( [ 'v1' ] ) ;
2020-09-05 14:55:19 +02:00
} ) ;
2020-09-06 15:06:40 +02:00
test ( 'union queries' , ( ) = > {
const query = ModelQuery . select ( { table : 'model' } as unknown as ModelFactory < Model > , '*' ) ;
const query2 = ModelQuery . select ( { table : 'model2' } as unknown as ModelFactory < Model > , '*' ) ;
2020-09-06 16:02:53 +02:00
query2 . where ( 'f2' , 'v2' ) ;
2020-09-06 15:06:40 +02:00
query . union ( query2 , 'model.f1' , 'DESC' , false , 8 ) ;
2020-09-06 16:02:53 +02:00
expect ( query . toString ( true ) ) . toBe ( "(SELECT `model`.* FROM `model`) UNION (SELECT `model2`.* FROM `model2` WHERE `f2`=?) ORDER BY `model`.`f1` DESC LIMIT 8" ) ;
2020-09-25 23:42:15 +02:00
expect ( query . variables ) . toStrictEqual ( [ 'v2' ] ) ;
2020-09-06 15:06:40 +02:00
} ) ;
2020-09-04 22:15:55 +02:00
} ) ;