Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 12, 2025
1 parent 5c67faa commit 77607c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
Binary file modified backup.db
Binary file not shown.
65 changes: 49 additions & 16 deletions test/parallel/test-sqlite-backup.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import common from '../common/index.js';
import tmpdir from '../common/tmpdir.js';
import { join } from 'path';
import { DatabaseSync } from 'node:sqlite';
import assert from 'assert';
import { test } from 'node:test';
import { writeFileSync } from 'fs';

test('throws exception when trying to start backup from a closed database', async (t) => {
t.assert.rejects(async () => {
const database = new DatabaseSync(':memory:');
let cnt = 0;

database.close();
tmpdir.refresh();

await database.backup('backup.db');
}, common.expectsError({
code: 'ERR_INVALID_STATE',
message: 'database is not open'
}));
});
function nextDb() {
return join(tmpdir.path, `database-${cnt++}.db`);
}

test.only('database backup', async (t) => {
const progressFn = t.mock.fn();
function makeSourceDb() {
const database = new DatabaseSync(':memory:');

database.exec(`
Expand All @@ -33,18 +29,55 @@ test.only('database backup', async (t) => {
insert.run(i, `value-${i}`);
}

await database.backup('backup.db', {
return database;
}

test('throws exception when trying to start backup from a closed database', async (t) => {
t.assert.rejects(async () => {
const database = new DatabaseSync(':memory:');

database.close();

await database.backup('backup.db');
}, common.expectsError({
code: 'ERR_INVALID_STATE',
message: 'database is not open'
}));
});

test('database backup', async (t) => {
const progressFn = t.mock.fn();
const database = makeSourceDb();
const destDb = nextDb();

await database.backup(destDb, {
rate: 1,
progress: progressFn,
});

const backup = new DatabaseSync('backup.db');
const backup = new DatabaseSync(destDb);
const rows = backup.prepare('SELECT * FROM data').all();

// The source database has two pages, so the progress function should be called once
// The source database has two pages - using the default page size -, so the progress function should be called once (the last call is not made since
// the promise resolves)
t.assert.strictEqual(progressFn.mock.calls.length, 1);
t.assert.deepStrictEqual(rows, [
{ __proto__: null, key: 1, value: 'value-1' },
{ __proto__: null, key: 2, value: 'value-2' },
]);
});

test('database backup fails when dest file is not writable', (t) => {
const readonlyDestDb = nextDb();
writeFileSync(readonlyDestDb, '', { mode: 0o444 });

const database = makeSourceDb();

t.assert.rejects(async () => {
await database.backup(readonlyDestDb);
}, common.expectsError({
code: 'ERR_SQLITE_ERROR',
message: 'attempt to write a readonly database'
}));
});

0 comments on commit 77607c3

Please sign in to comment.