From 7d7203edc86f7bdd7f96c0ecfb25d313b33e5309 Mon Sep 17 00:00:00 2001 From: Charly Abraham Date: Fri, 13 Dec 2024 18:51:10 +0530 Subject: [PATCH 1/3] fix: unable to index json filed named userId --- src/utils/sharedUtils.js | 6 +++--- test/unit/utils/db-test.spec.js | 4 ++-- test/unit/utils/query-test.spec.js | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/utils/sharedUtils.js b/src/utils/sharedUtils.js index 54d1496..b79a742 100644 --- a/src/utils/sharedUtils.js +++ b/src/utils/sharedUtils.js @@ -11,9 +11,9 @@ const VARIABLE_REGEX = /^[a-zA-Z_]\w*$/; // vars of form aA4_f allowed export function getColumNameForJsonField(jsonField) { // ignoring sonar security error as md5 function is not used for security // Md5 function is used here to increase the length of jsonfield to more than 64 characters - return crypto.createHash('md5').update(jsonField).digest('hex'); //NOSONAR + const hash = crypto.createHash('md5').update(jsonField).digest('hex'); //NOSONAR + return "col_" + hash; } - export function isAlphaNumChar(char) { char = char.charCodeAt(0); if (!(char > 47 && char < 58) && // numeric (0-9) @@ -69,4 +69,4 @@ export function isNestedVariableNameLike(nameToTest) { } } return true; -} \ No newline at end of file +} diff --git a/test/unit/utils/db-test.spec.js b/test/unit/utils/db-test.spec.js index af0e6ba..76c6ae3 100644 --- a/test/unit/utils/db-test.spec.js +++ b/test/unit/utils/db-test.spec.js @@ -928,7 +928,7 @@ describe('Unit tests for db.js', function () { const tableName = 'test.hello'; await deleteDocuments(tableName, "$.x<10 AND $.hotel.name='oyo'", ['hotel.name']); expect(savedSql).to.eql("DELETE FROM test.hello WHERE document->>\"$.x\"<10" + - " AND b978c733175ca5d9503b1cc095eece1f='oyo';"); + " AND col_b978c733175ca5d9503b1cc095eece1f='oyo';"); mockedFunctions.connection.execute = saveExecute; }); }); @@ -2566,7 +2566,7 @@ describe('Unit tests for db.js', function () { await _validateQueryPass("$.s<10 && !$.j.k || $.y!='hello'", ["s", "j.k"], "SELECT documentID,document FROM test.customer " + - "WHERE 03c7c0ace395d80182db07ae2c30f034<10 && !91e12519d4a93e0ce72a98c42383e747 " + + "WHERE col_03c7c0ace395d80182db07ae2c30f034<10 && !col_91e12519d4a93e0ce72a98c42383e747 " + "|| document->>\"$.y\"!='hello' LIMIT 1000"); }); diff --git a/test/unit/utils/query-test.spec.js b/test/unit/utils/query-test.spec.js index 937ca53..e82dff2 100644 --- a/test/unit/utils/query-test.spec.js +++ b/test/unit/utils/query-test.spec.js @@ -269,7 +269,8 @@ describe('Query Utils test', function () { it('should transformCocoToSQLQuery success cases with index field', function () { expect(Query.transformCocoToSQLQuery("$.y>5 || $.x > 10 && $.price.tax = 18", ["x", "price.tax"])) - .to.eql('document->>"$.y">5 || 9dd4e461268c8034f5c8564e155c67a6 > 10 && 6a1a731895e201b727851bc567ac1e8a = 18'); + // eslint-disable-next-line max-len + .to.eql('document->>"$.y">5 || col_9dd4e461268c8034f5c8564e155c67a6 > 10 && col_6a1a731895e201b727851bc567ac1e8a = 18'); }); it('should transformCocoToSQLQuery error cases', function () { From 077802a3ea1015fabc5ccaa4a0eb6b15d908551d Mon Sep 17 00:00:00 2001 From: Charly Abraham Date: Fri, 13 Dec 2024 18:59:04 +0530 Subject: [PATCH 2/3] test: adding integ test --- test/integration/libmysql-test.spec.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/integration/libmysql-test.spec.js b/test/integration/libmysql-test.spec.js index e562d35..4733108 100644 --- a/test/integration/libmysql-test.spec.js +++ b/test/integration/libmysql-test.spec.js @@ -404,6 +404,28 @@ describe('Integration: libMySql', function () { }); await deleteData(results); }); + it('create and validate Index should pass', async function () { + const numberOfEntries = 1000; + const results = await testReadWrite(numberOfEntries); + let isSuccess = await createIndexForJsonField(tableName, 'userId', DATA_TYPES.VARCHAR(50), + false); + expect(isSuccess).to.eql(true); + isSuccess = await createIndexForJsonField(tableName, 'Age', DATA_TYPES.INT, false); + expect(isSuccess).to.eql(true); + const queryResults = await getFromIndex(tableName, { + 'lastName': 'Alice', + 'Age': 100 + }); + expect(queryResults.length).to.eql(numberOfEntries); + queryResults.forEach(result => { + expect(result.lastName).to.eql('Alice'); + expect(results.userId).to.eql("ABC"); + expect(result.Age).to.eql(100); + expect(result.active).to.eql(true); + expect(result.documentId.length).gt(0); + }); + await deleteData(results); + }); it('create and validate Index return empty list if nothing matches', async function () { let isSuccess = await createIndexForJsonField(tableName, 'location.layout.block', DATA_TYPES.VARCHAR(50), @@ -786,6 +808,7 @@ async function testReadWrite(numberOfWrites) { async function populateTestTable(numberOfWrites) { const document = { 'lastName': 'Alice', + 'userId': "ABCD", 'Age': 100, 'active': true, 'location': { From a68c5713a9931bcce65f2a73737a9b23da8c4d95 Mon Sep 17 00:00:00 2001 From: Charly Abraham Date: Fri, 13 Dec 2024 19:03:00 +0530 Subject: [PATCH 3/3] fix: integ test for userId not working --- test/integration/libmysql-test.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/libmysql-test.spec.js b/test/integration/libmysql-test.spec.js index 4733108..14bc3e6 100644 --- a/test/integration/libmysql-test.spec.js +++ b/test/integration/libmysql-test.spec.js @@ -413,13 +413,13 @@ describe('Integration: libMySql', function () { isSuccess = await createIndexForJsonField(tableName, 'Age', DATA_TYPES.INT, false); expect(isSuccess).to.eql(true); const queryResults = await getFromIndex(tableName, { - 'lastName': 'Alice', + 'userId': 'ABCD', 'Age': 100 }); expect(queryResults.length).to.eql(numberOfEntries); queryResults.forEach(result => { expect(result.lastName).to.eql('Alice'); - expect(results.userId).to.eql("ABC"); + expect(results.userId).to.eql("ABCD"); expect(result.Age).to.eql(100); expect(result.active).to.eql(true); expect(result.documentId.length).gt(0);