model-definition.spec.js
2.69 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
var config = require("./config/config")
, Helpers = require("./config/helpers")
, Sequelize = require("../index")
describe('ModelDefinition', function() {
var sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
, User = sequelize.define('User', { name: Sequelize.STRING, bio: Sequelize.TEXT })
beforeEach(function() {
Helpers.async(function(done) {
sequelize.sync({force: true}).on('success', done).on('failure', function(err) { console.log(err) })
})
})
afterEach(function() {
Helpers.async(function(done) {
sequelize.drop().on('success', done).on('failure', function(err) { console.log(err) })
})
})
//////////// all //////////////
describe('.all', function() {
beforeEach(function() {
var UserFactory = function(options, callback, count) {
count = count || 1
User.create(options).on('success', function(user){
--count ? UserFactory(options, callback, count) : callback(user)
}).on('failure', function(err) {
console.log(err)
})
}
Helpers.async(function(done) { UserFactory({name: 'user', bio: 'foobar'}, done, 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) })
})
})
})
/////////// create ////////////
describe('.create with options', function() {
var Person = sequelize.define('Person', { name: Sequelize.STRING, options: Sequelize.TEXT })
it('should allow the creation of an object with options as attribute', function() {
Helpers.async(function(done) {
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
Person.create({name: 'John Doe', options: options}).on('success', function(person) {
expect(person.options).toEqual(options)
done()
}).on('failure', function(err) { console.log(err) })
})
})
})
/////////// many-to-many with same prefix ////////////
describe('many-to-many', function() {
describe('where tables have the same prefix', function() {
var Table2 = sequelize.define('wp_table2', {foo: Sequelize.STRING})
, Table1 = sequelize.define('wp_table1', {foo: Sequelize.STRING})
Table1.hasMany(Table2)
Table2.hasMany(Table1)
it("should create a table wp_table1wp_table2s", function() {
var models = sequelize.modelManager.models.filter(function(model) {
return model.tableName.indexOf('wp_table1swp_table2s') > -1
})
expect(models.length).toBe(1)
})
})
})
})