| Current Path : /home/deltalab/PMS/partner-manager-backend/test/ |
| Current File : //home/deltalab/PMS/partner-manager-backend/test/model.test.js |
/**
* This is a simple test example.
*
* You can use this as a template for other examples
*/
// DEPENDENCIES ====================================
const { expect, assert } = require ('chai');
const { dotenv } = require ('dotenv').config();
const mongoose = require('mongoose');
// CONFIGURATION ===================================
const mongooseOptions = {
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4, // Use IPv4, skip trying IPv6,
useUnifiedTopology: true, // To use the new Server Discover and Monitoring engine
useNewUrlParser: true, // Use the new URL parser;
};
// RESOURCES =======================================
class TestClass {
name = 'String';
age = 'Number';
}
class TestClass2 {
name = 'String';
other = 'TestClass';
//others = [TestClass];
}
// TEST CASES ======================================
describe ('model', () => {
before(()=> {
mongoose.connect(process.env.database_url, mongooseOptions);
// mongoose.set('useCreateIndex', true); No more needed in Mongoose >v6. True by default and set deprecated TBR
mongoose.connection.once('once', () => {
console.log('Connected to database')
});
console.log("Whatsgoinon here");
});
it ('use class as mongoose schema', (done) => {
const TestClassSchema = new mongoose.Schema(
{
name: 'String',
age: 'Number',
},
{ versionKey: false }
);
const tModel = mongoose.model('TestClass', TestClassSchema);
const TestClass2Schema = new mongoose.Schema(
new TestClass2(),
{ versionKey: false }
);
const tModel2 = mongoose.model('TestClass2', TestClass2Schema);
const tObject = new tModel();
tObject.name = "T name";
// tObject.save().then(() => {
// console.log('Done');
// done();
// }).catch(done);
const tObject2 = new tModel2();
tObject2.name = "T2 name";
tObject2.other = new tModel();
tObject2.other.name = "T name";
tObject2.save().then(() => {
console.log('Done');
done();
}).catch(done);
// const testSchema = classToSchema(new TestClass(), { versionKey: false });
// const testSchema2 = classToSchema(new TestClass2(), { versionKey: false });
// const testModel = mongoose.model('TestObject', testSchema);
// const testModel2 = mongoose.model('TestObject2', testSchema2);
// const testObject = new testModel();
// testObject.name = "Test 2 Name";
// // testObject2.other = new testModel();
// // testObject2.other.name = "Test Name";
// // testObject2.other.age = 42;
// testObject.save().then(() => {
// console.log('Done');
// done();
// }).catch(done);
});
});
// INTERNALS ========================================
function isAttribute(obj) {
if (
obj === String
|| obj === Number
|| obj === Boolean
|| obj === Date
|| obj === mongoose.Schema.Types.Mixed
|| obj === mongoose.Schema.Types.ObjectId
) {
return true;
}
return true;
}
function getAllProps(obj) {
var props = {
methods: {},
staticMethods: {},
attributes: {},
staticAttributes: {}
};
var usedNames = new Set();
do {
var tempStatics = Object.getOwnPropertyNames(obj.constructor).filter(function (value) {
return value !== 'length' && value !== 'name' && value !== 'prototype' && !usedNames.has(value);
});
tempStatics.forEach(function (value) {
var pValue = obj.constructor[value];
if (typeof pValue === 'function' && !isAttribute(pValue)) {
props.staticMethods[value] = pValue;
} else {
props.staticAttributes[value] = pValue;
}
usedNames.add(value);
});
var temp = Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj).map(function (s) {
return s.toString();
})).filter(function (value, i, arr) {
return value !== 'constructor' && (i == 0 || value !== arr[i - 1]) && !usedNames.has(value);
});
temp.forEach(function (value) {
var pValue = obj[value];
if (typeof pValue === 'function' && !isAttribute(pValue)) {
props.methods[value] = pValue;
} else {
props.attributes[value] = pValue;
}
usedNames.add(value);
});
} while ((obj = Object.getPrototypeOf(obj)) && Object.getPrototypeOf(obj));
return props;
}
function classToSchema(obj, schemaOptions) {
var props = getAllProps(obj);
var schema = new mongoose.Schema(props.attributes, schemaOptions);
schema.statics = props.staticMethods;
schema.methods = props.methods;
return schema;
}