Skip to content

Commit c6f2688

Browse files
committed
attempt at meta
1 parent 61342c5 commit c6f2688

File tree

10 files changed

+17890
-5168
lines changed

10 files changed

+17890
-5168
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`AST to AST with runtime schema should generate t.ast.* for wrapped nodes and t.nodes.* for non-wrapped nodes 1`] = `
4+
"t.ast.selectStmt({
5+
targetList: [t.ast.resTarget({
6+
val: t.ast.columnRef({
7+
fields: [t.ast.aStar({})]
8+
})
9+
})],
10+
fromClause: [t.ast.rangeVar({
11+
relname: "users_table",
12+
inh: true,
13+
relpersistence: "p"
14+
})],
15+
limitOption: "LIMIT_OPTION_DEFAULT",
16+
op: "SETOP_NONE"
17+
})"
18+
`;
19+
20+
exports[`AST to AST with runtime schema should handle arrays of nodes 1`] = `
21+
"t.ast.list({
22+
items: [t.ast.string({
23+
sval: "first"
24+
}), t.ast.string({
25+
sval: "second"
26+
}), t.ast.string({
27+
sval: "third"
28+
})]
29+
})"
30+
`;
31+
32+
exports[`AST to AST with runtime schema should handle empty objects and null values 1`] = `
33+
"t.ast.resTarget({
34+
name: null,
35+
val: t.ast.columnRef({
36+
fields: [t.ast.aStar({})]
37+
})
38+
})"
39+
`;
40+
41+
exports[`AST to AST with runtime schema should handle mixed wrapped and non-wrapped nodes 1`] = `
42+
"t.ast.aExpr({
43+
kind: "AEXPR_OP",
44+
name: [t.ast.string({
45+
sval: "="
46+
})],
47+
lexpr: t.ast.columnRef({
48+
fields: [t.ast.string({
49+
sval: "id"
50+
})]
51+
}),
52+
rexpr: t.ast.aConst({
53+
ival: t.nodes.aConst({
54+
ival: 42
55+
})
56+
})
57+
})"
58+
`;
59+
60+
exports[`AST to AST with runtime schema should use t.nodes.* for non-wrapped nodes 1`] = `
61+
"t.nodes.parseResult({
62+
version: 160001,
63+
stmts: [t.ast.rawStmt({
64+
stmt: t.nodes.selectStmt({
65+
targetList: [t.nodes.resTarget({
66+
val: t.nodes.columnRef({
67+
fields: [t.nodes.aStar({})]
68+
})
69+
})],
70+
limitOption: "LIMIT_OPTION_DEFAULT",
71+
op: "SETOP_NONE"
72+
})
73+
})]
74+
})"
75+
`;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import * as t from '../test-utils/meta';
2+
import { A_Expr, List, ParseResult, ResTarget, SelectStmt } from '@pgsql/types';
3+
import { generateTsAstCodeFromPgAstWithSchema } from '../src/utils';
4+
import generate from '@babel/generator';
5+
import { runtimeSchema } from '../test-utils/meta/runtime-schema';
6+
7+
describe('AST to AST with runtime schema', () => {
8+
it('should generate t.ast.* for wrapped nodes and t.nodes.* for non-wrapped nodes', () => {
9+
// Create a simple AST using the wrapped helpers
10+
const selectStmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
11+
targetList: [
12+
t.nodes.resTarget({
13+
val: t.nodes.columnRef({
14+
fields: [t.nodes.aStar()]
15+
})
16+
})
17+
],
18+
fromClause: [
19+
t.nodes.rangeVar({
20+
relname: 'users_table',
21+
inh: true,
22+
relpersistence: 'p'
23+
})
24+
],
25+
limitOption: 'LIMIT_OPTION_DEFAULT',
26+
op: 'SETOP_NONE'
27+
});
28+
29+
// Generate code using the new function with runtime schema
30+
const astForAst = generateTsAstCodeFromPgAstWithSchema(selectStmt, runtimeSchema);
31+
const generatedCode = generate(astForAst).code;
32+
expect(generatedCode).toMatchSnapshot();
33+
});
34+
35+
it('should handle mixed wrapped and non-wrapped nodes', () => {
36+
// Create an AST with both wrapped and non-wrapped nodes
37+
const testAst: { A_Expr: A_Expr } = t.nodes.aExpr({
38+
kind: 'AEXPR_OP',
39+
name: [t.nodes.string({ sval: '=' })],
40+
lexpr: t.nodes.columnRef({
41+
fields: [t.nodes.string({ sval: 'id' })]
42+
}),
43+
rexpr: t.nodes.aConst({
44+
ival: t.ast.integer({ ival: 42 })
45+
})
46+
});
47+
48+
const astForAst = generateTsAstCodeFromPgAstWithSchema(testAst, runtimeSchema);
49+
const generatedCode = generate(astForAst).code;
50+
expect(generatedCode).toMatchSnapshot();
51+
});
52+
53+
it('should handle arrays of nodes', () => {
54+
const testAst: { List: List } = t.nodes.list({
55+
items: [
56+
t.nodes.string({ sval: 'first' }),
57+
t.nodes.string({ sval: 'second' }),
58+
t.nodes.string({ sval: 'third' })
59+
]
60+
});
61+
62+
const astForAst = generateTsAstCodeFromPgAstWithSchema(testAst, runtimeSchema);
63+
const generatedCode = generate(astForAst).code;
64+
expect(generatedCode).toMatchSnapshot();
65+
});
66+
67+
it('should handle empty objects and null values', () => {
68+
const testAst: { ResTarget: ResTarget } = t.nodes.resTarget({
69+
name: null,
70+
val: t.nodes.columnRef({
71+
fields: [t.nodes.aStar()]
72+
})
73+
});
74+
75+
const astForAst = generateTsAstCodeFromPgAstWithSchema(testAst, runtimeSchema);
76+
const generatedCode = generate(astForAst).code;
77+
expect(generatedCode).toMatchSnapshot();
78+
});
79+
80+
it('should use t.nodes.* for non-wrapped nodes', () => {
81+
// Test that unwrapped nodes (plain objects) are converted to t.nodes.* calls
82+
const testAst: { ParseResult: ParseResult } = t.nodes.parseResult({
83+
version: 160001,
84+
stmts: [
85+
t.ast.rawStmt({
86+
stmt: {
87+
// This is an unwrapped SelectStmt - just a plain object with SelectStmt fields
88+
targetList: [
89+
{
90+
ResTarget: {
91+
val: {
92+
ColumnRef: {
93+
fields: [
94+
{ A_Star: {} }
95+
]
96+
}
97+
}
98+
}
99+
}
100+
],
101+
limitOption: 'LIMIT_OPTION_DEFAULT',
102+
op: 'SETOP_NONE'
103+
}
104+
})
105+
]
106+
});
107+
108+
const astForAst = generateTsAstCodeFromPgAstWithSchema(testAst, runtimeSchema);
109+
const generatedCode = generate(astForAst).code;
110+
expect(generatedCode).toMatchSnapshot();
111+
});
112+
});

packages/proto-parser/__tests__/meta.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import ast from '../test-utils/utils/asts';
1+
import wrapped from '../test-utils/meta/wrapped';
22
import { generateTsAstCodeFromPgAst } from '../src/utils'
33
import generate from '@babel/generator';
44

55
it('AST to AST to create AST — meta 🤯', () => {
6-
const selectStmt = ast.selectStmt({
6+
const selectStmt = wrapped.selectStmt({
77
targetList: [
8-
ast.resTarget({
9-
val: ast.columnRef({
10-
fields: [ast.aStar()]
8+
wrapped.resTarget({
9+
val: wrapped.columnRef({
10+
fields: [wrapped.aStar()]
1111
})
1212
})
1313
],
1414
fromClause: [
15-
ast.rangeVar({
15+
wrapped.rangeVar({
1616
relname: 'some_amazing_table',
1717
inh: true,
1818
relpersistence: 'p'

0 commit comments

Comments
 (0)