From a80c37b316684e8fc66dad59426787d8dbfdcdd9 Mon Sep 17 00:00:00 2001 From: Shing Rui Date: Sat, 25 May 2024 22:28:37 +0800 Subject: [PATCH] Fixed coverage. --- __test__/sqlite3.test.js | 158 +++++++++++++++++++++++++++++++++++---- src/db/sqlite3.js | 3 +- 2 files changed, 144 insertions(+), 17 deletions(-) diff --git a/__test__/sqlite3.test.js b/__test__/sqlite3.test.js index ceff803..c17c04e 100644 --- a/__test__/sqlite3.test.js +++ b/__test__/sqlite3.test.js @@ -1,4 +1,4 @@ -import sqlite3 from 'sqlite3' +import sqlite3, { Database } from 'sqlite3' import sqlite3Rewire, { options, execute, @@ -11,50 +11,178 @@ import sqlite3Rewire, { remove } from '../src/db/sqlite3' +jest.mock('sqlite3') + +beforeEach(() => { + jest.clearAllMocks() +}) + +test('Test getDb', async () => { + const getDb = sqlite3Rewire.__get__('getDb') + await getDb() + expect(options.db).toBeInstanceOf(Database) + + sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => { + if (callback !== undefined) { + callback(new Error('Test')) + } + }) + + options.db = undefined + await getDb() + expect(options.db).toBeInstanceOf(sqlite3.Database) +}) + test('Test Create Table', async () => { const sql = 'CREATE TABLE IF NOT EXISTS test (' + 'id Integer PRIMARY KEY AUTOINCREMENT,' + 'content VARCHAR(255) NOT NULL DEFAULT ""' + ')' + sqlite3.Database.prototype.exec = jest.fn((sql, callback) => { + callback() + }) + options.db = undefined + execute(sql).then((err) => { expect(err).toBe(undefined) }) + + sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => { + return { + exec: jest.fn((sql, callback) => { + callback('Test') + }) + } + }) + options.db = undefined + try { + await execute(sql) + } catch (e) { + expect(e.message).toBe('Test') + } }) test('Test options', async () => { - expect(options.db).toBeInstanceOf(sqlite3.Database) + // expect(options.db).toBeInstanceOf(Object) options.before = () => {} + + const getDb = sqlite3Rewire.__get__('getDb') + await getDb() + options.after = () => {} }) test('Test CRUD', async () => { - // Test insert. - const insertId = await save('test', { content: 'Hello World!' }) - expect(insertId).toBeGreaterThan(0) + // Mock database instance + sqlite3.Database = jest.fn().mockImplementation(() => { + return { + run: jest.fn(function (sql, callback) { + this.lastID = 1 + callback.apply(this) + }), + all: jest.fn(function (sql, callback) { + callback(undefined, [{ name: 'John Doe', age: 30 }]) + }), + get: jest.fn(function (sql, callback) { + callback(undefined, { id: 1, content: 'Hello World!', count: 1 }) + }) + } + }) + options.db = undefined + + // Call the save function + const insertId = await save('test_table', { name: 'John Doe', age: 30 }) + + // Assertions + expect(insertId).toBe(1) // Test select list. const rows = await list('test') expect(rows.length).toBeGreaterThan(0) + // Test page. + const pageList = await page('test') + expect(pageList.total).toBeGreaterThan(0) + + // Test records count. + const total = await count('test') + expect(total).toBeGreaterThan(0) + // Test select one record. let row = await getOne('test', [['id', insertId]]) expect(row.content).toBe('Hello World!') + row = await getOne('test', [['id', '=', insertId]]) + expect(row.content).toBe('Hello World!') + // Test update records. const updateId = await updateById('test', insertId, { content: 'Hello China!' }) expect(updateId).toBe(insertId) - row = await getOne('test', [['id', insertId]]) - expect(row.content).toBe('Hello China!') - - // Test records count. - const total = await count('test') - expect(total).toBeGreaterThan(0) - - // Test page. - const pageList = await page('test') - expect(pageList.total).toBeGreaterThan(0) // Test delete records. remove('test', ['id', insertId]) }) + +test('Test CRUD error', async () => { + // Test error. + sqlite3.Database = jest.fn().mockImplementation(() => { + return { + run: jest.fn(function (sql, callback) { + callback('Test') + }), + all: jest.fn(function (sql, callback) { + callback('Test') + }), + get: jest.fn(function (sql, callback) { + callback('Test') + }) + } + }) + options.db = undefined + + try { + await save('test_table', { name: 'John Doe', age: 30 }) + } catch (e) { + expect(e.message).toBe('Test') + } + + try { + await list( + 'test', + [ + ['id', 1], + ['content', 'Hello China!'] + ], + 'id ASC', + 0, + 10 + ) + } catch (e) { + expect(e.message).toBe('Test') + } + + try { + await count('test', [['id', 1]]) + } catch (e) { + expect(e.message).toBe('Test') + } + + try { + await getOne('test', [['content', '=', 'Hello China!']]) + } catch (e) { + expect(e.message).toBe('Test') + } + + try { + await updateById('test', 1, { content: 'Hello China!' }) + } catch (e) { + expect(e.message).toBe('Test') + } + + try { + await remove('test', ['id', 1]) + } catch (e) { + expect(e.message).toBe('Test') + } +}) diff --git a/src/db/sqlite3.js b/src/db/sqlite3.js index bb2c145..3a818cc 100755 --- a/src/db/sqlite3.js +++ b/src/db/sqlite3.js @@ -1,5 +1,5 @@ const path = require('path') -const sqlite3 = require('sqlite3').verbose() +const sqlite3 = require('sqlite3') const dbFile = path.join(process.resourcesPath || '', 'sqlite3.db') export const options = { db: undefined, before: undefined, after: undefined } @@ -14,7 +14,6 @@ const getDb = async () => { } else { options.db = new sqlite3.Database(dbFile, (err) => { if (err !== null) { - console.log(666) options.db = new sqlite3.Database(':memory:') resolve(options.db) }