dao.test.js
4.01 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
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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../../support'),
Sequelize = Support.Sequelize,
Op = Sequelize.Op,
dialect = Support.getTestDialect(),
DataTypes = require(__dirname + '/../../../../lib/data-types');
if (dialect === 'sqlite') {
describe('[SQLITE Specific] DAO', () => {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
emergency_contact: DataTypes.JSON,
emergencyContact: DataTypes.JSON,
dateField: {
type: DataTypes.DATE,
field: 'date_field'
}
});
this.Project = this.sequelize.define('project', {
dateField: {
type: DataTypes.DATE,
field: 'date_field'
}
});
this.User.hasMany(this.Project);
return this.sequelize.sync({ force: true });
});
describe('findAll', () => {
it('handles dates correctly', function() {
const user = this.User.build({ username: 'user' });
user.dataValues.createdAt = new Date(2011, 4, 4);
return user.save().then(() => {
return this.User.create({ username: 'new user' }).then(() => {
return this.User.findAll({
where: { createdAt: { [Op.gt]: new Date(2012, 1, 1) } }
}).then(users => {
expect(users).to.have.length(1);
});
});
});
});
it('handles dates with aliasses correctly #3611', function() {
return this.User.create({
dateField: new Date(2010, 10, 10)
}).then(() => {
return this.User.findAll().get(0);
}).then(user => {
expect(user.get('dateField')).to.be.an.instanceof(Date);
expect(user.get('dateField')).to.equalTime(new Date(2010, 10, 10));
});
});
it('handles dates in includes correctly #2644', function() {
return this.User.create({
projects: [
{ dateField: new Date(1990, 5, 5) }
]
}, { include: [this.Project] }).then(() => {
return this.User.findAll({
include: [this.Project]
}).get(0);
}).then(user => {
expect(user.projects[0].get('dateField')).to.be.an.instanceof(Date);
expect(user.projects[0].get('dateField')).to.equalTime(new Date(1990, 5, 5));
});
});
});
describe('json', () => {
it('should be able to retrieve a row with json_extract function', function() {
return this.sequelize.Promise.all([
this.User.create({ username: 'swen', emergency_contact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergency_contact: { name: 'joe' } })
]).then(() => {
return this.User.find({
where: Sequelize.json('json_extract(emergency_contact, \'$.name\')', 'kate'),
attributes: ['username', 'emergency_contact']
});
}).then(user => {
expect(user.emergency_contact.name).to.equal('kate');
});
});
it('should be able to retrieve a row by json_type function', function() {
return this.sequelize.Promise.all([
this.User.create({ username: 'swen', emergency_contact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergency_contact: ['kate', 'joe'] })
]).then(() => {
return this.User.find({
where: Sequelize.json('json_type(emergency_contact)', 'array'),
attributes: ['username', 'emergency_contact']
});
}).then(user => {
expect(user.username).to.equal('anna');
});
});
});
describe('regression tests', () => {
it('do not crash while parsing unique constraint errors', function() {
const Payments = this.sequelize.define('payments', {});
return Payments.sync({ force: true }).then(() => {
return expect(Payments.bulkCreate([{ id: 1 }, { id: 1 }], { ignoreDuplicates: false })).to.eventually.be.rejected;
});
});
});
});
}