datatypes.test.js
4.39 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
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-connector-mysql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
require('./init.js');
var assert = require('assert');
var db, EnumModel, ANIMAL_ENUM;
var mysqlVersion;
describe('MySQL specific datatypes', function() {
before(setup);
it('should run migration', function(done) {
db.automigrate(function() {
done();
});
});
it('An enum should parse itself', function(done) {
assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM('cat'));
assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM('CAT'));
assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM(2));
assert.equal(ANIMAL_ENUM.CAT, 'cat');
assert.equal(ANIMAL_ENUM(null), null);
assert.equal(ANIMAL_ENUM(''), '');
assert.equal(ANIMAL_ENUM(0), '');
done();
});
it('should create a model instance with Enums', function(done) {
var em = EnumModel.create({animal: ANIMAL_ENUM.CAT, condition: 'sleepy', mood: 'happy'}, function(err, obj) {
assert.ok(!err);
assert.equal(obj.condition, 'sleepy');
EnumModel.findOne({where: {animal: ANIMAL_ENUM.CAT}}, function(err, found) {
assert.ok(!err);
assert.equal(found.mood, 'happy');
assert.equal(found.animal, ANIMAL_ENUM.CAT);
done();
});
});
});
it('should fail spectacularly with invalid enum values', function(done) {
// In MySQL 5.6/5.7, An ENUM value must be one of those listed in the column definition,
// or the internal numeric equivalent thereof. Invalid values are rejected.
// Reference: http://dev.mysql.com/doc/refman/5.7/en/constraint-enum.html
EnumModel.create({animal: 'horse', condition: 'sleepy', mood: 'happy'}, function(err, obj) {
assert.ok(err);
assert.equal(err.code, 'WARN_DATA_TRUNCATED');
assert.equal(err.errno, 1265);
done();
});
});
it('should create a model instance with object/json types', function(done) {
var note = {a: 1, b: '2'};
var extras = {c: 3, d: '4'};
var em = EnumModel.create({animal: ANIMAL_ENUM.DOG, condition: 'sleepy',
mood: 'happy', note: note, extras: extras}, function(err, obj) {
assert.ok(!err);
assert.equal(obj.condition, 'sleepy');
EnumModel.findOne({where: {animal: ANIMAL_ENUM.DOG}}, function(err, found) {
assert.ok(!err);
assert.equal(found.mood, 'happy');
assert.equal(found.animal, ANIMAL_ENUM.DOG);
assert.deepEqual(found.note, note);
assert.deepEqual(found.extras, extras);
done();
});
});
});
it('should disconnect when done', function(done) {
db.disconnect();
done();
});
});
function setup(done) {
require('./init.js');
db = getSchema();
ANIMAL_ENUM = db.EnumFactory('dog', 'cat', 'mouse');
EnumModel = db.define('EnumModel', {
animal: {type: ANIMAL_ENUM, null: false},
condition: {type: db.EnumFactory('hungry', 'sleepy', 'thirsty')},
mood: {type: db.EnumFactory('angry', 'happy', 'sad')},
note: Object,
extras: 'JSON',
});
query('SELECT VERSION()', function(err, res) {
mysqlVersion = res && res[0] && res[0]['VERSION()'];
blankDatabase(db, done);
});
}
var query = function(sql, cb) {
db.adapter.execute(sql, cb);
};
var blankDatabase = function(db, cb) {
var dbn = db.settings.database;
var cs = db.settings.charset;
var co = db.settings.collation;
query('DROP DATABASE IF EXISTS ' + dbn, function(err) {
var q = 'CREATE DATABASE ' + dbn;
if (cs) {
q += ' CHARACTER SET ' + cs;
}
if (co) {
q += ' COLLATE ' + co;
}
query(q, function(err) {
query('USE ' + dbn, cb);
});
});
};
var getFields = function(model, cb) {
query('SHOW FIELDS FROM ' + model, function(err, res) {
if (err) {
cb(err);
} else {
var fields = {};
res.forEach(function(field) {
fields[field.Field] = field;
});
cb(err, fields);
}
});
};
var getIndexes = function(model, cb) {
query('SHOW INDEXES FROM ' + model, function(err, res) {
if (err) {
console.log(err);
cb(err);
} else {
var indexes = {};
// Note: this will only show the first key of compound keys
res.forEach(function(index) {
if (parseInt(index.Seq_in_index, 10) == 1) {
indexes[index.Key_name] = index;
}
});
cb(err, indexes);
}
});
};