fix some sqlite errors.
This commit is contained in:
parent
52bb6f812c
commit
68cc052417
@ -1,44 +0,0 @@
|
||||
// __mocks__/expo-sqlite.js
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
|
||||
class SQLiteDatabase {
|
||||
constructor(name) {
|
||||
this.db = new sqlite3.Database(':memory:');
|
||||
}
|
||||
|
||||
runAsync(sql, params = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.run(sql, params, function (err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve({ changes: this.changes });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
execAsync(sql) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.exec(sql, err => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
closeAsync() {
|
||||
return new Promise(resolve => {
|
||||
this.db.close(() => resolve());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const SQLite = {
|
||||
openDatabaseAsync: jest.fn(name => new SQLiteDatabase(name)),
|
||||
};
|
||||
|
||||
module.exports = SQLite;
|
@ -7,12 +7,14 @@ describe('Settings', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
// Initialize your Settings class here with a fresh database instance
|
||||
settings = new Settings(await getDb());
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Could not get db");
|
||||
settings = new Settings(db);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up the database after each test
|
||||
await settings.db.executeSql('DELETE FROM settings');
|
||||
settings && await settings.db.executeSql('DELETE FROM settings');
|
||||
});
|
||||
|
||||
describe('setHostLanguage', () => {
|
||||
@ -57,7 +59,7 @@ describe('Settings', () => {
|
||||
it('should return the LibreTranslate base URL from the database', async () => {
|
||||
const value = 'https://another-example.com';
|
||||
await settings.db.executeSql(
|
||||
`INSERT INTO settings (libetransalte_base_url) VALUES (?)`,
|
||||
`INSERT INTO settings (libretranslate_base_url) VALUES (?)`,
|
||||
[value]
|
||||
);
|
||||
|
||||
|
@ -2,7 +2,7 @@ import * as SQLite from 'expo-sqlite';
|
||||
|
||||
export const MIGRATE_UP = {
|
||||
1: [
|
||||
`CREATE TABLE IF NOT EXIST settings (
|
||||
`CREATE TABLE IF NOT EXISTS settings (
|
||||
host_language TEXT,
|
||||
libretranslate_base_url TEXT,
|
||||
ui_direction INTEGER
|
||||
|
@ -19,7 +19,7 @@ export class Settings {
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT ?
|
||||
SELECT ${key}
|
||||
FROM settings
|
||||
LIMIT 1`
|
||||
const result = await this.db.getFirstAsync(
|
||||
@ -33,10 +33,10 @@ LIMIT 1`
|
||||
if (!Settings.KEYS.includes(key)) {
|
||||
throw new Error(`Invalid setting: '${key}'`)
|
||||
}
|
||||
const statement = `INSERT INTO settings (${key})
|
||||
SELECT '?'
|
||||
ON CONFLICT DO UPDATE SET ${key} = ?`
|
||||
await this.db.runAsync(statement, [value, value]);
|
||||
const statement = `REPLACE INTO settings ('${key}')
|
||||
VALUES (?)`
|
||||
const args = [value]
|
||||
await this.db.runAsync(statement, args);
|
||||
}
|
||||
|
||||
async setHostLanguage(value: string) {
|
||||
|
15
jestSetup.ts
15
jestSetup.ts
@ -1,5 +1,6 @@
|
||||
// jestSetup.ts
|
||||
|
||||
/**
|
||||
jest.mock('expo-sqlite', () => {
|
||||
return {
|
||||
openDatabaseAsync: async (name: string) => {
|
||||
@ -11,14 +12,20 @@ jest.mock('expo-sqlite', () => {
|
||||
executeSql: jest.fn((sql: string) => db.exec(sql)),
|
||||
runAsync: jest.fn(async (sql: string, params = []) => {
|
||||
const stmt = db.prepare(sql);
|
||||
stmt.run(params)
|
||||
// console.log("Running %s with %s", sql, params);
|
||||
try {
|
||||
stmt.run(params);
|
||||
} catch (e) {
|
||||
throw new Error(`running ${sql} with params ${JSON.stringify(params)}: ${e}`);
|
||||
}
|
||||
}),
|
||||
getFirstAsync: jest.fn(async (sql : string, params = []) => {
|
||||
const stmt = db.prepare(sql)
|
||||
const result = stmt.run(params);
|
||||
return stmt.all(params)[0]
|
||||
// const result = stmt.run(...params);
|
||||
return stmt.get(params)
|
||||
})
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
});
|
||||
*/
|
@ -66,9 +66,7 @@
|
||||
"!**/.expo/**"
|
||||
],
|
||||
"automock": false,
|
||||
"setupFilesAfterEnv": [
|
||||
"<rootDir>/jestSetup.ts"
|
||||
],
|
||||
"setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"],
|
||||
"testTimeout": 10000
|
||||
},
|
||||
"devDependencies": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user