Skip to content

Commit

Permalink
null autotests
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Jul 8, 2024
1 parent 06d7a79 commit 108e22d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 10 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@

## features

- support `null` for properties: highlight, heta standard
- AnyUnit for zero numbers
- atStart to exports: Matlab, DBSolve
- write reusable `Build` class
- alternative solution to substitute "pkg": Node.js 21 https://nodejs.org/api/single-executable-applications.html
wait until Node.js 22 will be released
- support `null` for properties: highlight, heta standard
- use `Component` as a subclass of `Top`

## ideas

Expand Down
1 change: 1 addition & 0 deletions cases/27-null/src/index.heta
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ p1 {title: null};
p1 .= null;
//p1 := null;
p1 [xxx]= null;
p1 {units: null};

// exports
#export { format: JSON, filepath: output };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/unit/parse-and-stringify --config=./test/.mocharc.json",
"test:dev": "mocha test/null --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
2 changes: 1 addition & 1 deletion src/core/_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _Size extends Component {
let msg = this.index + ': '+ e.message;
logger && logger.error(msg, {type: 'ValidationError', space: this.space});
}
} else if (q.unit === null) {
} else if (q.units === null) {
delete this.unitsParsed;
} else {
this.unitsParsed = Unit.fromQ(q.units);
Expand Down
2 changes: 1 addition & 1 deletion src/core/function-def.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FunctionDef extends Top {
super(q, isCore);

// check arguments here
let logger = this._container.logger;
let logger = this._container?.logger;
let valid = FunctionDef.isValid(q, logger);
if (!valid) { this.errored = true; return; }

Expand Down
4 changes: 2 additions & 2 deletions src/core/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Top { // or const Top = class {...}
new Top({id: 'ttt1'});
*/
constructor(q = {}, isCore = false){
let logger = this._container.logger;
let logger = this._container?.logger;
let valid = Top.isValid(q, logger);
if (!valid) { this.errored = true; return; }

Expand Down Expand Up @@ -72,7 +72,7 @@ class Top { // or const Top = class {...}
let msg = `${q.id} Some of properties do not satisfy requirements for class "${this.name}"\n`
+ this.validate.errors.map((x, i) => ` ${i+1}. ${x.dataPath} ${x.message}`)
.join('\n');
logger.error(msg, {type: 'ValidationError', space: q.space});
logger?.error(msg, {type: 'ValidationError', space: q.space});
}

return valid;
Expand Down
6 changes: 3 additions & 3 deletions src/core/unit-def.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ class UnitDef extends Top {
//this.unitsParsed = new Unit(); // XXX: I am not sure maybe this was important

// check arguments here
let logger = this._container.logger;
let logger = this._container?.logger;
let valid = UnitDef.isValid(q, logger);
if (!valid) { this.errored = true; return; }

// units or terms are required but not both
if (q.units && q.terms) {
logger.error(`UnitDef "${q.id}" must include "units" or "terms" property but not both`, {type: 'ValidationError'});
logger?.error(`UnitDef "${q.id}" must include "units" or "terms" property but not both`, {type: 'ValidationError'});
this.errored = true;
} else if (!q.units && !q.terms) {
logger.error(`UnitDef "${q.id}" must include "units" or "terms" property`, {type: 'ValidationError'});
logger?.error(`UnitDef "${q.id}" must include "units" or "terms" property`, {type: 'ValidationError'});
this.errored = true;
}

Expand Down
7 changes: 6 additions & 1 deletion src/heta.json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@
]},
"aux": {"oneOf": [
{ "type": "null" },
{ "type": "object" }
{
"type": "object",
"additionalProperties": {
"oneOf": [ { "type": "string" }, { "type": "number" }, {"type": "array"}, { "type": "object" }, { "type": "boolean"} ]
}
}
]}
}
},
Expand Down
69 changes: 69 additions & 0 deletions test/null/null-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Throw errors when null is not allowed as value

/* global describe, it */

const { Record } = require('../../src/core/record');
const { FunctionDef } = require('../../src/core/function-def');
const { UnitDef } = require('../../src/core/unit-def');
const { expect } = require('chai');

describe('Unit tests for wrong null properties.', () => {
it('Set null for aux subproperties', () => {
let rec = (new Record).merge({
aux: {a: 1, b: 2, c: null}
});
rec._id = 'x1';

// in current version of API is hard to check if there are errors at validation
// we use only checking of empty aux
expect(rec.aux).to.be.deep.equal({});
});

it('Set null for tags', () => {
let rec = (new Record).merge({
tags: ['eee', 'ttt', null]
});
rec._id = 'x1';

// this is also result of error in validation
expect(rec.tags).to.be.deep.equal([]);
});

it('Cannot set null for math FunctionDef', () => {
let fd = new FunctionDef({
id: 'f1',
math: null
});

expect(fd.errored).to.be.true;
});

it('Cannot set null for arguments FunctionDef 1', () => {
let fd = new FunctionDef({
id: 'f1',
arguments: null,
math: '15'
});

expect(fd.errored).to.be.true;
});

it('Cannot set null for arguments FunctionDef 2', () => {
let fd = new FunctionDef({
id: 'f1',
arguments: [null, 'x1', 'x2'],
math: 'x1 + x2'
});

expect(fd.errored).to.be.true;
});

it('Cannot set null for units UnitDef', () => {
let ud = new UnitDef({
id: 'u1',
units: null
});

expect(ud.errored).to.be.true;
});
});
51 changes: 51 additions & 0 deletions test/null/null-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// test creation of components with properties set with null

/* global describe, it */
const { Record } = require('../../src/core/record');
const { expect } = require('chai');

describe('Unit tests for null properties.', () => {
it('Set all null properties', () => {
let rec = (new Record).merge({
title: null,
notes: null,
assignments: {start_: null, ode_: null, xxx: null},
tags: null,
aux: null
});
rec._id = 'x1';

expect(rec.toQ()).to.be.deep.equal({
class: 'Record',
id: 'x1',
assignments: {}
});
});

it('Set all properties, than delete by null', () => {
let rec = (new Record).merge({
title: 'Title',
notes: 'Notes',
units: 'mm',
assignments: {start_: 'start', ode_: 'ode', xxx: 'xxx'},
tags: ['tag1', 'tag2'],
aux: {a: 1, b: 2}
});
rec._id = 'x1';

rec.merge({
title: null,
notes: null,
units: null,
assignments: {start_: null, ode_: null, xxx: null},
tags: null,
aux: null
});

expect(rec.toQ()).to.be.deep.equal({
class: 'Record',
id: 'x1',
assignments: {}
});
});
});

0 comments on commit 108e22d

Please sign in to comment.