<commit_before>var assert = require('assert'),
    _ = require('lodash');

describe('Association Interface', function() {

  describe('n:m association :: .destroy()', function() {

    /////////////////////////////////////////////////////
    // TEST SETUP
    ////////////////////////////////////////////////////

    var driverRecord;

    before(function(done) {
      Associations.Driver.create({ name: 'manymany destroy'}, function(err, driver) {
        if(err) return done(err);

        driverRecord = driver;

        var taxis = [];
        for(var i=0; i<2; i++) {
          driverRecord.taxis.add({ medallion: i });
        }

        driverRecord.save(function(err) {
          if(err) return done(err);
          done();
        });
      });
    });

    /////////////////////////////////////////////////////
    // TEST METHODS
    ////////////////////////////////////////////////////

    it('should remove join table records when a parent is destroyed', function(done) {

      var parentId = driverRecord.id;

      Associations.Driver.destroy({ name: 'manymany destroy' })
      .exec(function(err, status) {
        assert(!err);

        Associations.Driver_taxis__taxi_drivers.find({ driver_taxis: parentId })
        .exec(function(err, records) {
          assert(!err);
          assert(records.length === 0);
          done();
        });
      });
    });

  });

  describe('destroying an entire collection', function () {

    it('should not result in an error', function (done) {
      Associations.Driver.destroy(function (err, status) {
        assert(!err);
        done();
      });
    });
  });

  describe('destroying an entire collection, now empty', function () {

    it('should not result in an error', function (done) {
      Associations.Driver.destroy(function (err, status) {
        assert(!err);
        done();
      });
    });
  });<commit_msg>add test for making sure join table records don't get destroyed when criteria is missing<commit_after>var assert = require('assert'),
    _ = require('lodash');

describe('Association Interface', function() {

  describe('n:m association :: .destroy()', function() {

    /////////////////////////////////////////////////////
    // TEST SETUP
    ////////////////////////////////////////////////////

    var driverRecord;

    before(function(done) {
      Associations.Driver.create({ name: 'manymany destroy'}, function(err, driver) {
        if(err) return done(err);

        driverRecord = driver;

        var taxis = [];
        for(var i=0; i<2; i++) {
          driverRecord.taxis.add({ medallion: i });
        }

        driverRecord.save(function(err) {
          if(err) return done(err);
          done();
        });
      });
    });

    /////////////////////////////////////////////////////
    // TEST METHODS
    ////////////////////////////////////////////////////

    it('should remove join table records when a parent is destroyed', function(done) {

      var parentId = driverRecord.id;

      Associations.Driver.destroy({ name: 'manymany destroy' })
      .exec(function(err, status) {
        assert(!err);

        Associations.Driver_taxis__taxi_drivers.find({ driver_taxis: parentId })
        .exec(function(err, records) {
          assert(!err);
          assert(records.length === 0);
          done();
        });
      });
    });

  });

  describe('when criteria returns no results', function () {

    /////////////////////////////////////////////////////
    // TEST SETUP
    ////////////////////////////////////////////////////

    var driverRecord;

    before(function(done) {
      Associations.Driver.create({ name: 'manymany destroy'}, function(err, driver) {
        if(err) return done(err);

        driverRecord = driver;

        var taxis = [];
        for(var i=0; i<2; i++) {
          driverRecord.taxis.add({ medallion: i });
        }

        driverRecord.save(function(err) {
          if(err) return done(err);
          done();
        });
      });
    });

    /////////////////////////////////////////////////////
    // TEST METHODS
    ////////////////////////////////////////////////////

    it('should not destroy any join table records', function (done) {
      Associations.Driver.destroy({ name: 'manymany destroy_foo' }, function (err, status) {
        assert(!err);

        // Find records in the join table
        Associations.Driver_taxis__taxi_drivers.find({ driver_taxis: driverRecord.id }).exec(function(err, records) {
          if(err) return done(err);
          if(!records.length) {
            return done(new Error('Removed all the join table records!'));
          }

          return done();
        });
      });
    });

  });


  describe('destroying an entire collection', function () {

    it('should not result in an error', function (done) {
      Associations.Driver.destroy(function (err, status) {
        assert(!err);
        done();
      });
    });
  });

  describe('destroying an entire collection, now empty', function () {

    it('should not result in an error', function (done) {
      Associations.Driver.destroy(function (err, status) {
        assert(!err);
        done();
      });
    });
  });
{'fraction_non_alphanumeric': 0.2468354430379747, 'fraction_numerical': 0.0015343306482546988, 'mean_word_length': 1.5327829043224865, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531530', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1295, 'n_words': 316}