Skip to content

Commit

Permalink
Added a test for the new CTE materialization option
Browse files Browse the repository at this point in the history
  • Loading branch information
gp27 committed Jan 7, 2025
1 parent 7ea6b9a commit 163f7d7
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions integration-tests/tests/pg/pg-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,49 @@ export function tests() {
]);
});

test('join on materialized aliased sql from with clause', async (ctx) => {
const { db } = ctx.pg;

const users = db.$with('users', { materialized: true }).as(
db.select({
id: sql<number>`id`.as('userId'),
name: sql<string>`name`.as('userName'),
city: sql<string>`city`.as('city'),
}).from(
sql`(select 1 as id, 'John' as name, 'New York' as city) as users`,
),
);

const cities = db.$with('cities', { materialized: false }).as(
db.select({
id: sql<number>`id`.as('cityId'),
name: sql<string>`name`.as('cityName'),
}).from(
sql`(select 1 as id, 'Paris' as name) as cities`,
),
);

const result = await db
.with(users, cities)
.select({
userId: users.id,
name: users.name,
userCity: users.city,
cityId: cities.id,
cityName: cities.name,
})
.from(users)
.leftJoin(cities, (cols) => eq(cols.cityId, cols.userId));

Expect<
Equal<{ userId: number; name: string; userCity: string; cityId: number; cityName: string }[], typeof result>
>;

expect(result).toEqual([
{ userId: 1, name: 'John', userCity: 'New York', cityId: 1, cityName: 'Paris' },
]);
});

test('prefixed table', async (ctx) => {
const { db } = ctx.pg;

Expand Down

0 comments on commit 163f7d7

Please sign in to comment.