Skip to content

Commit

Permalink
Test user api.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed May 31, 2024
1 parent b0f5c13 commit 1a0f701
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
55 changes: 54 additions & 1 deletion __test__/api/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
import { pageList, login, del, edit, add } from '../../src/api/user'

jest.mock('../../src/db/sqlite3', () => {
return {
page: jest.fn().mockResolvedValue({
records: [
{
id: 1,
username: 'admin',
password: '123456',
nickname: '管理员'
}
],
total: 1
}),
getOne: jest.fn().mockResolvedValue({
id: 1,
username: 'admin',
password: 'e10adc3949ba59abbe56e057f20f883e',
nickname: '管理员'
}),
remove: jest.fn().mockResolvedValue(1),
updateById: jest.fn().mockResolvedValue(1),
save: jest.fn().mockResolvedValue(1)
}
})

describe('Test user api', () => {
test('Test pageList', async () => {})
test('Test pageList', async () => {
const res = await pageList(1, 10)
expect(res.total).toBe(1)
})

test('Test login', async () => {
let res = await login('admin', '123456')
expect(res.username).toBe('admin')

res = await login('admin', '1234567')
expect(res).toBeInstanceOf(Error)
})

test('Test del', async () => {
const res = await del([['id', '=', 1]])
expect(res).toBe(1)
})

test('Test edit', async () => {
const res = await edit(1, { nickname: '管理员' })
expect(res).toBe(1)
})

test('Test add', async () => {
const res = await add({ username: 'admin', nickname: '管理员' })
expect(res).toBe(1)
})
})
10 changes: 8 additions & 2 deletions __test__/db/sqlite3.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sqlite3, { Database } from 'sqlite3'
// @ts-ignore
import sqlite3Rewire, {
options,
execute,
Expand All @@ -22,6 +23,7 @@ test('Test getDb', async () => {
await getDb()
expect(options.db).toBeInstanceOf(Database)

// @ts-ignore
sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => {
if (callback !== undefined) {
callback(new Error('Test'))
Expand All @@ -39,6 +41,7 @@ test('Test Create Table', async () => {
'id Integer PRIMARY KEY AUTOINCREMENT,' +
'content VARCHAR(255) NOT NULL DEFAULT ""' +
')'
// @ts-ignore
sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => {
return {
exec: jest.fn((sql, callback) => {})
Expand All @@ -50,6 +53,7 @@ test('Test Create Table', async () => {
expect(err).toBe(undefined)
})

// @ts-ignore
sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => {
return {
exec: jest.fn((sql, callback) => {
Expand Down Expand Up @@ -77,6 +81,7 @@ test('Test options', async () => {

test('Test CRUD', async () => {
// Mock database instance
// @ts-ignore
sqlite3.Database = jest.fn().mockImplementation(() => {
return {
run: jest.fn(function (sql, callback) {
Expand Down Expand Up @@ -104,7 +109,7 @@ test('Test CRUD', async () => {
expect(rows.length).toBeGreaterThan(0)

// Test page.
const pageList = await page('test')
const pageList = await page('test', 1, 10)
expect(pageList.total).toBeGreaterThan(0)

// Test records count.
Expand All @@ -128,6 +133,7 @@ test('Test CRUD', async () => {

test('Test CRUD error', async () => {
// Test error.
// @ts-ignore
sqlite3.Database = jest.fn().mockImplementation(() => {
return {
run: jest.fn(function (sql, callback) {
Expand Down Expand Up @@ -183,7 +189,7 @@ test('Test CRUD error', async () => {
}

try {
await remove('test', ['id', 1])
await remove('test', [['id', 1]])
} catch (e) {
expect(e.message).toBe('Test')
}
Expand Down
1 change: 1 addition & 0 deletions __test__/styleMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
testEnvironmentOptions: {
customExportConditions: ['node', 'node-addons']
},
// moduleNameMapper: {
// '\\.(css|less)$': '<rootDir>/__test__/styleMock.ts'
// }
coveragePathIgnorePatterns: ['.d.ts'],
moduleNameMapper: {
'\\.(css|less)$': '<rootDir>/__test__/styleMock.ts'
}
}
4 changes: 2 additions & 2 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { datetime } from '../utils/date'

const TABLE_NAME = 'user'

export async function pageList(current: number, pageSize: number, where?: string[][], orderBy?: string): Promise<any> {
export async function pageList(current: number, pageSize: number, where?: any[][], orderBy?: string): Promise<any> {
return page(TABLE_NAME, current, pageSize, where, orderBy)
}

Expand All @@ -20,7 +20,7 @@ export async function login(username: string, password: string): Promise<any> {
return user
}

export async function del(where: string[][]): Promise<any> {
export async function del(where: any[][]): Promise<any> {
return remove(TABLE_NAME, where)
}

Expand Down
12 changes: 6 additions & 6 deletions src/db/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const dbFile = path.join(process?.resourcesPath || '', 'sqlite3.db')
export const options = {
db: undefined,
before: (resolve: any, reject: any) => {},
after: undefined
after: () => {}
}

const getDb = async (): Promise<any> => {
Expand Down Expand Up @@ -93,7 +93,7 @@ export async function updateById(tableName: string, id: any, data: any): Promise

export async function list(
tableName: string,
where?: string[][],
where?: any[][],
orderBy?: string,
offset?: number,
limit?: number
Expand All @@ -120,7 +120,7 @@ export async function list(
})
}

export async function count(tableName: string, where?: string[][]): Promise<any> {
export async function count(tableName: string, where?: any[][]): Promise<any> {
let sql = `SELECT COUNT(*) AS count FROM ${tableName}`
if (where) {
sql = parseWhere(sql, where)
Expand All @@ -137,7 +137,7 @@ export async function count(tableName: string, where?: string[][]): Promise<any>
})
}

function parseWhere(sql: string, where: string[][]): string {
function parseWhere(sql: string, where: any[][]): string {
let whereArray = []
for (let item of where) {
if (!(item instanceof Array)) continue
Expand Down Expand Up @@ -165,7 +165,7 @@ function parseWhere(sql: string, where: string[][]): string {
return sql
}

export async function getOne(tableName: string, where: string[][]): Promise<any> {
export async function getOne(tableName: string, where: any[][]): Promise<any> {
let sql = `SELECT * FROM ${tableName}`
if (where) {
sql = parseWhere(sql, where)
Expand All @@ -182,7 +182,7 @@ export async function getOne(tableName: string, where: string[][]): Promise<any>
})
}

export async function remove(tableName: string, where: string[][]): Promise<any> {
export async function remove(tableName: string, where: any[][]): Promise<any> {
let sql = `DELETE FROM ${tableName}`
if (where) {
sql = parseWhere(sql, where)
Expand Down

0 comments on commit 1a0f701

Please sign in to comment.