Skip to content

Commit

Permalink
Merge pull request #16 from sunquakes/development
Browse files Browse the repository at this point in the history
Login and user list.
  • Loading branch information
sunquakes authored May 25, 2024
2 parents 7811b62 + 995a9f3 commit f2f210c
Show file tree
Hide file tree
Showing 31 changed files with 1,120 additions and 298 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/sonarcloud-scan.yml
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 }}
4 changes: 4 additions & 0 deletions .sonarlint/connectedMode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sonarCloudOrganization": "sunquakes",
"projectKey": "sunquakes_electron-kits"
}
2 changes: 1 addition & 1 deletion .vscode/extensions.json
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"]
}
173 changes: 165 additions & 8 deletions __test__/sqlite3.test.js
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')
}
})
3 changes: 2 additions & 1 deletion babel.config.js
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']
}
6 changes: 5 additions & 1 deletion jest.config.js
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']
}
8 changes: 5 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { app, BrowserWindow, globalShortcut, Menu } = require('electron')
const { app, BrowserWindow, globalShortcut, Menu, ipcRenderer } = require('electron')
const path = require('path')

const NODE_ENV = process.env.NODE_ENV
console.log('NODE_ENV', NODE_ENV)

process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
minWidth: 1024,
minHeight: 768,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@
},
"dependencies": {
"ant-design-vue": "^4.2.0",
"crypto-js": "^4.2.0",
"echarts": "^5.5.0",
"sqlite3": "^5.1.6",
"vue": "^3.4.27",
"vue-i18n": "^9.13.1",
"vue-router": "^4.3.2"
"vue-router": "^4.3.2",
"vuex": "^4.1.0"
},
"devDependencies": {
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@vitejs/plugin-vue": "^4.2.3",
"aws-sdk": "^2.1473.0",
"babel-jest": "^29.7.0",
"babel-plugin-rewire": "^1.2.0",
"concurrently": "^8.2.1",
"cross-env": "^7.0.3",
"electron": "^26.3.0",
Expand Down
6 changes: 6 additions & 0 deletions sonar-project.properties
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
43 changes: 43 additions & 0 deletions src/api/user.js
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)
}
39 changes: 0 additions & 39 deletions src/components/ELayout.vue

This file was deleted.

Loading

0 comments on commit f2f210c

Please sign in to comment.