Skip to content

Commit

Permalink
refactoring connector testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Signorini committed Sep 20, 2019
1 parent bc96b36 commit 839eb04
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 73 deletions.
3 changes: 2 additions & 1 deletion app/app.js
Expand Up @@ -7,6 +7,7 @@ const kraken = require('kraken-js');

const db_connect = require('core/libs/db_run');
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();
const Connector = require('core/repositories/daos/connector/connector');

/*
Expand All @@ -21,7 +22,7 @@ const options = {
*/

db_connect(function *() {
yield Connector.connect(dbpath);
yield Connector.connect(dbpath, dbname);
next(null, config);
console.log("Maestro: Mongo online");
});
Expand Down
6 changes: 6 additions & 0 deletions app/core/libs/dbname.js
@@ -0,0 +1,6 @@
'use strict';


module.exports = () => {
return process.env.MAESTRO_MONGO_DATABASE || 'maestro-client';
};
29 changes: 18 additions & 11 deletions app/core/repositories/daos/DBConnector.js
Expand Up @@ -93,7 +93,7 @@ class Dao extends Model {
});

return this._collection()
.then((collection) => collection.insert(attrs))
.then((collection) => collection.insertOne(attrs))
.then((inserted) => {
let doc = inserted.ops[0];
this.set('_id', doc._id);
Expand All @@ -109,7 +109,7 @@ class Dao extends Model {
this.set('updated_at', new Date());

return this._collection()
.then((collection) => collection.update({ _id: attrs._id }, attrs))
.then((collection) => collection.updateOne({ _id: attrs._id }, attrs))
.return(this);
}

Expand All @@ -122,20 +122,14 @@ class Dao extends Model {
return bcrypt.hashSync(string, crypto.getCryptLevel());
}

remove() {
return this._collection()
.then((collection) => collection.remove({ _id: this.get('_id') }))
.return(this);
}

inc(props) {
let id = this.get('_id');

if (!id)
throw new Error('Can\'t atomically increment a property of unsaved document.');

return this._collection()
.then( (collection) => collection.update({ _id: id }, { '$inc': props }))
.then( (collection) => collection.updateOne({ _id: id }, { '$inc': props }))
.then(() => {
Object.keys(props).forEach((key) => {
let value = this.get(key);
Expand All @@ -151,7 +145,20 @@ class Dao extends Model {
const opp = `update${opts}Factory`;
this.set('updated_at', new Date());

return this[opp](filter, null, options);
let entity = filter
let entry = null

this.passHash();

return this._collection()
.then((collection) => {
const subs = entry ? {[entry]: this.get()} : this.get();
return collection.replaceOne(entity, subs, options);
})
.then((e) => {
return this.isUpdater = e.result;
})
.return(this);
}

updateAndModify(filter, options) {
Expand Down Expand Up @@ -190,7 +197,7 @@ class Dao extends Model {
return this._collection()
.then((collection) => {
const subs = entry ? {[entry]: this.get()} : this.get();
return collection.update(entity, subs, options);
return collection.updateOne(entity, subs, options);
})
.then((e) => {
return this.isUpdater = e.result;
Expand Down
34 changes: 12 additions & 22 deletions app/core/repositories/daos/connector/connector.js
Expand Up @@ -12,34 +12,24 @@ const MongoClient = mongodb.MongoClient;

class Connector {}

Connector.connect = function () {
let args = Array.prototype.slice.call(arguments);
Connector.connect = function (path, dbname, options = {}) {

this.url = path;
this.dbname = path;

let urls = [];
let options = {};
const strOpts = {
useUnifiedTopology: true,
useNewUrlParser: true
};

args.forEach(function (arg) {
if (_.isString(arg))
urls.push(arg);

if (_.isObject(arg))
options = arg;
});



let connection = MongoClient.connect(urls.join(','), strOpts, options).then((db) => {
if (!this.db) {
db.url = urls.join(',');
this.db = db;
}
let connection = MongoClient.connect(path, strOpts, options)
.then((client) => {
if (!this.db)
this.db = client.db(dbname);

return db;
});
return this.db;
})
.catch(console.log);

if (!this._connection)
this._connection = connection;
Expand Down Expand Up @@ -77,7 +67,7 @@ Connector.close = function () {
*/

Connector._collection = function (db, name) {
let url = db.url;
const url = this.url;
let collections = this._collections[url];

if (!collections)
Expand Down
28 changes: 14 additions & 14 deletions app/core/repositories/daos/connector/query.js
Expand Up @@ -12,7 +12,7 @@ class Query {
this.options = {
populate: {},
sort: {},
fields: {}
projection: {}
};
this.lastKey = key;
}
Expand Down Expand Up @@ -63,37 +63,37 @@ Query.prototype.match = function () {

Query.prototype.include = function (key, value) {
if (Array.isArray(key)) {
let fields = key;
fields.forEach((k) => this.include(k));
let projection = key;
projection.forEach((k) => this.include(k));

} else if (_.isObject(key)) {
let fields = key;
let keys = Object.keys(fields);
let projection = key;
let keys = Object.keys(projection);

keys.forEach((k) => this.include(k, fields[k]));
keys.forEach((k) => this.include(k, projection[k]));
}

if (_.isString(key))
this.options.fields[key] = value === undefined ? 1 : value;
this.options.projection[key] = value === undefined ? 1 : value;

return this;
};

Query.prototype.exclude = function (key, value) {

if (Array.isArray(key)) {
let fields = key;
fields.forEach((k) => this.exclude(k));
let projection = key;
projection.forEach((k) => this.exclude(k));

} else if (_.isObject(key)) {
let fields = key;
let keys = Object.keys(fields);
let projection = key;
let keys = Object.keys(projection);

keys.forEach((k) => this.exclude(k, fields[k]));
keys.forEach((k) => this.exclude(k, projection[k]));
}

if (_.isString(key))
this.options.fields[key] = value === undefined ? 0 : value;
this.options.projection[key] = value === undefined ? 0 : value;

return this;
};
Expand Down Expand Up @@ -267,7 +267,7 @@ Query.prototype.findById = function (id) {

Query.prototype.remove = function (query) {
this.where(query);
return this.collection.then((collection) => collection.remove(this.query, this.options));
return this.collection.then((collection) => collection.deleteMany(this.query, this.options));
};

// Setting up functions that
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
"test": "mocha test/**/*js --timeout 5000 --reporter spec --exit",
"e2e": "gulp test_e2e",
"unit": "gulp test_unit",
"integration": "gulp test_integration",
"lint": "gulp eslint",
"tdd": "gulp tdd_single",
"package": "docker build -t 'server-maestro' .",
Expand Down
2 changes: 1 addition & 1 deletion tasks/test_integration.js
Expand Up @@ -12,7 +12,7 @@ module.exports = function (gulp, $) {
timeout: 6000,
'check-leaks': true,
ui: 'bdd',
reporter: 'tap' //spec, tap
reporter: 'nyan'
}));
});
};
3 changes: 2 additions & 1 deletion test/behavior/libs/bootApp.js
Expand Up @@ -8,14 +8,15 @@ const kraken = require('kraken-js');
const db_connect = require('core/libs/db_run');
const Connector = require('core/repositories/daos/connector/connector');
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();
const path = require('path');

module.exports = function (conn = dbpath) {
let options = {
basedir: path.resolve(__dirname, '../../../app/'),
onconfig: function (config, next) {
db_connect(function *() {
yield Connector.connect(conn);
yield Connector.connect(conn, dbname);
next(null, config);
});
}
Expand Down
9 changes: 5 additions & 4 deletions test/behavior/libs/cleaner_db.js
Expand Up @@ -5,6 +5,7 @@ require('app-module-path').addPath(`${__dirname}/../../../app`); //make more rea
const _ = require('lodash');
let MongoClient = require("mongodb").MongoClient;
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();

const interactC = function (db, collections) {
let pros=[];
Expand All @@ -20,7 +21,7 @@ const interactC = function (db, collections) {
}

db.collection(collection.tb, ids, (err1, coll) => {
pros.push(coll.remove({}));
pros.push(coll.deleteMany({}));
});

});
Expand All @@ -35,14 +36,14 @@ module.exports = function (collections, done, mock, conn = dbpath) {
};

MongoClient.connect(conn, strOpts)
.then((db) => {

.then((client) => {
const db = client.db(dbname);
const pros = interactC(db, collections);

Promise.all(pros)
.then(() => {
done();
db.close();
client.close();
//mock.close(done);
});
});
Expand Down
7 changes: 5 additions & 2 deletions test/behavior/libs/insert_db.js
Expand Up @@ -3,6 +3,7 @@
const _ = require('lodash');
let MongoClient = require("mongodb").MongoClient;
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();

module.exports = function (data, table, done, conn = dbpath) {
const strOpts = {
Expand All @@ -11,8 +12,10 @@ module.exports = function (data, table, done, conn = dbpath) {
};

MongoClient.connect(conn, strOpts)
.then((db) => {
.then((client) => {
const db = client.db(dbname);

let pets = db.collection(table);
pets.insert(data, done);
pets.insertMany(data, done);
});
};
4 changes: 3 additions & 1 deletion test/e2e/libs/adminer_connections.js
Expand Up @@ -3,6 +3,7 @@
const _ = require('lodash');
let MongoClient = require("mongodb").MongoClient;
const dbpath = require('../../../app/core/libs/dbpath')();
const dbname = require('../../../app/core/libs/dbname')();

module.exports = function (done, conn = dbpath) {
const strOpts = {
Expand All @@ -11,7 +12,8 @@ module.exports = function (done, conn = dbpath) {
};

MongoClient.connect(conn, strOpts)
.then((db) => {
.then((client) => {
const db = client.db(dbname);
let pets = db.collection('adminer');

const data = [
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/libs/bootApp.js
Expand Up @@ -8,6 +8,7 @@ const kraken = require('kraken-js');
const db_connect = require('core/libs/db_run');
const Connector = require('core/repositories/daos/connector/connector');
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();
const path = require('path');


Expand All @@ -17,7 +18,7 @@ module.exports = function (conn = dbpath) {
basedir: path.resolve(__dirname, '../../../app/'),
onconfig: function (config, next) {
db_connect(function* () {
yield Connector.connect(conn);
yield Connector.connect(conn, dbname);
next(null, config);
});
}
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/libs/cleaner_db.js
Expand Up @@ -3,6 +3,7 @@
const _ = require('lodash');
let MongoClient = require("mongodb").MongoClient;
const dbpath = require('../../../app/core/libs/dbpath')();
const dbname = require('../../../app/core/libs/dbname')();

const interactC = function (db, collections) {
let pros=[];
Expand All @@ -18,7 +19,7 @@ const interactC = function (db, collections) {
}

db.collection(collection.tb, ids, (err1, coll) => {
pros.push(coll.remove({}));
pros.push(coll.deleteMany({}));
});

});
Expand All @@ -33,14 +34,14 @@ module.exports = function (collections, done, mock, conn = dbpath) {
};

MongoClient.connect(conn, strOpts)
.then((db) => {

.then((client) => {
const db = client.db(dbname);
const pros = interactC(db, collections);

Promise.all(pros)
.then(() => {
done();
db.close();
client.close();
//mock.close(done);
});
});
Expand Down

0 comments on commit 839eb04

Please sign in to comment.