Skip to content

Commit e5b9ea3

Browse files
committed
Fix base tests; Add test stub for foreign key constraint in createTable;
1 parent bf2e7c4 commit e5b9ea3

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

test/foreign_key_batch.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const dbmeta = require('db-meta')
2+
const dataType = require('db-migrate-shared').dataType
3+
const fs = require('fs')
4+
5+
module.exports = (driver, config, internals) => ({
6+
'columnForeignKeySpec': {
7+
topic: function () {
8+
driver.connect(config, internals, function (err, db) {
9+
db.createTable('event_type', {
10+
11+
id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
12+
title: { type: dataType.STRING }
13+
}, function(err) {
14+
if (err) {
15+
return this.callback(err);
16+
}
17+
db.createTable('event', {
18+
id: {
19+
type: dataType.INTEGER,
20+
primaryKey: true,
21+
autoIncrement: true
22+
},
23+
event_id: {
24+
type: dataType.INTEGER,
25+
notNull: true,
26+
foreignKey: {
27+
name: 'fk_event_event_type',
28+
table: 'event_type',
29+
mapping: 'id',
30+
rules: {
31+
onDelete: 'CASCADE'
32+
},
33+
} },
34+
title: {
35+
type: dataType.STRING
36+
}
37+
}, this.callback.bind(this, null, db));
38+
}.bind(this));
39+
}.bind(this))
40+
},
41+
42+
teardown: function(db) {
43+
db.close(function (err) {
44+
fs.unlink(config.filename, this.callback);
45+
}.bind(this));
46+
},
47+
48+
'sets usage and constraints': {
49+
topic: function (db) {
50+
dbmeta('sqlite3', {connection: db.connection}, function (err, meta) {
51+
if (err) {
52+
return this.callback(err);
53+
}
54+
meta.getColumns('event', this.callback);
55+
}.bind(this));
56+
},
57+
'that has foreign key column with the expected reference': function (err, columns) {
58+
console.log(err, columns)
59+
/*var column = findByName(columns, 'id');
60+
assert.equal(column.getDataType(), 'INTEGER');
61+
assert.equal(column.isPrimaryKey(), true);
62+
assert.equal(column.isNullable(), false);
63+
assert.equal(column.isAutoIncrementing(), true);*/
64+
}
65+
66+
}
67+
}
68+
})

test/sqlite3_test.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var driver = require('../');
88

99
var config = require('./db.config.json').sqlite3;
1010

11+
var foreignKeyBatch = require('./foreign_key_batch')
12+
1113
var internals = {};
1214
internals.mod = {
1315
log: log,
@@ -22,7 +24,9 @@ internals.migrationTable = 'migrations';
2224
vows.describe('sqlite3').addBatch({
2325
'createTable': {
2426
topic: function () {
27+
console.log("cc")
2528
driver.connect(config, internals, function (err, db) {
29+
console.log("cc_cc")
2630
db.createTable('event', {
2731
id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true, notNull: true },
2832
str: { type: dataType.STRING, unique: true },
@@ -36,9 +40,10 @@ vows.describe('sqlite3').addBatch({
3640
},
3741

3842
teardown: function (db) {
43+
console.log('tt')
3944
db.close(function (err) {
4045
fs.unlink(config.filename, this.callback);
41-
});
46+
}.bind(this));
4247
},
4348

4449
'has resulting table metadata': {
@@ -138,7 +143,7 @@ vows.describe('sqlite3').addBatch({
138143
teardown: function (db) {
139144
db.close(function (err) {
140145
fs.unlink(config.filename, this.callback);
141-
});
146+
}.bind(this));
142147
},
143148

144149
'has table metadata': {
@@ -172,7 +177,7 @@ vows.describe('sqlite3').addBatch({
172177
teardown: function (db) {
173178
db.close(function (err) {
174179
fs.unlink(config.filename, this.callback);
175-
});
180+
}.bind(this));
176181
},
177182

178183
'has table metadata': {
@@ -208,7 +213,7 @@ vows.describe('sqlite3').addBatch({
208213
teardown: function (db) {
209214
db.close(function (err) {
210215
fs.unlink(config.filename, this.callback);
211-
});
216+
}.bind(this));
212217
},
213218

214219
'has column metadata': {
@@ -249,7 +254,7 @@ vows.describe('sqlite3').addBatch({
249254
teardown: function (db) {
250255
db.close(function (err) {
251256
fs.unlink(config.filename, this.callback);
252-
});
257+
}.bind(this));
253258
},
254259

255260
'has resulting index metadata': {
@@ -287,7 +292,7 @@ vows.describe('sqlite3').addBatch({
287292
teardown: function (db) {
288293
db.close(function (err) {
289294
fs.unlink(config.filename, this.callback);
290-
});
295+
}.bind(this));
291296
},
292297

293298
'with additional row': function (db) {
@@ -312,7 +317,7 @@ vows.describe('sqlite3').addBatch({
312317
teardown: function (db) {
313318
db.close(function (err) {
314319
fs.unlink(config.filename, this.callback);
315-
});
320+
}.bind(this));
316321
},
317322

318323
'with additional row': function (db) {
@@ -339,7 +344,7 @@ vows.describe('sqlite3').addBatch({
339344
teardown: function (db) {
340345
db.close(function (err) {
341346
fs.unlink(config.filename, this.callback);
342-
});
347+
}.bind(this));
343348
},
344349

345350
'has resulting index metadata': {
@@ -376,7 +381,7 @@ vows.describe('sqlite3').addBatch({
376381
teardown: function (db) {
377382
db.close(function (err) {
378383
fs.unlink(config.filename, this.callback);
379-
});
384+
}.bind(this));
380385
},
381386

382387
'has resulting index metadata': {
@@ -406,7 +411,7 @@ vows.describe('sqlite3').addBatch({
406411
teardown: function (db) {
407412
db.close(function (err) {
408413
fs.unlink(config.filename, this.callback);
409-
});
414+
}.bind(this));
410415
},
411416

412417
'has migrations table': {
@@ -452,7 +457,8 @@ vows.describe('sqlite3').addBatch({
452457
}
453458
}
454459
}
455-
}).export(module);
460+
}).addBatch( foreignKeyBatch(driver, config, internals) )
461+
.export(module);
456462

457463
function findByName(columns, name) {
458464
for (var i = 0; i < columns.length; i++) {

0 commit comments

Comments
 (0)