<commit_before>var config = require(__dirname+'/../config.js');

var r = require('rethinkdb');
var assert = require('assert');

var connections = {};
var TEST_DB = 'reqlitetest';
var TEST_TABLE = 'reqlitetestaggregation';
var MISSING_ID = 'nonExistingId';
var MISSING_FIELD = 'nonExistingField';
var MISSING_INDEX = 'nonExistingIndex';

var compare = require(__dirname+'/util.js').generateCompare(connections);

describe('aggregation.js', function(){
  before(function(done) {
    setTimeout(function() { // Delay for nodemon to restart the server
      r.connect(config.rethinkdb).bind({}).then(function(conn) {
        connections.rethinkdb = conn;
        return r.connect(config.reqlite);
      }).then(function(conn) {
        connections.reqlite = conn;
        this.query = r.dbCreate(TEST_DB);
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).tableDrop(TEST_TABLE)
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).tableCreate(TEST_TABLE)
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).insert([
          {id: 1, foo: 20, bar: [100, 101, 102], optional: 1},
          {id: 2, foo: 10, bar: [200, 201, 202], optional: 1},
          {id: 3, foo: 10, bar: [300, 301, 302], optional: 2},
          {id: 4, foo: 10, bar: [400, 401, 402]},
        ]);
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('foo')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('bar')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('barmulti', r.row('bar'), {multi: true})
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('optional')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {

        done();
      });
    }, 400)
  });

  it('group - 1', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').count();
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 2', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('foo').count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 3', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group({index: 'id'}).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 4', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group(MISSING_FIELD).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 5', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group({index: 'optional'}).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 6', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {id: 2},
      {id: 3}
    ]).group('id')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 7', function(done) {
    var query = r.expr([
      {id: 1},
      {},
      {},
      {id: 3}
    ]).group('id')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 8', function(done) {
    var query = r.expr([
      {id: 1},
      {},
      {},
      {id: 3}
    ]).group('id').count().ungroup().orderBy(r.row('group'));
    compare(query, done);
  });

  it('group - 9', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}
    ]).group('id', 'foo')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        if (a.group > b.group) {
          return 1
        }
        else if (a.group < b.group) {
          return -1
        }
        return 0;
      });
      return result;
    });
  });

  it('group - 10', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id').ungroup().count()
    compare(query, done);
  });

  it('group - 11', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group();
    compare(query, done);
  });

  it('group - 12',  function(done) {
    var query = r.expr('foo').group('id');
    compare(query, done);
  });

  it('group - 13',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(1);
    compare(query, done);
  });

  it('group - 14',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id', {bar: 2});
    compare(query, done);
  });

  it('group - 15',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id');
    }).ungroup().orderBy('group').map(function(doc) {
      return r.branch(
        doc('group').eq(1),
        doc.merge(function(x) { return { reduction: doc('reduction').orderBy(function(y) { return y }) }}),
        doc
      )
    });
    compare(query, done);
  });

  it('group - 16',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id');
    }).ungroup().orderBy('group').map(function(doc) {
      return r.branch(
        doc('group').eq(1),
        doc.merge(function(x) { return { reduction: x('reduction').orderBy(function(y) { return y }) }}),
        doc
      )
    });
    compare(query, done);
  });

  it('group - 17',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id').default(r.error('bar'));
    });
    compare(query, done);
  });

  it('group - 18',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id').typeOf()
    compare(query, done);
  });

  it('group - 19',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').typeOf()
    compare(query, done);
  });

  it('group - 20',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').count().typeOf()
    compare(query, done);
  });

  it('group - 21',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group().count()
    compare(query, done);
  });

  it('ungroup - 1', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}
    ]).group('id', 'foo').ungroup().map(function(doc) {
      return doc('group')
    }).orderBy(r.row);
    compare(query, done);
  });

  it('ungroup - 2', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}<commit_msg>Add one more test for group<commit_after>var config = require(__dirname+'/../config.js');

var r = require('rethinkdb');
var assert = require('assert');

var connections = {};
var TEST_DB = 'reqlitetest';
var TEST_TABLE = 'reqlitetestaggregation';
var MISSING_ID = 'nonExistingId';
var MISSING_FIELD = 'nonExistingField';
var MISSING_INDEX = 'nonExistingIndex';

var compare = require(__dirname+'/util.js').generateCompare(connections);

