1- import * as u from '../src';
2- import ast, { SelectStmt, RangeVar } from '../src ';
1+ import * as t from '../src' ;
2+ import { RangeVar , SelectStmt } from '@pgsql/types ' ;
33import { deparse } from 'pgsql-deparser' ;
44
5- it('getEnumValue snapshots', () => {
6- for (let i = 0; i <= 5; i++) {
7- expect(u.getEnumValue('A_Expr_Kind', i)).toMatchSnapshot();
8- }
9- });
10-
115it ( 'simple SelectStmt' , ( ) => {
12- const stmt: SelectStmt = ast .selectStmt({
6+ const stmt : { SelectStmt : SelectStmt } = t . nodes . selectStmt ( {
137 targetList : [
14- ast .resTarget({
15- val: ast .columnRef({
16- fields: [ast .aStar()]
8+ t . nodes . resTarget ( {
9+ val : t . nodes . columnRef ( {
10+ fields : [ t . nodes . aStar ( ) ]
1711 } )
1812 } )
1913 ] ,
2014 fromClause : [
21- ast .rangeVar({
15+ t . nodes . rangeVar ( {
2216 relname : 'some_table' ,
2317 inh : true ,
2418 relpersistence : 'p'
@@ -28,44 +22,43 @@ it('simple SelectStmt', () => {
2822 op : 'SETOP_NONE'
2923 } ) ;
3024
31- // @ts-ignore (because of optional args)
32- stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
25+ ( stmt . SelectStmt . fromClause [ 0 ] as { RangeVar : RangeVar } ) . RangeVar . relname = 'another_table' ;
3326
3427 expect ( stmt ) . toMatchSnapshot ( ) ;
3528 expect ( deparse ( stmt , { } ) ) . toMatchSnapshot ( ) ;
3629} ) ;
3730
3831it ( 'SelectStmt with WHERE clause' , ( ) => {
39- const selectStmt: SelectStmt = ast .selectStmt({
32+ const selectStmt : { SelectStmt : SelectStmt } = t . nodes . selectStmt ( {
4033 targetList : [
41- ast .resTarget({
42- val: ast .columnRef({
43- fields: [ast .aStar()]
34+ t . nodes . resTarget ( {
35+ val : t . nodes . columnRef ( {
36+ fields : [ t . nodes . aStar ( ) ]
4437 } )
4538 } )
4639 ] ,
4740 fromClause : [
48- ast .rangeVar({
41+ t . nodes . rangeVar ( {
4942 schemaname : 'myschema' ,
5043 relname : 'mytable' ,
5144 inh : true ,
5245 relpersistence : 'p'
5346 } )
5447 ] ,
55- whereClause: ast .aExpr({
48+ whereClause : t . nodes . aExpr ( {
5649 kind : 'AEXPR_OP' ,
57- name: [ast. string({ str : '=' })],
58- lexpr: ast .columnRef({
59- fields: [ast. string({ str : 'a' })]
50+ name : [ t . nodes . string ( { sval : '=' } ) ] ,
51+ lexpr : t . nodes . columnRef ( {
52+ fields : [ t . nodes . string ( { sval : 'a' } ) ]
6053 } ) ,
61- rexpr: ast .typeCast({
62- arg: ast .aConst({
63- val: ast.string({ str : 't' })
54+ rexpr : t . nodes . typeCast ( {
55+ arg : t . nodes . aConst ( {
56+ sval : t . ast . string ( { sval : 't' } )
6457 } ) ,
65- typeName: ast.typeName({
58+ typeName : t . ast . typeName ( {
6659 names : [
67- ast. string({ str : 'pg_catalog' }),
68- ast. string({ str : 'bool' })
60+ t . nodes . string ( { sval : 'pg_catalog' } ) ,
61+ t . nodes . string ( { sval : 'bool' } )
6962 ] ,
7063 typemod : - 1
7164 } )
@@ -74,7 +67,8 @@ it('SelectStmt with WHERE clause', () => {
7467 limitOption : 'LIMIT_OPTION_DEFAULT' ,
7568 op : 'SETOP_NONE'
7669 } ) ;
77- expect(deparse(selectStmt, {})).toEqual('SELECT * FROM myschema.mytable WHERE a = TRUE');
70+
71+ expect ( deparse ( selectStmt , { } ) ) . toEqual ( `SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)` ) ;
7872} ) ;
7973
8074it ( 'dynamic creation of tables' , ( ) => {
@@ -90,19 +84,19 @@ it('dynamic creation of tables', () => {
9084 } ;
9185
9286 // Construct the CREATE TABLE statement
93- const createStmt = ast .createStmt({
94- relation: ast.rangeVar({
87+ const createStmt = t . nodes . createStmt ( {
88+ relation : t . ast . rangeVar ( {
9589 relname : schema . tableName ,
9690 inh : true ,
9791 relpersistence : 'p'
98- }).RangeVar as RangeVar, // special case due to PG AST
99- tableElts: schema.columns.map(column => ast .columnDef({
92+ } ) ,
93+ tableElts : schema . columns . map ( column => t . nodes . columnDef ( {
10094 colname : column . name ,
101- typeName: ast.typeName({
102- names: [ast. string({ str : column.type })]
95+ typeName : t . ast . typeName ( {
96+ names : [ t . nodes . string ( { sval : column . type } ) ]
10397 } ) ,
10498 constraints : column . constraints ?. map ( constraint =>
105- ast .constraint({
99+ t . nodes . constraint ( {
106100 contype : constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL"
107101 } )
108102 )
0 commit comments