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 */
|
/* eslint-disable no-use-before-define */
|
||||||
|
|
||||||
import { requester } from './requester';
|
import { requester } from './requester';
|
||||||
|
import { updateDocument as updateDocumentInMongo } from './mongo';
|
||||||
import {
|
import {
|
||||||
assign,
|
assign,
|
||||||
each,
|
each,
|
||||||
@@ -14,10 +15,16 @@ class ApiObject {
|
|||||||
assign(this, options);
|
assign(this, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
update (options) {
|
async update (options) {
|
||||||
return new Promise((resolve) => {
|
if (isEmpty(options)) {
|
||||||
_updateDocument(this._docType, this, options, resolve);
|
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) {
|
function _updateLocalParameters (doc, update) {
|
||||||
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) {
|
|
||||||
each(update, (value, param) => {
|
each(update, (value, param) => {
|
||||||
set(doc, param, value);
|
set(doc, param, value);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,52 +1,76 @@
|
|||||||
import { MongoClient as mongo } from 'mongodb';
|
import { MongoClient as mongo } from 'mongodb';
|
||||||
|
|
||||||
|
const DB_URI = 'mongodb://localhost/habitrpg_test';
|
||||||
|
|
||||||
// Useful for checking things that have been deleted,
|
// Useful for checking things that have been deleted,
|
||||||
// but you no longer have access to,
|
// but you no longer have access to,
|
||||||
// like private parties or users
|
// 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) => {
|
return new Promise((resolve, reject) => {
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => {
|
let collection = db.collection(collectionName);
|
||||||
if (connectionError) return reject(connectionError);
|
|
||||||
let collection = db.collection(collectionName);
|
|
||||||
|
|
||||||
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
||||||
if (findError) return reject(findError);
|
if (findError) return reject(findError);
|
||||||
|
|
||||||
let exists = docs.length > 0;
|
let exists = docs.length > 0;
|
||||||
|
|
||||||
db.close();
|
db.close();
|
||||||
resolve(exists);
|
resolve(exists);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specifically helpful for the GET /groups tests,
|
// Specifically helpful for the GET /groups tests,
|
||||||
// resets the db to an empty state and creates a tavern document
|
// resets the db to an empty state and creates a tavern document
|
||||||
export function resetHabiticaDB () {
|
export async function resetHabiticaDB () {
|
||||||
|
let db = await connectToMongo();
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
db.dropDatabase((dbErr) => {
|
||||||
if (err) return reject(err);
|
if (dbErr) return reject(dbErr);
|
||||||
|
let groups = db.collection('groups');
|
||||||
|
|
||||||
db.dropDatabase((dbErr) => {
|
groups.insertOne({
|
||||||
if (dbErr) return reject(dbErr);
|
_id: 'habitrpg',
|
||||||
let groups = db.collection('groups');
|
chat: [],
|
||||||
|
leader: '9',
|
||||||
|
name: 'HabitRPG',
|
||||||
|
type: 'guild',
|
||||||
|
privacy: 'public',
|
||||||
|
members: [],
|
||||||
|
}, (insertErr) => {
|
||||||
|
if (insertErr) return reject(insertErr);
|
||||||
|
|
||||||
groups.insertOne({
|
db.close();
|
||||||
_id: 'habitrpg',
|
resolve();
|
||||||
chat: [],
|
|
||||||
leader: '9',
|
|
||||||
name: 'HabitRPG',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
members: [],
|
|
||||||
}, (insertErr) => {
|
|
||||||
if (insertErr) return reject(insertErr);
|
|
||||||
|
|
||||||
db.close();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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