From 92229c1ee07913424c652c04b61d3fc71fdecefe Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 3 Apr 2021 10:35:53 +0800 Subject: [PATCH] core: add unit test for model.user --- .eslintignore | 2 - jest-mongodb-config.js | 2 +- packages/hydrooj/src/upgrade.ts | 1 + packages/hydrooj/test/model.problem.spec.ts | 22 ++++++++- packages/hydrooj/test/model.user.spec.ts | 54 +++++++++++++++++++++ packages/hydrooj/test/utils.spec.ts | 10 ++-- 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 packages/hydrooj/test/model.user.spec.ts diff --git a/.eslintignore b/.eslintignore index 71bfd891..c490c346 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,6 +1,4 @@ dist *.d.ts *.js -test -tests node_modules diff --git a/jest-mongodb-config.js b/jest-mongodb-config.js index 1eff779c..b3d8b50f 100644 --- a/jest-mongodb-config.js +++ b/jest-mongodb-config.js @@ -8,5 +8,5 @@ module.exports = { dbName: 'jest' }, autoStart: false - } + }, }; \ No newline at end of file diff --git a/packages/hydrooj/src/upgrade.ts b/packages/hydrooj/src/upgrade.ts index 5fe210b1..6651bec3 100644 --- a/packages/hydrooj/src/upgrade.ts +++ b/packages/hydrooj/src/upgrade.ts @@ -263,6 +263,7 @@ const scripts: UpgradeScript[] = [ await problem.edit(pdoc.domainId, pdoc.docId, { content: JSON.stringify(pdoc.content) }); } }); + return true; }, ]; diff --git a/packages/hydrooj/test/model.problem.spec.ts b/packages/hydrooj/test/model.problem.spec.ts index 0bc4080d..6e3f753f 100644 --- a/packages/hydrooj/test/model.problem.spec.ts +++ b/packages/hydrooj/test/model.problem.spec.ts @@ -4,6 +4,7 @@ import { connect, dispose } from './db'; const DOMAIN_ID = 'system'; const TITLE = 'dummy_title'; const CONTENT = 'dummy_content'; +const CONTENT_1 = 'another_dummy_content'; const UID = 22; const PNAME = 'aaa'; @@ -18,7 +19,7 @@ describe('Model.Problem', () => { test('add_get', async () => { const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID); 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.title).toStrictEqual(TITLE); expect(pdoc.content).toStrictEqual(CONTENT); @@ -33,5 +34,22 @@ describe('Model.Problem', () => { 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); -}) \ No newline at end of file +}); diff --git a/packages/hydrooj/test/model.user.spec.ts b/packages/hydrooj/test/model.user.spec.ts new file mode 100644 index 00000000..b81e724f --- /dev/null +++ b/packages/hydrooj/test/model.user.spec.ts @@ -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); +}); diff --git a/packages/hydrooj/test/utils.spec.ts b/packages/hydrooj/test/utils.spec.ts index f493a285..e7273d7d 100644 --- a/packages/hydrooj/test/utils.spec.ts +++ b/packages/hydrooj/test/utils.spec.ts @@ -1,5 +1,5 @@ import 'hydrooj/src/loader'; -import * as utils from 'hydrooj/dist/utils'; +import * as utils from 'hydrooj/src/utils'; describe('Utils', () => { test('Array.isDiff', () => { @@ -70,11 +70,11 @@ describe('Utils', () => { test('isClass', () => { const classA = class { }; - const classB = function () { }; - classB.prototype.get = function () { return 1 }; - const funcA = function () { }; + const classB = function a() { }; + classB.prototype.get = function a() { return 1; }; + const funcA = function a() { }; expect(utils.isClass(classA)).toBeTruthy(); expect(utils.isClass(classB)).toBeTruthy(); expect(utils.isClass(funcA)).toBeFalsy(); - }) + }); });