Skip to content

Commit

Permalink
fix(orm): add test api and fix bugs (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW authored Aug 20, 2021
1 parent 5408f91 commit 4f71ce7
Show file tree
Hide file tree
Showing 19 changed files with 606 additions and 476 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/lint.yaml

This file was deleted.

156 changes: 138 additions & 18 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,87 @@ env:
CI: true

jobs:
prepare:
runs-on: ubuntu-latest

steps:
- name: Check out
uses: actions/checkout@v1
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: 14
- id: random
name: Generate UUID
run: echo "::set-output name=value::$(openssl rand -hex 10)"
- id: cache
name: Yarn Cache Dir
run: echo "::set-output name=value::$(yarn cache dir)"
- name: Install
run: yarn
- name: Prepare Lockfile
uses: actions/cache@v2
with:
path: yarn.lock
key: yarn-lock-${{ runner.os }}-${{ steps.random.outputs.value }}
- name: Prepare Cache
uses: actions/cache@v2
with:
path: ${{ steps.cache.outputs.value }}
key: yarn-cache-${{ runner.os }}-${{ steps.random.outputs.value }}-node-14

outputs:
random-uuid: ${{ steps.random.outputs.value }}
cache-dir: ${{ steps.cache.outputs.value }}

build:
runs-on: ubuntu-latest
needs:
- prepare

steps:
- name: Check out
uses: actions/checkout@v1
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: 14
- name: Restore Lockfile
uses: actions/cache@v2
with:
path: yarn.lock
key: yarn-lock-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}
- name: Restore Cache
uses: actions/cache@v2
with:
path: ${{ needs.prepare.outputs.cache-dir }}
key: yarn-cache-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}-node-14
- name: Install
run: yarn
- name: Lint
run: yarn lint
- name: Build JS
run: yarn compile
- name: Build DTS
run: yarn dtsc --listEmittedFiles
- name: Build for WebUI
run: yarn build:web
- name: Upload
uses: actions/upload-artifact@v2
with:
name: packages
path: packages

unit-test:
runs-on: ubuntu-latest
needs:
- prepare

continue-on-error: ${{ matrix.node-version == 16 }}
strategy:
fail-fast: false
matrix:
NODE_VERSION: [14, 16]
node-version: [14, 16]

services:
mongo:
Expand Down Expand Up @@ -42,42 +118,86 @@ jobs:
MYSQL_DATABASE: koishi
MYSQL_PASSWORD: koishi@114514
MYSQL_ROOT_PASSWORD: password
# mysql8:
# image: mysql:8
# ports:
# - 3308:3306
# env:
# MYSQL_PASSWORD: koishi@114514
# MYSQL_USER: koishi
mysql8:
image: mysql:8
ports:
- 3308:3306
options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 5
env:
MYSQL_USER: koishi
MYSQL_DATABASE: koishi
MYSQL_PASSWORD: koishi@114514
MYSQL_ROOT_PASSWORD: password

env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
DB_DATABASE: koishi
DB_USERNAME: koishi
DB_PASSWORD: koishi@114514

steps:
- name: Check out
uses: actions/checkout@v1
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.NODE_VERSION }}
- name: Create .npmrc
run: 'echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc'
node-version: ${{ matrix.node-version }}
- name: Restore Lockfile
uses: actions/cache@v2
with:
path: yarn.lock
key: yarn-lock-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}
- name: Restore Cache
uses: actions/cache@v2
with:
path: ${{ needs.prepare.outputs.cache-dir }}
key: yarn-cache-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}-node-${{ matrix.node-version }}
restore-keys: |
yarn-lock-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}-node
- name: Install
run: yarn
- name: Build
run: yarn build:ci
- name: Build JS
run: yarn compile
- name: Unit Test
run: yarn test:json
- name: Report Coverage
if: ${{ matrix.NODE_VERSION == 14 }}
if: ${{ matrix.node-version == 14 }}
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/coverage-final.json
name: codecov

publish:
if: ${{ github.ref == 'refs/heads/master' }}
runs-on: ubuntu-latest
needs:
- build
- unit-test

steps:
- name: Check out
uses: actions/checkout@v1
- name: Create .npmrc
run: 'echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc'
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: 14
- name: Restore Lockfile
uses: actions/cache@v2
with:
path: yarn.lock
key: yarn-lock-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}
- name: Restore Cache
uses: actions/cache@v2
with:
path: ${{ needs.prepare.outputs.cache-dir }}
key: yarn-cache-${{ runner.os }}-${{ needs.prepare.outputs.random-uuid }}-node-14
- name: Install
run: yarn
- name: Download
uses: actions/download-artifact@v2
with:
name: packages
path: packages
- name: Publish
if: ${{ matrix.NODE_VERSION == 14 }}
run: yarn pub
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const specs = [
'packages/plugin-common/tests/*.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
'packages/plugin-github/tests/*.spec.ts',
'packages/plugin-mysql/tests/*.spec.ts',
'packages/plugin-mongo/tests/*.spec.ts',
'packages/plugin-mysql/tests/*.spec.ts',
'packages/plugin-schedule/tests/*.spec.ts',
'packages/plugin-teach/tests/*.spec.ts',
]
Expand Down
4 changes: 0 additions & 4 deletions .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
"build/**",
"coverage/**",
"packages/*/tests/**",
"packages/chai-extended/**",
"packages/plugin-*/**/database/**",
"packages/plugin-*/**/database.js",
"packages/plugin-mongo/**",
"packages/plugin-mysql/**",
"packages/plugin-webui/**"
]
}
5 changes: 1 addition & 4 deletions build/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const KOISHI_VERSION = JSON.stringify(version)

