Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
zfben committed Feb 1, 2025
1 parent 71fc976 commit a32a3ab
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import postgres from 'postgres'
import { type Client, createClient } from '../client'
import { createTestingPostgres } from './utils'

describe('client', () => {
let client: Client

beforeAll(() => {
client = createClient(postgres(process.env.PG_CONNECTION || 'postgresql://development@pg/development'))
client = createClient(createTestingPostgres())
})

afterAll(async () => {
Expand All @@ -31,9 +31,9 @@ describe('client', () => {
})

it('string with type cast', async () => {
expect(await client.raw('SELECT ?::integer + ?::integer', [1, 1])).toEqual([
{ '?column?': 2 },
])
expect(
await client.raw('SELECT ?::integer + ?::integer', [1, 1])
).toEqual([{ '?column?': 2 }])
})

it('template with type cast', async () => {
Expand Down
90 changes: 66 additions & 24 deletions src/__tests__/query-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import postgres from 'postgres'
import { type Client, createClient } from '../client'
import { QueryBuilder } from '../query-builder'
import { createTestingPostgres } from './utils'

describe('QueryBuilder', () => {
let client: Client

beforeAll(async () => {
client = createClient(postgres('postgresql://development@pg/development', { debug: true }))
client = createClient(createTestingPostgres())

await client.raw`
CREATE TABLE users (
Expand All @@ -32,117 +32,159 @@ describe('QueryBuilder', () => {
it('selects all columns', async () => {
const result = await new QueryBuilder(client, 'users').select().all()

expect(result).toEqual([{
id: 1, name: 'Alice', metadata: {
age: 100
}
}, { id: 2, name: 'Bob', metadata: {} }])
expect(result).toEqual([
{
id: 1,
name: 'Alice',
metadata: {
age: 100,
},
},
{ id: 2, name: 'Bob', metadata: {} },
])
})

it('selects specific columns', async () => {
const result = await new QueryBuilder(client, 'users').select('name').all()
const result = await new QueryBuilder(client, 'users')
.select('name')
.all()

expect(result).toEqual([{ name: 'Alice' }, { name: 'Bob' }])
})
})

describe('where', () => {
it('column and value', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'Alice').pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'Alice')
.pluck('id')

expect(result).toEqual([1])
})

it('column, operator, and value', async () => {
const result = await new QueryBuilder(client, 'users').where('name', '!=', 'Alice').pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', '!=', 'Alice')
.pluck('id')

expect(result).toEqual([2])
})

describe('operator', () => {
it('IS NULL', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'IS NULL').pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'IS NULL')
.pluck('id')

expect(result).toEqual([])
})

it('IS NOT NULL', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'IS NOT NULL').pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'IS NOT NULL')
.pluck('id')

expect(result).toEqual([1, 2])
})

it('IN', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'IN', ['Alice']).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'IN', ['Alice'])
.pluck('id')

expect(result).toEqual([1])
})

it('NOT IN', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'NOT IN', ['Alice']).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'NOT IN', ['Alice'])
.pluck('id')

expect(result).toEqual([2])
})

it('>', async () => {
const result = await new QueryBuilder(client, 'users').where('id', '>', 1).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('id', '>', 1)
.pluck('id')

expect(result).toEqual([2])
})

it('>=', async () => {
const result = await new QueryBuilder(client, 'users').where('id', '>=', 1).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('id', '>=', 1)
.pluck('id')

expect(result).toEqual([1, 2])
})

it('<', async () => {
const result = await new QueryBuilder(client, 'users').where('id', '<', 2).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('id', '<', 2)
.pluck('id')

expect(result).toEqual([1])
})

it('<=', async () => {
const result = await new QueryBuilder(client, 'users').where('id', '<=', 2).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('id', '<=', 2)
.pluck('id')

expect(result).toEqual([1, 2])
})

it('!=', async () => {
const result = await new QueryBuilder(client, 'users').where('name', '!=', 'Alice').pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', '!=', 'Alice')
.pluck('id')

expect(result).toEqual([2])
})

it('@>', async () => {
const result = await new QueryBuilder(client, 'users').where('metadata', '@>', { age: 100 }).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('metadata', '@>', { age: 100 })
.pluck('id')

expect(result).toEqual([1])
})

it('throws error for invalid operator', async () => {
await expect(() => new QueryBuilder(client, 'users').where('name', 'invalid' as any, 'Alice').pluck('id')).toThrowError('Invalid operator: invalid')
await expect(() =>
new QueryBuilder(client, 'users')
.where('name', 'invalid' as any, 'Alice')
.pluck('id')
).toThrowError('Invalid operator: invalid')
})
})

it('multiple conditions', async () => {
const result = await new QueryBuilder(client, 'users').where('name', 'Alice').where('id', '>', 1).pluck('id')
const result = await new QueryBuilder(client, 'users')
.where('name', 'Alice')
.where('id', '>', 1)
.pluck('id')

expect(result).toEqual([])
})
})

describe('limit', () => {
it('limits the number of results', async () => {
const result = await new QueryBuilder(client, 'users').limit(1).pluck('id')
const result = await new QueryBuilder(client, 'users')
.limit(1)
.pluck('id')

expect(result).toEqual([1])
})
})

describe('offset', () => {
it('offsets the results', async () => {
const result = await new QueryBuilder(client, 'users').offset(1).pluck('id')
const result = await new QueryBuilder(client, 'users')
.offset(1)
.pluck('id')

expect(result).toEqual([2])
})
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import postgres from 'postgres'

export function createTestingPostgres() {
return postgres(process.env.PG_CONNECTION || 'postgresql://development@pg/development')
}

0 comments on commit a32a3ab

Please sign in to comment.