Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orm unit test api #347

Merged
merged 37 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b99a137
feat(test-utils): new orm unit test api
NWYLZW Aug 17, 2021
0ee38c5
Merge branch 'develop' into yijie/orm-unit-test-api
shigma Aug 18, 2021
6725d7e
refactor test units
shigma Aug 18, 2021
3dc5b92
finalize memory tests
shigma Aug 18, 2021
3158524
refactor mysql, mongo tests
shigma Aug 18, 2021
98c3439
do not fail fast
shigma Aug 18, 2021
7d91810
tweak node 16 unhandled rejections
shigma Aug 19, 2021
16a46c1
fix(core): throw error when not set bot type
NWYLZW Aug 14, 2021
54468d6
fix(database): resolve conversations
NWYLZW Aug 15, 2021
e205d97
fix(test-utils): mysql and mongo plugin isn't test
NWYLZW Aug 19, 2021
cef7ae2
adjust workflow
shigma Aug 19, 2021
cb523f7
why can't i use anchor
shigma Aug 19, 2021
05b8f44
try artifact, thanks to @NWYLZW
shigma Aug 19, 2021
b13bb23
chore: tweak
shigma Aug 19, 2021
c7d1920
chore: tweak
shigma Aug 19, 2021
d1303b1
build chai directly
shigma Aug 19, 2021
c06fb80
tweak
shigma Aug 19, 2021
4bf7e95
do not use incremental as default
shigma Aug 19, 2021
18ecf2d
tweak
shigma Aug 19, 2021
396423d
fix
shigma Aug 19, 2021
4552097
support drop()
shigma Aug 19, 2021
e622044
drop database after test
shigma Aug 19, 2021
0d33d6a
fix
shigma Aug 19, 2021
edfc9c3
fix mysql bugs
shigma Aug 19, 2021
151b517
fix: mongo plugin wrong
NWYLZW Aug 19, 2021
9d84779
feat: support drop table orm api
NWYLZW Aug 19, 2021
79497ac
optimize unit creation
shigma Aug 19, 2021
21fa7ee
fix
NWYLZW Aug 19, 2021
f50adb0
support Date shorthand
shigma Aug 19, 2021
88a292c
refactor tests
shigma Aug 19, 2021
53b35c2
renaming foo keys
shigma Aug 19, 2021
c8126e7
fix
NWYLZW Aug 20, 2021
a2619cc
fix: merge conflict
NWYLZW Aug 20, 2021
23acbb4
fix: add comment
NWYLZW Aug 20, 2021
2536083
tweak
shigma Aug 20, 2021
09cb9da
update workflow
shigma Aug 20, 2021
9f48640
final changes
shigma Aug 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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