Skip to content

Commit

Permalink
chore: add more aggregate functions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Sep 13, 2024
1 parent 4827272 commit b0a06fd
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
61 changes: 60 additions & 1 deletion test/relationships.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@ import { selectQueries } from './relationships'
expectType<TypeEqual<ExpectedType, typeof data>>(true)
}

// select with aggregate count and spread
{
const { data, error } = await selectQueries.selectWithAggregateCountAndSpread.limit(1).single()

if (error) {
throw new Error(error.message)
}
type ExpectedType = {
username: string
messages: Array<{
channels: {
count: number
details: string
}
}>
}
expectType<TypeEqual<ExpectedType, typeof data>>(true)
}


// select with aggregate sum function
{
const { data, error } = await selectQueries.selectWithAggregateSumFunction.limit(1).single()
Expand Down Expand Up @@ -462,9 +482,48 @@ import { selectQueries } from './relationships'
username: string
messages: Array<{
channels: {
sum: number
sum: number
}
}>
}
expectType<TypeEqual<ExpectedType, typeof data>>(true)
}

// select with aggregate sum and spread
{
const { data, error } = await selectQueries.selectWithAggregateSumAndSpread.limit(1).single()

if (error) {
throw new Error(error.message)
}
type ExpectedType = {
username: string
messages: Array<{
channels: {
sum: number
details: string
}
}>
}
expectType<TypeEqual<ExpectedType, typeof data>>(true)
}

// select with aggregate sum and spread on nested relation
{
const { data, error } = await selectQueries.selectWithAggregateSumAndSpreadOnNestedRelation.limit(1).single()

if (error) {
throw new Error(error.message)
}
type ExpectedType = {
username: string
messages: Array<{
channels: {
sum: number
details_sum: number
details: string
}
}>
}
expectType<TypeEqual<ExpectedType, typeof data>>(true)
}
100 changes: 100 additions & 0 deletions test/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,24 @@ export const selectQueries = {
selectWithAggregateNestedCountFunctionAndAlias: postgrest
.from('users')
.select('username, messages(channels(channel_count:count()))'),
selectWithAggregateCountAndSpread: postgrest
.from('users')
.select('username, messages(channels(count(), ...channel_details(details)))'),
selectWithAggregateSumFunction: postgrest.from('users').select('username, messages(id.sum())'),
selectWithAggregateAliasedSumFunction: postgrest
.from('users')
.select('username, messages(sum_id:id.sum())'),
selectWithAggregateSumFunctionOnNestedRelation: postgrest
.from('users')
.select('username, messages(channels(id.sum()))'),
selectWithAggregateSumAndSpread: postgrest
.from('users')
.select('username, messages(channels(id.sum(), ...channel_details(details)))'),
selectWithAggregateSumAndSpreadOnNestedRelation: postgrest
.from('users')
.select(
'username, messages(channels(id.sum(), ...channel_details(details_sum:id.sum(), details)))'
),
}

test('nested query with selective fields', async () => {
Expand Down Expand Up @@ -862,6 +873,35 @@ test('select with aggregate nested count function and alias', async () => {
`)
})

test('select with aggregate count and spread', async () => {
const res = await selectQueries.selectWithAggregateCountAndSpread.limit(1).single()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Object {
"messages": Array [
Object {
"channels": Object {
"count": 1,
"details": "Details for public channel",
},
},
Object {
"channels": Object {
"count": 1,
"details": "Details for random channel",
},
},
],
"username": "supabot",
},
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('select with aggregate sum function', async () => {
const res = await selectQueries.selectWithAggregateSumFunction.limit(1).single()
expect(res).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -928,3 +968,63 @@ test('select with aggregate sum function on nested relation', async () => {
}
`)
})

test('select with aggregate sum and spread', async () => {
const res = await selectQueries.selectWithAggregateSumAndSpread.limit(1).single()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Object {
"messages": Array [
Object {
"channels": Object {
"details": "Details for public channel",
"sum": 1,
},
},
Object {
"channels": Object {
"details": "Details for random channel",
"sum": 2,
},
},
],
"username": "supabot",
},
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('select with aggregate sum and spread on nested relation', async () => {
const res = await selectQueries.selectWithAggregateSumAndSpreadOnNestedRelation.limit(1).single()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Object {
"messages": Array [
Object {
"channels": Object {
"details": "Details for public channel",
"details_sum": 1,
"sum": 1,
},
},
Object {
"channels": Object {
"details": "Details for random channel",
"details_sum": 2,
"sum": 2,
},
},
],
"username": "supabot",
},
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

0 comments on commit b0a06fd

Please sign in to comment.