<commit_before>var assert = require('assert');
var _ = require('lodash' );
var Promises = require('backbone-promises');
var when = Promises.when;
var sequence = require('when/sequence');
var setup = require('./setup');
var MyCollection = setup.MyCollection;
var MyModel = setup.MyModel;
var store = setup.store;
var TestCollection = setup.TestIndexedCollection;

var TestCollection2 = TestCollection.extend({
  indexKey: 'test:i:Foo:relation2'
});

describe('Test IndexedCollection', function () {
  var collection;
  var collection2;

  before(function(done) {
    collection = new TestCollection();
    var fns = [
      collection.create({data: 'aaa', score: 22}),
      collection.create({data: 'bbb', score: 1}),
      collection.create({data: 'ccc', score: 99})
    ];
    when.all(fns).then(function() {
      done();
    }).otherwise(done);
  });

  after(function(done) {
    setup.clearDb(done);
  });

  it('should index a new item', function(done) {
    collection
      .addToIndex(collection.at(0))
      .then(function() {
        done();
      }).otherwise(done);
  });

  it('should index another item', function(done) {
    collection
      .addToIndex(collection.at(1))
      .then(function() {
        done();
      }).otherwise(done);
  });

  it('should index another item', function(done) {
    collection
      .addToIndex(collection.at(2))
      .then(function() {
        done();
      }).otherwise(done);
  });


  it('should get count from index', function(done) {
    collection = new TestCollection();
    collection
      .count()
      .then(function(count) {
        assert.equal(count, 3);
        done();
      }).otherwise(done);
  });

  it('should read ids with given score options', function(done) {
    var opts = {
      score: {
        min: 22,
        max: 999,
        // defines how to format scores into model
        conversion: {
          fn: function(score) {
            return score;
          },
          attribute: 'score'
        }
      },
      limit: 2,
      offset: 1
    };
    collection = new TestCollection();
    collection
      .readFromIndex(opts)
      .then(function() {
        assert.equal(collection.length, 1);
        assert.equal(collection.at(0).get('score'), '22');
        done();
      }).otherwise(done);
  });

  it('should read ids from index', function(done) {
    collection = new TestCollection();
    collection
      .readFromIndex()
      .then(function() {
        assert.equal(collection.length, 3);
        assert.equal(collection.pluck('id').length, 3);
        done();
      }).otherwise(done);
  });

  it('should fetch models', function(done) {
    var fetchOpts = {
      where: {
        id: {
          $in: collection.pluck('id')
        }
      }
    };
    collection
      .fetch(fetchOpts)
      .then(function() {
        assert.equal(collection.length, 3);
        assert.equal(collection.at(0).get('data'), 'ccc');
        assert.equal(collection.at(1).get('data'), 'aaa');
        done();
      }).otherwise(done);
  });

  it('should read with limit', function() {
    collection = new TestCollection();
    return collection
      .readFromIndex({limit: 1, offset: 0})
      .then(function() {
        collection.length.should.equal(1);
        collection.at(0).id.should.equal('3');
      });
  });

  it('should read with offset', function() {
    collection = new TestCollection();
    return collection
      .readFromIndex({limit: 2, offset: 1})
      .then(function() {
        collection.length.should.equal(2);
        collection.at(0).id.should.equal('1');
        collection.at(1).id.should.equal('2');
      });
  });


  it('should add item to another index', function(done) {
    collection2 = new  TestCollection2();
    collection2
      .create({data: 'ddd', score: 22})
      .then(function(m) {
        collection2
          .addToIndex(m)
          .then(function() {
            done();
          }).otherwise(done);
      }).otherwise(done);
  });

  it('should read from multiple indexes', function(done) {
    var opts = {
      indexKeys: ['test:i:Foo:relation', 'test:i:Foo:relation2'],
      unionKey: 'test:i:UnionFoo'
    };
    collection2 = new  TestCollection2();
    collection2
      .readFromIndexes(opts)
      .then(function() {
        assert.equal(collection2.length, 4);
        done();
      }).otherwise(done);
  });

  it('should read from multiple indexes with weights', function() {
    var opts = {
      indexKeys: ['test:i:Foo:relation'],<commit_msg>added a test for reading a set with min range<commit_after>var assert = require('assert');
var _ = require('lodash' );
var Promises = require('backbone-promises');
var when = Promises.when;
var sequence = require('when/sequence');
var setup = require('./setup');
var MyCollection = setup.MyCollection;
var MyModel = setup.MyModel;
var store = setup.store;
var TestCollection = setup.TestIndexedCollection;

var TestCollection2 = TestCollection.extend({
  indexKey: 'test:i:Foo:relation2'
});

describe('Test IndexedCollection', function () {
  var collection;
  var collection2;

  before(function(done) {
    collection = new TestCollection();
    var fns = [
      collection.create({data: 'aaa', score: 22}),
      collection.create({data: 'bbb', score: 1}),
      collection.create({data: 'ccc', score: 99})
    ];
    when.all(fns).then(function() {
      done();
    }).otherwise(done);
  });

  after(function(done) {
    setup.clearDb(done);
  });

  it('should index a new item', function(done) {
    collection
      .addToIndex(collection.at(0))
      .then(function() {
        done();
      }).otherwise(done);
  });

  it('should index another item', function(done) {
    collection
      .addToIndex(collection.at(1))
      .then(function() {
        done();
      }).otherwise(done);
  });

  it('should index another item', function(done) {
    collection
      .addToIndex(collection.at(2))
      .then(function() {
        done();
      }).otherwise(done);
  });


  it('should get count from index', function(done) {
    collection = new TestCollection();
    collection
      .count()
      .then(function(count) {
        assert.equal(count, 3);
        done();
      }).otherwise(done);
  });

  it('should read from index with min range', function(done) {
    var opts = {
      score: {
        min: '(22',
        conversion: {
          fn: function(score) {
            return score;
          },
          attribute: 'score'
        }
      }
    };
    collection = new TestCollection();
    collection
      .readFromIndex(opts)
      .then(function() {
        assert.equal(collection.length, 1);
        assert.equal(collection.at(0).get('score'), '99');
        done();
      }).otherwise(done);
  });

  it('should read ids with given score options', function(done) {
    var opts = {
      score: {
        min: 22,
        max: 999,
        // defines how to format scores into model
        conversion: {
          fn: function(score) {
            return score;
          },
          attribute: 'score'
        }
      },
      limit: 2,
      offset: 1
    };
    collection = new TestCollection();
    collection
      .readFromIndex(opts)
      .then(function() {
        assert.equal(collection.length, 1);
        assert.equal(collection.at(0).get('score'), '22');
        done();
      }).otherwise(done);
  });

  it('should read ids from index in reverse', function(done) {
    collection = new TestCollection();
    collection
      .readFromIndex({sortOrder: 1})
      .then(function() {
        assert.equal(collection.length, 3);
        assert.equal(collection.pluck('id').length, 3);
        collection.at(0).id.should.equal('2');
        done();
      }).otherwise(done);
  });

  it('should read ids from index', function(done) {
    collection = new TestCollection();
    collection
      .readFromIndex()
      .then(function() {
        assert.equal(collection.length, 3);
        assert.equal(collection.pluck('id').length, 3);
        collection.at(0).id.should.equal('3');
        done();
      }).otherwise(done);
  });

  it('should fetch models', function(done) {
    var fetchOpts = {
      where: {
        id: {
          $in: collection.pluck('id')
        }
      }
    };
    collection
      .fetch(fetchOpts)
      .then(function() {
        assert.equal(collection.length, 3);
        assert.equal(collection.at(0).get('data'), 'ccc');
        assert.equal(collection.at(1).get('data'), 'aaa');
        done();
      }).otherwise(done);
  });

  it('should read with limit', function() {
    collection = new TestCollection();
    return collection
      .readFromIndex({limit: 1, offset: 0})
      .then(function() {
        collection.length.should.equal(1);
        collection.at(0).id.should.equal('3');
      });
  });

  it('should read with offset', function() {
    collection = new TestCollection();
    return collection
      .readFromIndex({limit: 2, offset: 1})
      .then(function() {
        collection.length.should.equal(2);
        collection.at(0).id.should.equal('1');
        collection.at(1).id.should.equal('2');
      });
  });

  it('should add item to another index', function(done) {
    collection2 = new  TestCollection2();
    collection2
      .create({data: 'ddd', score: 22})
      .then(function(m) {
        collection2
          .addToIndex(m)
          .then(function() {
            done();
          }).otherwise(done);
      }).otherwise(done);
  });

  it('should read from multiple indexes', function(done) {
    var opts = {
      indexKeys: ['test:i:Foo:relation', 'test:i:Foo:relation2'],
      unionKey: 'test:i:UnionFoo'
    };
    collection2 = new  TestCollection2();
    collection2
      .readFromIndexes(opts)
      .then(function() {
        assert.equal(collection2.length, 4);
        done();
      }).otherwise(done);
  });

  it('should read from multiple indexes with weights', function() {
    var opts = {
      indexKeys: ['test:i:Foo:relation'],
{'fraction_non_alphanumeric': 0.15934340906816136, 'fraction_numerical': 0.011710539485536983, 'mean_word_length': 2.4562435143548944, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4988606', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2929, 'n_words': 637}