/
bigint.js
125 lines (118 loc) · 3.57 KB
/
bigint.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*global expect*/
'use strict';
module.exports = function(knex) {
const bigintTimestamp = 1464294366973;
const negativeBigintTimestamp = -1464294366973;
const unsafeBigint = 99071992547409911;
const negativeUnsafeBigint = -99071992547409911;
it('#test number mssql should not allow unsafe bigint', function() {
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const constraintName = 'pk_id';
const tableName = 'bigint_test';
return knex
.transaction(function(tr) {
return tr.schema.dropTableIfExists(tableName).then(function() {
return tr.schema.createTable(tableName, function(table) {
table.string('id').primary(constraintName);
table.bigInteger('expiry');
});
});
})
.then(function() {
return knex(tableName)
.where('expiry', unsafeBigint)
.select('*');
})
.then(function(row) {
// triggers request execution
})
.then(function() {
return knex(tableName)
.where('expiry', negativeUnsafeBigint)
.select('*');
})
.then(function(row) {
// triggers request execution
})
.catch(function(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.contain(
'Bigint must be safe integer or must be passed as string'
);
});
});
it('#test number mssql should allow safe bigint', function() {
if (!/mssql/i.test(knex.client.dialect)) {
return Promise.resolve();
}
const constraintName = 'pk_id';
const tableName = 'bigint_test';
return knex
.transaction(function(tr) {
return tr.schema.dropTableIfExists(tableName).then(function() {
return tr.schema.createTable(tableName, function(table) {
table.string('id').primary(constraintName);
table.bigInteger('expiry');
});
});
})
.then(function() {
return knex(tableName).insert({
id: 'positive',
expiry: bigintTimestamp,
});
})
.then(function() {
return knex(tableName).insert({
id: 'negative',
expiry: negativeBigintTimestamp,
});
})
.then(function() {
return knex(tableName)
.where('expiry', bigintTimestamp)
.select('*');
})
.then(function(rows) {
rows.forEach((row) => expect(row.id).to.equal('positive'));
})
.then(function() {
return knex(tableName)
.where('expiry', negativeBigintTimestamp)
.select('*');
})
.then(function(rows) {
rows.forEach((row) => expect(row.id).to.equal('negative'));
})
.catch(function(err) {
expect(err).to.be.undefined;
});
});
it('#1781 - decimal value must not be converted to integer', function() {
if (!/mssql/i.test(knex.client.driverName)) {
return;
}
const tableName = 'decimal_test';
const value = 123.4567;
return knex
.transaction(function(tr) {
return tr.schema.dropTableIfExists(tableName).then(function() {
return tr.schema.createTable(tableName, function(table) {
table.increments();
table.decimal('value', 14, 4).notNullable();
});
});
})
.then(function() {
return knex(tableName).insert({ value: value });
})
.then(function() {
return knex(tableName).first('value');
})
.then(function(response) {
expect(response.value).to.be.eql(value);
});
});
};