From 75aeb12e81d5625267a5a7b90720034a3d8317aa Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 22 Apr 2025 18:24:26 +0000 Subject: [PATCH] fix(lucene adapter and search tests): Improve range query parsing in Lucene adapter and expand search test coverage --- changelog.md | 8 ++++++++ package.json | 1 + test/test.search.advanced.ts | 15 +++++++++++++++ test/test.search.ts | 20 ++++++++++++++++++++ ts/00_commitinfo_data.ts | 2 +- ts/classes.lucene.adapter.ts | 4 ++-- 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index b710197..cb8d130 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,13 @@ # Changelog +## 2025-04-22 - 5.11.3 - fix(lucene adapter and search tests) +Improve range query parsing in Lucene adapter and expand search test coverage + +- Added a new 'testSearch' script in package.json to run search tests. +- Introduced advanced search tests for range queries and combined field filters in test/search.advanced.ts. +- Enhanced robustness tests in test/search.ts for wildcard and empty query scenarios. +- Fixed token validation in the parseRange method of the Lucene adapter to ensure proper error handling. + ## 2025-04-21 - 5.11.2 - fix(readme) Update readme to clarify usage of searchable fields retrieval diff --git a/package.json b/package.json index 5a71601..7857997 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "type": "module", "scripts": { "test": "tstest test/", + "testSearch": "tsx test/test.search.ts", "build": "tsbuild --web --allowimplicitany", "buildDocs": "tsdoc" }, diff --git a/test/test.search.advanced.ts b/test/test.search.advanced.ts index 8d8b3b4..e450679 100644 --- a/test/test.search.advanced.ts +++ b/test/test.search.advanced.ts @@ -172,6 +172,21 @@ tap.test('grouping: (Furniture OR Electronics) AND Chair', async () => { expect(names).toEqual(['Gaming Chair', 'Office Chair']); }); +// Additional range and combined query tests +tap.test('range query price:[30 TO 300] returns expected products', async () => { + const res = await Product.search('price:[30 TO 300]'); + // Expect products with price between 30 and 300 inclusive: Day Light Lamp, Gaming Chair, Office Chair, AirPods + expect(res.length).toEqual(4); + const names = res.map((r) => r.name).sort(); + expect(names).toEqual(['AirPods', 'Day Light Lamp', 'Gaming Chair', 'Office Chair']); +}); + +tap.test('should filter category and price range', async () => { + const res = await Product.search('category:Lighting AND price:[30 TO 40]'); + expect(res.length).toEqual(1); + expect(res[0].name).toEqual('Day Light Lamp'); +}); + // Teardown tap.test('cleanup advanced search database', async () => { await testDb.mongoDb.dropDatabase(); diff --git a/test/test.search.ts b/test/test.search.ts index 4309619..936a371 100644 --- a/test/test.search.ts +++ b/test/test.search.ts @@ -257,6 +257,26 @@ tap.test('should search hyphenated terms "E-reader"', async () => { expect(ereaderResults[0].name).toEqual('Kindle Paperwhite'); }); +// Additional robustness tests +tap.test('should return all products for empty search', async () => { + const searchResults = await Product.search(''); + const allProducts = await Product.getInstances({}); + expect(searchResults.length).toEqual(allProducts.length); +}); + +tap.test('should support wildcard plain term across all fields', async () => { + const results = await Product.search('*book*'); + const names = results.map((r) => r.name).sort(); + expect(names).toEqual(['Harry Potter', 'Kindle Paperwhite', 'MacBook Pro']); +}); + +tap.test('should support wildcard plain term with question mark pattern', async () => { + const results = await Product.search('?one?'); + const names = results.map((r) => r.name).sort(); + expect(names).toEqual(['Galaxy S21', 'iPhone 12']); +}); + +// Close database connection tap.test('close database connection', async () => { await testDb.mongoDb.dropDatabase(); await testDb.close(); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index efc710e..4bdda03 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartdata', - version: '5.11.2', + version: '5.11.3', description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.' } diff --git a/ts/classes.lucene.adapter.ts b/ts/classes.lucene.adapter.ts index 8436071..5552db8 100644 --- a/ts/classes.lucene.adapter.ts +++ b/ts/classes.lucene.adapter.ts @@ -290,11 +290,11 @@ export class LuceneParser { const includeLower = this.tokens[this.pos] === '['; const includeUpper = this.tokens[this.pos + 4] === ']'; - this.pos++; // Skip open bracket - + // Ensure tokens for lower, TO, upper, and closing bracket exist if (this.pos + 4 >= this.tokens.length) { throw new Error('Invalid range query syntax'); } + this.pos++; // Skip open bracket const lower = this.tokens[this.pos]; this.pos++;