;(async () => {
const root = resolve(__dirname, '../packages')
const chai = 'koishi-test-utils/chai'
const workspaces = [chai, ...await readdir(root)]
const workspaces = await readdir(root)
const tasks: Record<string, Promise<void>> = {}

await Promise.all(workspaces.map(async (name) => {
Expand Down Expand Up @@ -79,8 +78,6 @@ const KOISHI_VERSION = JSON.stringify(version)
entryPoints.push(base + '/src/transfer.ts')
entryPoints.push(...loaders.map(name => `${base}/src/loaders/${name}`))
options.define.BUILTIN_LOADERS = JSON.stringify(loaders.map(name => name.slice(0, -3)))
} else if (name === 'koishi-test-utils') {
await tasks[chai]
}

if (name !== 'plugin-eval') {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"addons": "yarn workspace addons",
"cli": "yarn compile koishi && yarn workspace test koishi",
"build": "yarn compile && yarn dtsc",
"build:ci": "yarn compile && yarn compile && yarn compile && yarn dtsc --listEmittedFiles && yarn build:web",
"build:web": "node -r ./build/register packages/plugin-webui/build",
"dtsc": "node -r ./build/register build/dtsc",
"bump": "node -r ./build/register build/bump",
Expand Down
10 changes: 7 additions & 3 deletions packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export namespace Tables {
export function extend(name: string, meta: Extension = {}) {
const { unique = [], foreign, fields = {} } = config[name] || {}
config[name] = {
type: 'incremental',
primary: 'id',
...meta,
unique: [...unique, ...meta.unique || []],
Expand Down Expand Up @@ -182,8 +181,12 @@ export namespace Query {
$not?: Expr<T>
}

export type Shorthand<T extends Primitive = Primitive> = T | T[] | Extract<T, string, RegExp>
export type FieldQuery<T = any> = FieldExpr<T> | (T extends Primitive ? Shorthand<T> : never)
export type Shorthand<T = any> =
| Extract<T, Comparable, T>
| Extract<T, Primitive, T[]>
| Extract<T, string, RegExp>

export type FieldQuery<T = any> = FieldExpr<T> | Shorthand<T>
export type Expr<T = any> = LogicalExpr<T> & {
[K in keyof T]?: FieldQuery<T[K]>
}
Expand Down Expand Up @@ -214,6 +217,7 @@ export namespace Query {
remove<T extends TableType>(table: T, query: Query<T>): Promise<void>
create<T extends TableType>(table: T, data: Partial<Tables[T]>): Promise<Tables[T]>
update<T extends TableType>(table: T, data: Partial<Tables[T]>[], key?: Index<T>): Promise<void>
drop(table?: TableType): Promise<void>
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/koishi-test-utils/chai/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "chai-extended",
"private": true,
"main": "lib/index.js",
"main": "src/index.ts",
"typings": "index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
"files": [
"index.d.ts",
"lib"
"index.d.ts"
]
}
13 changes: 12 additions & 1 deletion packages/koishi-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Logger } from 'koishi-utils'
import { MemoryDatabase } from './memory'

export default MemoryDatabase

const logger = new Logger('test')

process.on('unhandledRejection', (error) => {
logger.warn(error)
})

export function createArray<T>(length: number, create: (index: number) => T) {
return [...new Array(length).keys()].map(create)
}

export * from './app'
export * from './database'
export * from './tests'
12 changes: 10 additions & 2 deletions packages/koishi-test-utils/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function executeFieldQuery(query: Query.FieldQuery, data: any) {
return query.includes(data)
} else if (query instanceof RegExp) {
return query.test(data)
} else if (typeof query === 'string' || typeof query === 'number') {
return query === data
} else if (typeof query === 'string' || typeof query === 'number' || query instanceof Date) {
return data.valueOf() === query.valueOf()
}

// query operators
Expand Down Expand Up @@ -107,6 +107,14 @@ function executeQuery(query: Query.Expr, data: any): boolean {
}

Database.extend(MemoryDatabase, {
async drop(name) {
if (name) {
delete this.$store[name]
} else {
this.$store = {}
}
},

async get(name, query, modifier) {
const expr = Query.resolve(name, query)
const { fields, limit = Infinity, offset = 0 } = Query.resolveModifier(modifier)
Expand Down
Loading

0 comments on commit 4f71ce7

Please sign in to comment.