-
-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce typeCast for
execute
method (#2398)
* ci(bun): include `execute` to simple query tests * ci: include typeCast tests for `execute` * ci: include connection level and overwriting typeCast tests * feat: introduce typeCast for `execute` method * chore: remove typeCast comment warnings for `execute`
- Loading branch information
1 parent
15a8a57
commit baaa92a
Showing
11 changed files
with
497 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
test/integration/connection/test-type-cast-null-fields-execute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const connection = common.createConnection(); | ||
const assert = require('assert'); | ||
|
||
common.useTestDb(connection); | ||
|
||
const table = 'insert_test'; | ||
connection.execute( | ||
[ | ||
`CREATE TEMPORARY TABLE \`${table}\` (`, | ||
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', | ||
'`date` DATETIME NULL,', | ||
'`number` INT NULL,', | ||
'PRIMARY KEY (`id`)', | ||
') ENGINE=InnoDB DEFAULT CHARSET=utf8', | ||
].join('\n'), | ||
err => { | ||
if (err) throw err; | ||
}, | ||
); | ||
|
||
connection.execute( | ||
`INSERT INTO ${table} (date, number) VALUES (?, ?)`, | ||
[null, null], | ||
err => { | ||
if (err) throw err; | ||
}, | ||
); | ||
|
||
let results; | ||
connection.execute(`SELECT * FROM ${table}`, (err, _results) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
results = _results; | ||
connection.end(); | ||
}); | ||
|
||
process.on('exit', () => { | ||
assert.strictEqual(results[0].date, null); | ||
assert.strictEqual(results[0].number, null); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const driver = require('../../../index.js'); //needed to check driver.Types | ||
const connection = common.createConnection(); | ||
const assert = require('assert'); | ||
|
||
common.useTestDb(connection); | ||
|
||
connection.execute('select 1', waitConnectErr => { | ||
assert.ifError(waitConnectErr); | ||
|
||
const tests = require('./type-casting-tests')(connection); | ||
|
||
const table = 'type_casting'; | ||
|
||
const schema = []; | ||
const inserts = []; | ||
|
||
tests.forEach((test, index) => { | ||
const escaped = test.insertRaw || connection.escape(test.insert); | ||
|
||
test.columnName = `${test.type}_${index}`; | ||
|
||
schema.push(`\`${test.columnName}\` ${test.type},`); | ||
inserts.push(`\`${test.columnName}\` = ${escaped}`); | ||
}); | ||
|
||
const createTable = [ | ||
`CREATE TEMPORARY TABLE \`${table}\` (`, | ||
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', | ||
] | ||
.concat(schema) | ||
.concat(['PRIMARY KEY (`id`)', ') ENGINE=InnoDB DEFAULT CHARSET=utf8']) | ||
.join('\n'); | ||
|
||
connection.execute(createTable); | ||
|
||
connection.execute(`INSERT INTO ${table} SET ${inserts.join(',\n')}`); | ||
|
||
let row; | ||
let fieldData; // to lookup field types | ||
connection.execute(`SELECT * FROM ${table}`, (err, rows, fields) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
row = rows[0]; | ||
// build a fieldName: fieldType lookup table | ||
fieldData = fields.reduce((a, v) => { | ||
a[v['name']] = v['type']; | ||
return a; | ||
}, {}); | ||
connection.end(); | ||
}); | ||
|
||
process.on('exit', () => { | ||
tests.forEach(test => { | ||
// check that the column type matches the type name stored in driver.Types | ||
const columnType = fieldData[test.columnName]; | ||
assert.equal( | ||
test.columnType === driver.Types[columnType], | ||
true, | ||
test.columnName, | ||
); | ||
let expected = test.expect || test.insert; | ||
let got = row[test.columnName]; | ||
let message; | ||
|
||
if (expected instanceof Date) { | ||
assert.equal(got instanceof Date, true, test.type); | ||
|
||
expected = String(expected); | ||
got = String(got); | ||
} else if (Buffer.isBuffer(expected)) { | ||
assert.equal(Buffer.isBuffer(got), true, test.type); | ||
|
||
expected = String(Array.prototype.slice.call(expected)); | ||
got = String(Array.prototype.slice.call(got)); | ||
} | ||
|
||
if (test.deep) { | ||
message = `got: "${JSON.stringify(got)}" expected: "${JSON.stringify( | ||
expected, | ||
)}" test: ${test.type}`; | ||
assert.deepEqual(expected, got, message); | ||
} else { | ||
message = `got: "${got}" (${typeof got}) expected: "${expected}" (${typeof expected}) test: ${ | ||
test.type | ||
}`; | ||
assert.strictEqual(expected, got, message); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.