mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
tests: Extract mongo functions into separate file
This commit is contained in:
@@ -4,7 +4,6 @@ import {
|
||||
each,
|
||||
times,
|
||||
} from 'lodash';
|
||||
import { MongoClient as mongo } from 'mongodb';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
import { ApiUser, ApiGroup } from './api-integration/api-classes';
|
||||
@@ -17,26 +16,7 @@ export { requester };
|
||||
|
||||
export { translate } from './api-integration/translate';
|
||||
|
||||
// Useful for checking things that have been deleted,
|
||||
// but you no longer have access to,
|
||||
// like private parties or users
|
||||
export function checkExistence (collectionName, id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => {
|
||||
if (connectionError) return reject(connectionError);
|
||||
let collection = db.collection(collectionName);
|
||||
|
||||
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
||||
if (findError) return reject(findError);
|
||||
|
||||
let exists = docs.length > 0;
|
||||
|
||||
db.close();
|
||||
resolve(exists);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
export { checkExistence, resetHabiticaDB } from './api-integration/mongo';
|
||||
|
||||
// Creates a new user and returns it
|
||||
// If you need the user to have specific requirements,
|
||||
@@ -152,33 +132,3 @@ export function createAndPopulateGroup (settings = {}) {
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
// Specifically helpful for the GET /groups tests,
|
||||
// resets the db to an empty state and creates a tavern document
|
||||
export function resetHabiticaDB () {
|
||||
return new Promise((resolve, reject) => {
|
||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
db.dropDatabase((dbErr) => {
|
||||
if (dbErr) return reject(dbErr);
|
||||
let groups = db.collection('groups');
|
||||
|
||||
groups.insertOne({
|
||||
_id: 'habitrpg',
|
||||
chat: [],
|
||||
leader: '9',
|
||||
name: 'HabitRPG',
|
||||
type: 'guild',
|
||||
privacy: 'public',
|
||||
members: [],
|
||||
}, (insertErr) => {
|
||||
if (insertErr) return reject(insertErr);
|
||||
|
||||
db.close();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user