<commit_before>var should = require('should');
var Store = require('../../../lib/store');


module.exports = function(title, beforeFn, afterFn, store_conf){
  
  describe(title + ': Select', function(){
    var store;
  
    before(beforeFn);
    after(function(next){
      afterFn(next, store);
    }); 
    
  
    before(function(){
      store = new Store(store_conf);
      store.setMaxListeners(0);

      store.Model('User', function(){
        this.hasMany('posts');
        this.hasMany('threads');
      });
      store.Model('Post', function(){
        this.belongsTo('user');
        this.belongsTo('thread');
      });
      store.Model('Thread', function(){
        this.belongsTo('user');
        this.hasMany('posts');
      });
    });
  
    
    it('returns only selected fields', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('id', 'login').exec(function(user){
          user.login.should.be.equal('phil');
          should.not.exist(user.email);
          done();
        });
      });
    });
    
    it('returns only selected fields (asJson())', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('id', 'login').asJson().exec(function(user){
          user.should.be.eql({
            id: 1,
            login: 'phil'
          });
          done();
        });
      });
    });
    
    it('works with joins (automatic asJson())', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('users.id', 'login', 'message').join('posts').exec(function(user){
          user.should.be.eql([{ //TODO: should this be an array? we use a find() which returns an objects, if only one record was found...
            id: 1,
            login: 'phil',
            message: 'first message'
          }]);
          done();
        });
      });
    });
    
    it('works with aggregate functions', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.select('count(*) as count').exec(function(count){
          count.should.be.eql([{
            count: 4
          }]);<commit_msg>fixes some weird assertion bug<commit_after>var should = require('should');
var Store = require('../../../lib/store');


module.exports = function(title, beforeFn, afterFn, store_conf){
  
  describe(title + ': Select', function(){
    var store;
  
    before(beforeFn);
    after(function(next){
      afterFn(next, store);
    }); 
    
  
    before(function(){
      store = new Store(store_conf);
      store.setMaxListeners(0);

      store.Model('User', function(){
        this.hasMany('posts');
        this.hasMany('threads');
      });
      store.Model('Post', function(){
        this.belongsTo('user');
        this.belongsTo('thread');
      });
      store.Model('Thread', function(){
        this.belongsTo('user');
        this.hasMany('posts');
      });
    });
  
    
    it('returns only selected fields', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('id', 'login').exec(function(user){
          user.login.should.be.equal('phil');
          should.not.exist(user.email);
          done();
        });
      });
    });
    
    it('returns only selected fields (asJson())', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('id', 'login').asJson().exec(function(user){
          user.should.be.eql({
            id: 1,
            login: 'phil'
          });
          done();
        });
      });
    });
    
    it('works with joins (automatic asJson())', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.find(1).select('users.id', 'login', 'message').join('posts').exec(function(user){
          
          //TODO: should this be an array? we use a find() which returns an objects, if only one record was found...
          user[0].id.should.be.equal(1);
          user[0].login.should.be.equal('phil');
          user[0].message.should.be.equal('first message');
          
          done();
        });
      });
    });
    
    it('works with aggregate functions', function(done){
      store.ready(function(){
        var User = store.Model('User');
        
        User.select('count(*) as count').exec(function(count){
          count[0].count.should.be.equal(4);
{'fraction_non_alphanumeric': 0.17776807163136055, 'fraction_numerical': 0.003930989298973575, 'mean_word_length': 2.0270984798413747, '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': '3724468', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1322, 'n_words': 250}