Skip to content

Commit

Permalink
Merge branch '2.1.1' of 10.73.97.24:oecloud.io/oe-common-mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsee committed Feb 18, 2020
2 parents 3b3d2fe + a490ad3 commit ae52102
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 104 deletions.
20 changes: 17 additions & 3 deletions common/mixins/history-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var loopback = require('loopback');
var logger = require('oe-logger');
var log = logger('history-mixin');
log.info('history-mixin Loaded');

const oecloudutil = require('oe-cloud/lib/common/util');

module.exports = function HistoryMixin(Model) {
// Skip this mixin where ever not applicable.
Expand Down Expand Up @@ -231,6 +231,22 @@ function createHistoryData(ctx, next) {
});
return next();
});
} else if ( ctx.data || ctx.instance ) {
var instance = ctx.data || ctx.instance;
var id = oecloudutil.getIdValue(ctx.Model, instance);
ctx.Model.findById(id, {notify: false}, function (err, data) {
if (err) {
return next(err);
}
if (!data) {
var e = new Error('Model ID error ' + id);
e.message = 'Model ID error ' + id;
return next(e);
}
ctx.hookState.historyData = [data.toObject()];
ctx.currentInstance = data;
return next();
});
} else {
return next();
}
Expand Down Expand Up @@ -287,5 +303,3 @@ function createHistoryDataForDelete(ctx, next) {
function insertIntoHistoryForDelete(ctx, next) {
return insertIntoHistory(ctx, next);
}


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oe-common-mixins",
"version": "2.1.0",
"version": "2.1.1",
"description": "oe-cloud modularization project aka oecloud.io",
"engines": {
"node": ">=6"
Expand Down
225 changes: 125 additions & 100 deletions test/history-mixin-test.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,59 @@
/**
*
* 2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/

// Author : Atul
var oecloud = require('oe-cloud');
var loopback = require('loopback');
var bootstrap = require('./bootstrap');
const uuidv4 = require('uuid/v4');
var debug = require('debug')('history-mixin-test');
/*var oecloud = require('oe-cloud');
var loopback = require('loopback');
oecloud.observe('loaded', function (ctx, next) {
oecloud.attachMixinsToBaseEntity("VersionMixin");
return next();
})
oecloud.boot(__dirname, function (err) {
if (err) {
console.log(err);
process.exit(1);
}
oecloud.start();
oecloud.emit('test-start');
});
*/

var chalk = require('chalk');
var chai = require('chai');
var async = require('async');
chai.use(require('chai-things'));

var expect = chai.expect;

var app = oecloud;
var defaults = require('superagent-defaults');
var supertest = require('supertest');
var Customer;
var api = defaults(supertest(app));
var basePath = app.get('restApiRoot');
var url = basePath + '/Employees';

var models = oecloud.models;

function deleteAllUsers(done) {
var userModel = loopback.findModel("User");
userModel.destroyAll({}, { notify: false }, function (err) {
if (err) {
return done(err);
}
userModel.find({}, {}, function (err2, r2) {
if (err2) {
return done(err2);
}
if (r2 && r2.length > 0) {
return done(new Error("Error : users were not deleted"));
}
});
return done(err);
});
}

var globalCtx = {
ignoreAutoScope: true,
ctx: { tenantId: '/default' }
};

describe(chalk.blue('History Mixin Test Started'), function (done) {

/**
*
* 2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/

// Author : Atul
var oecloud = require('oe-cloud');
var loopback = require('loopback');
var bootstrap = require('./bootstrap');
const uuidv4 = require('uuid/v4');
var debug = require('debug')('history-mixin-test');
/*var oecloud = require('oe-cloud');
var loopback = require('loopback');
oecloud.observe('loaded', function (ctx, next) {
oecloud.attachMixinsToBaseEntity("VersionMixin");
return next();
})
oecloud.boot(__dirname, function (err) {
if (err) {
console.log(err);
process.exit(1);
}
oecloud.start();
oecloud.emit('test-start');
});
*/

var chalk = require('chalk');
var chai = require('chai');
var async = require('async');
chai.use(require('chai-things'));

var expect = chai.expect;

var app = oecloud;
var defaults = require('superagent-defaults');
var supertest = require('supertest');
var api = defaults(supertest(app));

var basePath = app.get('restApiRoot');

var models = oecloud.models;


var globalCtx = {
ignoreAutoScope: true,
ctx: { tenantId: '/default' }
};

describe(chalk.blue('History Mixin Test Started'), function (done) {

var modelName = 'MixinTest';
var modelDetails = {
name: modelName,
Expand All @@ -85,13 +67,13 @@ describe(chalk.blue('History Mixin Test Started'), function (done) {
},
strict: false,
plural: modelName
};


this.timeout(10000);
var model;

before('Starting history mixin tests', function (done) {
};


this.timeout(10000);
var model;

before('Starting history mixin tests', function (done) {
models.ModelDefinition.create(modelDetails, globalCtx, function (err, res) {
if (err) {
debug('unable to create historyMixinTest model');
Expand All @@ -100,24 +82,24 @@ describe(chalk.blue('History Mixin Test Started'), function (done) {
model = loopback.getModel(modelName, globalCtx);
done();
}
});
});

afterEach('destroy context', function (done) {
done();
});


});
});

afterEach('destroy context', function (done) {
done();
});



it('t1 (oecloud 1.x tests) should create a history model for Test model', function (done) {
var mainModel = loopback.getModel(modelName, globalCtx);
var model = loopback.getModel(mainModel.modelName + 'History', globalCtx);
expect(model).not.to.be.null;
expect(model).not.to.be.undefined;
done();
});


});


it('t2 (oecloud 1.x tests) should insert data to TestModel, check if version is set and history model is empty ---programmatically',
function (done) {
this.timeout(50000);
Expand Down Expand Up @@ -281,11 +263,54 @@ describe(chalk.blue('History Mixin Test Started'), function (done) {
});
});



});






it('t6 (oecloud 2.x test) create record and then update using replacebyid', function (done) {
this.timeout(50000);
var postData = {
id : 123,
name : "Atul",
age : 30
};
var customerModel = loopback.findModel('Customer');
customerModel.create(postData, globalCtx, function (err, customer) {
if (err) {
return done(err);
} else {
customerModel.replaceById(customer.id, {name: "Atul111", age : 31, _version : customer._version}, globalCtx, function(err, customer2){
if(err){
return done(err);
}
var newData = customer2.toObject();
newData.name = 'Atul222';
customerModel.replaceOrCreate(newData, globalCtx, function(err, customer3){
if(err){
return done(err);
}
customer3.updateAttributes({name : "Atul3333", age : 35, id : customer3.id, _version : customer3._version}, globalCtx, function(err, customer4){
if(err){
return done(err);
}
if(customer4.name !== 'Atul3333' || customer4.age !== 35){
return done(new Error("data not matching. expetcing name change"));
}
var url = basePath + '/Customers/history?filter={"where" : { "_modelId" : 123 } }';

api
.get(url)
.send()
.expect(200).end(function (err, historyRes) {
if (err) {
return done(err);
} else {
expect(historyRes.body).not.to.be.empty;
expect(historyRes.body).to.have.length(3);
return done();
}
});
})
})
})
}
});
});
});

0 comments on commit ae52102

Please sign in to comment.