-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from sunquakes/development
Login and user list.
- Loading branch information
Showing
31 changed files
with
1,120 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: SonarCloud Scan | ||
|
||
on: | ||
push: | ||
branches: | ||
- development | ||
- main | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
sonarcloud: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Test and coverage | ||
run: yarn test --coverage | ||
- name: SonarCloud Scan | ||
uses: sonarsource/sonarcloud-github-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sonarCloudOrganization": "sunquakes", | ||
"projectKey": "sunquakes_electron-kits" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"] | ||
"recommendations": ["Vue.vscode-typescript-vue-plugin"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,188 @@ | ||
import { execute, save, list, getOne, updateById, remove } from '../src/db/sqlite3' | ||
import sqlite3, { Database } from 'sqlite3' | ||
import sqlite3Rewire, { | ||
options, | ||
execute, | ||
save, | ||
list, | ||
page, | ||
count, | ||
getOne, | ||
updateById, | ||
remove | ||
} from '../src/db/sqlite3' | ||
|
||
test('Create Table', () => { | ||
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('CRUD', async () => { | ||
// Test insert. | ||
const insertId = await save('test', { content: 'Hello World!' }) | ||
expect(insertId).toBeGreaterThan(0) | ||
test('Test options', async () => { | ||
// expect(options.db).toBeInstanceOf(Object) | ||
options.before = () => {} | ||
|
||
const getDb = sqlite3Rewire.__get__('getDb') | ||
await getDb() | ||
|
||
options.after = () => {} | ||
}) | ||
|
||
test('Test CRUD', async () => { | ||
// 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 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') | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module.exports = { | ||
presets: [['@babel/preset-env', { targets: { node: 'current' } }]] | ||
presets: [['@babel/preset-env', { targets: { node: 'current' } }]], | ||
plugins: ['babel-plugin-rewire'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
module.exports = { | ||
transform: { | ||
'\\.js$': 'babel-jest' | ||
} | ||
}, | ||
collectCoverage: true, | ||
collectCoverageFrom: ['./src/db/sqlite3.js'], | ||
coverageDirectory: 'coverage', | ||
coverageReporters: ['text', 'lcov'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
sonar.organization=sunquakes | ||
sonar.projectKey=sunquakes_electron-kits | ||
|
||
sonar.sources=src/db/sqlite3.js | ||
sonar.exclusions=src/lang/**.js | ||
sonar.javascript.lcov.reportPaths=coverage/lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { page, getOne, remove, save, updateById } from '../db/sqlite3' | ||
import CryptoJS from 'crypto-js' | ||
import { datetime } from '../utils/date' | ||
|
||
const TABLE_NAME = 'user' | ||
|
||
export async function pageList(current, pageSize, where, orderBy) { | ||
return page(TABLE_NAME, current, pageSize, where, orderBy) | ||
} | ||
|
||
export async function login(username, password) { | ||
const user = await getOne(TABLE_NAME, [['username', username]]) | ||
if (!user) { | ||
return new Error('login.username_not_exist') | ||
} | ||
const passwordMd5 = CryptoJS.MD5(password).toString() | ||
if (passwordMd5 !== user.password) { | ||
return new Error('login.wrong_password') | ||
} | ||
return user | ||
} | ||
|
||
export async function del(where) { | ||
return remove(TABLE_NAME, where) | ||
} | ||
|
||
export async function edit(id, data) { | ||
if (data.password) { | ||
const passwordMd5 = CryptoJS.MD5(data.password).toString() | ||
data.password = passwordMd5 | ||
} else { | ||
delete data.password | ||
} | ||
data.update_time = datetime(new Date()) | ||
return updateById(TABLE_NAME, id, data) | ||
} | ||
|
||
export async function add(data) { | ||
const passwordMd5 = CryptoJS.MD5(data.password).toString() | ||
data.password = passwordMd5 | ||
data.create_time = datetime(new Date()) | ||
return await save(TABLE_NAME, data) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.