Skip to content

Commit

Permalink
Fixed coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed May 25, 2024
1 parent 93f9d33 commit a80c37b
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 17 deletions.
158 changes: 143 additions & 15 deletions __test__/sqlite3.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sqlite3 from 'sqlite3'
import sqlite3, { Database } from 'sqlite3'
import sqlite3Rewire, {
options,
execute,
Expand All @@ -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')
}
})
3 changes: 1 addition & 2 deletions src/db/sqlite3.js
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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)
}
Expand Down

0 comments on commit a80c37b

Please sign in to comment.