Skip to content

Commit

Permalink
Unit tests (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jun 24, 2024
1 parent 2c816be commit d81bc8f
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 13 deletions.
158 changes: 155 additions & 3 deletions packages/jsapi-utils/src/DateUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('dateTimeString parsing tests', () => {
minutes,
seconds,
nanos,
overflow,
}: {
year?: string;
month?: string;
Expand All @@ -51,17 +52,23 @@ describe('dateTimeString parsing tests', () => {
minutes?: string;
seconds?: string;
nanos?: string;
}
overflow?: string;
},
allowOverflow = false
) {
expect(DateUtils.parseDateTimeString(text)).toMatchObject({
const expected = {
year,
month,
date,
hours,
minutes,
seconds,
nanos,
});
};

expect(DateUtils.parseDateTimeString(text, allowOverflow)).toMatchObject(
allowOverflow ? { ...expected, overflow } : expected
);
}

function testDateTimeStringThrows(text) {
Expand All @@ -74,13 +81,32 @@ describe('dateTimeString parsing tests', () => {
testDateTimeString('2012', {
year: '2012',
});

testDateTimeString(
'2012 overflow',
{
year: '2012',
overflow: ' overflow',
},
true
);
});

it('handles YYYY-mm', () => {
testDateTimeString('2012-04', {
year: '2012',
month: '04',
});

testDateTimeString(
'2012-04 overflow',
{
year: '2012',
month: '04',
overflow: ' overflow',
},
true
);
});

it('handles YYYY-mm-dd', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', {
Expand All @@ -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', {
Expand All @@ -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', {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
10 changes: 0 additions & 10 deletions packages/jsapi-utils/src/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,6 @@ export class DateUtils {
* string
* @returns Containing the date time components
*/
static parseDateTimeString(
dateTimeString: string,
allowOverflow?: false
): DateParts<string>;

static parseDateTimeString(
dateTimeString: string,
allowOverflow: true
): DateParts<string> & { overflow?: string };

static parseDateTimeString(
dateTimeString: string,
allowOverflow = false
Expand Down
65 changes: 65 additions & 0 deletions packages/jsapi-utils/src/TableUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit d81bc8f

Please sign in to comment.