'use strict';
const mongoose = require('../index');
* Generates a random string
*/
exports.random = function() {
return Math.random().toString().substring(3);
};
exports.clearTestData = async function clearTestData(db) {
async function _inner() {
await db.dropDatabase();
}
let retries = 5;
while (retries > 0) {
retries -= 1;
try {
await _inner();
return;
} catch (err) {
const retryable = err instanceof mongoose.mongo.MongoWriteConcernError && /operation was interrupted/.test(err.message);
if (retryable && retries > 0) {
console.log('DropDB operation interrupted, retrying');
continue;
}
throw err;
}
}
};
exports.stopRemainingOps = function stopRemainingOps(db) {
for (const name of Object.keys(db.models)) {
const model = db.models[name];
model.collection.buffer = true;
if (db.collections[model.collection.name] === model.collection) {
delete db.collections[model.collection.name];
}
}
};