This commit is contained in:
@@ -8,9 +8,10 @@ beforeEach(async () => {
|
||||
});
|
||||
afterEach(async () => {
|
||||
await fixture.cleanup();
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe('prepared statement caching', () => {
|
||||
describe('database statements', () => {
|
||||
it('tunes the controller connection and stops periodic optimization when closed', async () => {
|
||||
const { db, closeDb } = await import('../../src/lib/server/db/index.js');
|
||||
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
||||
@@ -30,7 +31,7 @@ describe('prepared statement caching', () => {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
it('reuses statements with fresh parameters and remains usable after rollback', async () => {
|
||||
it('keeps caller-owned statements reusable with fresh parameters after rollback', async () => {
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
const insertSql = 'INSERT INTO settings (key, value) VALUES (?, ?)';
|
||||
const selectSql = 'SELECT value FROM settings WHERE key = ?';
|
||||
@@ -45,8 +46,8 @@ describe('prepared statement caching', () => {
|
||||
).toThrow('rollback');
|
||||
db().prepare(insertSql).run('cache-second', 'two');
|
||||
|
||||
expect(db().prepare(insertSql)).toBe(insert);
|
||||
expect(db().prepare(selectSql)).toBe(select);
|
||||
expect(db().prepare(insertSql)).not.toBe(insert);
|
||||
expect(db().prepare(selectSql)).not.toBe(select);
|
||||
expect(select.get('cache-first')).toEqual({ value: 'one' });
|
||||
expect(select.get('cache-second')).toEqual({ value: 'two' });
|
||||
expect(select.get('cache-rollback')).toBeUndefined();
|
||||
@@ -65,6 +66,56 @@ describe('prepared statement caching', () => {
|
||||
expect(statement).not.toBe(oldStatement);
|
||||
expect(statement.database).not.toBe(oldStatement.database);
|
||||
expect(statement.get('cache-persisted')).toEqual({ value: 'saved' });
|
||||
expect(db().prepare(sql)).toBe(statement);
|
||||
expect(db().prepare(sql)).not.toBe(statement);
|
||||
});
|
||||
|
||||
describe.each([0, 20])('statement isolation with EC_SLOW_SQL_MS=%i', (slowMs) => {
|
||||
beforeEach(() => vi.stubEnv('EC_SLOW_SQL_MS', String(slowMs)));
|
||||
|
||||
it.each([
|
||||
['raw', [1]],
|
||||
['pluck', 1],
|
||||
['expand', { $: { value: 1 } }],
|
||||
['safeIntegers', { value: 1n }]
|
||||
] as const)('keeps %s mode local to its statement', async (mode, expected) => {
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
const sql = 'SELECT 1 AS value';
|
||||
const customized = db().prepare(sql)[mode]();
|
||||
const standard = db().prepare(sql);
|
||||
|
||||
expect(standard.get()).toEqual({ value: 1 });
|
||||
expect(customized.get()).toEqual(expected);
|
||||
customized[mode](false);
|
||||
standard[mode]();
|
||||
expect(customized.get()).toEqual({ value: 1 });
|
||||
expect(standard.get()).toEqual(expected);
|
||||
expect(db().prepare(sql).get()).toEqual({ value: 1 });
|
||||
});
|
||||
|
||||
it('keeps permanent bindings local to their statement', async () => {
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
const sql = 'SELECT ? AS value';
|
||||
const bound = db().prepare(sql).bind('first');
|
||||
|
||||
expect(db().prepare(sql).get('second')).toEqual({ value: 'second' });
|
||||
expect(bound.get()).toEqual({ value: 'first' });
|
||||
expect(db().prepare(sql).bind('third').get()).toEqual({ value: 'third' });
|
||||
expect(bound.get()).toEqual({ value: 'first' });
|
||||
});
|
||||
|
||||
it('allows identical queries while another statement is being iterated', async () => {
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
const sql = 'SELECT 1 AS value UNION ALL SELECT 2 AS value';
|
||||
const iterator = db().prepare(sql).iterate();
|
||||
try {
|
||||
expect(iterator.next()).toEqual({ value: { value: 1 }, done: false });
|
||||
expect(db().prepare(sql).get()).toEqual({ value: 1 });
|
||||
expect([...db().prepare(sql).iterate()]).toEqual([{ value: 1 }, { value: 2 }]);
|
||||
expect(iterator.next()).toEqual({ value: { value: 2 }, done: false });
|
||||
expect(iterator.next().done).toBe(true);
|
||||
} finally {
|
||||
iterator.return?.();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user