model-factory.spec.js
4.97 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var config = require("./config/config")
, Sequelize = require("../index")
, sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
, Helpers = new (require("./config/helpers"))(sequelize)
describe('ModelFactory', function() {
var User = null
var setup = function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
age: Sequelize.INTEGER,
name: Sequelize.STRING,
bio: Sequelize.TEXT
})
User.sync({force: true}).success(done)
})
}
beforeEach(function() { Helpers.dropAllTables(); setup() })
afterEach(function() { Helpers.dropAllTables() })
describe('constructor', function() {
it("uses the passed model name as tablename if freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: true})
expect(User.tableName).toEqual('User')
})
it("uses the pluralized modelname as tablename unless freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: false})
expect(User.tableName).toEqual('Users')
})
it("attaches class and instance methods", function() {
var User = sequelize.define('User', {}, {
classMethods: { doSmth: function(){ return 1 } },
instanceMethods: { makeItSo: function(){ return 2}}
})
expect(User.doSmth).toBeDefined()
expect(User.doSmth()).toEqual(1)
expect(User.makeItSo).toBeUndefined()
expect(User.build().makeItSo).toBeDefined()
expect(User.build().makeItSo()).toEqual(2)
})
it("throws an error if 2 autoIncrements are passed", function() {
expect(function () {
var User = sequelize.define('User', {
userid: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
userscore: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
})
}).toThrow('Invalid model definition. Only one autoincrement field allowed.')
})
})
describe('build', function() {
it("doesn't create database entries", function() {
Helpers.async(function(done) {
User.build({ name: 'John Wayne', bio: 'noot' })
User.all().success(function(users) {
expect(users.length).toEqual(0)
done()
})
})
})
it("fills the objects with default values", function() {
var Task = sequelize.define('Task' + config.rand(), {
title: {type: Sequelize.STRING, defaultValue: 'a task!'},
foo: {type: Sequelize.INTEGER, defaultValue: 2},
bar: {type: Sequelize.DATE},
foobar: {type: Sequelize.TEXT, defaultValue: 'asd'},
flag: {type: Sequelize.BOOLEAN, defaultValue: false}
})
expect(Task.build().title).toEqual('a task!')
expect(Task.build().foo).toEqual(2)
expect(Task.build().bar).toEqual(null)
expect(Task.build().foobar).toEqual('asd')
expect(Task.build().flag).toEqual(false)
})
})
describe('find', function() {
beforeEach(function() {
Helpers.Factories.User({name: 'user', bio: 'foobar'}, null, 2)
})
it("should make aliased attributes available", function() {
Helpers.async(function(done) {
User.find({ where: 'id = 1', attributes: ['id', ['name', 'username']] }).success(function(user) {
expect(user.username).toEqual('user')
done()
})
})
})
})
describe('all', function() {
beforeEach(function() {
Helpers.Factories.User({name: 'user', bio: 'foobar'}, null, 2)
})
it("should return all users", function() {
Helpers.async(function(done) {
User.all().on('success', function(users) {
done()
expect(users.length).toEqual(2)
}).on('failure', function(err) { console.log(err) })
})
})
})
describe('create with options', function() {
var Person = sequelize.define('Person', { name: Sequelize.STRING, options: Sequelize.TEXT })
beforeEach(function() {
Helpers.async(function(done) {
Person.sync({ force: true }).success(done)
})
})
it('should allow the creation of an object with options as attribute', function() {
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
Helpers.Factories.Model('Person', {name: 'John Doe', options: options}, function(people) {
expect(people[0].options).toEqual(options)
})
})
})
describe('min', function() {
it("should return the min value", function() {
for(var i = 2; i < 5; i++) Helpers.Factories.User({ age: i })
Helpers.async(function(done) {
User.min('age').on('success', function(min) {
expect(min).toEqual(2); done()
})
})
})
})
describe('max', function() {
it("should return the max value", function() {
for(var i = 2; i <= 5; i++) Helpers.Factories.User({ age: i })
Helpers.async(function(done) {
User.max('age').on('success', function(max) {
expect(max).toEqual(5); done()
})
})
})
})
})