Skip to content

Commit eb3a588

Browse files
Add snapshots with pretty=false to validate formatting scoping
- Added tests with pretty=false for all 6 misc SQL cases to ensure no formatting is applied when pretty is disabled - Generated 12 new snapshots (6 pretty=true, 6 pretty=false) to validate correct scoping behavior - Verified that pretty=false produces single-line SQL output while pretty=true produces formatted SQL - All pretty formatting logic in deparser.ts is properly guarded with this.formatter.isPretty() checks - Ensures formatting is only applied when pretty=true, addressing scoping concerns Co-Authored-By: Dan Lynch <[email protected]>
1 parent daf8085 commit eb3a588

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

packages/deparser/__tests__/pretty/__snapshots__/misc-pretty.test.ts.snap

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Pretty Misc SQL formatting should format misc-1: Complex CTE with joins and aggregation 1`] = `
3+
exports[`Pretty Misc SQL formatting should format misc-1: Complex CTE with joins and aggregation (non-pretty) 1`] = `"WITH recent_orders AS (SELECT o.id, o.user_id, o.created_at FROM orders AS o WHERE o.created_at > (now() - '30 days'::interval)), high_value_orders AS (SELECT r.user_id, count(*) AS order_count, sum(oi.price * oi.quantity) AS total_spent FROM recent_orders AS r JOIN order_items AS oi ON r.id = oi.order_id GROUP BY r.user_id) SELECT u.id, u.name, h.total_spent FROM users AS u JOIN high_value_orders AS h ON u.id = h.user_id WHERE h.total_spent > 1000 ORDER BY h.total_spent DESC"`;
4+
5+
exports[`Pretty Misc SQL formatting should format misc-1: Complex CTE with joins and aggregation (pretty) 1`] = `
46
"WITH
57
recent_orders AS (SELECT
68
o.id,
@@ -29,7 +31,9 @@ ORDER BY
2931
h.total_spent DESC"
3032
`;
3133

32-
exports[`Pretty Misc SQL formatting should format misc-2: Window functions with FILTER and GROUPING SETS 1`] = `
34+
exports[`Pretty Misc SQL formatting should format misc-2: Window functions with FILTER and GROUPING SETS (non-pretty) 1`] = `"SELECT department, employee_id, count(*) FILTER (WHERE status = 'active') OVER (PARTITION BY department) AS active_count, rank() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank FROM employee_status GROUP BY GROUPING SETS (department, (department, employee_id))"`;
35+
36+
exports[`Pretty Misc SQL formatting should format misc-2: Window functions with FILTER and GROUPING SETS (pretty) 1`] = `
3337
"SELECT
3438
department,
3539
employee_id,
@@ -43,7 +47,9 @@ GROUP BY
4347
GROUPING SETS (department, (department, employee_id))"
4448
`;
4549

46-
exports[`Pretty Misc SQL formatting should format misc-3: LATERAL joins with JSON functions 1`] = `
50+
exports[`Pretty Misc SQL formatting should format misc-3: LATERAL joins with JSON functions (non-pretty) 1`] = `"SELECT u.id, u.name, j.key, j.value FROM users AS u, LATERAL jsonb_each_text(u.preferences) AS j(key, value) WHERE j.key LIKE 'notif_%' AND CAST(j.value AS boolean) = true"`;
51+
52+
exports[`Pretty Misc SQL formatting should format misc-3: LATERAL joins with JSON functions (pretty) 1`] = `
4753
"SELECT
4854
u.id,
4955
u.name,
@@ -55,7 +61,9 @@ WHERE
5561
AND CAST(j.value AS boolean) = true"
5662
`;
5763

58-
exports[`Pretty Misc SQL formatting should format misc-4: EXISTS with nested subqueries and CASE 1`] = `
64+
exports[`Pretty Misc SQL formatting should format misc-4: EXISTS with nested subqueries and CASE (non-pretty) 1`] = `"SELECT p.id, p.title, CASE WHEN EXISTS (SELECT 1 FROM reviews AS r WHERE r.product_id = p.id AND r.rating >= 4) THEN 'Popular' ELSE 'Unrated' END AS status FROM products AS p WHERE p.archived = false"`;
65+
66+
exports[`Pretty Misc SQL formatting should format misc-4: EXISTS with nested subqueries and CASE (pretty) 1`] = `
5967
"SELECT
6068
p.id,
6169
p.title,
@@ -73,7 +81,9 @@ WHERE
7381
p.archived = false"
7482
`;
7583

76-
exports[`Pretty Misc SQL formatting should format misc-5: Nested CTEs with type casts and subqueries 1`] = `
84+
exports[`Pretty Misc SQL formatting should format misc-5: Nested CTEs with type casts and subqueries (non-pretty) 1`] = `"WITH logs AS (SELECT id, CAST(payload AS pg_catalog.json) ->> 'event' AS event, CAST(CAST(payload AS pg_catalog.json) ->> 'ts' AS pg_catalog.timestamp) AS ts FROM event_log WHERE ts > (now() - '7 days'::interval)) SELECT event, count(*) AS freq FROM ( SELECT DISTINCT event, ts::date AS event_day FROM logs ) AS d GROUP BY event ORDER BY freq DESC"`;
85+
86+
exports[`Pretty Misc SQL formatting should format misc-5: Nested CTEs with type casts and subqueries (pretty) 1`] = `
7787
"WITH
7888
logs AS (SELECT
7989
id,
@@ -95,7 +105,9 @@ ORDER BY
95105
freq DESC"
96106
`;
97107

