I have the following schema with mongoose:
var SimSchema = new Schema({ msisdn : { type : String , unique : true, required : true }, imsi : { type : String , unique : true, required : true }, status : { type : Boolean, default: true}, signal : { type : Number }, probe_name : { type: String , required : true } });
I have unique
option for the msisdn
and imsi
.
In some cases this condition is well respected. For the following mocha
test:
"use strict"; var app = require('../../app'); var http = require('http'); var request = require('supertest'); var mongoose = require('mongoose'); var should = require('should'); describe('[ Sim controller ] ', function(){ before(function(done) { app.set_env('test'); this.server = app.start(function() { mongoose.connection.db.dropDatabase(function() { done(); }) }); }); beforeEach(function(done){ done(); }); it('Sim with good parameters should be created in the database', function(done){ var newSim = { msisdn: '1234', imsi: '007', probe_name: 'BOUCHON_1' }; request(this.server) .post('/sims') .set('Content-Type', 'application/json') .send(newSim) .expect(200).end(function(err, res) { if (err) return done(err); res.body.should.have.property('imsi'); res.body.should.have.property('probe_name'); res.body.should.have.property('msisdn'); setTimeout(function() { done(); }, 1000); }); }); it('Sim imsi/msisdn is unique in the database', function(done){ var newSim = { msisdn: '1234', imsi: '007', probe_name: 'BOUCHON_1' }; request(this.server) .post('/sims') .set('Content-Type', 'application/json') .send(newSim) .expect(200).end(function(err, res) { if (err) return done(err); res.body.should.have.property('error').equal('Duplicate Item'); done(); }); }); after(function(done) { app.stop(done); }); });
It's working fine if I run it directly:
julio$ mocha test/controllers/ctrl_sim.js
But If I run it thanks to the recessive option it failed:
1) [ Sim controller ] Sim imsi/msisdn is unique in the database: Uncaught AssertionError: expected { __v: 0, imsi: '007', msisdn: '1234', probe_name: 'BOUCHON_1', _id: '530a2b7f52273aa90783baf0', status: true } to have property 'error'
I read on stack that sometimes the unique
condition is not well respected as the index is not refreshed. Do you think this is my case? In fact, I delete the database for each mocha test suite. Maybe mongo doesn't have the time to recreate all the indexes each time.
Any idea?
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.
Mongoose are a weasel-like animal totaling about 26″ in length with a long, brownish body, short legs and a tail as long as its body. They have small rounded ears and a pointed nose.
A mongoose is a small terrestrial carnivorous mammal belonging to the family Herpestidae.
Mongoose is a Node.js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as. SQLAlchemy. for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.
Use dropDups
to ensure dropping duplicate records in your schemas like;
var SimSchema = new Schema({ msisdn : { type : String , unique : true, required : true, dropDups: true }, imsi : { type : String , unique : true, required : true, dropDups: true }, status : { type : Boolean, default: true}, signal : { type : Number }, probe_name : { type: String , required : true } });
And before running your tests, restart mongodb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With