'use strict';

var should = require('should'),
    mongoose = require('mongoose'),
    soft_delete = require('../mongoose-soft-delete.js'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.ObjectId;
mongoose.connect('mongodb://localhost/softtest');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'Connection Error: please check if mongodb is running on localhost'));

var TestSchema = new Schema({
  name: { type: String, default: 'Riyadh' },
  comment: { type: String, default: 'lalalalalalala' },
  data: { type: ObjectId, ref:"Test"}
});

TestSchema.plugin(soft_delete);
  
var Test = mongoose.model('Test', TestSchema);
var test1 = new Test();
var test2 = new Test();
var test3 = new Test({ deleted: true });
  
test2.data = test1;
test1.data = test3;
describe('Mongoose Soft Delete Plugin', function () {
  it('should add fields to schema if they do not exist already', function (done) {
    (test1.deletedAt === undefined).should.be.true;
    (test1.deleted === undefined).should.be.true;

    test1.softdelete(function (err, newTest) {
      should.not.exist(err);
      newTest.deleted.should.exist;
      newTest.deletedAt.should.exist;
      done();
    });
  });

  it('should delete data in the DB', function (done) {
    test2.softdelete(function (err, newTest) {
      should.not.exist(err);
      newTest.deleted.should.be.true;
      newTest.deletedAt.should.be.an.instanceOf(Date);
      done();
    });
  });

  it('should restore data from the DB', function (done) {
    test3.restore(function (err, newTest) {
      should.not.exist(err);
      newTest.deleted.should.be.false;
      (newTest.deletedAt === null).should.be.true;
      done();
    });
  });

  it('should not have populate ', function(done){
    Test.findOne({_id:test1.id}).populate("data").exec(function(err, result){
      (result.data == undefined).should.be.false;
      should.exist(result.data)
      //console.log("test1",result.data);

      Test.findOne({_id:test2.id}).populate("data").exec(function(err, result){
        should.not.exist(result.data)
        //console.log("test2",result.data);
        done()
      })
    })
  })
});