mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
add db mock
This commit is contained in:
1 parent
d01034bca8
commit
09ced5cdff
7 files changed
+109
-9
No files matched your search
+3
-1
@@ -24,4 +24,6 @@ packages/**/*.d.ts
|
|||||||
|
|
||||||
*.mmdb
|
*.mmdb
|
||||||
|
|
||||||
.coverage
|
.coverage
|
||||||
|
!__mocks__
|
||||||
|
globalConfig.json
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
module.exports = {
|
||||||
|
mongodbMemoryServerOptions: {
|
||||||
|
binary: {
|
||||||
|
version: '4.0.3',
|
||||||
|
skipMD5: true
|
||||||
|
},
|
||||||
|
instance: {
|
||||||
|
dbName: 'jest'
|
||||||
|
},
|
||||||
|
autoStart: false
|
||||||
|
}
|
||||||
|
};
|
||||||
+3
-8
@@ -1,13 +1,8 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
globals: {
|
preset: '@shelf/jest-mongodb',
|
||||||
'ts-jest': {
|
transform: {
|
||||||
diagnostics: {
|
'^.+\\.ts?$': 'ts-jest'
|
||||||
warnOnly: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
preset: 'ts-jest',
|
|
||||||
testEnvironment: 'node',
|
|
||||||
moduleFileExtensions: ['ts', 'js', 'json'],
|
moduleFileExtensions: ['ts', 'js', 'json'],
|
||||||
collectCoverage: true,
|
collectCoverage: true,
|
||||||
coverageDirectory: '<rootDir>/.coverage',
|
coverageDirectory: '<rootDir>/.coverage',
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "AGPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@shelf/jest-mongodb": "^1.2.2",
|
||||||
"@types/cross-spawn": "^6.0.2",
|
"@types/cross-spawn": "^6.0.2",
|
||||||
"@types/jest": "^26.0.9",
|
"@types/jest": "^26.0.9",
|
||||||
"@types/node": "^14.0.27",
|
"@types/node": "^14.0.27",
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/* eslint-disable import/no-mutable-exports */
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
|
import mongodb, { Db, MongoClient } from 'mongodb';
|
||||||
|
|
||||||
|
export let db: Db = null;
|
||||||
|
export let client: MongoClient = null;
|
||||||
|
|
||||||
|
export const bus = new EventEmitter();
|
||||||
|
|
||||||
|
mongodb.MongoClient.connect(
|
||||||
|
// @ts-ignore
|
||||||
|
global.__MONGO_URI__,
|
||||||
|
{ useNewUrlParser: true, useUnifiedTopology: true },
|
||||||
|
).then((c) => {
|
||||||
|
// @ts-ignore
|
||||||
|
db = c.db(global.__MONGO_DB_NAME__);
|
||||||
|
client = c;
|
||||||
|
bus.emit('connect');
|
||||||
|
});
|
||||||
|
|
||||||
|
export function collection(c: string) {
|
||||||
|
return db.collection(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getClient() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDb() {
|
||||||
|
return db;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import 'hydrooj/src/utils';
|
||||||
|
|
||||||
|
jest.mock('hydrooj/src/service/db');
|
||||||
|
|
||||||
|
export async function connect() {
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
const db = require('hydrooj/src/service/db');
|
||||||
|
db.bus.once('connect', resolve);
|
||||||
|
});
|
||||||
|
const script = require('../src/script/upgrade0_1');
|
||||||
|
await script.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispose() {
|
||||||
|
const db = require('hydrooj/src/service/db');
|
||||||
|
await db.getClient().close();
|
||||||
|
await db.getDb().close();
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import 'hydrooj';
|
||||||
|
import { connect, dispose } from './db';
|
||||||
|
|
||||||
|
const DOMAIN_ID = 'system';
|
||||||
|
const TITLE = 'dummy_title';
|
||||||
|
const CONTENT = 'dummy_content';
|
||||||
|
const UID = 22;
|
||||||
|
const PNAME = 'aaa';
|
||||||
|
|
||||||
|
describe('Model.Problem', () => {
|
||||||
|
let problem: typeof import('hydrooj/src/model/problem');
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await connect();
|
||||||
|
problem = require('hydrooj/src/model/problem');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('add_get', async () => {
|
||||||
|
const pid = await problem.add(DOMAIN_ID, PNAME, TITLE, CONTENT, UID);
|
||||||
|
let pdoc = await problem.get(DOMAIN_ID, PNAME);
|
||||||
|
expect(pdoc.domainId).toStrictEqual(DOMAIN_ID);
|
||||||
|
expect(pdoc.title).toStrictEqual(TITLE);
|
||||||
|
expect(pdoc.content).toStrictEqual(CONTENT);
|
||||||
|
expect(pdoc.owner).toStrictEqual(UID);
|
||||||
|
expect(pdoc.docId).toStrictEqual(pid);
|
||||||
|
expect(pdoc.pid).toStrictEqual(PNAME);
|
||||||
|
pdoc = await problem.get(DOMAIN_ID, pid);
|
||||||
|
expect(pdoc.domainId).toStrictEqual(DOMAIN_ID);
|
||||||
|
expect(pdoc.title).toStrictEqual(TITLE);
|
||||||
|
expect(pdoc.content).toStrictEqual(CONTENT);
|
||||||
|
expect(pdoc.owner).toStrictEqual(UID);
|
||||||
|
expect(pdoc.docId).toStrictEqual(pid);
|
||||||
|
expect(pdoc.pid).toStrictEqual(PNAME);
|
||||||
|
const pdocs = await problem.getMulti(DOMAIN_ID, {}).toArray();
|
||||||
|
expect(pdocs.length).toStrictEqual(1);
|
||||||
|
expect(pdocs[0].docId).toStrictEqual(pid);
|
||||||
|
expect(pdocs[0].title).toStrictEqual(TITLE);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(dispose);
|
||||||
|
})
|
||||||
Reference in new issue
Block a user