build.test.js
4.86 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
var exec = require('child_process').exec;
var path = require('path');
var chai = require('chai');
var expect = chai.expect;
var helpers = require('./helpers');
var dialect = helpers.getTestDialect();
var testConfig = require('./config');
var _ = helpers.Sequelize.Utils._;
var lib = require('../index');
describe(helpers.getTestDialectTeaser("sequelize-auto build"), function() {
after(function(done) {
helpers.clearDatabase(this.sequelize, done);
});
before(function(done) {
var self = this
helpers.initTests({
dialect: dialect,
beforeComplete: function(sequelize) {
self.sequelize = sequelize
self.User = self.sequelize.define('User', {
username: { type: helpers.Sequelize.STRING },
touchedAt: { type: helpers.Sequelize.DATE, defaultValue: helpers.Sequelize.NOW },
aNumber: { type: helpers.Sequelize.INTEGER },
bNumber: { type: helpers.Sequelize.INTEGER },
validateTest: {
type: helpers.Sequelize.INTEGER,
allowNull: true
},
validateCustom: {
type: helpers.Sequelize.STRING,
allowNull: false
},
dateAllowNullTrue: {
type: helpers.Sequelize.DATE,
allowNull: true
},
defaultValueBoolean: {
type: helpers.Sequelize.BOOLEAN,
defaultValue: true
}
})
self.HistoryLog = self.sequelize.define('HistoryLog', {
someText: { type: helpers.Sequelize.STRING },
aNumber: { type: helpers.Sequelize.INTEGER },
aRandomId: { type: helpers.Sequelize.INTEGER }
})
self.ParanoidUser = self.sequelize.define('ParanoidUser', {
username: { type: helpers.Sequelize.STRING }
}, {
paranoid: true
})
self.ParanoidUser.belongsTo(self.User)
},
onComplete: function() {
self.sequelize.sync().then(function () {
done();
}, done);
}
});
});
var setupModels = function(self, callback) {
var options = _.extend({
spaces: true,
indentation: 2,
logging: false,
directory: testConfig.directory,
dialect: helpers.getTestDialect()
}, testConfig[helpers.getTestDialect()], self.sequelize.config);
var autoSequelize = new lib(self.sequelize.config.database, self.sequelize.config.username, self.sequelize.config.password, options);
autoSequelize.build(function (err) {
callback(err, autoSequelize);
});
}
describe("should be able to build", function() {
it("the models", function(done) {
var self = this;
setupModels(self, function(err, autoSequelize) {
expect(err).to.be.null;
expect(autoSequelize).to.include.keys(['tables', 'foreignKeys']);
expect(autoSequelize.tables).to.have.keys(['Users', 'HistoryLogs', 'ParanoidUsers']);
if (helpers.getTestDialect() === "sqlite") {
expect(autoSequelize.foreignKeys).to.have.keys(['ParanoidUsers']);
expect(autoSequelize.foreignKeys.ParanoidUsers).to.include.keys(['UserId']);
} else {
expect(autoSequelize.foreignKeys).to.have.keys(['Users', 'HistoryLogs', 'ParanoidUsers']);
expect(autoSequelize.foreignKeys.Users).to.include.keys('id');
expect(autoSequelize.foreignKeys.Users.id).to.include.keys(['source_schema', 'source_table', 'source_column', 'target_schema', 'target_table', 'target_column', 'isPrimaryKey', 'isSerialKey']);
expect(autoSequelize.foreignKeys.Users.id.isPrimaryKey).to.be.true;
expect(autoSequelize.foreignKeys.Users.id.isSerialKey).to.be.true;
expect(autoSequelize.foreignKeys.HistoryLogs).to.include.keys('id');
expect(autoSequelize.foreignKeys.HistoryLogs.id).to.include.keys(['source_schema', 'source_table', 'source_column', 'target_schema', 'target_table', 'target_column', 'isPrimaryKey', 'isSerialKey']);
expect(autoSequelize.foreignKeys.HistoryLogs.id.isPrimaryKey).to.be.true;
expect(autoSequelize.foreignKeys.HistoryLogs.id.isSerialKey).to.be.true;
expect(autoSequelize.foreignKeys.ParanoidUsers).to.include.keys(['id', 'UserId']);
expect(autoSequelize.foreignKeys.ParanoidUsers.id).to.include.keys(['source_schema', 'source_table', 'source_column', 'target_schema', 'target_table', 'target_column', 'isPrimaryKey', 'isSerialKey']);
expect(autoSequelize.foreignKeys.ParanoidUsers.id.isPrimaryKey).to.be.true;
expect(autoSequelize.foreignKeys.ParanoidUsers.id.isSerialKey).to.be.true;
}
expect(autoSequelize.foreignKeys.ParanoidUsers.UserId).to.include.keys(['source_schema', 'source_table', 'source_column', 'target_schema', 'target_table', 'target_column', 'isForeignKey']);
expect(autoSequelize.foreignKeys.ParanoidUsers.UserId.isForeignKey).to.be.true;
done();
});
});
});
});