describe('aggregation.js', function(){
  before(function(done) {
    setTimeout(function() { // Delay for nodemon to restart the server
      r.connect(config.rethinkdb).bind({}).then(function(conn) {
        connections.rethinkdb = conn;
        return r.connect(config.reqlite);
      }).then(function(conn) {
        connections.reqlite = conn;
        this.query = r.dbCreate(TEST_DB);
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).tableDrop(TEST_TABLE)
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).tableCreate(TEST_TABLE)
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).insert([
          {id: 1, foo: 20, bar: [100, 101, 102], optional: 1},
          {id: 2, foo: 10, bar: [200, 201, 202], optional: 1},
          {id: 3, foo: 10, bar: [300, 301, 302], optional: 2},
          {id: 4, foo: 10, bar: [400, 401, 402]},
        ]);
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('foo')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('bar')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('barmulti', r.row('bar'), {multi: true})
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {
        this.query = r.db(TEST_DB).table(TEST_TABLE).indexCreate('optional')
        return this.query.run(connections.rethinkdb);
      }).catch(function() { // ignore errors
      }).finally(function() {
        return this.query.run(connections.reqlite);
      }).catch(function() { // ignore errors
      }).finally(function() {

        done();
      });
    }, 400)
  });

  it('group - 1', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').count();
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 2', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('foo').count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 3', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group({index: 'id'}).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 4', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group(MISSING_FIELD).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 5', function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group({index: 'optional'}).count()
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 6', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {id: 2},
      {id: 3}
    ]).group('id')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 7', function(done) {
    var query = r.expr([
      {id: 1},
      {},
      {},
      {id: 3}
    ]).group('id')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        return a.group - b.group
      });
      return result;
    });
  });

  it('group - 8', function(done) {
    var query = r.expr([
      {id: 1},
      {},
      {},
      {id: 3}
    ]).group('id').count().ungroup().orderBy(r.row('group'));
    compare(query, done);
  });

  it('group - 9', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}
    ]).group('id', 'foo')
    compare(query, done, function(result) {
      result.sort(function(a, b) {
        if (a.group > b.group) {
          return 1
        }
        else if (a.group < b.group) {
          return -1
        }
        return 0;
      });
      return result;
    });
  });

  it('group - 10', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id').ungroup().count()
    compare(query, done);
  });

  it('group - 11', function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group();
    compare(query, done);
  });

  it('group - 12',  function(done) {
    var query = r.expr('foo').group('id');
    compare(query, done);
  });

  it('group - 13',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(1);
    compare(query, done);
  });

  it('group - 14',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id', {bar: 2});
    compare(query, done);
  });

  it('group - 15',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id');
    }).ungroup().orderBy('group').map(function(doc) {
      return r.branch(
        doc('group').eq(1),
        doc.merge(function(x) { return { reduction: doc('reduction').orderBy(function(y) { return y }) }}),
        doc
      )
    });
    compare(query, done);
  });

  it('group - 16',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id');
    }).ungroup().orderBy('group').map(function(doc) {
      return r.branch(
        doc('group').eq(1),
        doc.merge(function(x) { return { reduction: x('reduction').orderBy(function(y) { return y }) }}),
        doc
      )
    });
    compare(query, done);
  });

  it('group - 17',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group(function(doc) {
      return doc('id').default(r.error('bar'));
    });
    compare(query, done);
  });

  it('group - 18',  function(done) {
    var query = r.expr([
      {id: 1},
      {id: 1},
      {},
      {id: 3}
    ]).group('id').typeOf()
    compare(query, done);
  });

  it('group - 19',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').typeOf()
    compare(query, done);
  });

  it('group - 20',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group('id').count().typeOf()
    compare(query, done);
  });

  it('group - 21',  function(done) {
    var query = r.db(TEST_DB).table(TEST_TABLE).group().count()
    compare(query, done);
  });

  it('group - 22', function(done) {
    var query = r.expr([
        {name: "Michel", grownUp: true},
        {name: "Laurent", grownUp: true},
        {name: "Sophie", grownUp: true},
        {name: "Luke", grownUp: false},
        {name: "Mino", grownUp: false}
    ]).group('grownUp').orderBy(r.row).ungroup().orderBy(r.row)
    compare(query, done);
  });

  it('ungroup - 1', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}
    ]).group('id', 'foo').ungroup().map(function(doc) {
      return doc('group')
    }).orderBy(r.row);
    compare(query, done);
  });

  it('ungroup - 2', function(done) {
    var query = r.expr([
      {id: 1, foo: 2},
      {id: 1, foo: 2},
      {id: 1, foo: 3},
      {id: 3, foo: 3}
{'fraction_non_alphanumeric': 0.19685919616715464, 'fraction_numerical': 0.016289592760180997, 'mean_word_length': 2.3214285714285716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6395676', 'n_tokens_mistral': 6722, 'n_tokens_neox': 6311, 'n_words': 1275}