|
| 1 | +import { test, assert, describe } from 'poku'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | + |
| 4 | +const require = createRequire(import.meta.url); |
| 5 | +const common = require('../../../common.test.cjs'); |
| 6 | + |
| 7 | +const sql = `SELECT TO_VECTOR("[1.05, -17.8, 32, 123.456]") as test`; |
| 8 | +const expectedArray = [1.05, -17.8, 32, 123.456]; |
| 9 | +const epsilon = 1e-6; |
| 10 | + |
| 11 | +const compareFloat = (a, b) => Math.abs((a - b) / a) < epsilon; |
| 12 | +const compareFLoatsArray = (a, b) => a.every((v, i) => compareFloat(v, b[i])); |
| 13 | + |
| 14 | +(async () => { |
| 15 | + const connection = common.createConnection().promise(); |
| 16 | + |
| 17 | + const mySqlVersion = await common.getMysqlVersion(connection); |
| 18 | + |
| 19 | + if (mySqlVersion.major < 9) { |
| 20 | + console.log( |
| 21 | + `Skipping the test, required mysql version is 9 and above, actual version is ${mySqlVersion.major}`, |
| 22 | + ); |
| 23 | + await connection.end(); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + await test(async () => { |
| 28 | + describe( |
| 29 | + 'Execute PS with vector response is parsed correctly', |
| 30 | + common.describeOptions, |
| 31 | + ); |
| 32 | + |
| 33 | + const [_rows] = await connection.execute(sql); |
| 34 | + assert.equal( |
| 35 | + compareFLoatsArray(_rows[0].test, expectedArray), |
| 36 | + true, |
| 37 | + `${_rows[0].test} should be equal to ${expectedArray}`, |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + await test(async () => { |
| 42 | + describe( |
| 43 | + 'Select returning vector is parsed correctly', |
| 44 | + common.describeOptions, |
| 45 | + ); |
| 46 | + |
| 47 | + const [_rows] = await connection.query(sql); |
| 48 | + assert.equal( |
| 49 | + compareFLoatsArray(_rows[0].test, expectedArray), |
| 50 | + true, |
| 51 | + `${_rows[0].test} should be equal to ${expectedArray}`, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + await connection.end(); |
| 56 | +})(); |
0 commit comments