forked from loopbackio/loopback-connector-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql.dbdefaults.test.js
104 lines (93 loc) · 2.83 KB
/
postgresql.dbdefaults.test.js
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
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
const should = require('should'),
assert = require('assert');
let InvalidDefault, Post, db;
describe('database default field values', function() {
before(function() {
db = global.getDataSource();
Post = db.define('PostWithDbDefaultValue', {
created: {
type: 'Date',
postgresql: {
dbDefault: 'now()',
},
},
defaultInt: {
type: 'Number',
postgresql: {
dbDefault: '5',
},
},
oneMore: {
type: 'Number',
},
});
InvalidDefault = db.define('PostWithInvalidDbDefaultValue', {
created: {
type: 'Date',
postgresql: {
dbDefault: '\'5\'',
},
},
});
});
it('should run migration', function(done) {
db.automigrate('PostWithDbDefaultValue', function() {
done();
});
});
it('should report inconsistent default values used', function(done) {
db.automigrate('PostWithInvalidDbDefaultValue', function(err) {
should.exists(err);
// XXX(kjdelisle): The InvalidDefaults test is polluting the default date
// types of the other tests!
delete db.connector._models.PostWithInvalidDbDefaultValue;
done();
});
});
it('should have \'now()\' default value in SQL column definition',
function(done) {
const query = 'select column_name, data_type, character_maximum_length,' +
' column_default' +
' from information_schema.columns' +
" where table_name = 'postwithdbdefaultvalue'" +
" and column_name='created'";
function verifyColumnDefault() {
db.connector.execute(query, [], function(err, results) {
assert.equal(results[0].column_default, 'now()');
done(err);
});
}
if (db.connected) {
verifyColumnDefault();
} else {
db.once('connected', verifyColumnDefault);
}
});
it('should create a record with default value', function(done) {
Post.create({oneMore: 3}, function(err, p) {
should.not.exists(err);
Post.findOne({where: {defaultInt: 5}}, function(err, p) {
should.not.exists(err);
should.exists(p);
p.should.have.property('defaultInt', 5);
done();
});
});
});
it('should create a record with custom value', function(done) {
Post.create({oneMore: 2, defaultInt: 6}, function(err, p) {
should.not.exists(err);
Post.findOne({where: {defaultInt: 6}}, function(err, p) {
should.not.exists(err);
should.exists(p);
p.should.have.property('defaultInt', 6);
done();
});
});
});
});