'use strict';

var arrayUnique = require('../');
var assert = require('assert');
var mongoose = require('mongoose');

if (process.env.D) {
  mongoose.set('debug', true);
}

describe('arrayUnique', function() {
  before(function() {
    mongoose.connect('mongodb://localhost:27017/mongoose_test');
  });

  beforeEach(function(done) {
    mongoose.connection.dropDatabase(done);
  });

  it('works', function(done) {
    var schema = new mongoose.Schema({
      arr: [{ type: String, unique: true }],
      docArr: [{ name: { type: String, unique: true } }]
    });
    schema.plugin(arrayUnique);
    var M = mongoose.model('T1', schema);

    M.create({}, function(error, doc) {
      assert.ifError(error);
      doc.arr.push('test');
      doc.docArr.push({ name: 'test' });
      doc.save(function(error) {
        assert.ifError(error);
        doc.arr.push('test');
        doc.save(function(error) {
          assert.ok(error);
          assert.ok(error.errors['arr'].message.indexOf('Duplicate values') !== -1,
            error.errors['arr'].message);
          done();
        });
      });
    });
  });

  it('pushing onto doc array', function(done) {
    var schema = new mongoose.Schema({
      docArr: [{ name: { type: String, unique: true } }]
    });
    schema.plugin(arrayUnique);
    var M = mongoose.model('T2', schema);

    M.create({}, function(error, doc) {
      assert.ifError(error);
      doc.docArr.push({ name: 'test' });
      doc.save(function(error) {
        assert.ifError(error);
        doc.docArr.push('test');
        doc.save(function(error) {
          assert.ok(error);
          assert.ok(error.errors['docArr'].message.indexOf('Duplicate values') !== -1,
            error.errors['docArr'].message);
          done();
        });
      });
    });
  });

  it('with new docs', function(done) {
    var schema = new mongoose.Schema({
      arr: [{ type: String, unique: true }],
      docArr: [{ name: { type: String, unique: true } }]
    });
    schema.plugin(arrayUnique);
    var M = mongoose.model('T3', schema);
    var m = new M({
      arr: ['test', 'test'],
      docArr: [{ name: 'test' }, { name: 'test' }]
    });

    m.save(function(error) {
      assert.ok(error);
      assert.ok(error.errors['arr'].message.indexOf('Duplicate values') !== -1,
        error.errors['arr'].message);
      assert.ok(error.errors['docArr'].message.indexOf('Duplicate values') !== -1,
        error.errors['docArr'].message);
      done();
    });
  });

  it('nested', function(done) {
    var schema = new mongoose.Schema({
      nested: {
        arr: [{ type: String, unique: true }],
        docArr: [{ name: { type: String, unique: true } }]
      }
    });
    schema.plugin(arrayUnique);
    var M = mongoose.model('T4', schema);
    var m = new M({
      nested: {
        arr: ['test', 'test'],
        docArr: [{ name: 'test' }, { name: 'test' }]
      }
    });

    m.save(function(error) {
      assert.ok(error);
      assert.ok(error.errors['nested.arr'].message.indexOf('Duplicate values') !== -1,
        error.errors['nested.arr'].message);
      assert.ok(error.errors['nested.docArr'].message.indexOf('Duplicate values') !== -1,
        error.errors['nested.docArr'].message);
      done();
    });
  });
});

@@ -1789,24 +1789,810 @@
  %7D);%0A  %7D);%0A%0A
+  it('with race condition', function(done) %7B%0A    var schema = new mongoose.Schema(%7B%0A      arr: %5B%7B type: String, unique: true %7D%5D%0A    %7D);%0A    schema.plugin(arrayUnique);%0A    var M = mongoose.model('T3', schema);%0A%0A    M.create(%7B%7D, function(error, doc) %7B%0A      assert.ifError(error);%0A      M.findById(doc, function(error, doc1) %7B%0A        M.findById(doc, function(error, doc2) %7B%0A          doc1.arr.push('test');%0A          doc1.save(function(error) %7B%0A            assert.ifError(error);%0A            doc2.arr.push('test');%0A            doc2.save(function(error) %7B%0A              assert.ok(error);%0A              assert.ok(error.message.indexOf('No matching document') !== -1,%0A                error.message);%0A              done();%0A            %7D);%0A          %7D);%0A        %7D);%0A      %7D);%0A    %7D);%0A  %7D);%0A%0A
   it('with n
@@ -2610,32 +2610,32 @@
 unction(done) %7B%0A
-
     var schema =
@@ -2820,33 +2820,33 @@
 ongoose.model('T
-3
+4
 ', schema);%0A    
@@ -3536,33 +3536,33 @@
 ongoose.model('T
-4
+5
 ', schema);%0A    

add race condition test