<commit_before>var _    = require('lodash');
var uuid = require('uuid');

var Promise   = global.testPromise;

var assert    = require('assert')
var equal     = require('assert').strictEqual;
var deepEqual = require('assert').deepEqual;
var QueryBuilder = require('knex/lib/query/builder')

module.exports = function(bookshelf) {
  describe('Model', function() {
    var Models    = require('./helpers/objects')(bookshelf).Models;

    var stubSync = {
      first:  function() { return Promise.resolve({}); },
      select: function() { return Promise.resolve({}); },
      insert: function() { return Promise.resolve({}); },
      update: function() { return Promise.resolve(1); },
      del:    function() { return Promise.resolve({}); }
    };

    var dialect = bookshelf.knex.client.dialect;
    var formatNumber = require('./helpers').formatNumber(dialect);
    var checkCount = function(actual, expected) {
      expect(actual, formatNumber(expected));
    };

    describe('extend/constructor/initialize', function() {

      var User = bookshelf.Model.extend({
        idAttribute: 'user_id',
        getData: function() { return 'test'; }
      }, {
        classMethod: function() { return 'test'; }
      });

      var SubUser = User.extend({
        otherMethod: function() { return this.getData(); }
      }, {
        classMethod2: function() { return 'test2'; }
      });

      var OtherUser = bookshelf.Model.extend({
        idAttribute: 'user_id',
        getData: function() { return 'test'; }
      }, {
        classMethod: function() { return 'test'; }
      });

      it('can be extended', function() {
        var user = new User({name: "hoge"});
        var subUser = new SubUser();
        expect(user.idAttribute).to.equal('user_id');
        expect(user.getData()).to.equal('test');
        expect(subUser.otherMethod()).to.equal('test');
        expect(User.classMethod()).to.equal('test');
        expect(SubUser.classMethod()).to.equal('test');
        expect(SubUser.classMethod2()).to.equal('test2');
      });

      it('accepts a custom `constructor` property', function() {
        var User = bookshelf.Model.extend({
          constructor: function() {
            this.item = 'test';
            bookshelf.Model.apply(this, arguments);
          }
        });

        equal(new User().item, 'test');
      });

      it('initializes an empty object for storing changed attributes', function() {
        equal(User.prototype.changed, undefined);
        deepEqual((new User()).changed, {});
      });

      context('should have own errors: name of', function(){
        it('NotFoundError', function(){
          var err = new User.NotFoundError();
          var suberr = new SubUser.NotFoundError();
          expect(User.NotFoundError).to.not.be.eql(bookshelf.Model.NotFoundError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NotFoundError);
          expect(User.NotFoundError).to.not.be.eql(SubUser.NotFoundError);
          expect(err).to.not.be.an.instanceof(SubUser.NotFoundError);
          expect(suberr).to.be.an.instanceof(User.NotFoundError);
          expect(User.NotFoundError).to.not.be.eql(OtherUser.NotFoundError);
          expect(err).to.not.be.an.instanceof(OtherUser.NotFoundError);
        });

        it('NoRowsUpdatedError', function(){
          var err = new User.NoRowsUpdatedError();
          var suberr = new SubUser.NoRowsUpdatedError();
          expect(User.NoRowsUpdatedError).to.not.be.eql(bookshelf.Model.NoRowsUpdatedError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NoRowsUpdatedError);
          expect(User.NoRowsUpdatedError).to.not.be.eql(SubUser.NoRowsUpdatedError);
          expect(err).to.not.be.an.instanceof(SubUser.NoRowsUpdatedError);
          expect(suberr).to.be.an.instanceof(User.NoRowsUpdatedError);
          expect(User.NoRowsUpdatedError).to.not.be.eql(OtherUser.NoRowsUpdatedError);
          expect(err).to.not.be.an.instanceof(OtherUser.NoRowsUpdatedError);
        });

        it('NoRowsDeletedError', function(){
          var err = new User.NoRowsDeletedError();
          var suberr = new SubUser.NoRowsDeletedError();
          expect(User.NoRowsDeletedError).to.not.be.eql(bookshelf.Model.NoRowsDeletedError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NoRowsDeletedError);
          expect(User.NoRowsDeletedError).to.not.be.eql(SubUser.NoRowsDeletedError);
          expect(err).to.not.be.an.instanceof(SubUser.NoRowsDeletedError);
          expect(suberr).to.be.an.instanceof(User.NoRowsDeletedError);
          expect(User.NoRowsDeletedError).to.not.be.eql(OtherUser.NoRowsDeletedError);
          expect(err).to.not.be.an.instanceof(OtherUser.NoRowsDeletedError);
        });
      });
    });

    describe('forge', function() {

      it('should create a new model instance', function() {
        var User = bookshelf.Model.extend({
          tableName: 'users'
        });
        var user = User.forge();
        equal(user.tableName, 'users');
        expect(user).to.be.an.instanceof(User);
      });

    });

    describe('id, idAttribute', function() {

      it('should attach the id as a property on the model', function() {
        var test = new bookshelf.Model({id: 1});
        equal(test.id, (1));
      });

      it('should reference idAttribute as the key for model.id', function() {
        var Test = bookshelf.Model.extend({
          idAttribute: '_id'
        });
        var test2 = new Test({_id: 2});
        equal(test2.id, (2));
      });

    });

    describe('query', function() {

      var model;
      beforeEach(function() {
        model = new bookshelf.Model();
      });

      it('returns the Knex builder when no arguments are passed', function() {
        equal((model.query() instanceof QueryBuilder), true);
      });

      it('calls Knex builder method with the first argument, returning the model', function() {
        var q = model.query('where', {id:1});
        equal(q, (model));
      });

      it('passes along additional arguments to the Knex method in the first argument', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query('where', {id:1});
        equal(q, (model));
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 1);
      });

      it('allows passing an object to query', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query({where: {id: 1}, orWhere: ['id', '>', '10']});
        equal(q, model);
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 2);
      });

      it('allows passing a function to query', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query(function(qb) {
          this.where({id: 1}).orWhere('id', '>', '10');
        });
        equal(q, model);
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 2);
        qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        q = model.query(function(qb) {
          qb.where({id: 1}).orWhere('id', '>', '10');
        });
        equal(q, model);
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 2);
      });

    });

    describe('tableName', function() {
      var table;

      beforeEach(function() {
        table = new bookshelf.Model({}, {tableName: 'customers'});
      })

      it('can be passed in the initialize options', function() {
        equal(table.tableName, 'customers');
      });

      it('should set the tableName for the query builder', function() {
        equal(table.query()._single.table, 'customers');
      });

    });

    describe('toJSON', function() {

      it('includes the idAttribute in the hash', function() {
        var m = new (bookshelf.Model.extend({
          idAttribute: '_id'
        }))({'_id': 1, 'name': 'Joe'});
        deepEqual(m.toJSON(), {'_id': 1, 'name': 'Joe'});<commit_msg>Remove duplicate code in Model test<commit_after>var _    = require('lodash');
var uuid = require('uuid');

var Promise   = global.testPromise;

var assert    = require('assert')
var equal     = require('assert').strictEqual;
var deepEqual = require('assert').deepEqual;
var QueryBuilder = require('knex/lib/query/builder')

module.exports = function(bookshelf) {
  describe('Model', function() {
    var Models    = require('./helpers/objects')(bookshelf).Models;

    var stubSync = {
      first:  function() { return Promise.resolve({}); },
      select: function() { return Promise.resolve({}); },
      insert: function() { return Promise.resolve({}); },
      update: function() { return Promise.resolve(1); },
      del:    function() { return Promise.resolve({}); }
    };

    var dialect = bookshelf.knex.client.dialect;
    var formatNumber = require('./helpers').formatNumber(dialect);
    var checkCount = function(actual, expected) {
      expect(actual, formatNumber(expected));
    };

    describe('extend/constructor/initialize', function() {

      var User = bookshelf.Model.extend({
        idAttribute: 'user_id',
        getData: function() { return 'test'; }
      }, {
        classMethod: function() { return 'test'; }
      });

      var SubUser = User.extend({
        otherMethod: function() { return this.getData(); }
      }, {
        classMethod2: function() { return 'test2'; }
      });

      var OtherUser = bookshelf.Model.extend({
        idAttribute: 'user_id',
        getData: function() { return 'test'; }
      }, {
        classMethod: function() { return 'test'; }
      });

      it('can be extended', function() {
        var user = new User({name: "hoge"});
        var subUser = new SubUser();
        expect(user.idAttribute).to.equal('user_id');
        expect(user.getData()).to.equal('test');
        expect(subUser.otherMethod()).to.equal('test');
        expect(User.classMethod()).to.equal('test');
        expect(SubUser.classMethod()).to.equal('test');
        expect(SubUser.classMethod2()).to.equal('test2');
      });

      it('accepts a custom `constructor` property', function() {
        var User = bookshelf.Model.extend({
          constructor: function() {
            this.item = 'test';
            bookshelf.Model.apply(this, arguments);
          }
        });

        equal(new User().item, 'test');
      });

      it('initializes an empty object for storing changed attributes', function() {
        equal(User.prototype.changed, undefined);
        deepEqual((new User()).changed, {});
      });

      context('should have own errors: name of', function(){
        it('NotFoundError', function(){
          var err = new User.NotFoundError();
          var suberr = new SubUser.NotFoundError();
          expect(User.NotFoundError).to.not.be.eql(bookshelf.Model.NotFoundError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NotFoundError);
          expect(User.NotFoundError).to.not.be.eql(SubUser.NotFoundError);
          expect(err).to.not.be.an.instanceof(SubUser.NotFoundError);
          expect(suberr).to.be.an.instanceof(User.NotFoundError);
          expect(User.NotFoundError).to.not.be.eql(OtherUser.NotFoundError);
          expect(err).to.not.be.an.instanceof(OtherUser.NotFoundError);
        });

        it('NoRowsUpdatedError', function(){
          var err = new User.NoRowsUpdatedError();
          var suberr = new SubUser.NoRowsUpdatedError();
          expect(User.NoRowsUpdatedError).to.not.be.eql(bookshelf.Model.NoRowsUpdatedError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NoRowsUpdatedError);
          expect(User.NoRowsUpdatedError).to.not.be.eql(SubUser.NoRowsUpdatedError);
          expect(err).to.not.be.an.instanceof(SubUser.NoRowsUpdatedError);
          expect(suberr).to.be.an.instanceof(User.NoRowsUpdatedError);
          expect(User.NoRowsUpdatedError).to.not.be.eql(OtherUser.NoRowsUpdatedError);
          expect(err).to.not.be.an.instanceof(OtherUser.NoRowsUpdatedError);
        });

        it('NoRowsDeletedError', function(){
          var err = new User.NoRowsDeletedError();
          var suberr = new SubUser.NoRowsDeletedError();
          expect(User.NoRowsDeletedError).to.not.be.eql(bookshelf.Model.NoRowsDeletedError);
          expect(err).to.be.an.instanceof(bookshelf.Model.NoRowsDeletedError);
          expect(User.NoRowsDeletedError).to.not.be.eql(SubUser.NoRowsDeletedError);
          expect(err).to.not.be.an.instanceof(SubUser.NoRowsDeletedError);
          expect(suberr).to.be.an.instanceof(User.NoRowsDeletedError);
          expect(User.NoRowsDeletedError).to.not.be.eql(OtherUser.NoRowsDeletedError);
          expect(err).to.not.be.an.instanceof(OtherUser.NoRowsDeletedError);
        });
      });
    });

    describe('forge', function() {

      it('should create a new model instance', function() {
        var User = bookshelf.Model.extend({
          tableName: 'users'
        });
        var user = User.forge();
        equal(user.tableName, 'users');
        expect(user).to.be.an.instanceof(User);
      });

    });

    describe('id, idAttribute', function() {

      it('should attach the id as a property on the model', function() {
        var test = new bookshelf.Model({id: 1});
        equal(test.id, (1));
      });

      it('should reference idAttribute as the key for model.id', function() {
        var Test = bookshelf.Model.extend({
          idAttribute: '_id'
        });
        var test2 = new Test({_id: 2});
        equal(test2.id, (2));
      });

    });

    describe('query', function() {

      var model;
      beforeEach(function() {
        model = new bookshelf.Model();
      });

      it('returns the Knex builder when no arguments are passed', function() {
        equal((model.query() instanceof QueryBuilder), true);
      });

      it('calls Knex builder method with the first argument, returning the model', function() {
        var q = model.query('where', {id:1});
        equal(q, (model));
      });

      it('passes along additional arguments to the Knex method in the first argument', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query('where', {id:1});
        equal(q, (model));
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 1);
      });

      it('allows passing an object to query', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query({where: {id: 1}, orWhere: ['id', '>', '10']});
        equal(q, model);
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 2);
      });

      it('allows passing a function to query', function() {
        var qb = model.resetQuery().query();
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 0);
        var q = model.query(function(qb) {
          this.where({id: 1}).orWhere('id', '>', '10');
        });

        equal(q, model);
        equal(_.filter(qb._statements, {grouping: 'where'}).length, 2);
      });

    });

    describe('tableName', function() {
      var table;

      beforeEach(function() {
        table = new bookshelf.Model({}, {tableName: 'customers'});
      })

      it('can be passed in the initialize options', function() {
        equal(table.tableName, 'customers');
      });

      it('should set the tableName for the query builder', function() {
        equal(table.query()._single.table, 'customers');
      });

    });

    describe('toJSON', function() {

      it('includes the idAttribute in the hash', function() {
        var m = new (bookshelf.Model.extend({
          idAttribute: '_id'
        }))({'_id': 1, 'name': 'Joe'});
        deepEqual(m.toJSON(), {'_id': 1, 'name': 'Joe'});
{'fraction_non_alphanumeric': 0.16228671067380745, 'fraction_numerical': 0.0036741810935359324, 'mean_word_length': 2.6968232044198897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '2235069', 'n_tokens_mistral': 4817, 'n_tokens_neox': 4684, 'n_words': 919}