buster-helpers.js
2.61 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
const Sequelize = require(__dirname + "/../index")
, DataTypes = require(__dirname + "/../lib/data-types")
, config = require(__dirname + "/config/config")
, fs = require('fs')
var BusterHelpers = module.exports = {
Sequelize: Sequelize,
initTests: function(options) {
var sequelize = this.createSequelizeInstance(options)
this.clearDatabase(sequelize, function() {
if (options.context) {
options.context.sequelize = sequelize
}
if (options.beforeComplete) {
options.beforeComplete(sequelize, DataTypes)
}
if (options.onComplete) {
options.onComplete(sequelize, DataTypes)
}
})
},
createSequelizeInstance: function(options) {
options = options || {}
options.dialect = options.dialect || 'mysql'
options.logging = (options.hasOwnProperty('logging') ? options.logging : false)
var sequelizeOptions = {
logging: options.logging,
dialect: options.dialect,
port: config[options.dialect].port
}
if (process.env.DIALECT === 'postgres-native') {
sequelizeOptions.native = true
}
return new Sequelize(
config[options.dialect].database,
config[options.dialect].username,
config[options.dialect].password,
sequelizeOptions
)
},
clearDatabase: function(sequelize, callback) {
sequelize
.getQueryInterface()
.dropAllTables()
.success(function() {
sequelize.daoFactoryManager.daos = []
callback && callback()
})
.error(function(err) { console.log(err) })
},
getSupportedDialects: function() {
return fs.readdirSync(__dirname + '/../lib/dialects').filter(function(file) {
return ((file.indexOf('.js') === -1) && (file.indexOf('abstract') === -1))
})
},
getTestDialect: function() {
var envDialect = process.env.DIALECT || 'mysql'
if (envDialect === 'postgres-native') {
envDialect = 'postgres'
}
if (this.getSupportedDialects().indexOf(envDialect) === -1) {
throw new Error('The dialect you have passed is unknown. Did you really mean: ' + envDialect)
}
return envDialect
},
getTestDialectTeaser: function(moduleName) {
var dialect = this.getTestDialect()
if (process.env.DIALECT === 'postgres-native') {
dialect = 'postgres-native'
}
return "[" + dialect.toUpperCase() + "] " + moduleName
},
checkMatchForDialects: function(dialect, value, expectations) {
if (!!expectations[dialect]) {
expect(value).toMatch(expectations[dialect])
} else {
throw new Error('Undefined expectation for "' + dialect + '"!')
}
}
}