mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-18 00:54:19 +01:00
testing/javascript: Clean up after test runs
This commit is contained in:
@@ -4,7 +4,7 @@ import fs from 'fs';
|
||||
|
||||
|
||||
test.beforeEach(async (t) => {
|
||||
const [db, errorType] = await connect();
|
||||
const [db, path,errorType] = await connect();
|
||||
await db.exec(`
|
||||
DROP TABLE IF EXISTS users;
|
||||
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)
|
||||
@@ -17,14 +17,30 @@ test.beforeEach(async (t) => {
|
||||
);
|
||||
t.context = {
|
||||
db,
|
||||
path,
|
||||
errorType
|
||||
};
|
||||
});
|
||||
|
||||
test.after.always(async (t) => {
|
||||
test.afterEach.always(async (t) => {
|
||||
// Close the database connection
|
||||
if (t.context.db != undefined) {
|
||||
t.context.db.close();
|
||||
}
|
||||
// Remove the database file if it exists
|
||||
if (t.context.path) {
|
||||
const walPath = t.context.path + "-wal";
|
||||
const shmPath = t.context.path + "-shm";
|
||||
if (fs.existsSync(t.context.path)) {
|
||||
fs.unlinkSync(t.context.path);
|
||||
}
|
||||
if (fs.existsSync(walPath)) {
|
||||
fs.unlinkSync(walPath);
|
||||
}
|
||||
if (fs.existsSync(shmPath)) {
|
||||
fs.unlinkSync(shmPath);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test.serial("Open in-memory database", async (t) => {
|
||||
@@ -408,12 +424,12 @@ const connect = async (path, options = {}) => {
|
||||
if (provider === "turso") {
|
||||
const x = await import("@tursodatabase/turso");
|
||||
const db = new x.default(path, options);
|
||||
return [db, x.SqliteError];
|
||||
return [db, path, x.SqliteError];
|
||||
}
|
||||
if (provider === "libsql") {
|
||||
const x = await import("libsql/promise");
|
||||
const db = new x.default(path, options);
|
||||
return [db, x.SqliteError, path];
|
||||
return [db, path, x.SqliteError, path];
|
||||
}
|
||||
if (provider === "serverless") {
|
||||
const x = await import("@tursodatabase/serverless");
|
||||
@@ -426,7 +442,7 @@ const connect = async (path, options = {}) => {
|
||||
url,
|
||||
authToken,
|
||||
});
|
||||
return [db, x.SqliteError];
|
||||
return [db, null, x.SqliteError];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import crypto from 'crypto';
|
||||
import fs from 'fs';
|
||||
|
||||
test.beforeEach(async (t) => {
|
||||
const [db, errorType, provider] = await connect();
|
||||
const [db, path, provider, errorType] = await connect();
|
||||
db.exec(`
|
||||
DROP TABLE IF EXISTS users;
|
||||
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)
|
||||
@@ -16,15 +16,31 @@ test.beforeEach(async (t) => {
|
||||
);
|
||||
t.context = {
|
||||
db,
|
||||
path,
|
||||
provider,
|
||||
errorType,
|
||||
provider
|
||||
};
|
||||
});
|
||||
|
||||
test.after.always(async (t) => {
|
||||
test.afterEach.always(async (t) => {
|
||||
// Close the database connection
|
||||
if (t.context.db != undefined) {
|
||||
t.context.db.close();
|
||||
}
|
||||
// Remove the database file if it exists
|
||||
if (t.context.path) {
|
||||
const walPath = t.context.path + "-wal";
|
||||
const shmPath = t.context.path + "-shm";
|
||||
if (fs.existsSync(t.context.path)) {
|
||||
fs.unlinkSync(t.context.path);
|
||||
}
|
||||
if (fs.existsSync(walPath)) {
|
||||
fs.unlinkSync(walPath);
|
||||
}
|
||||
if (fs.existsSync(shmPath)) {
|
||||
fs.unlinkSync(shmPath);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test.serial("Open in-memory database", async (t) => {
|
||||
@@ -451,17 +467,17 @@ const connect = async (path, options = {}) => {
|
||||
if (provider === "turso") {
|
||||
const x = await import("@tursodatabase/turso/sync");
|
||||
const db = new x.default(path, options);
|
||||
return [db, x.SqliteError, provider];
|
||||
return [db, path, provider, x.SqliteError];
|
||||
}
|
||||
if (provider === "libsql") {
|
||||
const x = await import("libsql");
|
||||
const db = new x.default(path, options);
|
||||
return [db, x.SqliteError, provider, path];
|
||||
return [db, path, provider, x.SqliteError];
|
||||
}
|
||||
if (provider == "better-sqlite3") {
|
||||
const x = await import("better-sqlite3");
|
||||
const db = x.default(path, options);
|
||||
return [db, x.default.SqliteError, provider];
|
||||
return [db, path, provider, x.default.SqliteError];
|
||||
}
|
||||
throw new Error("Unknown provider: " + provider);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user