<commit_before>var expect = require('expect.js');
var mongoose = require('mongoose');
var express = require('express');
var passport = require('passport')
var LocalStrategy = require('passport-local').Strategy;
var request = require('request');
var baucis = require('..');

var fixtures = require('./fixtures');

describe('Queries', function () {
  before(fixtures.vegetable.init);
  beforeEach(fixtures.vegetable.create);
  after(fixtures.vegetable.deinit);

  it('should support skip 1', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?skip=1',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', vegetables.length - 1);
      done();
    });
  });

  it('should support skip 2', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?skip=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', vegetables.length - 2);
      done();
    });
  });

  it('should support limit 1', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=1',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', 1);
      done();
    });
  });

  it('should support limit 2', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', 2);
      done();
    });
  });

  it('should disallow using +fields with populate', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?populate={ "select": "%2Bfoo" }',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 500);
      done();
    });
  });

  it('should disallow using +fields with select', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?select=%2Bfoo',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 500);
      done();
    });
  });

  it('should allow selecting fields', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?select=-_id lastModified',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('lastModified');
      expect(body[0]).not.to.have.property('_id');
      expect(body[0]).not.to.have.property('name');
      done();
    });
  });

});<commit_msg>Added tests for paging links to queries.js<commit_after>var expect = require('expect.js');
var mongoose = require('mongoose');
var express = require('express');
var passport = require('passport')
var LocalStrategy = require('passport-local').Strategy;
var request = require('request');
var baucis = require('..');
var parselinks = require('parse-links');

var fixtures = require('./fixtures');

describe('Queries', function () {
  before(fixtures.vegetable.init);
  beforeEach(fixtures.vegetable.create);
  after(fixtures.vegetable.deinit);

  it('should support skip 1', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?skip=1',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', vegetables.length - 1);
      done();
    });
  });

  it('should support skip 2', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?skip=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', vegetables.length - 2);
      done();
    });
  });

  it('should support limit 1', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=1',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', 1);
      done();
    });
  });

  it('should support limit 2', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body).to.have.property('length', 2);
      done();
    });
  });

  it('should disallow using +fields with populate', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?populate={ "select": "%2Bfoo" }',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 500);
      done();
    });
  });

  it('should disallow using +fields with select', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?select=%2Bfoo',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 500);
      done();
    });
  });

  it('should allow selecting fields', function (done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?select=-_id lastModified',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('lastModified');
      expect(body[0]).not.to.have.property('_id');
      expect(body[0]).not.to.have.property('name');
      done();
    });
  });

  it('should allow adding paging links', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      done();
    });
  });
  
  it('should not return paging links if limit not set', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?sort=name',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).not.to.have.property('link');
      done();
    });
  });
    
  it('should return next for first page', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      expect(response.headers.link).to.contain('rel="next"');
      done();
    });
  });
  
  it('should return previous for second page', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      expect(response.headers.link).to.contain('rel="previous"');
      done();
    });
  });
  
  it('should not return paging links previous for first page', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      expect(response.headers.link).not.to.contain('rel="previous"');
      done();
    });
  });
  
  it('should not return paging links next for last page', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=6',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      expect(response.headers.link).not.to.contain('rel="next"');
      done();
    });
  });
  
  it('should preserve query in paging links', function(done) {
    var conditions = JSON.stringify({
        name: { 
          $regex: /.*i.*/
          }
      })
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=0&conditions=' + conditions,
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(response.headers).to.have.property('link');
      expect(response.headers.link).to.contain('rel="next"');
      var links = parselinks(response.headers.link);
      console.log(links);
      expect(links.next).to.contain('conditions=');
      done();
    });
  });
  
  it('should allow retriving paging links next', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=0',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('name');
      expect(body[0].name).to.equal('Carrot');
      expect(response.headers).to.have.property('link');
      var links = parselinks(response.headers.link);
      expect(links).to.have.property('next');
      var options = {
        url: 'http://localhost:8012/api/v1/vegetables' + links.next,
        json: true
      };
      request.get(options, function (err, response, body) {
        expect(response).to.have.property('statusCode', 200);
        expect(body[0]).to.have.property('name');
        expect(body[0].name).to.equal('Pea');
        done();
      })
    });
  });
  
  it('should allow retriving paging links previous', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=2',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('name');
      expect(body[0].name).to.equal('Pea');
      expect(response.headers).to.have.property('link');
      var links = parselinks(response.headers.link);
      expect(links).to.have.property('previous');
      var options = {
        url: 'http://localhost:8012/api/v1/vegetables' + links.previous,
        json: true
      };
      request.get(options, function (err, response, body) {
        expect(response).to.have.property('statusCode', 200);
        expect(body[0]).to.have.property('name');
        expect(body[0].name).to.equal('Carrot');
        done();
      })
    });
  });
  
  it('should allow retriving paging links last', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=6',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('name');
      expect(body[0].name).to.equal('Turnip');
      expect(response.headers).to.have.property('link');
      var links = parselinks(response.headers.link);
      expect(links).to.have.property('first');
      var options = {
        url: 'http://localhost:8012/api/v1/vegetables' + links.first,
        json: true
      };
      request.get(options, function (err, response, body) {
        expect(response).to.have.property('statusCode', 200);
        expect(body[0]).to.have.property('name');
        expect(body[0].name).to.equal('Carrot');
        done();
      })
    });
  });
  
  it('should allow retriving paging links first', function(done) {
    var options = {
      url: 'http://localhost:8012/api/v1/vegetables?limit=2&sort=name&skip=0',
      json: true
    };
    request.get(options, function (err, response, body) {
      if (err) return done(err);
      expect(response).to.have.property('statusCode', 200);
      expect(body[0]).to.have.property('name');
      expect(body[0].name).to.equal('Carrot');
      expect(response.headers).to.have.property('link');
      var links = parselinks(response.headers.link);
      expect(links).to.have.property('last');
      var options = {
        url: 'http://localhost:8012/api/v1/vegetables' + links.last,
        json: true
      };
      request.get(options, function (err, response, body) {
        expect(response).to.have.property('statusCode', 200);
        expect(body[0]).to.have.property('name');
        expect(body[0].name).to.equal('Turnip');
        done();
      })
    });
  });
});
{'fraction_non_alphanumeric': 0.17665771242754136, 'fraction_numerical': 0.021136717093171213, 'mean_word_length': 3.059397417503587, 'pattern_counts': {'":': 2, '<': 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': '3691913', 'n_tokens_mistral': 4657, 'n_tokens_neox': 4475, 'n_words': 842}