'use strict';

var Int32 = require('../');
var assert = require('assert');
var mongodb = require('mongodb');
var mongoose = require('mongoose');

describe('API', function() {
  let db;

  before(function(done) {
    const uri = 'mongodb://localhost:27017/test';
    mongoose.connect(uri);
    mongodb.MongoClient.connect(uri, function(error, _db) {
      assert.ifError(error);
      db = _db;
      done();
    });
  });

  beforeEach(function(done) {
    db.dropDatabase(done);
  });

  after(function() {
    db.close();
    mongoose.disconnect();
  });

  /**
   * If you declare a field of type `Int32`, this module will use
   * `Math.round()` to convert it to an integer so MongoDB can store it
   * properly.
   */
  it('casts to int using `Math.round()`', function(done) {
    const schema = new mongoose.Schema({
      test: Int32
    });
    const Test = mongoose.model('Test', schema);

    Test.create({ test: 1.49 }, function(error, doc) {
      assert.equal(doc.test, 1);
      // Int32.INT32_BSON_TYPE=16, see http://bsonspec.org/spec.html
      const query = { test: { $type: Int32.INT32_BSON_TYPE, $eq: 1 } };
      db.collection('tests').findOne(query, function(error, doc) {
        assert.ifError(error);
        assert.ok(doc);
        // acquit:ignore:start
        done();
        // acquit:ignore:end
      });
    });
  });

  /**
   * MongoDB int32's must be between -2147483648 and 2147483647. If the
   * rounded value is outside of this range, that's a CastError.
   */

  it('throws a CastError if outside allowed range', function(done) {
    const Test = mongoose.model('Test');
    const doc = new Test();
    doc.test = 0x7FFFFFFF + 1;
    assert.ok(doc.validateSync() instanceof mongoose.Error);
    assert.equal(doc.validateSync().errors['test'].name, 'CastError');
    assert.equal(doc.validateSync().errors['test'].message,
      'Cast to Int32 failed for value "2147483648" at path "test"');
    // acquit:ignore:start
    done();
    // acquit:ignore:end
  });

  /**
   * If the [`Number` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
   * doesn't recognize the value as a valid number, that's a
   * [CastError](http://mongoosejs.com/docs/api.html#error-js).
   */

  it('throws a CastError if not a number', function(done) {
    const Test = mongoose.model('Test');
    const doc = new Test();
    doc.test = 'NaN';
    assert.ok(doc.validateSync() instanceof mongoose.Error);
    assert.equal(doc.validateSync().errors['test'].name, 'CastError');
    assert.equal(doc.validateSync().errors['test'].message,
      'Cast to Int32 failed for value "NaN" at path "test"');
    // acquit:ignore:start
    done();
    // acquit:ignore:end
  });

  it('works with required validators', function(done) {
    const schema = new mongoose.Schema({
      strips: { type: Int32, required: true }
    });
    const Bacon = mongoose.model('Bacon', schema);
    const doc = new Bacon();
    assert.ok(doc.validateSync() instanceof mongoose.Error);
    assert.equal(doc.validateSync().errors['strips'].name, 'ValidatorError');
    assert.equal(doc.validateSync().errors['strips'].message,
      'Path `strips` is required.');
    // acquit:ignore:start
    done();
    // acquit:ignore:end
  });

  it('works with queries', function(done) {
    const schema = new mongoose.Schema({
      strips: { type: Int32, required: true }
    });
    const Bacon = mongoose.model('Bacon');
    Bacon.create({ strips: 4 }, function(error) {
      assert.ifError(error);
      Bacon.findOne({ strips: { $gt: 2 } }, function(error, bacon) {
        assert.ifError(error);
        assert.equal(bacon.strips, 4);
        // acquit:ignore:start
        done();
        // acquit:ignore:end
      });
    });
  });
});

@@ -168,16 +168,30 @@
 ion() %7B%0A
+  let client;%0A
   let db
@@ -293,16 +293,43 @@
 nect(uri
+, %7B useNewUrlParser: true %7D
 );%0A    m
@@ -359,16 +359,43 @@
 ect(uri,
+ %7B useNewUrlParser: true %7D,
  functio
@@ -404,18 +404,22 @@
 error, _
-db
+client
 ) %7B%0A    
@@ -453,16 +453,49 @@
     
-db = _db
+client = _client;%0A      db = _client.db()
 ;%0A  
@@ -543,28 +543,24 @@
 unction(
-done
 ) %7B%0A    
 db.dropD
@@ -551,18 +551,42 @@
 ) %7B%0A    
-db
+return mongoose.connection
 .dropDat
@@ -591,20 +591,16 @@
 atabase(
-done
 );%0A  %7D);
@@ -626,18 +626,22 @@
 ) %7B%0A    
-db
+client
 .close()

fix tests for mongodb 3.x