Skip to content

Commit 01c00f5

Browse files
authored
Merge pull request #229 from OP-Engineering/oscar/expand-test-suite
Oscar/expand test suite
2 parents b78875a + 108f5e3 commit 01c00f5

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

cpp/DBHostObject.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ void DBHostObject::create_jsi_functions() {
238238
});
239239

240240
function_map["close"] = HOSTFN("close") {
241+
invalidated = true;
242+
241243
#ifdef OP_SQLITE_USE_LIBSQL
242244
opsqlite_libsql_close(db);
243245
#else
@@ -248,6 +250,8 @@ void DBHostObject::create_jsi_functions() {
248250
});
249251

250252
function_map["delete"] = HOSTFN("delete") {
253+
invalidated = true;
254+
251255
std::string path = std::string(base_path);
252256

253257
if (count == 1) {
@@ -808,6 +812,10 @@ void DBHostObject::set(jsi::Runtime &_rt, const jsi::PropNameID &name,
808812
}
809813

810814
void DBHostObject::invalidate() {
815+
if(invalidated) {
816+
return;
817+
}
818+
811819
invalidated = true;
812820
_thread_pool->restartPool();
813821
#ifdef OP_SQLITE_USE_LIBSQL

example/src/tests/dbsetup.spec.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,26 @@ export function dbSetupTests() {
8282
'CREATE TABLE User ( id INT PRIMARY KEY, name TEXT NOT NULL, age INT, networth REAL) STRICT;',
8383
);
8484

85-
await androidDb.close();
85+
androidDb.close();
86+
});
87+
88+
it('Creates db in external nested directory on Android', async () => {
89+
let androidDb = open({
90+
name: 'AndroidSDCardDB.sqlite',
91+
location: `${ANDROID_EXTERNAL_FILES_PATH}/nested`,
92+
encryptionKey: 'test',
93+
});
94+
95+
await androidDb.execute('DROP TABLE IF EXISTS User;');
96+
await androidDb.execute(
97+
'CREATE TABLE User ( id INT PRIMARY KEY, name TEXT NOT NULL, age INT, networth REAL) STRICT;',
98+
);
99+
100+
androidDb.close();
86101
});
87102
}
88103

104+
// Currently this only tests the function is there
89105
it('Should load extension', async () => {
90106
let db = open({
91107
name: 'extensionDb',
@@ -112,13 +128,16 @@ export function dbSetupTests() {
112128
});
113129

114130
it('Should delete db with absolute path', async () => {
131+
let location =
132+
Platform.OS === 'ios' ? IOS_LIBRARY_PATH : ANDROID_DATABASE_PATH;
115133
let db = open({
116134
name: 'deleteTest',
117135
encryptionKey: 'test',
118-
location:
119-
Platform.OS === 'ios' ? IOS_LIBRARY_PATH : ANDROID_DATABASE_PATH,
136+
location,
120137
});
121138

139+
expect(db.getDbPath()).to.contain(location);
140+
122141
db.delete();
123142
});
124143

@@ -129,6 +148,20 @@ export function dbSetupTests() {
129148
location: 'myFolder',
130149
});
131150

151+
let path = db.getDbPath();
152+
expect(path).to.contain('myFolder');
153+
db.delete();
154+
});
155+
156+
it('Should create nested folders', async () => {
157+
let db = open({
158+
name: 'nestedFolderTest.sqlite',
159+
encryptionKey: 'test',
160+
location: 'myFolder/nested',
161+
});
162+
163+
let path = db.getDbPath();
164+
expect(path).to.contain('myFolder/nested');
132165
db.delete();
133166
});
134167

@@ -155,18 +188,17 @@ export function dbSetupTests() {
155188
});
156189

157190
expect(copied).to.equal(true);
158-
});
159191

160-
// it('Should fail creating in-memory with non-bool arg', async () => {
161-
// try {
162-
// open({
163-
// name: 'inMemoryTest',
164-
// });
165-
// expect.fail('Should throw');
166-
// } catch (e) {
167-
// expect(!!e).to.equal(true);
168-
// }
169-
// });
192+
let db = open({
193+
name: 'sample2.sqlite',
194+
encryptionKey: 'test',
195+
location: 'sqlite',
196+
});
197+
198+
let path = db.getDbPath();
199+
expect(path).to.contain('sqlite/sample2.sqlite');
200+
db.delete();
201+
});
170202

171203
it('Creates new connections per query and closes them', async () => {
172204
for (let i = 0; i < 100; i++) {
@@ -235,10 +267,11 @@ export function dbSetupTests() {
235267
});
236268

237269
if (isSQLCipher()) {
238-
it('Can open without encryption key', () => {
270+
it('Can open SQLCipher db without encryption key', () => {
239271
let db = open({
240272
name: 'pathTest.sqlite',
241273
});
274+
242275
db.close();
243276
});
244277
}

example/src/tests/queries.spec.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ export function queriesTests() {
238238

239239
const countRes = await db.execute('SELECT COUNT(*) as count FROM User');
240240

241-
// console.log(countRes);
242-
243-
// expect(countRes.metadata?.[0]?.type).to.equal('UNKNOWN');
244241
expect(countRes.rows?.length).to.equal(1);
245242
expect(countRes.rows?.[0]?.count).to.equal(1);
246243

@@ -257,18 +254,14 @@ export function queriesTests() {
257254

258255
const sumRes = await db.execute('SELECT SUM(age) as sum FROM User;');
259256

260-
// expect(sumRes.metadata?.[0]?.type).to.equal('UNKNOWN');
261257
expect(sumRes.rows[0]!.sum).to.equal(age + age2);
262258

263-
// MAX(networth), MIN(networth)
264259
const maxRes = await db.execute(
265260
'SELECT MAX(networth) as `max` FROM User;',
266261
);
267262
const minRes = await db.execute(
268263
'SELECT MIN(networth) as `min` FROM User;',
269264
);
270-
// expect(maxRes.metadata?.[0]?.type).to.equal('UNKNOWN');
271-
// expect(minRes.metadata?.[0]?.type).to.equal('UNKNOWN');
272265
const maxNetworth = Math.max(networth, networth2);
273266
const minNetworth = Math.min(networth, networth2);
274267

@@ -707,6 +700,32 @@ export function queriesTests() {
707700
await db.execute('SELECT ?; ', [1]);
708701
});
709702

703+
it('Handles concurrent transactions correctly', async () => {
704+
const id = chance.integer();
705+
const name = chance.name();
706+
const age = chance.integer();
707+
const networth = chance.floating();
708+
709+
const transaction1 = db.transaction(async tx => {
710+
await tx.execute(
711+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
712+
[id, name, age, networth],
713+
);
714+
});
715+
716+
const transaction2 = db.transaction(async tx => {
717+
await tx.execute(
718+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
719+
[id + 1, name, age, networth],
720+
);
721+
});
722+
723+
await Promise.all([transaction1, transaction2]);
724+
725+
const res = await db.execute('SELECT * FROM User');
726+
expect(res.rows.length).to.equal(2);
727+
});
728+
710729
it('Pragma user_version', () => {
711730
const res = db.executeSync('PRAGMA user_version');
712731
console.warn(res.rows);

0 commit comments

Comments
 (0)