Skip to content

Commit

Permalink
Merge pull request #208 from OP-Engineering/oscar/fix-get-db-path
Browse files Browse the repository at this point in the history
Fix flaky tests and fix getDbPath function
  • Loading branch information
ospfranco authored Dec 20, 2024
2 parents 48483f0 + b25c026 commit 6b6790f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
29 changes: 16 additions & 13 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,19 +738,22 @@ void DBHostObject::create_jsi_functions() {

function_map["getDbPath"] = HOSTFN("getDbPath") {
std::string path = std::string(base_path);
if (count == 1 && !args[0].isString()) {
throw std::runtime_error(
"[op-sqlite][open] database location must be a string");
}

std::string last_path = args[0].asString(rt).utf8(rt);

if (last_path == ":memory:") {
path = ":memory:";
} else if (last_path.rfind('/', 0) == 0) {
path = last_path;
} else {
path = path + "/" + last_path;

if (count == 1) {
if(!args[0].isString()) {
throw std::runtime_error(
"[op-sqlite][open] database location must be a string");
}

std::string last_path = args[0].asString(rt).utf8(rt);

if (last_path == ":memory:") {
path = ":memory:";
} else if (last_path.rfind('/', 0) == 0) {
path = last_path;
} else {
path = path + "/" + last_path;
}
}

auto result = opsqlite_get_db_path(db_name, path);
Expand Down
13 changes: 4 additions & 9 deletions example/src/tests/blob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export function blobTests() {
describe('Blobs', () => {
beforeEach(async () => {
try {
if (db) {
db.delete();
}

db = open({
name: 'blobs',
encryptionKey: 'test',
Expand All @@ -29,9 +25,7 @@ export function blobTests() {
});

afterAll(() => {
if (db) {
db.delete();
}
db.delete();
});

it('ArrayBuffer', async () => {
Expand Down Expand Up @@ -86,7 +80,7 @@ export function blobTests() {
const statement = db.prepareStatement(
'INSERT OR REPLACE INTO BlobTable VALUES (?, ?);',
);
statement.bind([1, uint8]);
await statement.bind([1, uint8]);

await statement.execute();

Expand All @@ -103,7 +97,8 @@ export function blobTests() {
const statement = db.prepareStatement(
'INSERT OR REPLACE INTO BlobTable VALUES (?, ?);',
);
statement.bind([1, uint8.buffer]);

await statement.bind([1, uint8.buffer]);

await statement.execute();

Expand Down
11 changes: 11 additions & 0 deletions example/src/tests/dbsetup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,15 @@ export function dbSetupTests() {
});
db2.delete();
});

it('Can get db path', () => {
let db = open({
name: 'pathTest.sqlite',
encryptionKey: 'test',
});

let path = db.getDbPath();
expect(path).to.exist;
db.close();
});
}

0 comments on commit 6b6790f

Please sign in to comment.