Skip to content

Commit

Permalink
Make test suite more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Sep 25, 2024
1 parent cbe3c4c commit 076589d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ BridgeResult opsqlite_open(std::string const &name,

if (dbMap.count(name) != 0) {
throw std::runtime_error(
"You can only have one JS connection per database");
"[OP-SQLITE] Only JS connection per database is allowed, db name: " +
name);
}
#endif
std::string dbPath = opsqlite_get_db_path(name, last_path);
Expand Down
12 changes: 11 additions & 1 deletion example/src/tests/MochaRNAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import 'mocha';
import type * as MochaTypes from 'mocha';

export const rootSuite = new Mocha.Suite('') as MochaTypes.Suite;
export let rootSuite = new Mocha.Suite('') as MochaTypes.Suite;
rootSuite.timeout(10 * 1000);
// rootSuite.timeout(60 * 60 * 1000);

let mochaContext = rootSuite;
let only = false;

export const clearTests = () => {
rootSuite.suites = [];
rootSuite.tests = [];
rootSuite = new Mocha.Suite('') as MochaTypes.Suite;
mochaContext = rootSuite;
only = false;
};
Expand Down Expand Up @@ -49,6 +51,14 @@ export const beforeEach = (f: () => void): void => {
mochaContext.beforeEach(f);
};

export const afterEach = (f: () => void): void => {
mochaContext.afterEach(f);
};

export const beforeAll = (f: any) => {
mochaContext.beforeAll(f);
};

export const afterAll = (f: any) => {
mochaContext.afterAll(f);
};
11 changes: 10 additions & 1 deletion example/src/tests/blob.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type DB, open} from '@op-engineering/op-sqlite';
import chai from 'chai';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {afterAll, beforeEach, describe, it} from './MochaRNAdapter';

let expect = chai.expect;

Expand Down Expand Up @@ -28,6 +28,15 @@ export function blobTests() {
}
});

afterAll(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

describe('Blobs', () => {
it('ArrayBuffer', async () => {
const uint8 = new Uint8Array(2);
Expand Down
4 changes: 4 additions & 0 deletions example/src/tests/dbsetup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export function dbSetupTests() {
} catch (e) {
// TODO load a sample extension
expect(e).to.exist;
} finally {
db.close();
db.delete();
}
});

Expand All @@ -100,6 +103,7 @@ export function dbSetupTests() {
encryptionKey: 'test',
});

db.close();
db.delete();
});

Expand Down
11 changes: 10 additions & 1 deletion example/src/tests/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Chance from 'chance';

import {type DB, open, isLibsql} from '@op-engineering/op-sqlite';
import chai from 'chai';
import {describe, it, beforeEach} from './MochaRNAdapter';
import {describe, it, beforeEach, afterAll} from './MochaRNAdapter';
import {sleep} from './utils';

const expect = chai.expect;
Expand Down Expand Up @@ -33,6 +33,15 @@ export function registerHooksTests() {
}
});

afterAll(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

describe('Hooks', () => {
// libsql does not support hooks
if (isLibsql()) {
Expand Down
11 changes: 10 additions & 1 deletion example/src/tests/preparedStatements.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {open, type DB} from '@op-engineering/op-sqlite';
import chai from 'chai';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {afterAll, beforeEach, describe, it} from './MochaRNAdapter';

let expect = chai.expect;

Expand Down Expand Up @@ -40,6 +40,15 @@ export function preparedStatementsTests() {
}
});

afterAll(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

describe('PreparedStatements', () => {
it('Creates a prepared statement and executes a prepared statement', async () => {
const statement = db.prepareStatement('SELECT * FROM User;');
Expand Down
21 changes: 13 additions & 8 deletions example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import {
type DB,
type SQLBatchTuple,
} from '@op-engineering/op-sqlite';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {afterEach, beforeEach, describe, it} from './MochaRNAdapter';
import chai from 'chai';

const expect = chai.expect;
const chance = new Chance();
let db: DB;

export function queriesTests() {
let db: DB;

beforeEach(async () => {
try {
if (db) {
db.close();
db.delete();
}

db = open({
name: 'queries.sqlite',
encryptionKey: 'test',
Expand All @@ -32,7 +28,16 @@ export function queriesTests() {
'CREATE TABLE User (id INT PRIMARY KEY, name TEXT NOT NULL, age INT, networth REAL, nickname TEXT) STRICT;',
);
} catch (e) {
console.error('error on before each', e);
console.error('🟥 Before each error!', e);
}
});

afterEach(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

Expand Down
11 changes: 10 additions & 1 deletion example/src/tests/reactive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isLibsql, open, type DB} from '@op-engineering/op-sqlite';
import chai from 'chai';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {afterAll, beforeEach, describe, it} from './MochaRNAdapter';
import {sleep} from './utils';
import Chance from 'chance';

Expand Down Expand Up @@ -30,6 +30,15 @@ export function reactiveTests() {
}
});

afterAll(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

describe('Reactive queries', () => {
// libsql does not support reactive queries
if (isLibsql()) {
Expand Down
7 changes: 0 additions & 7 deletions lefthook.yml

This file was deleted.

0 comments on commit 076589d

Please sign in to comment.