find-create-find.test.js
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const 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'), () => {
describe('findCreateFind', () => {
const 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() {
const result = {},
where = {prop: Math.random().toString()},
findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(result));
return expect(Model.findCreateFind({
where
})).to.eventually.eql([result, false]).then(() => {
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() {
const 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
})).to.eventually.eql([result, true]).then(() => {
expect(createSpy).to.have.been.calledWith(where);
});
});
it('should do a second find if create failed do to unique constraint', function() {
const result = {},
where = {prop: Math.random().toString()},
findSpy = this.sinon.stub(Model, 'findOne');
this.sinon.stub(Model, 'create', () => {
return Promise.reject(new UniqueConstraintError());
});
findSpy.onFirstCall().returns(Promise.resolve(null));
findSpy.onSecondCall().returns(Promise.resolve(result));
return expect(Model.findCreateFind({
where
})).to.eventually.eql([result, false]).then(() => {
expect(findSpy).to.have.been.calledTwice;
expect(findSpy.getCall(1).args[0].where).to.equal(where);
});
});
});
});