{"size": 5981, "ext": "js", "max_stars_repo_path": "test/write_preferences_test.js", "max_stars_repo_name": "rozza/node-mongodb-native", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/write_preferences_test.js", "max_issues_repo_name": "rozza/node-mongodb-native", "max_issues_repo_issues_event_min_datetime": "2021-02-23T22:24:33.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-23T22:24:33.000Z", "max_forks_repo_path": "test/write_preferences_test.js", "max_forks_repo_name": "rozza/node-mongodb-native", "max_forks_repo_forks_event_min_datetime": "2020-12-13T11:25:32.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-13T11:25:32.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 32.1559139785, "max_line_length": 168, "alphanum_fraction": 0.6234743354}
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
var useSSL = process.env['USE_SSL'] != null ? true : false;
var native_parser = (process.env['TEST_NATIVE'] != null);

var testCase = require('nodeunit').testCase,
  debug = require('util').debug,
  inspect = require('util').inspect,
  nodeunit = require('nodeunit'),
  gleak = require('../dev/tools/gleak'),
  Db = mongodb.Db,
  Cursor = mongodb.Cursor,
  Collection = mongodb.Collection,
  Server = mongodb.Server;

var MONGODB = 'integration_tests';
var client = null;

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
exports.setUp = function(callback) {
  var self = exports;
  client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}), {w:0, native_parser: (process.env['TEST_NATIVE'] != null)});
  client.open(function(err, db_p) {
    if(numberOfTestsRun == (Object.keys(self).length)) {
      // If first test drop the db
      client.dropDatabase(function(err, done) {
        callback();
      });
    } else {
      return callback();
    }
  });
}

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
exports.tearDown = function(callback) {
  var self = this;
  numberOfTestsRun = numberOfTestsRun - 1;
  // Close connection
  client.close();
  callback();
}

exports['insert with w=1 db level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:1, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with w=1 collection level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:0, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1', {w:1}).update({a:1}, {a:1}, {upsert:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with w=1 operation level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:0, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true, w:1}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with journal db level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {journal:true, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with journal collection level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:1, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1', {journal:true}).update({a:1}, {a:1}, {upsert:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with journal collection insert level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:1, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true, journal:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['insert with journal and w == 1 at db level'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:1, journal:true, wtimeout:1000, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true}, function(err, result) {
      test.equal(null, err);
      test.equal(1, result);
      test.done();
      db.close();
    });
  });
}

exports['throw error when combining w:0 and journal'] = function(test) {
  var db = new Db(MONGODB, 
    new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL})
    , {w:0, journal:true, wtimeout:1000, native_parser: (process.env['TEST_NATIVE'] != null)});
  db.open(function(err, db) {
    test.throws(function() { 
      db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true}, function(err, result) {
        test.equal(null, err);
        test.equal(1, result);
      });
    }, "No acknowlegement using w < 1 cannot be combined with journal:ture or fsync:true");

    test.done();
    db.close();
  });
}

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
exports.noGlobalsLeaked = function(test) {
  var leaks = gleak.detectNew();
  test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
  test.done();
}

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
var numberOfTestsRun = Object.keys(this).length - 2;