mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix unit test
This commit is contained in:
1 parent
5faa54c7ca
commit
49ec3c216f
6 files changed
+61
-69
No files matched your search
@@ -1,50 +1,50 @@
|
||||
/* eslint-disable import/no-mutable-exports */
|
||||
import { EventEmitter } from 'events';
|
||||
import mongodb, { Db, MongoClient } from 'mongodb';
|
||||
// @ts-nocheck
|
||||
import { Collection, Db, MongoClient } from 'mongodb';
|
||||
import * as bus from '../bus';
|
||||
import { Collections, BaseService } from '../../interface';
|
||||
|
||||
export let db: Db = null;
|
||||
export let client: MongoClient = null;
|
||||
export let db2: Db = null;
|
||||
export let client2: 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;
|
||||
mongodb.MongoClient.connect(
|
||||
// @ts-ignore
|
||||
global.__MONGO_URI__,
|
||||
{ useNewUrlParser: true, useUnifiedTopology: true },
|
||||
).then((c2) => {
|
||||
// @ts-ignore
|
||||
db2 = c2.db(global.__MONGO_DB_NAME__);
|
||||
client2 = c2;
|
||||
bus.emit('connect');
|
||||
});
|
||||
});
|
||||
|
||||
export function collection(c: string) {
|
||||
return db.collection(c);
|
||||
interface MongoConfig {
|
||||
protocol?: string,
|
||||
username?: string,
|
||||
password?: string,
|
||||
host?: string,
|
||||
port?: string,
|
||||
name?: string,
|
||||
url?: string,
|
||||
prefix?: string,
|
||||
}
|
||||
|
||||
export function getClient() {
|
||||
return client;
|
||||
class MongoService implements BaseService {
|
||||
public client: MongoClient;
|
||||
public client2: MongoClient;
|
||||
public db: Db;
|
||||
public db2: Db;
|
||||
public started = false;
|
||||
private opts: MongoConfig;
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
this.client = await MongoClient.connect(global.__MONGO_URI__, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db = this.client.db(global.__MONGO_DB_NAME_);
|
||||
this.client2 = await MongoClient.connect(global.__MONGO_URI__, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db2 = this.client2.db(global.__MONGO_DB_NAME_);
|
||||
await bus.parallel('database/connect', this.db);
|
||||
this.started = true;
|
||||
}
|
||||
|
||||
public collection<K extends keyof Collections>(c: K): Collection<Collections[K]> {
|
||||
if (this.opts.prefix) return this.db.collection(`${this.opts.prefix}.${c}`);
|
||||
return this.db.collection(c);
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
await this.db.close();
|
||||
await this.db2.close();
|
||||
await this.client.close();
|
||||
await this.client2.close();
|
||||
}
|
||||
}
|
||||
|
||||
export function getClient2() {
|
||||
return client2;
|
||||
}
|
||||
|
||||
export function getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
export function getDb2() {
|
||||
return db2;
|
||||
}
|
||||
const service = new MongoService();
|
||||
global.Hydro.service.db = service as any;
|
||||
export = service;
|
||||
@@ -1,28 +1,21 @@
|
||||
import 'hydrooj';
|
||||
import 'hydrooj/src/loader';
|
||||
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 modelSystem = require('hydrooj/src/model/system');
|
||||
const scripts = require('hydrooj/src/upgrade');
|
||||
let dbVer = (await modelSystem.get('db.ver')) || 0;
|
||||
const db = require('hydrooj/src/service/db');
|
||||
await db.start({});
|
||||
const scripts = require('hydrooj/src/upgrade').default;
|
||||
let dbVer = 0;
|
||||
const expected = scripts.length;
|
||||
while (dbVer < expected) {
|
||||
await scripts[dbVer]();
|
||||
const func = scripts[dbVer];
|
||||
dbVer++;
|
||||
await modelSystem.set('db.ver', dbVer);
|
||||
if (func.toString().includes('_FRESH_INSTALL_IGNORE')) continue;
|
||||
await func();
|
||||
}
|
||||
}
|
||||
|
||||
export async function dispose() {
|
||||
const db = require('hydrooj/src/service/db');
|
||||
await Promise.all([
|
||||
db.getClient().close(),
|
||||
db.getClient2().close(),
|
||||
db.getDb().close(),
|
||||
db.getDb2().close(),
|
||||
]);
|
||||
await db.stop();
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'hydrooj';
|
||||
import 'hydrooj/src/utils';
|
||||
import 'hydrooj/src/loader';
|
||||
import { md5, sha1 } from 'hydrooj/src/lib/crypto';
|
||||
import pwhash from 'hydrooj/src/lib/hash.hydro';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'hydrooj';
|
||||
import 'hydrooj/src/loader';
|
||||
import * as validator from 'hydrooj/src/lib/validator';
|
||||
|
||||
describe('Validator', () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'hydrooj';
|
||||
import 'hydrooj/src/loader';
|
||||
import { connect, dispose } from './db';
|
||||
|
||||
const DOMAIN_ID = 'system';
|
||||
@@ -8,11 +8,11 @@ const UID = 22;
|
||||
const PNAME = 'aaa';
|
||||
|
||||
describe('Model.Problem', () => {
|
||||
let problem: typeof import('hydrooj/src/model/problem');
|
||||
let problem: typeof import('hydrooj/src/model/problem').default;
|
||||
|
||||
beforeAll(async () => {
|
||||
await connect();
|
||||
problem = require('hydrooj/src/model/problem');
|
||||
problem = require('hydrooj/src/model/problem').default;
|
||||
});
|
||||
|
||||
test('add_get', async () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'hydrooj';
|
||||
import * as utils from 'hydrooj/src/utils';
|
||||
import 'hydrooj/src/loader';
|
||||
import * as utils from 'hydrooj/dist/utils';
|
||||
|
||||
describe('Utils', () => {
|
||||
test('Array.isDiff', () => {
|
||||
|
||||
Reference in new issue
Block a user