Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Jan 17, 2025
1 parent 91f7afa commit 4ff593f
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 85 deletions.
12 changes: 6 additions & 6 deletions src/__tests__/search.bsons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ test('names', () => {
('Alice', 2000),
('Bob', 1990);
*/
//expect(search('name:$e,n', db)).toHaveLength(2);
//expect(search('year:1990,2000 name:$e,n', db)).toHaveLength(2);
// expect(search('bson.year:1990', db)).toHaveLength(2);
//expect(search('bson.name:$e', db)).toHaveLength(2);
expect(search('bson.name:$e,n', db)).toHaveLength(2);
//expect(search('bson.year:1990,2000 bson.name:$e,n', db)).toHaveLength(2);
expect(search('name:$e,n', db)).toHaveLength(3);
expect(search('year:1990,2000 name:$e,n', db)).toHaveLength(2);
expect(search('bson.year:1990', db)).toHaveLength(2);
expect(search('bson.name:$e', db)).toHaveLength(2);
expect(search('bson.name:$e,n', db)).toHaveLength(3);
expect(search('bson.year:1990,2000 bson.name:$e,n', db)).toHaveLength(2);
});
15 changes: 15 additions & 0 deletions src/getSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Database } from 'better-sqlite3';

import type { Schema } from './types/Schema';
import type { TableInfo } from './types/TableInfo';

export function getSchema(db: Database, tableName: string): Schema {
const stmt = db.prepare(`PRAGMA table_info(${tableName})`);
const tableInfo = stmt.all() as TableInfo[];

const schema: Schema = {};
for (const column of tableInfo) {
schema[column.name] = column;
}
return schema;
}
16 changes: 1 addition & 15 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Database } from 'better-sqlite3';
import type { Logger } from 'cheminfo-types';

import type { Schema } from './types/Schema';
import type { TableInfo } from './types/TableInfo';
import { getSchema } from './getSchema';
import { appendSQLForCriteria } from './utils/appendSQLForCriteria';
import { parseQueryString } from './utils/parseQueryString';

Expand Down Expand Up @@ -78,8 +77,6 @@ export function search(
//console.log(values);

logger?.debug(values, `SQL statement: ${sqls.join(' ')}`);
console.log(sqls);
console.log(values);
const stmt = db.prepare(sqls.join(' '));
return stmt.all(values) as Entry[];
}
Expand Down Expand Up @@ -107,14 +104,3 @@ function getTableName(db: Database): string {
}
return tables[0].name;
}

function getSchema(db: Database, tableName: string): Schema {
const stmt = db.prepare(`PRAGMA table_info(${tableName})`);
const tableInfo = stmt.all() as TableInfo[];

const schema: Schema = {};
for (const column of tableInfo) {
schema[column.name] = column;
}
return schema;
}
2 changes: 1 addition & 1 deletion src/types/QueryCriterium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface QueryCriterium {
parameters: Parameter[];
operator: string;
values: string[];
valuesType?: 'number' | 'string' | 'boolean';
valuesType: string;
negate: boolean;
sql?: string;
}
19 changes: 16 additions & 3 deletions src/utils/__tests__/parseQueryString.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { test, expect } from 'vitest';

import { getDB } from '../../__tests__/utils/getDB';
import { getSchema } from '../../getSchema';
import { parseQueryString } from '../parseQueryString';

