Skip to content

Commit

Permalink
fixup: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 14, 2025
1 parent a050cd6 commit 9d8538b
Showing 1 changed file with 46 additions and 58 deletions.
104 changes: 46 additions & 58 deletions test/parallel/test-sqlite-backup.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as common from '../common/index.mjs';
import '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
import { join } from 'path';
import { join } from 'node:path';
import { DatabaseSync } from 'node:sqlite';
import { describe, test } from 'node:test';
import { writeFileSync } from 'fs';
import { writeFileSync } from 'node:fs';

let cnt = 0;

Expand Down Expand Up @@ -36,77 +36,70 @@ describe('DatabaseSync.prototype.backup()', () => {
test('throws if path is not a string', async (t) => {
const database = makeSourceDb();

t.assert.rejects(async () => {
await database.backup();
}, common.expectsError({
t.assert.throws(() => {
database.backup();
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "destination" argument must be a string.'
}));
});

t.assert.rejects(async () => {
await database.backup({});
}, common.expectsError({
t.assert.rejects(() => {
database.backup({});
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "destination" argument must be a string.'
}));
});
});

test('throws if options is not an object', async (t) => {
test('throws if options is not an object', (t) => {
const database = makeSourceDb();

t.assert.rejects(async () => {
await database.backup('hello.db', 'invalid');
}, common.expectsError({
t.assert.rejects(() => {
database.backup('hello.db', 'invalid');
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be an object.'
}));

t.assert.rejects(async () => {
await database.backup({});
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "destination" argument must be a string.'
}));
});
});

test('throws if any of provided options is invalid', async (t) => {
test('throws if any of provided options is invalid', (t) => {
const database = makeSourceDb();

t.assert.rejects(async () => {
await database.backup('hello.db', {
t.assert.rejects(() => {
database.backup('hello.db', {
sourceDb: 42
});
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.sourceDb" argument must be a string.'
}));
});

t.assert.rejects(async () => {
await database.backup('hello.db', {
t.assert.rejects(() => {
database.backup('hello.db', {
targetDb: 42
});
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.targetDb" argument must be a string.'
}));
});

t.assert.rejects(async () => {
await database.backup('hello.db', {
t.assert.rejects(() => {
database.backup('hello.db', {
rate: 'invalid'
});
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.rate" argument must be an integer.'
}));
});

t.assert.rejects(async () => {
await database.backup('hello.db', {
t.assert.rejects(() => {
database.backup('hello.db', {
progress: 'invalid'
});
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.progress" argument must be a function.'
}));
});
});
});

Expand Down Expand Up @@ -155,16 +148,16 @@ test('database backup in a single call', async (t) => {
});

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

database.close();

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

test('database backup fails when dest file is not writable', (t) => {
Expand All @@ -175,10 +168,10 @@ test('database backup fails when dest file is not writable', (t) => {

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

test('backup fails when progress function throws', async (t) => {
Expand All @@ -194,36 +187,31 @@ test('backup fails when progress function throws', async (t) => {
rate: 1,
progress: progressFn,
});
}, common.expectsError({
}, {
message: 'progress error'
}));
});
});

test('backup fails source db is invalid', async (t) => {
test('backup fails when source db is invalid', async (t) => {
const database = makeSourceDb();
const destDb = nextDb();

const progressFn = t.mock.fn(() => {
throw new Error('progress error');
});

t.assert.rejects(async () => {
await database.backup(destDb, {
rate: 1,
progress: progressFn,
sourceDb: 'invalid',
});
}, common.expectsError({
}, {
message: 'unknown database invalid'
}));
});
});

test('backup fails when destination cannot be opened', async (t) => {
const database = makeSourceDb();

t.assert.rejects(async () => {
await database.backup('/invalid/path/to/db.sqlite');
}, common.expectsError({
}, {
message: 'unable to open database file'
}));
});
});

0 comments on commit 9d8538b

Please sign in to comment.