Skip to content

Commit

Permalink
Use vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
haishanh committed Jun 30, 2024
1 parent e32f6da commit 63d3e00
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 215 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"scripts": {
"dev": "vite dev",
"test": "NODE_OPTIONS='--import tsx' uvu src .test.ts",
"test": "vitest",
"test:2": "NODE_OPTIONS='--import tsx' uvu src .test.ts",
"do": "NODE_OPTIONS='--import tsx' node",
"test:1": "uvu -r esbuild-register -r tsconfig-paths/register",
"build": "vite build",
Expand Down Expand Up @@ -71,8 +72,8 @@
"typescript": "5.5.2",
"typescript-eslint": "8.0.0-alpha.34",
"undici": "6.19.2",
"uvu": "0.5.6",
"vite": "5.3.2"
"vite": "5.3.2",
"vitest": "1.6.0"
},
"type": "module",
"dependencies": {
Expand Down
437 changes: 416 additions & 21 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions src/lib/server/db/builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { test, expect } from 'vitest';

import { buildSelect } from './builder';

Expand All @@ -12,8 +11,8 @@ test('01', () => {
},
params: [1],
});
assert.equal(ret.query.split('\n'), ['SELECT id,title', 'FROM bookmark b', 'WHERE b.userId = ?']);
assert.equal(ret.params, [1]);
expect(ret.query.split('\n')).toEqual(['SELECT id,title', 'FROM bookmark b', 'WHERE b.userId = ?']);
expect(ret.params).toEqual([1]);
});

test('with limit offset', () => {
Expand All @@ -27,14 +26,14 @@ test('with limit offset', () => {
},
params: [1],
});
assert.equal(ret.query.split('\n'), [
expect(ret.query.split('\n')).toEqual([
'SELECT id,title',
'FROM bookmark b',
'WHERE b.userId = ?',
'LIMIT 20',
'OFFSET 100',
]);
assert.equal(ret.params, [1]);
expect(ret.params).toEqual([1]);
});

test('with as', () => {
Expand All @@ -59,7 +58,7 @@ test('with as', () => {
},
params: ['react'],
});
assert.equal(ret.query.split('\n'), [
expect(ret.query.split('\n')).toEqual([
'WITH original AS (SELECT id,title',
' FROM bookmark b',
' WHERE b.userId = ?)',
Expand All @@ -69,7 +68,7 @@ test('with as', () => {
'WHERE bookmark_fts match ?',
'ORDER BY bookmark_fts.rank',
]);
assert.equal(ret.params, [1, 'react']);
expect(ret.params).toEqual([1, 'react']);
});

test('with as and multiple joins', () => {
Expand All @@ -95,7 +94,7 @@ test('with as and multiple joins', () => {
},
params: ['react'],
});
assert.equal(ret.query.split('\n'), [
expect(ret.query.split('\n')).toEqual([
'WITH original AS (SELECT id,title',
' FROM bookmark b',
' JOIN bookmark_tag t0 on (t0.bookmarkId = b.id)',
Expand All @@ -107,7 +106,5 @@ test('with as and multiple joins', () => {
'WHERE bookmark_fts match ?',
'ORDER BY bookmark_fts.rank',
]);
assert.equal(ret.params, [1, 2, 3, 'react']);
expect(ret.params).toEqual([1, 2, 3, 'react']);
});

test.run();
261 changes: 125 additions & 136 deletions src/lib/server/db/builder2.test.ts
Original file line number Diff line number Diff line change
@@ -1,147 +1,136 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { describe, it, expect } from 'vitest';

import { delete_from, Eq, insert_into, Lte, OrderByDir, select_from, update_table, ValueToken } from './builder2';
import { Column, Table } from './identifier';

const select = suite('SelectFrom');

select('simple', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title]).build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0';
assert.equal(ret.source, expected);
assert.equal(ret.params, []);
});

select('+where', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 3))
.build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE t0.id = ?';
assert.equal(ret.source, expected);
assert.equal(ret.params, [3]);
});

select('+where+orderBy', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 10))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE t0.id = ? ORDER BY t0.id DESC';
assert.equal(ret.source, expected);
assert.equal(ret.params, [10]);
});

