Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formula): fix networkdays.intl #4152

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/engine-formula/src/basics/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export function countWorkingDays(startDateSerialNumber: number, endDateSerialNum
workingDays++;
}

return end > start ? workingDays : -workingDays;
return end >= start ? workingDays : -workingDays;
}

export function getDateSerialNumberByWorkingDays(startDateSerialNumber: number, workingDays: number, weekend: number | string = 1, holidays?: number[]): (number | ErrorValueObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import { describe, expect, it } from 'vitest';

import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_DATE } from '../../function-names';
import { NetworkdaysIntl } from '../index';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../../basics/error-type';

describe('Test networkdays.intl function', () => {
const testFunction = new NetworkdaysIntl(FUNCTION_NAMES_DATE.NETWORKDAYS);
Expand All @@ -31,12 +32,12 @@ describe('Test networkdays.intl function', () => {
const startDate = StringValueObject.create('2012-10-1');
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(110);
expect(getObjectValue(result)).toStrictEqual(110);

const weekend2 = NumberValueObject.create(6);
const holidays2 = StringValueObject.create('2012-11-22');
const result2 = testFunction.calculate(startDate, endDate, weekend2, holidays2);
expect(result2.getValue()).toStrictEqual(108);
expect(getObjectValue(result2)).toStrictEqual(108);

const weekend3 = StringValueObject.create('0011001');
const holidays3 = ArrayValueObject.create({
Expand All @@ -53,114 +54,114 @@ describe('Test networkdays.intl function', () => {
column: 0,
});
const result3 = testFunction.calculate(startDate, endDate, weekend3, holidays3);
expect(result3.getValue()).toStrictEqual(85);
expect(getObjectValue(result3)).toStrictEqual(85);
});

it('Value is number or date string', () => {
const startDate = NumberValueObject.create(45122);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(-2706);
expect(getObjectValue(result)).toStrictEqual(-2706);
});

it('Value is not date string', () => {
const startDate = StringValueObject.create('test');
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = StringValueObject.create('test');
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = StringValueObject.create('test');
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = StringValueObject.create('1110001');
const holidays4 = StringValueObject.create('test');
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);
});

it('Value is blank cell', () => {
const startDate = NullValueObject.create();
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(29525);
expect(getObjectValue(result)).toStrictEqual(29525);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = NullValueObject.create();
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(-29416);
expect(getObjectValue(result2)).toStrictEqual(-29416);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = NullValueObject.create();
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.NUM);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.NUM);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(5);
const holidays4 = NullValueObject.create();
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(108);
expect(getObjectValue(result4)).toStrictEqual(108);
});

it('Value is boolean', () => {
const startDate = BooleanValueObject.create(true);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = BooleanValueObject.create(false);
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = BooleanValueObject.create(true);
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(110);
expect(getObjectValue(result3)).toStrictEqual(110);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(1);
const holidays4 = BooleanValueObject.create(true);
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);
});

it('Value is error', () => {
const startDate = ErrorValueObject.create(ErrorType.NAME);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = ErrorValueObject.create(ErrorType.NAME);
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NAME);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = ErrorValueObject.create(ErrorType.NAME);
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.NAME);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(1);
const holidays4 = ErrorValueObject.create(ErrorType.NAME);
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.NAME);
});

it('Weekend value is array', () => {
Expand Down Expand Up @@ -194,12 +195,29 @@ describe('Test networkdays.intl function', () => {
column: 0,
});
const result = testFunction.calculate(startDate, endDate, weekend, holidays);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
expect(getObjectValue(result)).toStrictEqual([
[107, ErrorType.NUM, ErrorType.NUM],
[ErrorType.VALUE, ErrorType.NUM, ErrorType.NUM],
[106, ErrorType.NUM, ErrorType.NUM],
[ErrorType.NUM, ErrorType.NUM, ErrorType.NUM],
]);
});

it('More test', () => {
const startDate = StringValueObject.create('2024-11-25');
const endDate = StringValueObject.create('2024-11-25');
const result = testFunction.calculate(startDate, endDate);
expect(getObjectValue(result)).toBe(1);

const startDate2 = StringValueObject.create('2024-11-24');
const endDate2 = StringValueObject.create('2024-11-25');
const result2 = testFunction.calculate(startDate2, endDate2);
expect(getObjectValue(result2)).toBe(1);

const startDate3 = StringValueObject.create('2024-11-25');
const endDate3 = StringValueObject.create('2024-11-26');
const result3 = testFunction.calculate(startDate3, endDate3);
expect(getObjectValue(result3)).toBe(2);
});
});
});