Skip to content

Commit

Permalink
Add test for Kysely query with relation
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Aug 1, 2024
1 parent d087638 commit 2a7f1c5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 63 deletions.
121 changes: 60 additions & 61 deletions test/drizzle/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,66 @@ describe('drizzle driver', () => {
expect(select2).toEqual([{ name: 'white rice' }, { name: 'bread' }]);
});

it('should accept batched queries', async () => {
const data = await db.batch([
db.insert(groceries).values({ name: 'bread' }),
db
.insert(groceries)
.values({ name: 'rice' })
.returning({ name: groceries.name }),
db.insert(groceries).values({ name: 'milk' }).returning(),
db.select().from(groceries),
]);

expect(data).toEqual([
{ rows: [], columns: [] },
[{ name: 'rice' }],
[{ id: 3, name: 'milk' }],
[
{ id: 1, name: 'bread' },
{ id: 2, name: 'rice' },
{ id: 3, name: 'milk' },
],
]);
});

it('should execute relational queries', async () => {
await db.batch([
db.insert(groceries).values([{ name: 'chicken' }, { name: 'beef' }]),
db.insert(prices).values([
{ groceryId: 1, price: 3.29 },
{ groceryId: 1, price: 2.99 },
{ groceryId: 1, price: 3.79 },
{ groceryId: 2, price: 5.29 },
{ groceryId: 2, price: 4.49 },
]),
]);

const data = await db.query.groceries.findMany({
columns: {
name: true,
},
with: {
prices: {
columns: {
price: true,
},
},
},
});

expect(data).toEqual([
{
name: 'chicken',
prices: [{ price: 3.29 }, { price: 2.99 }, { price: 3.79 }],
},
{
name: 'beef',
prices: [{ price: 5.29 }, { price: 4.49 }],
},
]);
});

it('should perform successful transaction using sqlocal way', async () => {
const productName = 'rice';
const productPrice = 2.99;
Expand Down Expand Up @@ -203,65 +263,4 @@ describe('drizzle driver', () => {
expect(data).toEqual([{ name: 'x' }, { name: 'b' }]);
expect(order).toEqual([1, 2, 3]);
});

it('should accept batched queries', async () => {
const data = await db.batch([
db.insert(groceries).values({ name: 'bread' }),
db
.insert(groceries)
.values({ name: 'rice' })
.returning({ name: groceries.name }),
db.insert(groceries).values({ name: 'milk' }).returning(),
db.select().from(groceries),
]);

expect(data).toEqual([
{ rows: [], columns: [] },
[{ name: 'rice' }],
[{ id: 3, name: 'milk' }],
[
{ id: 1, name: 'bread' },
{ id: 2, name: 'rice' },
{ id: 3, name: 'milk' },
],
]);
});

it('should execute relational queries', async () => {
await db.batch([
db.insert(groceries).values({ name: 'chicken' }),
db.insert(groceries).values({ name: 'beef' }),
db.insert(prices).values([
{ groceryId: 1, price: 3.29 },
{ groceryId: 1, price: 2.99 },
{ groceryId: 1, price: 3.79 },
{ groceryId: 2, price: 5.29 },
{ groceryId: 2, price: 4.49 },
]),
]);

const data = await db.query.groceries.findMany({
columns: {
name: true,
},
with: {
prices: {
columns: {
price: true,
},
},
},
});

expect(data).toEqual([
{
name: 'chicken',
prices: [{ price: 3.29 }, { price: 2.99 }, { price: 3.79 }],
},
{
name: 'beef',
prices: [{ price: 5.29 }, { price: 4.49 }],
},
]);
});
});
49 changes: 47 additions & 2 deletions test/kysely/dialect.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { Generated, Kysely } from 'kysely';
import { Generated, Kysely, ParseJSONResultsPlugin } from 'kysely';
import { jsonArrayFrom } from 'kysely/helpers/sqlite';
import { SQLocalKysely } from '../../src/kysely';
import { sleep } from '../test-utils/sleep';

describe('kysely dialect', () => {
const { dialect, transaction } = new SQLocalKysely(
'kysely-dialect-test.sqlite3'
);
const db = new Kysely<DB>({ dialect });
const db = new Kysely<DB>({
dialect,
plugins: [new ParseJSONResultsPlugin()],
});

type DB = {
groceries: {
Expand Down Expand Up @@ -81,6 +85,47 @@ describe('kysely dialect', () => {
expect(select2).toEqual([{ name: 'white rice' }, { name: 'bread' }]);
});

it('should execute queries with relations', async () => {
await db
.insertInto('groceries')
.values([{ name: 'chicken' }, { name: 'beef' }])
.execute();
await db
.insertInto('prices')
.values([
{ groceryId: 1, price: 3.29 },
{ groceryId: 1, price: 2.99 },
{ groceryId: 1, price: 3.79 },
{ groceryId: 2, price: 5.29 },
{ groceryId: 2, price: 4.49 },
])
.execute();

const data = await db
.selectFrom('groceries')
.select('name')
.select((eb) => [
jsonArrayFrom(
eb
.selectFrom('prices')
.select('price')
.whereRef('groceries.id', '=', 'prices.groceryId')
).as('prices'),
])
.execute();

expect(data).toEqual([
{
name: 'chicken',
prices: [{ price: 3.29 }, { price: 2.99 }, { price: 3.79 }],
},
{
name: 'beef',
prices: [{ price: 5.29 }, { price: 4.49 }],
},
]);
});

it('should perform successful transaction using sqlocal way', async () => {
const productName = 'rice';
const productPrice = 2.99;
Expand Down

0 comments on commit 2a7f1c5

Please sign in to comment.