<commit_before>'use strict';

/* jshint -W030 */
var chai = require('chai')
  , expect = chai.expect
  , Support = require(__dirname + '/../support')
  , UniqueConstraintError = require(__dirname + '/../../../lib/errors').UniqueConstraintError
  , current = Support.sequelize
  , cls = require('continuation-local-storage')
  , sinon = require('sinon')
  , Promise = require('bluebird');

describe(Support.getTestDialectTeaser('Model'), function() {
  describe('findCreateFind', function () {
    var Model = current.define('Model', {});

    beforeEach(function () {
      this.sinon = sinon.sandbox.create();
    });

    afterEach(function () {
      this.sinon.restore();
    });

    it('should return the result of the first find call if not empty', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(result));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, false]).then(function () {
        expect(findSpy).to.have.been.calledOnce;
        expect(findSpy.getCall(0).args[0].where).to.equal(where);
      });
    });

    it('should create if first find call is empty', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(null))
        , createSpy = this.sinon.stub(Model, 'create').returns(Promise.resolve(result));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, true]).then(function () {
        expect(createSpy).to.have.been.calledWith(where);
      });
    });

    it('should do a second find if create failed do to unique constraint', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , findSpy = this.sinon.stub(Model, 'findOne')
        , createSpy = this.sinon.stub(Model, 'create').returns(Promise.reject(new UniqueConstraintError()));

      findSpy.onFirstCall().returns(Promise.resolve(null));
      findSpy.onSecondCall().returns(Promise.resolve(result));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, false]).then(function () {
        expect(findSpy).to.have.been.calledTwice;
        expect(findSpy.getCall(1).args[0].where).to.equal(where);
      });
    });
  });
});<commit_msg>fix jshint<commit_after>'use strict';

/* jshint -W030 */
var chai = require('chai')
  , expect = chai.expect
  , Support = require(__dirname + '/../support')
  , UniqueConstraintError = require(__dirname + '/../../../lib/errors').UniqueConstraintError
  , current = Support.sequelize
  , sinon = require('sinon')
  , Promise = require('bluebird');

describe(Support.getTestDialectTeaser('Model'), function() {
  describe('findCreateFind', function () {
    var Model = current.define('Model', {});

    beforeEach(function () {
      this.sinon = sinon.sandbox.create();
    });

    afterEach(function () {
      this.sinon.restore();
    });

    it('should return the result of the first find call if not empty', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(result));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, false]).then(function () {
        expect(findSpy).to.have.been.calledOnce;
        expect(findSpy.getCall(0).args[0].where).to.equal(where);
      });
    });

    it('should create if first find call is empty', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , createSpy = this.sinon.stub(Model, 'create').returns(Promise.resolve(result));

      this.sinon.stub(Model, 'findOne').returns(Promise.resolve(null));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, true]).then(function () {
        expect(createSpy).to.have.been.calledWith(where);
      });
    });

    it('should do a second find if create failed do to unique constraint', function () {
      var result = {}
        , where = {prop: Math.random().toString()}
        , findSpy = this.sinon.stub(Model, 'findOne');

      this.sinon.stub(Model, 'create').returns(Promise.reject(new UniqueConstraintError()));

      findSpy.onFirstCall().returns(Promise.resolve(null));
      findSpy.onSecondCall().returns(Promise.resolve(result));

      return expect(Model.findCreateFind({
        where: where
      })).to.eventually.eql([result, false]).then(function () {
        expect(findSpy).to.have.been.calledTwice;
        expect(findSpy.getCall(1).args[0].where).to.equal(where);
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.17286515686681903, 'fraction_numerical': 0.002908788697278205, 'mean_word_length': 3.1145299145299146, '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': '6332351', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1458, 'n_words': 249}