test: add a few test cases

This commit is contained in:
chen 2020-11-11 22:52:27 +08:00
parent bf2e315c9e
commit 204a516165
3 changed files with 29 additions and 16 deletions

View File

@ -199,7 +199,7 @@ export class Resourcer {
getAction(name: string, action: ActionName): Action { getAction(name: string, action: ActionName): Action {
// 支持注册局部 action // 支持注册局部 action
if (this.handlers.has(`${name}.${action}`)) { if (this.handlers.has(`${name}:${action}`)) {
return this.getResource(name).getAction(`${name}:${action}`); return this.getResource(name).getAction(`${name}:${action}`);
} }
return this.getResource(name).getAction(action); return this.getResource(name).getAction(action);

View File

@ -22,17 +22,9 @@ describe('middleware', () => {
ctx.body = [3,4]; ctx.body = [3,4];
await next(); await next();
}, },
}); 'foo2s.bar2s:list': async (ctx, next) => {
console.log({ ctx.body = [5,6];
username: process.env.TEST_DB_USER, await next();
password: process.env.TEST_DB_PASSWORD,
database: process.env.TEST_DB_DATABASE,
host: process.env.TEST_DB_HOST,
port: process.env.TEST_DB_PORT as any,
dialect: process.env.TEST_DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
}, },
}); });
database = new Database({ database = new Database({
@ -84,4 +76,28 @@ describe('middleware', () => {
response = await request(http.createServer(app.callback())).get('/api/bars/1/foo'); response = await request(http.createServer(app.callback())).get('/api/bars/1/foo');
expect(response.body).toEqual([3,4]); expect(response.body).toEqual([3,4]);
}); });
it('shound work', async () => {
database.table({
name: 'foo2s',
fields: [
{
type: 'belongsToMany',
name: 'bar2s',
}
]
});
database.table({
name: 'bar2s',
fields: [
{
type: 'belongsToMany',
name: 'foo2s',
},
],
});
let response = await request(http.createServer(app.callback())).get('/api/foo2s/1/bar2s');
expect(response.body).toEqual([5,6]);
response = await request(http.createServer(app.callback())).get('/api/bar2s/1/foo2s');
expect(response.body).toEqual([1,2]);
});
}); });

View File

@ -18,7 +18,6 @@ export default function middleware(options: MiddlewareOptions = {}) {
paramsKey = 'params', paramsKey = 'params',
nameRule = getNameByParams, nameRule = getNameByParams,
} = options; } = options;
console.log(resourcer)
return async (ctx: ResourcerContext, next: () => Promise<any>) => { return async (ctx: ResourcerContext, next: () => Promise<any>) => {
ctx.resourcer = resourcer; ctx.resourcer = resourcer;
let params = parseRequest({ let params = parseRequest({
@ -34,7 +33,6 @@ export default function middleware(options: MiddlewareOptions = {}) {
try { try {
const resourceName = nameRule(params); const resourceName = nameRule(params);
if (!resourcer.isDefined(resourceName)) { if (!resourcer.isDefined(resourceName)) {
console.log('undefined')
const names = resourceName.split('.'); const names = resourceName.split('.');
const tableName = names.shift(); const tableName = names.shift();
if (database.isDefined(tableName)) { if (database.isDefined(tableName)) {
@ -75,10 +73,9 @@ export default function middleware(options: MiddlewareOptions = {}) {
return next(); return next();
} }
} }
console.log(resource); // console.log(resource);
// action 需要 clone 之后再赋给 ctx // action 需要 clone 之后再赋给 ctx
ctx.action = resourcer.getAction(resourceName, params.actionName).clone(); ctx.action = resourcer.getAction(resourceName, params.actionName).clone();
console.log(ctx.action);
ctx.action.setContext(ctx); ctx.action.setContext(ctx);
// 自带 query 处理的不太给力,需要用 qs 转一下 // 自带 query 处理的不太给力,需要用 qs 转一下
const query = qs.parse(qs.stringify(ctx.query)); const query = qs.parse(qs.stringify(ctx.query));