test('parseQueryString', () => {
const result = parseQueryString('name:abc');
const db = getDB();
const schema = getSchema(db, 'entries');
const result = parseQueryString('textColumn:abc', schema);
expect(result).toStrictEqual([
{
fields: ['name'],
parameters: [
{
column: 'textColumn',
type: 'TEXT',
jpath: 'textColumn',
accessor: 'textColumn',
isJpath: false,
},
],
operator: '',
index: 0,
values: ['abc'],
valuesType: 'string',
negate: false,
index: 0,
},
]);
});
2 changes: 1 addition & 1 deletion src/utils/appendSQLForCriteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function buildSQL(
break;
default:
throw new Error(
`Type ${column.type} is not supported. Consider using STRICT mode to avoid issues. The following types are supported: TEXT, REAL, INTEGER, BOOLEAN`,
`Type ${parameter.type} is not supported. Consider using STRICT mode to avoid issues. The following types are supported: TEXT, REAL, INTEGER, BOOLEAN`,
);
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/utils/parseQueryString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { parseString } from 'dynamic-typing';

import type { Parameter, QueryCriterium } from '../types/QueryCriterium';
import type { Schema } from '../types/Schema';

import { splitString } from './splitString';
import { trimQuotes } from './trimQuotes';

interface ParseQueryStringOptions {
fieldsAliases?: Record<string, string[]>;
Expand Down Expand Up @@ -59,17 +60,17 @@ export function parseQueryString(
operator = '..';
}

const values = splitString(token, { delimiter: ',' });
token = trimQuotes(token);
const { values, valuesType } = getValuesInfo(token);
queryCriteria.push({
parameters,
operator,
values,
valuesType,
negate,
index: index++,
});
}
// if no values
// if no values we remove the criterium
return queryCriteria.filter((criterium) => criterium.values.length > 0);
}

Expand Down Expand Up @@ -117,3 +118,25 @@ function getParameters(

return parameters;
}

function getValuesInfo(string: string) {
const values = splitString(string, { delimiter: ',' });

// need to deal with the special operator '..' for numbers range
const allValues = values.flatMap((value) => value.split('..'));

const valuesKinds = allValues.map((value) => typeof parseString(value));
if (valuesKinds.every((kind) => kind === 'boolean')) {
return {
values,
valuesType: 'boolean',
};
}
if (valuesKinds.every((kind) => kind === 'number')) {
return {
values,
valuesType: 'number',
};
}
return { values, valuesType: 'string' };
}
60 changes: 9 additions & 51 deletions src/utils/processBlob.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { parseString } from 'dynamic-typing';

import type { Parameter, QueryCriterium } from '../types/QueryCriterium';

import type {
Expand All @@ -17,54 +15,14 @@ export function processBlob(
options: AppendSQLForCriteriaOptions,
) {
const { logger } = options;
const joinOperator = 'OR';

const sqls = [];
for (let valueIndex = 0; valueIndex < criterium.values.length; valueIndex++) {
const value = parseString(criterium.values[valueIndex]);

let sql;
switch (typeof value) {
case 'string':
sql = processText(
parameter,
getCriteriumWithOneValue(criterium, valueIndex),
values,
);
break;
case 'number':
sql = processNumber(
parameter,
getCriteriumWithOneValue(criterium, valueIndex),
values,
options,
);
break;
case 'boolean':
sql = processBoolean(
parameter,
getCriteriumWithOneValue(criterium, valueIndex),
values,
options,
);
break;
default:
logger?.info(`Invalid value for BLOB: ${criterium.values[valueIndex]}`);
continue;
}
if (!sql) {
continue;
}
sqls.push(sql);
switch (criterium.valuesType) {
case 'string':
return processText(parameter, criterium, values);
case 'number':
return processNumber(parameter, criterium, values, options);
case 'boolean':
return processBoolean(parameter, criterium, values, options);
default:
logger?.info(`Invalid values for BLOB: ${criterium.values.toString()}`);
}
if (sqls.length === 0) {
return '';
}
return `(${sqls.join(` ${joinOperator} `)})`;
}
function getCriteriumWithOneValue(criterium: QueryCriterium, index: number) {
return {
...criterium,
values: [criterium.values[index]],
};
}
1 change: 0 additions & 1 deletion src/utils/processText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export function processText(
criterium: QueryCriterium,
values: Values,
) {
console.log(criterium.values);
const operator = criterium.operator || '^';
const sqls = [];
for (let valueIndex = 0; valueIndex < criterium.values.length; valueIndex++) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/splitString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { trimQuotes } from './trimQuotes';

/**
* Split a string by a delimiter, but ignore delimiters inside quotes.
* @param string
* @param options
* @param options.delimiter
* @param string - The string to split
* @param [options={}] - Options
* @param [options.delimiter=/\s/] - The delimiter to split by
* @returns An array of strings
*/
export function splitString(
Expand Down

0 comments on commit 4ff593f

Please sign in to comment.