Skip to content

Commit

Permalink
Added minor unit tests to confirm relational queries return objects i…
Browse files Browse the repository at this point in the history
…nstead of value arrays.
  • Loading branch information
Chriztiaan committed Dec 3, 2024
1 parent 3513514 commit 83ac246
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions packages/drizzle-driver/tests/sqlite/watch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common';
import { PowerSyncDatabase } from '@powersync/web';
import { count, eq, sql } from 'drizzle-orm';
import { count, eq, relations, sql } from 'drizzle-orm';
import { integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import * as SUT from '../../src/sqlite/db';
Expand Down Expand Up @@ -52,7 +52,18 @@ const customers = sqliteTable('customers', {
email: text('email')
});

const DrizzleSchema = { assets, customers };
export const customersRelations = relations(customers, ({ many }) => ({
assets: many(assets)
}));

export const assetsRelations = relations(assets, ({ one }) => ({
customer: one(customers, {
fields: [assets.customer_id],
references: [customers.id]
})
}));

const DrizzleSchema = { assets, customers, assetsRelations, customersRelations };

/**
* There seems to be an issue with Vitest browser mode's setTimeout and
Expand Down Expand Up @@ -280,4 +291,85 @@ describe('Watch Tests', () => {
expect(receivedWithManagedOverflowCount).greaterThan(2);
expect(receivedWithManagedOverflowCount).toBeLessThanOrEqual(4);
});

it('should contain relational data (one to many)', async () => {
const abortController = new AbortController();

const query = db.query.customers.findMany({ with: { assets: true } });

let receivedResult: any = undefined;

const receivedUpdates = new Promise<void>((resolve) => {
const onUpdate = (update) => {
receivedResult = update;
resolve();
};

db.watch(query, { onResult: onUpdate }, { signal: abortController.signal });
});

const customerId = '39281cf9-9989-4b31-bb21-8f45ce3b3e60';
const assetId = '00000000-9989-4b31-bb21-8f45ce3b3e61';
await db
.insert(customers)
.values({
id: customerId,
name: 'Alice'
})
.execute();

await db
.insert(assets)
.values({
id: assetId,
customer_id: customerId
})
.execute();

await receivedUpdates;
abortController.abort();

expect(receivedResult[0].assets[0]['id']).toEqual(assetId);
expect(receivedResult[0].assets[0]['customer_id']).toEqual(customerId);
});

it('should contain relational data (many to one)', async () => {
const abortController = new AbortController();

const query = db.query.assets.findFirst({ with: { customer: true } });

let receivedResult: any = undefined;

const receivedUpdates = new Promise<void>((resolve) => {
const onUpdate = (update) => {
receivedResult = update;
resolve();
};

db.watch(query, { onResult: onUpdate }, { signal: abortController.signal });
});

const customerId = '39281cf9-9989-4b31-bb21-8f45ce3b3e60';
const assetId = '00000000-9989-4b31-bb21-8f45ce3b3e61';
await db
.insert(customers)
.values({
id: customerId,
name: 'Alice'
})
.execute();

await db
.insert(assets)
.values({
id: assetId,
customer_id: customerId
})
.execute();

await receivedUpdates;
abortController.abort();

expect(receivedResult[0].customer['id']).toEqual(customerId);
});
});

0 comments on commit 83ac246

Please sign in to comment.