2015-09-18 16:32:17 +00:00
'use strict' ;
/ * *
* Module dependencies .
* /
var should = require ( 'should' ) ,
mongoose = require ( 'mongoose' ) ,
User = mongoose . model ( 'User' ) ,
Form = mongoose . model ( 'Form' ) ,
Field = mongoose . model ( 'Field' ) ,
_ = require ( 'lodash' ) ,
2015-10-07 01:28:11 +00:00
async = require ( 'async' ) ,
soap = require ( 'soap' ) ,
2015-09-18 16:32:17 +00:00
config = require ( '../../config/config' ) ,
2015-10-07 01:28:11 +00:00
OscarSecurity = require ( '../../scripts/oscarhost/OscarSecurity' ) ,
2015-09-18 16:32:17 +00:00
FormSubmission = mongoose . model ( 'FormSubmission' ) ;
var exampleDemo = {
address : '880-9650 Velit. St.' ,
city : '' ,
dateOfBirth : '10' ,
2015-10-07 01:16:47 +00:00
displayName : 'Test User' ,
email : 'polydaic@gmail.com' ,
firstName : 'Test User' ,
2015-09-18 16:32:17 +00:00
hin : '' ,
2015-10-07 01:16:47 +00:00
lastName : 'AddDemographic' ,
2015-10-30 20:14:48 +00:00
lastUpdateDate : '2014-10-01 00:00:00' ,
2015-09-18 16:32:17 +00:00
monthOfBirth : '05' ,
officialLanguage : 'English' ,
2015-10-07 01:16:47 +00:00
phone : '250-222-2222' ,
2015-09-18 16:32:17 +00:00
phone2 : '' ,
2015-11-23 21:06:02 +00:00
postal : 'S4M 7T8' ,
2015-09-18 16:32:17 +00:00
province : 'BC' ,
2015-10-07 01:16:47 +00:00
sex : 'F' ,
2015-09-18 16:32:17 +00:00
sexDesc : 'Female' ,
sin : '' ,
spokenLanguage : 'English' ,
title : 'MS.' ,
yearOfBirth : '2015'
2015-11-23 21:06:02 +00:00
} ;
2015-10-07 01:16:47 +00:00
2015-10-06 20:14:38 +00:00
var sampleFormFields = [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] , 'fieldValue' : '' } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : '' } ,
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '' }
] ;
var sampleSubmission = [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : 'David' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : 'Baldwynn' } ,
2015-10-07 01:16:47 +00:00
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldValue' : 'M' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : 'Tue Oct 06 2015 15:17:48 GMT-0700 (PDT)' } ,
2015-10-06 20:14:38 +00:00
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '6043158008' }
] ;
2015-09-18 16:32:17 +00:00
/ * *
* Globals
* /
var user , myForm , mySubmission ;
/ * *
* Unit tests
* /
describe ( 'FormSubmission Model Unit Tests:' , function ( ) {
2015-10-07 01:16:47 +00:00
this . timeout ( 15000 ) ;
2015-09-18 16:32:17 +00:00
beforeEach ( function ( done ) {
user = new User ( {
firstName : 'Full' ,
lastName : 'Name' ,
displayName : 'Full Name' ,
2015-10-06 20:14:38 +00:00
email : 'test1@test.com' + Date . now ( ) ,
username : 'test1@test.com' + Date . now ( ) ,
password : 'password' ,
provider : 'local'
2015-09-18 16:32:17 +00:00
} ) ;
2015-10-06 20:14:38 +00:00
user . save ( function ( err ) {
if ( err ) {
console . log ( err . errors ) ;
done ( err ) ;
}
2015-09-18 16:32:17 +00:00
myForm = new Form ( {
2015-10-06 20:14:38 +00:00
title : 'Form Title1' ,
2015-10-30 18:40:02 +00:00
admin : user . _id ,
2015-09-18 16:32:17 +00:00
language : 'english' ,
form _fields : [
{ 'fieldType' : 'textfield' , 'title' : 'What\'s your first name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'textfield' , 'title' : 'And your last name' , 'fieldValue' : '' } ,
{ 'fieldType' : 'radio' , 'title' : 'And your sex' , 'fieldOptions' : [ { 'option_id' : 0 , 'option_title' : 'Male' , 'option_value' : 'M' } , { 'option_id' : 1 , 'option_title' : 'Female' , 'option_value' : 'F' } ] , 'fieldValue' : '' } ,
{ 'fieldType' : 'date' , 'title' : 'When were you born?' , 'fieldValue' : '' } ,
{ 'fieldType' : 'number' , 'title' : 'What\'s your phone #?' , 'fieldValue' : '' } ,
] ,
plugins : {
oscarhost : {
baseUrl : config . oscarhost . baseUrl ,
settings : {
updateType : 'force_add' ,
} ,
auth : config . oscarhost . auth ,
}
}
} ) ;
2015-10-06 20:14:38 +00:00
myForm . save ( function ( err , form ) {
if ( err ) {
console . log ( err . errors ) ;
done ( err ) ;
}
var submissionFields = _ . clone ( myForm . form _fields ) ;
for ( var z = 0 ; z < submissionFields . length ; z ++ ) {
2015-10-07 01:16:47 +00:00
submissionFields [ z ] = _ . extend ( myForm . form _fields [ z ] , sampleSubmission [ z ] ) ;
2015-10-06 20:14:38 +00:00
}
mySubmission = new FormSubmission ( {
2015-10-30 18:40:02 +00:00
admin : user . _id ,
form : myForm . _id ,
2015-10-06 20:14:38 +00:00
timeElapsed : 17.55 ,
form _fields : submissionFields
} ) ;
done ( ) ;
} ) ;
2015-09-18 16:32:17 +00:00
} ) ;
} ) ;
describe ( 'Method Save' , function ( ) {
2015-10-07 01:28:11 +00:00
var oscar _demo _num ;
2015-09-18 16:32:17 +00:00
beforeEach ( function ( done ) {
2015-10-06 20:14:38 +00:00
var myFieldMap = { } ;
2015-09-18 16:32:17 +00:00
myFieldMap [ myForm . form _fields [ 0 ] . _id + '' ] = 'firstName' ;
myFieldMap [ myForm . form _fields [ 1 ] . _id + '' ] = 'lastName' ;
myFieldMap [ myForm . form _fields [ 2 ] . _id + '' ] = 'sex' ;
2015-10-30 18:40:02 +00:00
myFieldMap [ myForm . form _fields [ 3 ] . _id + '' ] = 'DOB' ;
2015-09-18 16:32:17 +00:00
myFieldMap [ myForm . form _fields [ 4 ] . _id + '' ] = 'phone' ;
myForm . plugins . oscarhost . settings . fieldMap = myFieldMap ;
myForm . save ( function ( err , form ) {
if ( err ) done ( err ) ;
done ( ) ;
} ) ;
} ) ;
it ( 'should be able to save a FormSubmission without problems' , function ( done ) {
return mySubmission . save ( function ( err , submission ) {
2015-10-06 20:14:38 +00:00
if ( err ) done ( err ) ;
2015-10-07 01:28:11 +00:00
2015-09-18 16:32:17 +00:00
should . not . exist ( err ) ;
2015-10-06 20:14:38 +00:00
should . exist ( submission ) ;
2015-11-12 21:37:36 +00:00
// should.exist(submission.oscarDemoNum);
// oscar_demo_num = submission.oscarDemoNum;
2015-10-07 01:28:11 +00:00
2015-09-18 16:32:17 +00:00
done ( ) ;
} ) ;
} ) ;
2015-11-06 21:39:49 +00:00
// it('should add Patient to OscarHost EMR after save', function(done){
// var url_login = myForm.plugins.oscarhost.baseUrl+'/LoginService?wsdl',
// url_demo = myForm.plugins.oscarhost.baseUrl+'/DemographicService?wsdl',
// args_login = {arg0: config.oscarhost.auth.user, arg1: config.oscarhost.auth.pass};
// var options = {
// ignoredNamespaces: {
// namespaces: ['targetNamespace', 'typedNamespace'],
// override: true
// }
// };
// async.waterfall([
// function (callback) {
// //Authenticate with API
// soap.createClient(url_login, options, function(err, client) {
// client.login(args_login, function (err, result) {
// if(err) callback(err);
// callback(null, result.return);
// });
// });
// },
// function (security_obj, callback) {
// soap.createClient(url_demo, options, function(err, client) {
// client.setSecurity(new OscarSecurity(security_obj.securityId, security_obj.securityTokenKey) );
// client.getDemographic({ arg0: oscar_demo_num }, function (err, result) {
// if(err) callback(err);
// callback(null, result);
// });
// });
// },
// ], function(err, result) {
// if(err) done(err);
// should.exist(result);
// console.log(result.return);
// done();
// });
// });
2015-09-18 16:32:17 +00:00
} ) ;
2015-10-06 20:14:38 +00:00
describe ( 'Method Find' , function ( ) {
beforeEach ( function ( done ) {
mySubmission . save ( function ( err ) {
done ( ) ;
} ) ;
} ) ;
it ( 'should be able to findOne FormSubmission without problems' , function ( done ) {
2015-10-30 18:40:02 +00:00
return FormSubmission . findOne ( { _id : mySubmission . _id } ) . exec ( function ( err , submission ) {
2015-10-06 20:14:38 +00:00
should . not . exist ( err ) ;
should . exist ( submission ) ;
should . deepEqual ( submission . toObject ( ) , mySubmission . toObject ( ) ) ;
done ( ) ;
} ) ;
} ) ;
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
it ( 'should be able to find FormSubmission by $elemMatch on form_fields id' , function ( done ) {
return FormSubmission . findOne ( { form : myForm . _id , admin : user , form _fields : { $elemMatch : { _id : myForm . form _fields [ 0 ] . _id } } } )
. exec ( function ( err , submission ) {
should . not . exist ( err ) ;
should . exist ( submission ) ;
should . deepEqual ( submission . toObject ( ) , mySubmission . toObject ( ) ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
describe ( 'Test FormField and Submission Logic' , function ( ) {
var new _form _fields _add1 , new _form _fields _del ;
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
beforeEach ( function ( done ) {
new _form _fields _add1 = _ . clone ( myForm . toObject ( ) . form _fields ) ;
new _form _fields _add1 . push (
{ 'fieldType' : 'textfield' , 'title' : 'Last Name' , 'fieldValue' : '' }
) ;
//Create Submission
mySubmission = new FormSubmission ( {
form _fields : sampleSubmission ,
admin : user ,
form : myForm ,
timeElapsed : 17.55
} ) ;
mySubmission . save ( function ( err ) {
if ( err ) done ( err ) ;
done ( ) ;
} ) ;
} ) ;
2015-11-06 19:03:26 +00:00
// it('should preserve deleted form_fields that have submissions without any problems', function(done) {
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// var old_fields = myForm.toObject().form_fields;
// var new_form_fields = _.clone(myForm.toObject().form_fields);
// new_form_fields.splice(0, 1);
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// myForm.form_fields = new_form_fields;
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// myForm.save(function(err, _form) {
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// should.not.exist(err);
// should.exist(_form);
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// // var actual_fields = _.map(_form.toObject().form_fields, function(o){ _.omit(o, '_id')});
// // old_fields = _.map(old_fields, function(o){ _.omit(o, '_id')});
2015-09-18 16:32:17 +00:00
2015-11-06 19:03:26 +00:00
// // console.log(old_fields);
// should.deepEqual(JSON.stringify(_form.toObject().form_fields), JSON.stringify(old_fields), 'old form_fields not equal to newly saved form_fields');
// done();
// });
// });
2015-10-06 20:14:38 +00:00
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
// it('should delete \'preserved\' form_fields whose submissions have been removed without any problems', function(done) {
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
// myForm.form_fields = new_form_fields_del;
// myForm.save(function(err, form
// should.not.exist(err);
// (form.form_fields).should.be.eql(old_fields, 'old form_fields not equal to newly saved form_fields');
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
// //Remove submission
// mySubmission.remove(function(err){
// myForm.submissions.should.have.length(0);
// myForm.form_fields.should.not.containDeep(old_fields[0]);
// });
// });
// });
afterEach ( function ( done ) {
mySubmission . remove ( function ( ) {
done ( ) ;
} ) ;
} ) ;
} ) ;
2015-09-18 16:32:17 +00:00
afterEach ( function ( done ) {
Form . remove ( ) . exec ( function ( ) {
2015-10-06 20:14:38 +00:00
User . remove ( ) . exec ( function ( ) {
FormSubmission . remove ( ) . exec ( done ) ;
} ) ;
2015-11-23 21:06:02 +00:00
} ) ;
2015-09-18 16:32:17 +00:00
} ) ;
} ) ;