<commit_before>var Collection = require('../../../lib/waterline/collection'),
    assert = require('assert');

describe('.afterCreate()', function() {

  describe('basic function', function() {
    var person;

    before(function(done) {
      var Model = Collection.extend({
        identity: 'user',
        adapter: 'foo',
        attributes: {
          name: 'string'
        },

        afterCreate: function(values, cb) {
          values.name = values.name + ' updated';
          cb();
        }
      });

      // Fixture Adapter Def
      var adapterDef = {
        find: function(col, criteria, cb) { return cb(null, null); },
        create: function(col, values, cb) { return cb(null, values); }
      };

      new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
        if(err) done(err);
        person = coll;
        done();
      });
    });

    /**
     * findOrCreate
     */

    describe('.findOrCreate()', function() {

      it('should run afterCreate and mutate values', function(done) {
        person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
          assert(!err);
          assert(user.name === 'test updated');
          done();
        });
      });
    });
  });


  /**
   * Test Callbacks can be defined as arrays and run in order.
   */

  describe('array of functions', function() {
    var person;

    before(function(done) {
      var Model = Collection.extend({
        identity: 'user',
        adapter: 'foo',
        attributes: {
          name: 'string'
        },

        afterCreate: [
          // Function 1
          function(values, cb) {
            values.name = values.name + ' fn1';
            cb();
          },

          // Function 2
          function(values, cb) {
            values.name = values.name + ' fn2';
            cb();
          }
        ]
      });

      // Fixture Adapter Def
      var adapterDef = {
        find: function(col, criteria, cb) { return cb(null, null); },
        create: function(col, values, cb) { return cb(null, values); }
      };

      new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
        if(err) done(err);
        person = coll;
        done();
      });
    });

    it('should run the functions in order', function(done) {
      person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
        assert(!err);
        assert(user.name === 'test fn1 fn2');
        done();
      });
    });
  });

});<commit_msg>pass tests for findOrCreate afterCreate callback<commit_after>var Collection = require('../../../lib/waterline/collection'),
    assert = require('assert');

describe('.afterCreate()', function() {

  describe('basic function', function() {
    var person,
        Model;

    before(function(done) {
      Model = Collection.extend({
        identity: 'user',
        adapter: 'foo',
        attributes: {
          name: 'string'
        },

        afterCreate: function(values, cb) {
          values.name = values.name + ' updated';
          cb();
        }
      });

      done();
    });

    /**
     * findOrCreate
     */

    describe('.findOrCreate()', function() {

      describe('without a record', function() {

        before(function(done) {
          // Fixture Adapter Def
          var adapterDef = {
            find: function(col, criteria, cb) { return cb(null, []); },
            create: function(col, values, cb) { return cb(null, values); }
          };

          new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
            if(err) done(err);
            person = coll;
            done();
          });
        });

        it('should run afterCreate and mutate values on create', function(done) {
          person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
            assert(!err);
            assert(user.name === '<NAME>');
            done();
          });
        });
      });

      describe('with a record', function() {

        before(function(done) {
          var adapterDef = {
            find: function(col, criteria, cb) { return cb(null, [criteria.where]); },
            create: function(col, values, cb) { return cb(null, values); }
          };

          new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
            if(err) done(err);
            person = coll;
            done();
          });
        });

        it('should not run afterCreate and mutate values on find', function(done) {
          person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
            assert(!err);
            assert(user.name === 'test');
            done();
          });
        });
      });
    });
  });


  /**
   * Test Callbacks can be defined as arrays and run in order.
   */

  describe('array of functions', function() {
    var person,
        Model;

    before(function(done) {
      Model = Collection.extend({
        identity: 'user',
        adapter: 'foo',
        attributes: {
          name: 'string'
        },

        afterCreate: [
          // Function 1
          function(values, cb) {
            values.name = values.name + ' fn1';
            cb();
          },

          // Function 2
          function(values, cb) {
            values.name = values.name + ' fn2';
            cb();
          }
        ]
      });

      done();
    });

    describe('without a record', function() {

      before(function(done) {
        // Fixture Adapter Def
        var adapterDef = {
          find: function(col, criteria, cb) { return cb(null, []); },
          create: function(col, values, cb) { return cb(null, values); }
        };

        new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
          if(err) done(err);
          person = coll;
          done();
        });
      });

      it('should run the functions in order', function(done) {
        person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
          assert(!err);
          assert(user.name === 'test fn1 fn2');
          done();
        });
      });
    });

    describe('with a record', function() {

      before(function(done) {
        // Fixture Adapter Def
        var adapterDef = {
          find: function(col, criteria, cb) { return cb(null, [criteria.where]); },
          create: function(col, values, cb) { return cb(null, values); }
        };

        new Model({ adapters: { foo: adapterDef }}, function(err, coll) {
          if(err) done(err);
          person = coll;
          done();
        });
      });

      it('should not run any of the functions', function(done) {
        person.findOrCreate({ name: 'test' }, { name: 'test' }, function(err, user) {
          assert(!err);
          assert(user.name === 'test');
          done();
        });
      });
    });

  });

});
{'fraction_non_alphanumeric': 0.15838419064225515, 'fraction_numerical': 0.0017436791630340018, 'mean_word_length': 1.7119779353821907, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5555933', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1898, 'n_words': 483}