select('+where+orderBy+join', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 9))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.join(Bookmark.Id, Column.BookmarkTag.BookmarkId)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 JOIN bookmark_tag t1 ON t0.id = t1.bookmarkId WHERE t0.id = ? ORDER BY t0.id DESC';
assert.equal(ret.source, expected);
assert.equal(ret.params, [9]);
});

select('+where+orderBy+fts', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 3))
.where(Eq(Column.BookmarkFts.Fts, 'hello world'))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.join(Bookmark.Id, Column.BookmarkTag.BookmarkId)
.join(Bookmark.Id, Column.BookmarkFts.RowId)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 JOIN bookmark_tag t1 ON t0.id = t1.bookmarkId JOIN bookmark_fts t2 ON t0.id = t2.rowid WHERE t0.id = ? AND bookmark_fts = ? ORDER BY t0.id DESC';
assert.equal(ret.source, expected);
assert.equal(ret.params, [3, 'hello world']);
});

select('multi column where', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Lte([Bookmark.UpdatedAt, Bookmark.Id], [1234, 5]))
.orderBy(Bookmark.UpdatedAt, OrderByDir.Descending)
.orderBy(Bookmark.Id, OrderByDir.Descending)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE (t0.updatedAt,t0.id) <= (?,?) ORDER BY t0.updatedAt DESC,t0.id DESC';
assert.equal(ret.source, expected);
assert.equal(ret.params, [1234, 5]);
describe('SelectFrom', () => {
it('simple', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title]).build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([]);
});
it('+where', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 3))
.build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE t0.id = ?';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([3]);
});

it('+where+orderBy', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 10))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.build();
const expected = 'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE t0.id = ? ORDER BY t0.id DESC';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([10]);
});

it('+where+orderBy+join', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 9))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.join(Bookmark.Id, Column.BookmarkTag.BookmarkId)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 JOIN bookmark_tag t1 ON t0.id = t1.bookmarkId WHERE t0.id = ? ORDER BY t0.id DESC';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([9]);
});

it('+where+orderBy+fts', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Eq(Bookmark.Id, 3))
.where(Eq(Column.BookmarkFts.Fts, 'hello world'))
.orderBy(Bookmark.Id, OrderByDir.Descending)
.join(Bookmark.Id, Column.BookmarkTag.BookmarkId)
.join(Bookmark.Id, Column.BookmarkFts.RowId)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 JOIN bookmark_tag t1 ON t0.id = t1.bookmarkId JOIN bookmark_fts t2 ON t0.id = t2.rowid WHERE t0.id = ? AND bookmark_fts = ? ORDER BY t0.id DESC';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([3, 'hello world']);
});

it('multi column where', () => {
const Bookmark = Column.Bookmark;
const ret = select_from(Table.Bookmark, [Bookmark.Id, Bookmark.Url, Bookmark.Title])
.where(Lte([Bookmark.UpdatedAt, Bookmark.Id], [1234, 5]))
.orderBy(Bookmark.UpdatedAt, OrderByDir.Descending)
.orderBy(Bookmark.Id, OrderByDir.Descending)
.build();
const expected =
'SELECT t0.id,t0.url,t0.title FROM bookmark t0 WHERE (t0.updatedAt,t0.id) <= (?,?) ORDER BY t0.updatedAt DESC,t0.id DESC';
expect(ret.source).toEqual(expected);
expect(ret.params).toEqual([1234, 5]);
});
});

select.run();

const insert = suite('InsertInto');

insert('basic', () => {
const cols = Column.Bookmark;
const { source, params } = insert_into(Table.Bookmark)
.column(cols.UserId, 1)
.column(cols.Title, 'title0')
.column(cols.Desc, 'desc0')
.column(cols.Url, 'https://example.com')
.build();
assert.equal(source, 'insert into bookmark (userId,title,desc,url) values (?,?,?,?) returning *');
assert.equal(params, [1, 'title0', 'desc0', 'https://example.com']);
});

