mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
tests(api): Outsource mongo updates to mongo module in api helper
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/* eslint-disable no-use-before-define */
|
||||
|
||||
import { requester } from './requester';
|
||||
import { updateDocument as updateDocumentInMongo } from './mongo';
|
||||
import {
|
||||
assign,
|
||||
each,
|
||||
@@ -14,10 +15,16 @@ class ApiObject {
|
||||
assign(this, options);
|
||||
}
|
||||
|
||||
update (options) {
|
||||
return new Promise((resolve) => {
|
||||
_updateDocument(this._docType, this, options, resolve);
|
||||
});
|
||||
async update (options) {
|
||||
if (isEmpty(options)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await updateDocumentInMongo(this._docType, this, options);
|
||||
|
||||
_updateLocalParameters (this, options);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,26 +51,7 @@ export class ApiGroup extends ApiObject {
|
||||
}
|
||||
}
|
||||
|
||||
function _updateDocument (collectionName, doc, update, cb) {
|
||||
if (isEmpty(update)) {
|
||||
return cb();
|
||||
}
|
||||
|
||||
mongo.connect('mongodb://localhost/habitrpg_test', (connectErr, db) => {
|
||||
if (connectErr) throw new Error(`Error connecting to database when updating ${collectionName} collection: ${connectErr}`);
|
||||
|
||||
let collection = db.collection(collectionName);
|
||||
|
||||
collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => {
|
||||
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
||||
_updateLocalDocument(doc, update);
|
||||
db.close();
|
||||
cb();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function _updateLocalDocument (doc, update) {
|
||||
function _updateLocalParameters (doc, update) {
|
||||
each(update, (value, param) => {
|
||||
set(doc, param, value);
|
||||
});
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { MongoClient as mongo } from 'mongodb';
|
||||
|
||||
const DB_URI = 'mongodb://localhost/habitrpg_test';
|
||||
|
||||
// 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) {
|
||||
export async function checkExistence (collectionName, id) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
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) => {
|
||||
@@ -18,16 +20,14 @@ export function checkExistence (collectionName, id) {
|
||||
resolve(exists);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 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);
|
||||
export async function resetHabiticaDB () {
|
||||
let db = await connectToMongo();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
db.dropDatabase((dbErr) => {
|
||||
if (dbErr) return reject(dbErr);
|
||||
let groups = db.collection('groups');
|
||||
@@ -48,5 +48,29 @@ export function resetHabiticaDB () {
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateDocument (collectionName, doc, update) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
let collection = db.collection(collectionName);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => {
|
||||
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
||||
db.close();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function connectToMongo () {
|
||||
return new Promise((resolve, reject) => {
|
||||
mongo.connect(DB_URI, (err, db) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve(db);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user