mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Merge pull request #27936 from overleaf/em-unit-tests-mongo
Make Mongo available to unit tests in all services GitOrigin-RevId: b65bbb69883d5bba31d09802b89f35bdc523fe4d
This commit is contained in:
@@ -20,18 +20,7 @@ function client(feature) {
|
||||
|
||||
async function cleanupTestRedis() {
|
||||
const rclient = client()
|
||||
ensureTestRedis(rclient)
|
||||
await rclient.flushall()
|
||||
}
|
||||
|
||||
function ensureTestRedis(rclient) {
|
||||
const host = rclient.options.host
|
||||
const env = process.env.NODE_ENV
|
||||
if (host !== 'redis_test' || env !== 'test') {
|
||||
throw new Error(
|
||||
`Refusing to clear Redis instance '${host}' in environment '${env}'`
|
||||
)
|
||||
}
|
||||
await redis.cleanupTestRedis(rclient)
|
||||
}
|
||||
|
||||
module.exports = { client, cleanupTestRedis }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const mongodb = require('mongodb-legacy')
|
||||
const OError = require('@overleaf/o-error')
|
||||
const Settings = require('@overleaf/settings')
|
||||
const MongoUtils = require('@overleaf/mongo-utils')
|
||||
const Mongoose = require('./Mongoose')
|
||||
const { addConnectionDrainer } = require('./GracefulShutdown')
|
||||
|
||||
@@ -93,32 +94,11 @@ async function getCollectionNames() {
|
||||
}
|
||||
|
||||
async function cleanupTestDatabase() {
|
||||
ensureTestDatabase()
|
||||
const collectionNames = await getCollectionNames()
|
||||
const collections = []
|
||||
for (const name of collectionNames) {
|
||||
if (name in db && name !== 'migrations') {
|
||||
collections.push(db[name])
|
||||
}
|
||||
}
|
||||
await Promise.all(collections.map(coll => coll.deleteMany({})))
|
||||
await MongoUtils.cleanupTestDatabase(mongoClient)
|
||||
}
|
||||
|
||||
async function dropTestDatabase() {
|
||||
ensureTestDatabase()
|
||||
await mongoClient.db().dropDatabase()
|
||||
}
|
||||
|
||||
function ensureTestDatabase() {
|
||||
const internalDb = mongoClient.db()
|
||||
const dbName = internalDb.databaseName
|
||||
const env = process.env.NODE_ENV
|
||||
|
||||
if (dbName !== 'test-overleaf' || env !== 'test') {
|
||||
throw new OError(
|
||||
`Refusing to clear database '${dbName}' in environment '${env}'`
|
||||
)
|
||||
}
|
||||
await MongoUtils.dropTestDatabase(mongoClient)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user