insert('strftime now', () => {
const cols = Column.Bookmark;
const { source, params } = insert_into(Table.Bookmark)
.column(cols.UserId, 1)
.column(cols.Title, 'title0')
.column(cols.CreatedAt, ValueToken.Now)
.column(cols.Desc, 'desc0')
.column(cols.Url, 'https://example.com')
.build();
assert.equal(
source,
"insert into bookmark (userId,title,createdAt,desc,url) values (?,?,strftime('%s','now'),?,?) returning *",
);
assert.equal(params, [1, 'title0', 'desc0', 'https://example.com']);
describe('InsertInto', () => {
it('basic', () => {
const cols = Column.Bookmark;
const { source, params } = insert_into(Table.Bookmark)
.column(cols.UserId, 1)
.column(cols.Title, 'title0')
.column(cols.Desc, 'desc0')
.column(cols.Url, 'https://example.com')
.build();
expect(source).toEqual('insert into bookmark (userId,title,desc,url) values (?,?,?,?) returning *');
expect(params).toEqual([1, 'title0', 'desc0', 'https://example.com']);
});

it('strftime now', () => {
const cols = Column.Bookmark;
const { source, params } = insert_into(Table.Bookmark)
.column(cols.UserId, 1)
.column(cols.Title, 'title0')
.column(cols.CreatedAt, ValueToken.Now)
.column(cols.Desc, 'desc0')
.column(cols.Url, 'https://example.com')
.build();
expect(source).toEqual(
"insert into bookmark (userId,title,createdAt,desc,url) values (?,?,strftime('%s','now'),?,?) returning *",
);
expect(params).toEqual([1, 'title0', 'desc0', 'https://example.com']);
});
});

insert.run();

const update = suite('UpdateTable');

update('basic', () => {
const cols = Column.Bookmark;
const { source, params } = update_table(Table.Bookmark)
.column(cols.Title, 'hello world')
.column(cols.UpdatedAt, ValueToken.Now)
.build();
assert.equal(source, "update bookmark set title = ?, updatedAt = strftime('%s','now')");
assert.equal(params, ['hello world']);
describe('UpdateTable', () => {
it('basic', () => {
const cols = Column.Bookmark;
const { source, params } = update_table(Table.Bookmark)
.column(cols.Title, 'hello world')
.column(cols.UpdatedAt, ValueToken.Now)
.build();
expect(source).toEqual("update bookmark set title = ?, updatedAt = strftime('%s','now')");
expect(params).toEqual(['hello world']);
});

it('with where', () => {
const cols = Column.Bookmark;
const { source, params } = update_table(Table.Bookmark)
.where(Eq(cols.Id, 10))
.column(cols.Title, 'hello world')
.column(cols.UpdatedAt, ValueToken.Now)
.build();
expect(source).toEqual("update bookmark set title = ?, updatedAt = strftime('%s','now') WHERE id = ?");
expect(params).toEqual(['hello world', 10]);
});
});

update('with where', () => {
const cols = Column.Bookmark;
const { source, params } = update_table(Table.Bookmark)
.where(Eq(cols.Id, 10))
.column(cols.Title, 'hello world')
.column(cols.UpdatedAt, ValueToken.Now)
.build();
assert.equal(source, "update bookmark set title = ?, updatedAt = strftime('%s','now') WHERE id = ?");
assert.equal(params, ['hello world', 10]);
describe('DeleteFrom', () => {
it('basic', () => {
const cols = Column.Bookmark;
const { source, params } = delete_from(Table.Bookmark).where(Eq(cols.Id, 10)).build();
expect(source).toEqual('delete from bookmark WHERE id = ?');
expect(params).toEqual([10]);
});
});

update.run();

const test_delete_from = suite('DeleteFrom');

test_delete_from('basic', () => {
const cols = Column.Bookmark;
const { source, params } = delete_from(Table.Bookmark).where(Eq(cols.Id, 10)).build();
assert.equal(source, 'delete from bookmark WHERE id = ?');
assert.equal(params, [10]);
});

test_delete_from.run();
Loading

0 comments on commit 63d3e00

Please sign in to comment.