индекс mongoose не работает, когда используется мокко

Вопрос:

Мои файлы проекта:

У меня есть модель User:

user.model.js

var UserSchema = new Schema({
username:        { type: String, required: true, unique: true },
location:        { 'type': {type: String, enum: "Point", default: "Point"}, coordinates: { type: [Number], default: [0,0]} },
});

UserSchema.index({location: '2dsphere'});

И иметь экспресс-маршрут:

user.route.js

function create(req, res) {
User.create(req.body, function(err, user) {
if (err) return res.send(err);

return res.send(user);
})
}

user.test.js

describe('test_user', function() {
it('create user', function(done) {
request('http://localhost/user')
.post('/')
.send({
username: 'john',
password: "123"
})
.end(function(err, res) {
if (err) cb(err);
done()
});
});
});

У меня есть файл drop_databse.js, он запускается каждый раз перед запуском теста:

drop_databse.js

var MongoClient = require('mongoose/node_modules/mongodb').MongoClient


MongoClient.connect('mongodb://localhost/miccity_test', function(err, db) {
if (err) return console.log(err);
db.dropDatabase(function(err) {
if (err) return console.log('drop databse failed!! :\n' + err);
db.close();
})
})

когда я тестирую

когда я запускаю тест:

$ node drop_database.js && mocha

test_user
✓ create user


1 passing (21ms)

Но, я замечаю, что коллекция user в базе данных не имеет индекса location !

Но я создаю один файл:

test.js

var mongoose = require('mongoose');
var Schema       = mongoose.Schema;

mongoose.connect('mongodb://localhost/miccity_test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'mongodb connection error:'));


var UserSchema = new Schema({
username:        { type: String, required: true, unique: true },
location:        { 'type': {type: String, enum: "Point", default: "Point"}, coordinates: { type: [Number], default: [0,0]} },
});


UserSchema.index({location: '2dsphere'});

var User = mongoose.model('User', UserSchema);

User.create({username: "test"}, function (err) {
if (err) console.log(err);
})

И запустите: node drop_databse.js && node test.js, тогда я проверяю индекс местоположения, он существует!

Это значит, что мокка не может создать индекс сбора?

Редактировать:

я нашел трюк:

используйте Model.ensureIndexes для Sends makeIndex команды для mongo для каждого индекса, объявленного в схеме.

function create(req, res) {
User.ensureIndexes(function(err){
User.create(req.body, function(err, user) {
if (err) return res.send(err);

return res.send(user);
})
}

это может быть ошибка для мангуста, я уже передаю эту проблему github.

Оцените статью
TechArks.Ru
Добавить комментарий