98-
exports[`Pretty Misc SQL formatting should format misc-6: Complex multi-table joins with nested conditions 1`] = `
108+
exports[`Pretty Misc SQL formatting should format misc-6: Complex multi-table joins with nested conditions (non-pretty) 1`] = `"SELECT o.id AS order_id, u.name AS user_name, p.name AS product_name, s.status, sh.shipped_at, r.refund_amount FROM orders AS o JOIN users AS u ON o.user_id = u.id JOIN order_items AS oi ON oi.order_id = o.id JOIN products AS p ON (p.id = oi.product_id AND p.available = true) OR (p.sku = oi.product_sku AND (p.discontinued = false OR p.replacement_id IS NOT NULL)) LEFT JOIN shipping AS sh ON sh.order_id = o.id AND ((sh.carrier = 'UPS' AND sh.tracking_number IS NOT NULL) OR (sh.carrier = 'FedEx' AND sh.shipped_at > (o.created_at + '1 day'::interval))) LEFT JOIN statuses AS s ON s.id = o.status_id AND (s.name <> 'cancelled' OR (s.name = 'cancelled' AND s.updated_at > (now() - '7 days'::interval))) LEFT JOIN refunds AS r ON r.order_id = o.id AND ((r.status = 'approved' AND r.processed_at IS NOT NULL) OR (r.status = 'pending' AND r.requested_at < (now() - '14 days'::interval))) WHERE o.created_at > (now() - '90 days'::interval) AND u.active = true AND (s.status = 'shipped' OR (s.status = 'processing' AND EXISTS (SELECT 1 FROM order_notes AS n WHERE (n.order_id = o.id AND n.note ILIKE '%expedite%')))) ORDER BY o.created_at DESC"`;
109+
110+
exports[`Pretty Misc SQL formatting should format misc-6: Complex multi-table joins with nested conditions (pretty) 1`] = `
99111
"SELECT
100112
o.id AS order_id,
101113
u.name AS user_name,

packages/deparser/__tests__/pretty/misc-pretty.test.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,66 @@ WHERE o.created_at > NOW() - INTERVAL '90 days'
109109
)
110110
ORDER BY o.created_at DESC`;
111111

112-
it('should format misc-1: Complex CTE with joins and aggregation', async () => {
112+
it('should format misc-1: Complex CTE with joins and aggregation (pretty)', async () => {
113113
const result = await expectParseDeparse(misc1Sql, { pretty: true });
114114
expect(result).toMatchSnapshot();
115115
});
116116

117-
it('should format misc-2: Window functions with FILTER and GROUPING SETS', async () => {
117+
it('should format misc-1: Complex CTE with joins and aggregation (non-pretty)', async () => {
118+
const result = await expectParseDeparse(misc1Sql, { pretty: false });
119+
expect(result).toMatchSnapshot();
120+
});
121+
122+
it('should format misc-2: Window functions with FILTER and GROUPING SETS (pretty)', async () => {
118123
const result = await expectParseDeparse(misc2Sql, { pretty: true });
119124
expect(result).toMatchSnapshot();
120125
});
121126

122-
it('should format misc-3: LATERAL joins with JSON functions', async () => {
127+
it('should format misc-2: Window functions with FILTER and GROUPING SETS (non-pretty)', async () => {
128+
const result = await expectParseDeparse(misc2Sql, { pretty: false });
129+
expect(result).toMatchSnapshot();
130+
});
131+
132+
it('should format misc-3: LATERAL joins with JSON functions (pretty)', async () => {
123133
const result = await expectParseDeparse(misc3Sql, { pretty: true });
124134
expect(result).toMatchSnapshot();
125135
});
126136

127-
it('should format misc-4: EXISTS with nested subqueries and CASE', async () => {
137+
it('should format misc-3: LATERAL joins with JSON functions (non-pretty)', async () => {
138+
const result = await expectParseDeparse(misc3Sql, { pretty: false });
139+
expect(result).toMatchSnapshot();
140+
});
141+
142+
it('should format misc-4: EXISTS with nested subqueries and CASE (pretty)', async () => {
128143
const result = await expectParseDeparse(misc4Sql, { pretty: true });
129144
expect(result).toMatchSnapshot();
130145
});
131146

132-
it('should format misc-5: Nested CTEs with type casts and subqueries', async () => {
147+
it('should format misc-4: EXISTS with nested subqueries and CASE (non-pretty)', async () => {
148+
const result = await expectParseDeparse(misc4Sql, { pretty: false });
149+
expect(result).toMatchSnapshot();
150+
});
151+
152+
it('should format misc-5: Nested CTEs with type casts and subqueries (pretty)', async () => {
133153
const result = await expectParseDeparse(misc5Sql, { pretty: true });
134154
expect(result).toMatchSnapshot();
135155
});
136156

137-
it('should format misc-6: Complex multi-table joins with nested conditions', async () => {
157+
it('should format misc-5: Nested CTEs with type casts and subqueries (non-pretty)', async () => {
158+
const result = await expectParseDeparse(misc5Sql, { pretty: false });
159+
expect(result).toMatchSnapshot();
160+
});
161+
162+
it('should format misc-6: Complex multi-table joins with nested conditions (pretty)', async () => {
138163
const result = await expectParseDeparse(misc6Sql, { pretty: true });
139164
expect(result).toMatchSnapshot();
140165
});
141166

167+
it('should format misc-6: Complex multi-table joins with nested conditions (non-pretty)', async () => {
168+
const result = await expectParseDeparse(misc6Sql, { pretty: false });
169+
expect(result).toMatchSnapshot();
170+
});
171+
142172
it('should validate AST equivalence for all misc cases', async () => {
143173
const testCases = [misc1Sql, misc2Sql, misc3Sql, misc4Sql, misc5Sql, misc6Sql];
144174

0 commit comments

Comments
 (0)