<commit_before>var assert = require('assert');
var _ = require('@sailshq/lodash');

describe('Association Interface', function() {

  describe('n:m association :: .find().populate([WHERE])', function() {

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

    var driverRecord;

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

        driverRecord = driver;

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

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

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

    it('should return only taxis with medallions less than 2', function(done) {
      Associations.Driver.find({ name: 'manymany find where' })
      .populate('taxis', { medallion: { '<': 2 }})
      .exec(function(err, drivers) {
        assert(!err, err);

        assert(Array.isArray(drivers));
        assert.strictEqual(drivers.length, 1);
        assert(Array.isArray(drivers[0].taxis));
        assert.strictEqual(drivers[0].taxis.length, 2, 'Expected first driver to have 2 taxis, but got '+drivers[0].taxis.length+', see?\n'+require('util').inspect(drivers[0]) );

        done();
      });
    });

    it('should return taxis using skip and limit', function(done) {
      Associations.Driver.find({ name: 'manymany find where' })
      .populate('taxis', { skip: 1, limit: 2, sort: 'medallion ASC' })
      .exec(function(err, drivers) {
        assert.ifError(err);

        assert(Array.isArray(drivers));
        assert.strictEqual(drivers.length, 1);

        assert(Array.isArray(drivers[0].taxis));

        assert.strictEqual(drivers[0].taxis.length, 2, 'Expected first driver to have 2 taxis, but got '+drivers[0].taxis.length+', see?\n'+require('util').inspect(drivers[0]));
        assert.strictEqual(drivers[0].taxis[0].medallion, 1, 'Expected first driver\'s first taxi to have medallion===1, but heres what I got for the first driver: '+require('util').inspect(drivers[0], false, null));
        assert.strictEqual(drivers[0].taxis[1].medallion, 2, 'Expected first driver\'s second taxi to have medallion===2, but heres what I got for the first driver: '+require('util').inspect(drivers[0], false, null));

        done();
      });
    });

  });
});<commit_msg>update many to many populate where tests<commit_after>var assert = require('assert');
var _ = require('@sailshq/lodash');

describe('Association Interface', function() {
  describe('n:m association :: .find().populate([WHERE])', function() {
    var driverRecord;

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

        driverRecord = driver;

        var taxis = [];
        for(var i=0; i<4; i++) {
          taxis.push({ medallion: i });
        }

        Associations.Taxi.createEach(taxis, function(err, taxis) {
          if (err) {
            return done(err);
          }

          var childrenIds = _.map(taxis, function(taxi) {
            return taxi.id;
          });

          Associations.Driver.addToCollection(driver.id, 'taxis', childrenIds)
          .exec(function(err) {
            if (err) {
              return done(err);
            }

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

    it('should return only taxis with medallions less than 2', function(done) {
      Associations.Driver.find({ name: 'manymany find where' })
      .populate('taxis', { medallion: { '<': 2 }})
      .exec(function(err, drivers) {
        if (err) {
          return done(err);
        }

        assert(_.isArray(drivers));
        assert.equal(drivers.length, 1);
        assert(_.isArray(drivers[0].taxis));
        assert.equal(drivers[0].taxis.length, 2);

        return done();
      });
    });

    it('should return taxis using skip and limit', function(done) {
      Associations.Driver.find({ name: '<NAME>' })
      .populate('taxis', { skip: 1, limit: 2, sort: 'medallion ASC' })
      .exec(function(err, drivers) {
        if (err) {
          return done(err);
        }

        assert(_.isArray(drivers));
        assert.equal(drivers.length, 1);

        assert(_.isArray(drivers[0].taxis));

        assert.equal(drivers[0].taxis.length, 2);
        assert.equal(drivers[0].taxis[0].medallion, 1);
        assert.equal(drivers[0].taxis[1].medallion, 2);

        return done();
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.19896049896049897, 'fraction_numerical': 0.010395010395010396, 'mean_word_length': 2.156824146981627, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, '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': '6611384', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1421, 'n_words': 306}