Skip to content

Commit

Permalink
more update to node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
77it committed Apr 21, 2024
1 parent 2ab021d commit 61ac7a3
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 163 deletions.
39 changes: 39 additions & 0 deletions test/engine_tests/simobject/parts/simobject_metadata__test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SimObject_Metadata } from '../../../../src/engine/simobject/parts/simobject_metadata.js';

import { eqObj } from '../../../../test2/deps.js';

import { test } from 'node:test';
import assert from 'node:assert';
/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test; // to force testing under Deno with its logic and internals

t('SimObject_Metadata tests - test normal use, successful', async () => {
const p1 = { metadata__Name: ['AA'], metadata__Value: ['BB'], metadata__PercentageWeight: [0.5] };
const _1 = new SimObject_Metadata(p1);
assert(eqObj(_1, p1));

const p2 = { metadata__Name: ['AA', 'AAA'], metadata__Value: ['BB', 'BBB'], metadata__PercentageWeight: [0.5, 0.6] };
const _2 = new SimObject_Metadata(p2);
assert(eqObj(_2, p2));
});

t('SimObject_Metadata tests - test error, array of different length', async () => {
let _error = '';
try {
const p1 = { metadata__Name: ['AA'], metadata__Value: ['BB'], metadata__PercentageWeight: [] };
const _1 = new SimObject_Metadata(p1);
} catch (error) {
_error = error.message;
}
assert.deepStrictEqual(_error, 'length of metadata arrays must be equal, got name = 1, value = 1, weight= 0');
});

