diff --git a/packages/jsapi-utils/src/DateUtils.test.ts b/packages/jsapi-utils/src/DateUtils.test.ts index b2f20a90a..ecd844b05 100644 --- a/packages/jsapi-utils/src/DateUtils.test.ts +++ b/packages/jsapi-utils/src/DateUtils.test.ts @@ -43,6 +43,7 @@ describe('dateTimeString parsing tests', () => { minutes, seconds, nanos, + overflow, }: { year?: string; month?: string; @@ -51,9 +52,11 @@ describe('dateTimeString parsing tests', () => { minutes?: string; seconds?: string; nanos?: string; - } + overflow?: string; + }, + allowOverflow = false ) { - expect(DateUtils.parseDateTimeString(text)).toMatchObject({ + const expected = { year, month, date, @@ -61,7 +64,11 @@ describe('dateTimeString parsing tests', () => { minutes, seconds, nanos, - }); + }; + + expect(DateUtils.parseDateTimeString(text, allowOverflow)).toMatchObject( + allowOverflow ? { ...expected, overflow } : expected + ); } function testDateTimeStringThrows(text) { @@ -74,6 +81,15 @@ describe('dateTimeString parsing tests', () => { testDateTimeString('2012', { year: '2012', }); + + testDateTimeString( + '2012 overflow', + { + year: '2012', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm', () => { @@ -81,6 +97,16 @@ describe('dateTimeString parsing tests', () => { year: '2012', month: '04', }); + + testDateTimeString( + '2012-04 overflow', + { + year: '2012', + month: '04', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-dd', () => { @@ -89,6 +115,17 @@ describe('dateTimeString parsing tests', () => { month: '04', date: '20', }); + + testDateTimeString( + '2012-04-20 overflow', + { + year: '2012', + month: '04', + date: '20', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-ddTHH', () => { @@ -98,6 +135,18 @@ describe('dateTimeString parsing tests', () => { date: '20', hours: '12', }); + + testDateTimeString( + '2012-04-20T12 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-ddTHH:mm', () => { @@ -108,6 +157,19 @@ describe('dateTimeString parsing tests', () => { hours: '12', minutes: '13', }); + + testDateTimeString( + '2012-04-20T12:13 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-ddTHH:mm:ss', () => { @@ -119,6 +181,20 @@ describe('dateTimeString parsing tests', () => { minutes: '13', seconds: '14', }); + + testDateTimeString( + '2012-04-20T12:13:14 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + seconds: '14', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-ddTHH:mm:ss.SSS', () => { @@ -131,6 +207,21 @@ describe('dateTimeString parsing tests', () => { seconds: '14', nanos: '321', }); + + testDateTimeString( + '2012-04-20T12:13:14.321 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + seconds: '14', + nanos: '321', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-dd HH:mm:ss.SSSSSS', () => { testDateTimeString('2012-04-20 12:13:14.654321', { @@ -142,6 +233,21 @@ describe('dateTimeString parsing tests', () => { seconds: '14', nanos: '654321', }); + + testDateTimeString( + '2012-04-20 12:13:14.654321 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + seconds: '14', + nanos: '654321', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-dd HH:mm:ss.SSSSSS', () => { testDateTimeString('2012-04-20 12:13:14.654321', { @@ -153,6 +259,21 @@ describe('dateTimeString parsing tests', () => { seconds: '14', nanos: '654321', }); + + testDateTimeString( + '2012-04-20 12:13:14.654321 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + seconds: '14', + nanos: '654321', + overflow: ' overflow', + }, + true + ); }); it('handles YYYY-mm-dd HH:mm:ss.SSSSSSSSS', () => { testDateTimeString('2012-04-20 12:13:14.987654321', { @@ -164,6 +285,21 @@ describe('dateTimeString parsing tests', () => { seconds: '14', nanos: '987654321', }); + + testDateTimeString( + '2012-04-20 12:13:14.987654321 overflow', + { + year: '2012', + month: '04', + date: '20', + hours: '12', + minutes: '13', + seconds: '14', + nanos: '987654321', + overflow: ' overflow', + }, + true + ); }); it('throws an error for invalid dates', () => { @@ -312,3 +448,19 @@ describe('getJsDate', () => { expect(DateUtils.getJsDate(dateWrapper)).toEqual(dateWrapper.asDate()); }); }); + +describe('trimDateTimeStringOverflow', () => { + it.each([ + '2024', + '2012-04', + '2012-04-20', + '2012-04-20T12', + '2012-04-20T12:13', + '2012-04-20T12:13:14', + '2012-04-20T12:13:14.321', + ])('should trim date time string overflow: %s', expected => { + const given = `${expected} overflow`; + const actual = DateUtils.trimDateTimeStringOverflow(given); + expect(actual).toEqual(expected); + }); +}); diff --git a/packages/jsapi-utils/src/DateUtils.ts b/packages/jsapi-utils/src/DateUtils.ts index b413f4234..b1190c0e6 100644 --- a/packages/jsapi-utils/src/DateUtils.ts +++ b/packages/jsapi-utils/src/DateUtils.ts @@ -221,16 +221,6 @@ export class DateUtils { * string * @returns Containing the date time components */ - static parseDateTimeString( - dateTimeString: string, - allowOverflow?: false - ): DateParts; - - static parseDateTimeString( - dateTimeString: string, - allowOverflow: true - ): DateParts & { overflow?: string }; - static parseDateTimeString( dateTimeString: string, allowOverflow = false diff --git a/packages/jsapi-utils/src/TableUtils.test.ts b/packages/jsapi-utils/src/TableUtils.test.ts index d074f1e03..73f0927ff 100644 --- a/packages/jsapi-utils/src/TableUtils.test.ts +++ b/packages/jsapi-utils/src/TableUtils.test.ts @@ -618,6 +618,71 @@ describe('getValueType', () => { ); }); +describe('makeFilterValue', () => { + const mockDh = { + FilterValue: { + ofString: jest.fn(), + ofNumber: jest.fn(), + }, + LongWrapper: { + ofString: jest.fn(), + }, + } as unknown as typeof dh; + + const mockTableUtils = new TableUtils(mockDh); + + it.each(['char', 'java.lang.Character', 'java.lang.String'])( + 'should handle text type: %s', + columnType => { + const value = 'test'; + mockTableUtils.makeFilterValue(columnType, value); + expect(mockDh.FilterValue.ofString).toHaveBeenCalledWith(value); + } + ); + + it.each(['long', 'java.lang.Long'])( + 'should handle long type: %s', + columnType => { + const value = '1,000'; + mockTableUtils.makeFilterValue(columnType, value); + expect(mockDh.LongWrapper.ofString).toHaveBeenCalledWith('1000'); + } + ); + + it.each([ + 'io.deephaven.db.tables.utils.DBDateTime', + 'io.deephaven.time.DateTime', + 'java.time.Instant', + 'java.time.ZonedDateTime', + 'com.illumon.iris.db.tables.utils.DBDateTime', + ])('should handle date type: %s', columnType => { + const value = '2023-12-21'; + mockTableUtils.makeFilterValue(columnType, value); + expect(mockDh.FilterValue.ofNumber).toHaveBeenCalledWith( + DateUtils.trimDateTimeStringOverflow(value) + ); + }); + + it.each([ + 'int', + 'java.lang.Integer', + 'java.math.BigInteger', + 'short', + 'java.lang.Short', + 'byte', + 'java.lang.Byte', + 'double', + 'java.lang.Double', + 'java.math.BigDecimal', + 'float', + 'java.lang.Float', + ])('should handle number type: %s', columnType => { + const value = '1,234'; + mockTableUtils.makeFilterValue(columnType, value); + expect(mockDh.FilterValue.ofNumber).toHaveBeenCalledWith('1234'); + }); +}); + describe('makeSearchTextFilter', () => { const mockTableUtils = new TableUtils(dh); const mockSearchText = 'mock.searchText';