mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: add unit test for model.user
This commit is contained in:
1 parent
b9fdf3bca9
commit
92229c1ee0
6 files changed
+81
-10
No files matched your search
@@ -1,6 +1,4 @@
|
|||||||
dist
|
dist
|
||||||
*.d.ts
|
*.d.ts
|
||||||
*.js
|
*.js
|
||||||
test
|
|
||||||
tests
|
|
||||||
node_modules
|
node_modules
|
||||||
@@ -8,5 +8,5 @@ module.exports = {
|
|||||||
dbName: 'jest'
|
dbName: 'jest'
|
||||||
},
|
},
|
||||||
autoStart: false
|
autoStart: false
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
@@ -263,6 +263,7 @@ const scripts: UpgradeScript[] = [
|
|||||||
await problem.edit(pdoc.domainId, pdoc.docId, { content: JSON.stringify(pdoc.content) });
|
await problem.edit(pdoc.domainId, pdoc.docId, { content: JSON.stringify(pdoc.content) });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return true;
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { connect, dispose } from './db';
|
|||||||
const DOMAIN_ID = 'system';
|
const DOMAIN_ID = 'system';
|
||||||
const TITLE = 'dummy_title';
|
const TITLE = 'dummy_title';
|
||||||
const CONTENT = 'dummy_content';
|
const CONTENT = 'dummy_content';
|
||||||
|
const CONTENT_1 = 'another_dummy_content';
|
||||||
const UID = 22;
|
const UID = 22;
|
||||||
const PNAME = 'aaa';
|
const PNAME = 'aaa';
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ describe('Model.Problem', () => {
|
|||||||
test('add_get', async () => {
|
test('add_get', async () => {
|
||||||
const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID);
|
const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID);
|
||||||
expect(pid).toBeTruthy();
|
expect(pid).toBeTruthy();
|
||||||
let pdoc = await problem.get(DOMAIN_ID, PNAME);
|
const pdoc = await problem.get(DOMAIN_ID, PNAME);
|
||||||
expect(pdoc.domainId).toStrictEqual(DOMAIN_ID);
|
expect(pdoc.domainId).toStrictEqual(DOMAIN_ID);
|
||||||
expect(pdoc.title).toStrictEqual(TITLE);
|
expect(pdoc.title).toStrictEqual(TITLE);
|
||||||
expect(pdoc.content).toStrictEqual(CONTENT);
|
expect(pdoc.content).toStrictEqual(CONTENT);
|
||||||
@@ -33,5 +34,22 @@ describe('Model.Problem', () => {
|
|||||||
expect(pdocs[0].title).toStrictEqual(TITLE);
|
expect(pdocs[0].title).toStrictEqual(TITLE);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('edit', async () => {
|
||||||
|
const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID);
|
||||||
|
const pdoc = await problem.edit(DOMAIN_ID, pid, { content: CONTENT_1 });
|
||||||
|
expect(pdoc.content).toStrictEqual(CONTENT_1);
|
||||||
|
});
|
||||||
|
|
||||||
|
/* FIXME doesn't work as storage isn't mocked yet
|
||||||
|
test('del', async () => {
|
||||||
|
const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID);
|
||||||
|
let pdocs = await problem.getMulti(DOMAIN_ID, {}).toArray();
|
||||||
|
let count = pdocs.length;
|
||||||
|
await problem.del(DOMAIN_ID, pid);
|
||||||
|
pdocs = await problem.getMulti(DOMAIN_ID, {}).toArray();
|
||||||
|
expect(pdocs.length).toStrictEqual(count - 1);
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
|
||||||
afterAll(dispose);
|
afterAll(dispose);
|
||||||
})
|
});
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import 'hydrooj/src/loader';
|
||||||
|
import * as bus from 'hydrooj/src/service/bus';
|
||||||
|
import { PRIV } from 'hydrooj/src/model/builtin';
|
||||||
|
import { connect, dispose } from './db';
|
||||||
|
|
||||||
|
describe('Model.User', () => {
|
||||||
|
let user: typeof import('hydrooj/src/model/user').default;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await connect();
|
||||||
|
user = require('hydrooj/src/model/user').default;
|
||||||
|
require('hydrooj/src/model/setting');
|
||||||
|
await bus.serial('app/started');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create', async () => {
|
||||||
|
const uid = await user.create('i@undefined.moe', 'undefined', '123456');
|
||||||
|
expect(uid).toStrictEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getById', async () => {
|
||||||
|
const udoc = await user.getById('system', 2);
|
||||||
|
expect(udoc.mail).toStrictEqual('i@undefined.moe');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getByEmail', async () => {
|
||||||
|
const udoc = await user.getByEmail('system', 'i@undefined.moe');
|
||||||
|
expect(udoc.uname).toStrictEqual('undefined');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getByUname', async () => {
|
||||||
|
const udoc = await user.getByUname('system', 'undefined');
|
||||||
|
expect(udoc.mail).toStrictEqual('i@undefined.moe');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('createWithExtraArgs', async () => {
|
||||||
|
const uid1 = await user.create('test@undefined.moe', 'test', '123456', -1, '127.0.0.1', -1);
|
||||||
|
expect(uid1).toStrictEqual(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getList', async () => {
|
||||||
|
const udoc1 = await user.getList('system', [-1, 1]);
|
||||||
|
expect(udoc1[-1].uname).toStrictEqual('test');
|
||||||
|
expect(udoc1[-1].hasPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN)).toStrictEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getPrefixList', async () => {
|
||||||
|
const udocs = await user.getPrefixList('system', 'u', 1);
|
||||||
|
expect(udocs.length).toStrictEqual(1);
|
||||||
|
expect(udocs[0].uname).toStrictEqual('undefined');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(dispose);
|
||||||
|
});
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import 'hydrooj/src/loader';
|
import 'hydrooj/src/loader';
|
||||||
import * as utils from 'hydrooj/dist/utils';
|
import * as utils from 'hydrooj/src/utils';
|
||||||
|
|
||||||
describe('Utils', () => {
|
describe('Utils', () => {
|
||||||
test('Array.isDiff', () => {
|
test('Array.isDiff', () => {
|
||||||
@@ -70,11 +70,11 @@ describe('Utils', () => {
|
|||||||
|
|
||||||
test('isClass', () => {
|
test('isClass', () => {
|
||||||
const classA = class { };
|
const classA = class { };
|
||||||
const classB = function () { };
|
const classB = function a() { };
|
||||||
classB.prototype.get = function () { return 1 };
|
classB.prototype.get = function a() { return 1; };
|
||||||
const funcA = function () { };
|
const funcA = function a() { };
|
||||||
expect(utils.isClass(classA)).toBeTruthy();
|
expect(utils.isClass(classA)).toBeTruthy();
|
||||||
expect(utils.isClass(classB)).toBeTruthy();
|
expect(utils.isClass(classB)).toBeTruthy();
|
||||||
expect(utils.isClass(funcA)).toBeFalsy();
|
expect(utils.isClass(funcA)).toBeFalsy();
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user