t('SimObject_Metadata tests - test error, extraneous property not present in validation object', async () => {
let _error = '';
try {
const p1 = { extraneous_property: 99, metadata__Name: ['AA'], metadata__Value: ['BB'], metadata__PercentageWeight: [0.5] };
const _1 = new SimObject_Metadata(p1);
} catch (error) {
_error = error.message;
}
assert.deepStrictEqual(_error, 'Validation error: ["extraneous_property is not a valid key, is missing from validation object"]');
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { SimObject } from '../../../src/engine/simobject/simobject.js';

import { assert, assertFalse, assertEquals, assertNotEquals, assertThrows } from '../../deps.js';
import { SimObjectTypes_enum } from '../../../src/engine/simobject/enums/simobject_types_enum.js';
import { DoubleEntrySide_enum } from '../../../src/engine/simobject/enums/doubleentryside_enum.js';
import { toBigInt } from '../../../src/engine/simobject/utils/simobject_utils.js';

import { test } from 'node:test';
import assert from 'node:assert';
/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test; // to force testing under Deno with its logic and internals

const DECIMALPLACES = 4;
const ROUNDINGMODEISROUND = true;

const p = {
decimalPlaces: 4,
decimalPlaces: DECIMALPLACES,
type: SimObjectTypes_enum.BS_CASH__BANKACCOUNT_FINANCIALACCOUNT,
id: '1',
dateTime: new Date(2020, 0, 1),
Expand Down Expand Up @@ -38,34 +41,32 @@ const p = {
versionId: 1
};

Deno.test('SimObject tests', async (t) => {
await t.step('test normal use, successful', async () => {
t('SimObject tests - test normal use, successful', async () => {
new SimObject(p);
});
});

await t.step('test error, metadata array of different length', async () => {
const p1 = structuredClone(p);
//@ts-ignore
p1.metadata__Name = ['AA'];
let _error = '';
try {
new SimObject(p1);
} catch (error) {
_error = error.message;
}
assertEquals(_error, 'length of metadata arrays must be equal, got name = 1, value = 0, weight= 0');
});
t('SimObject tests - test error, metadata array of different length', async () => {
const p1 = structuredClone(p);
//@ts-ignore
p1.metadata__Name = ['AA'];
let _error = '';
try {
new SimObject(p1);
} catch (error) {
_error = error.message;
}
assert.deepStrictEqual(_error, 'length of metadata arrays must be equal, got name = 1, value = 0, weight= 0');
});

await t.step('test error, extraneous property not present in validation object', async () => {
const p1 = structuredClone(p);
//@ts-ignore
p1.extraneous_property = 99;
let _error = '';
try {
new SimObject(p1);
} catch (error) {
_error = error.message;
}
assertEquals(_error, 'Validation error: ["extraneous_property is not a valid key, is missing from validation object"]');
});
t('SimObject tests - test error, extraneous property not present in validation object', async () => {
const p1 = structuredClone(p);
//@ts-ignore
p1.extraneous_property = 99;
let _error = '';
try {
new SimObject(p1);
} catch (error) {
_error = error.message;
}
assert.deepStrictEqual(_error, 'Validation error: ["extraneous_property is not a valid key, is missing from validation object"]');
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { assert, assertFalse, assertEquals, assertNotEquals, assertThrows } from '../../deps.js';

import { SimObject } from '../../../src/engine/simobject/simobject.js';
import { simObjectToDto, toBigInt } from '../../../src/engine/simobject/utils/simobject_utils.js';
import { SimObjectTypes_enum } from '../../../src/engine/simobject/enums/simobject_types_enum.js';
import { DoubleEntrySide_enum } from '../../../src/engine/simobject/enums/doubleentryside_enum.js';

import { test } from 'node:test';
import assert from 'node:assert';
/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test; // to force testing under Deno with its logic and internals

const decimalPlaces = 4;
const roundingModeIsRound = true;

const _so = new SimObject({
decimalPlaces: 4,
decimalPlaces: decimalPlaces,
type: SimObjectTypes_enum.BS_CASH__BANKACCOUNT_FINANCIALACCOUNT,
id: '1',
dateTime: new Date(2020, 0, 1),
Expand Down Expand Up @@ -39,7 +41,7 @@ const _so = new SimObject({
});

const _so2_withExtras = new SimObject({
decimalPlaces: 4,
decimalPlaces: decimalPlaces,
type: SimObjectTypes_enum.BS_CASH__BANKACCOUNT_FINANCIALACCOUNT,
id: '1',
dateTime: new Date(2020, 0, 1),
Expand Down Expand Up @@ -69,7 +71,7 @@ const _so2_withExtras = new SimObject({
extras: {a: 999, b: 'aaa'}
});

Deno.test('SimObject.simObjectToDto() & .with() without value tests', async () => {
t('SimObject.simObjectToDto() & .with() without value tests', async () => {
const _so_With = _so.with();
const _so_With2_withExtras = _so2_withExtras.with({});

Expand Down Expand Up @@ -132,7 +134,7 @@ Deno.test('SimObject.simObjectToDto() & .with() without value tests', async () =
extras: {a: 999, b: 'aaa'}
};

assertEquals(_so_With.decimalPlaces, 4);
assert.deepStrictEqual(_so_With.decimalPlaces, 4);

let _error = '';

Expand All @@ -142,19 +144,19 @@ Deno.test('SimObject.simObjectToDto() & .with() without value tests', async () =
// try to change the value of the DTO, but the changes will go in error, because the DTO is frozen
try {_so_With_Dto.type = 'abcd';}
catch (e) {_error = e.message;}
assertEquals(_error, "Cannot assign to read only property 'type' of object '#<SimObjectDto>'");
assert.deepStrictEqual(_error, "Cannot assign to read only property 'type' of object '#<SimObjectDto>'");
try {
//@ts-ignore
_so_With_Dto.newField = 44;}
catch (e) {_error = e.message;}
assertEquals(_error, "Cannot add property newField, object is not extensible");
assert.deepStrictEqual(_error, "Cannot add property newField, object is not extensible");

assertEquals(JSON.stringify(_so_With_Dto), JSON.stringify(_soDump_Expected));
assert.deepStrictEqual(JSON.stringify(_so_With_Dto), JSON.stringify(_soDump_Expected));

assertEquals(JSON.stringify(simObjectToDto(_so_With2_withExtras)), JSON.stringify(_soDump_Expected2_withExtras));
assert.deepStrictEqual(JSON.stringify(simObjectToDto(_so_With2_withExtras)), JSON.stringify(_soDump_Expected2_withExtras));
});

Deno.test('SimObject.simObjectToDto() & .with() tests', async () => {
t('SimObject.simObjectToDto() & .with() tests', async () => {
const _so_With = _so2_withExtras.with({
//@ts-ignore
decimalPlaces: 44, // ignored
Expand Down Expand Up @@ -217,5 +219,5 @@ Deno.test('SimObject.simObjectToDto() & .with() tests', async () => {
extras: {a: 9991, b: 'aaax'}
};

assertEquals(JSON.stringify(simObjectToDto(_so_With)), JSON.stringify(_soDump_Expected));
assert.deepStrictEqual(JSON.stringify(simObjectToDto(_so_With)), JSON.stringify(_soDump_Expected));
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { assert, assertFalse, assertEquals, assertNotEquals, assertThrows } from '../../../deps.js';

import { doubleEntrySideFromSimObjectType } from '../../../../src/engine/simobject/enums/doubleentryside_enum.js';
import { SimObjectTypes_enum } from '../../../../src/engine/simobject/enums/simobject_types_enum.js';
import { DoubleEntrySide_enum } from '../../../../src/engine/simobject/enums/doubleentryside_enum.js';

Deno.test('doubleEntrySideFromSimObjectType() test, all values', async () => {
import { test } from 'node:test';
import assert from 'node:assert';
/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test; // to force testing under Deno with its logic and internals

t('doubleEntrySideFromSimObjectType() test, all values', async () => {
// get all DoubleEntrySide_enum values
const _doubleEntrySideValues = Object.values(DoubleEntrySide_enum);

Expand All @@ -16,34 +18,34 @@ Deno.test('doubleEntrySideFromSimObjectType() test, all values', async () => {
}
});

Deno.test('doubleEntrySideFromSimObjectType() test, some lowercase values', async () => {
t('doubleEntrySideFromSimObjectType() test, some lowercase values', async () => {
const BS_CASH__BANKACCOUNT_FINANCIALACCOUNT = SimObjectTypes_enum.BS_CASH__BANKACCOUNT_FINANCIALACCOUNT.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_CASH__BANKACCOUNT_FINANCIALACCOUNT), DoubleEntrySide_enum.BALANCESHEET_DEBIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_CASH__BANKACCOUNT_FINANCIALACCOUNT), DoubleEntrySide_enum.BALANCESHEET_DEBIT);

const BS_CREDIT__ACCRUALSCREDITS = SimObjectTypes_enum.BS_CREDIT__ACCRUALSCREDITS.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_CREDIT__ACCRUALSCREDITS), DoubleEntrySide_enum.BALANCESHEET_DEBIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_CREDIT__ACCRUALSCREDITS), DoubleEntrySide_enum.BALANCESHEET_DEBIT);

const BS_EQUITYINSTRUMENTS__EQUITYINSTRUMENTS_ASSETS__FINANCIALINVESTMENT = SimObjectTypes_enum.BS_EQUITYINSTRUMENTS__EQUITYINSTRUMENTS_ASSETS__FINANCIALINVESTMENT.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_EQUITYINSTRUMENTS__EQUITYINSTRUMENTS_ASSETS__FINANCIALINVESTMENT), DoubleEntrySide_enum.BALANCESHEET_DEBIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_EQUITYINSTRUMENTS__EQUITYINSTRUMENTS_ASSETS__FINANCIALINVESTMENT), DoubleEntrySide_enum.BALANCESHEET_DEBIT);

const BS_GOOD__INVENTORIES__CONTRACTWORKINPROGRESS = SimObjectTypes_enum.BS_GOOD__INVENTORIES__CONTRACTWORKINPROGRESS.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_GOOD__INVENTORIES__CONTRACTWORKINPROGRESS), DoubleEntrySide_enum.BALANCESHEET_DEBIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_GOOD__INVENTORIES__CONTRACTWORKINPROGRESS), DoubleEntrySide_enum.BALANCESHEET_DEBIT);

const BS_EQUITY__NETINCOMEORLOSSOFTHEYEAR_EQUITY__RELATEDTOMAJORITYSHAREHOLDERS = SimObjectTypes_enum.BS_EQUITY__NETINCOMEORLOSSOFTHEYEAR_EQUITY__RELATEDTOMAJORITYSHAREHOLDERS.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_EQUITY__NETINCOMEORLOSSOFTHEYEAR_EQUITY__RELATEDTOMAJORITYSHAREHOLDERS), DoubleEntrySide_enum.BALANCESHEET_CREDIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_EQUITY__NETINCOMEORLOSSOFTHEYEAR_EQUITY__RELATEDTOMAJORITYSHAREHOLDERS), DoubleEntrySide_enum.BALANCESHEET_CREDIT);

const BS_LIABILITY__ACCRUALSDEBTS = SimObjectTypes_enum.BS_LIABILITY__ACCRUALSDEBTS.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(BS_LIABILITY__ACCRUALSDEBTS), DoubleEntrySide_enum.BALANCESHEET_CREDIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(BS_LIABILITY__ACCRUALSDEBTS), DoubleEntrySide_enum.BALANCESHEET_CREDIT);

const IS_INCOME__CAPITALGAIN = SimObjectTypes_enum.IS_INCOME__CAPITALGAIN.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(IS_INCOME__CAPITALGAIN), DoubleEntrySide_enum.INCOMESTATEMENT_CREDIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(IS_INCOME__CAPITALGAIN), DoubleEntrySide_enum.INCOMESTATEMENT_CREDIT);

const IS_PROFITLOSS__CHANGESININVENTORIES__CONTRACTWORKINPROGRESS = SimObjectTypes_enum.IS_PROFITLOSS__CHANGESININVENTORIES__CONTRACTWORKINPROGRESS.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(IS_PROFITLOSS__CHANGESININVENTORIES__CONTRACTWORKINPROGRESS), DoubleEntrySide_enum.INCOMESTATEMENT_CREDIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(IS_PROFITLOSS__CHANGESININVENTORIES__CONTRACTWORKINPROGRESS), DoubleEntrySide_enum.INCOMESTATEMENT_CREDIT);

const IS_EXPENSE__AMORTIZATION = SimObjectTypes_enum.IS_EXPENSE__AMORTIZATION.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(IS_EXPENSE__AMORTIZATION), DoubleEntrySide_enum.INCOMESTATEMENT_DEBIT);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(IS_EXPENSE__AMORTIZATION), DoubleEntrySide_enum.INCOMESTATEMENT_DEBIT);

const MEMO__DATAFROMSIMULATION = SimObjectTypes_enum.MEMO__DATAFROMSIMULATION.toLowerCase();
assertEquals(doubleEntrySideFromSimObjectType(MEMO__DATAFROMSIMULATION), DoubleEntrySide_enum.MEMO);
assert.deepStrictEqual(doubleEntrySideFromSimObjectType(MEMO__DATAFROMSIMULATION), DoubleEntrySide_enum.MEMO);
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { assert, assertFalse, assertEquals, assertNotEquals, assertThrows } from '../../../deps.js';

import { splitPrincipal, bigIntToNumberWithDecimals, bigIntToStringWithDecimals } from '../../../../src/engine/simobject/utils/simobject_utils.js';

import { test } from 'node:test';
import assert from 'node:assert';
/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test; // to force testing under Deno with its logic and internals

const decimalPlaces = 4;
const roundingModeIsRound = true;

Deno.test('splitPrincipal() tests #1, split with Indefinite + Schedule', async () => {
t('splitPrincipal() tests #1, split with Indefinite + Schedule', async () => {
const value = 1000;
const bs_Principal__PrincipalToPay_IndefiniteExpiryDate = 10;
const bs_Principal__PrincipalToPay_AmortizationSchedule__Principal = [100, 100, 100, 690];
const decimalPlaces = 4;
const roundingModeIsRound = true;

const { principalIndefiniteExpiryDate, principalAmortizationSchedule } = splitPrincipal({
value,
Expand All @@ -25,16 +25,14 @@ Deno.test('splitPrincipal() tests #1, split with Indefinite + Schedule', async (
const expectedPrincipalIndefiniteExpiryDate = 10_0000n;
const expectedPrincipalAmortizationSchedule = [100_0000n, 100_0000n, 100_0000n, 690_0000n];

assertEquals(principalIndefiniteExpiryDate, expectedPrincipalIndefiniteExpiryDate);
assertEquals(principalAmortizationSchedule, expectedPrincipalAmortizationSchedule);
assert.deepStrictEqual(principalIndefiniteExpiryDate, expectedPrincipalIndefiniteExpiryDate);
assert.deepStrictEqual(principalAmortizationSchedule, expectedPrincipalAmortizationSchedule);
});

Deno.test('splitPrincipal() tests #2, split with Indefinite + Schedule testing conversion to number', async () => {
t('splitPrincipal() tests #2, split with Indefinite + Schedule testing conversion to number', async () => {
const value = 999;
const bs_Principal__PrincipalToPay_IndefiniteExpiryDate = 10;
const bs_Principal__PrincipalToPay_AmortizationSchedule__Principal = [1, 1, 1, 2];
const decimalPlaces = 4;
const roundingModeIsRound = true;

const { principalIndefiniteExpiryDate, principalAmortizationSchedule } = splitPrincipal({
value,
Expand All @@ -55,16 +53,14 @@ Deno.test('splitPrincipal() tests #2, split with Indefinite + Schedule testing c
const expectedPrincipalAmortizationSchedule = [197.8, 197.8, 197.8, 395.6];

// assert
assertEquals(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assertEquals(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
assert.deepStrictEqual(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assert.deepStrictEqual(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
});

Deno.test('splitPrincipal() tests #3, split with Indefinite + Schedule testing conversion to string', async () => {
t('splitPrincipal() tests #3, split with Indefinite + Schedule testing conversion to string', async () => {
const value = 1000;
const bs_Principal__PrincipalToPay_IndefiniteExpiryDate = 0;
const bs_Principal__PrincipalToPay_AmortizationSchedule__Principal = [333, 333, 333];
const decimalPlaces = 4;
const roundingModeIsRound = true;

const { principalIndefiniteExpiryDate, principalAmortizationSchedule } = splitPrincipal({
value,
Expand All @@ -85,17 +81,15 @@ Deno.test('splitPrincipal() tests #3, split with Indefinite + Schedule testing c
const expectedPrincipalAmortizationSchedule = ['333.3333', '333.3333', '333.3334'];

// assert
assertEquals(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assertEquals(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
assert.deepStrictEqual(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assert.deepStrictEqual(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
});

Deno.test('splitPrincipal() tests #4, split with Indefinite without Schedule', async () => {
t('splitPrincipal() tests #4, split with Indefinite without Schedule', async () => {
const value = 1000;
const bs_Principal__PrincipalToPay_IndefiniteExpiryDate = 1000;
/** @type {number[]} */
const bs_Principal__PrincipalToPay_AmortizationSchedule__Principal = [];
const decimalPlaces = 4;
const roundingModeIsRound = true;

const { principalIndefiniteExpiryDate, principalAmortizationSchedule } = splitPrincipal({
value,
Expand All @@ -117,17 +111,15 @@ Deno.test('splitPrincipal() tests #4, split with Indefinite without Schedule', a
const expectedPrincipalAmortizationSchedule = [];

// assert
assertEquals(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assertEquals(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
assert.deepStrictEqual(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assert.deepStrictEqual(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
});

Deno.test('splitPrincipal() tests #5, split without Indefinite and no Schedule', async () => {
t('splitPrincipal() tests #5, split without Indefinite and no Schedule', async () => {
const value = 1000;
const bs_Principal__PrincipalToPay_IndefiniteExpiryDate = 0;
/** @type {number[]} */
const bs_Principal__PrincipalToPay_AmortizationSchedule__Principal = [];
const decimalPlaces = 4;
const roundingModeIsRound = true;

const { principalIndefiniteExpiryDate, principalAmortizationSchedule } = splitPrincipal({
value,
Expand All @@ -149,6 +141,6 @@ Deno.test('splitPrincipal() tests #5, split without Indefinite and no Schedule',
const expectedPrincipalAmortizationSchedule = [];

// assert
assertEquals(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assertEquals(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
assert.deepStrictEqual(principalIndefiniteExpiryDateNumber, expectedPrincipalIndefiniteExpiryDate);
assert.deepStrictEqual(principalAmortizationScheduleNumber, expectedPrincipalAmortizationSchedule);
});
Loading

0 comments on commit 61ac7a3

Please sign in to comment.