forked from sequelize/sequelize
/
find-one.test.js
128 lines (108 loc) · 3.86 KB
/
find-one.test.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../support');
const Sequelize = Support.Sequelize;
const Op = Sequelize.Op;
const current = Support.sequelize;
const sinon = require('sinon');
const DataTypes = require('sequelize/lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('method findOne', () => {
before(function () {
this.oldFindAll = Sequelize.Model.findAll;
});
after(function () {
Sequelize.Model.findAll = this.oldFindAll;
});
beforeEach(function () {
this.stub = Sequelize.Model.findAll = sinon.stub().resolves();
});
describe('should not add limit when querying on a primary key', () => {
it('with id primary key', async function () {
const Model = current.define('model');
await Model.findOne({ where: { id: 42 } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
it('with custom primary key', async function () {
const Model = current.define('model', {
uid: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
});
await Model.findOne({ where: { uid: 42 } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
it('with blob primary key', async function () {
const Model = current.define('model', {
id: {
type: DataTypes.BLOB,
primaryKey: true,
autoIncrement: true,
},
});
await Model.findOne({ where: { id: Buffer.from('foo') } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
});
it('should add limit when using { $ gt on the primary key', async function () {
const Model = current.define('model');
await Model.findOne({ where: { id: { [Op.gt]: 42 } } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
});
describe('should not add limit when querying on an unique key', () => {
it('with custom unique key', async function () {
const Model = current.define('model', {
unique: {
type: DataTypes.INTEGER,
unique: true,
},
});
await Model.findOne({ where: { unique: 42 } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
it('with blob unique key', async function () {
const Model = current.define('model', {
unique: {
type: DataTypes.BLOB,
unique: true,
},
});
await Model.findOne({ where: { unique: Buffer.from('foo') } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
});
it('should add limit when using multi-column unique key', async function () {
const Model = current.define('model', {
unique1: {
type: DataTypes.INTEGER,
unique: 'unique',
},
unique2: {
type: DataTypes.INTEGER,
unique: 'unique',
},
});
await Model.findOne({ where: { unique1: 42 } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
});
it('should call internal findAll() method if findOne() is overridden', async () => {
const Model = current.define('model', {
unique1: {
type: DataTypes.INTEGER,
unique: 'unique',
},
unique2: {
type: DataTypes.INTEGER,
unique: 'unique',
},
});
Model.findAll = sinon.stub();
await Model.findOne();
Model.findAll.should.not.have.been.called;
Sequelize.Model.findAll.should.have.been.called;
});
});
});