Skip to content

Commit 3c75c5f

Browse files
authored
Merge pull request #68 from Exabyte-io/hotfix/2023-08-test-timeout
hotfix: avoid timeout in unit tests
2 parents a2ad79e + 2903c59 commit 3c75c5f

8 files changed

+126
-31
lines changed

src/JSONSchemasInterface.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class JSONSchemasInterface {
7878

7979
/**
8080
*
81-
* @param {Object} - external schema
81+
* @param globalSchema
8282
*/
8383
static registerGlobalSchema(globalSchema: JSONSchema) {
8484
if (JSONSchemasInterface._schema === globalSchema) {
@@ -159,4 +159,14 @@ export class JSONSchemasInterface {
159159
}
160160
return ajv.compile(schema);
161161
}
162+
163+
/**
164+
* Register global schema only if none has been registered yet.
165+
* @param globalSchema
166+
*/
167+
static registerGlobalSchemaIfEmpty(globalSchema: JSONSchema) {
168+
if (!JSONSchemasInterface._schema) {
169+
JSONSchemasInterface.registerGlobalSchema(globalSchema);
170+
}
171+
}
162172
}

src/utils/yaml.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const esseType = new yaml.Type("!esse", {
170170
},
171171
construct(data) {
172172
try {
173-
JSONSchemasInterface.registerGlobalSchema(esseSchema);
173+
JSONSchemasInterface.registerGlobalSchemaIfEmpty(esseSchema);
174174
const { filePath: schemaId, objPath } = splitReference(data);
175175
const schema = JSONSchemasInterface.schemaById(schemaId);
176176
if (objPath) {

tests/enums.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from "path";
1+
import * as path from "path";
22

33
export const FIXTURES_DIR = path.resolve(__dirname, "./fixtures");
44
export const YAML_COMBINE_FILE = path.resolve(FIXTURES_DIR, "yaml_combine_tag.yml");

tests/fixtures/mock_esse_schema.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
export const MOCK_GLOBAL_SCHEMA = {
2+
$id: "esse-global-schema",
3+
$schema: "http://json-schema.org/draft-04/schema#",
4+
title: "Global schema",
5+
type: "object",
6+
definitions: {
7+
"core:primitive:scalar": {
8+
$id: "core/primitive/scalar",
9+
$schema: "http://json-schema.org/draft-04/schema#",
10+
title: "scalar schema",
11+
type: "object",
12+
required: ["value"],
13+
properties: {
14+
value: {
15+
type: "number",
16+
},
17+
},
18+
},
19+
"definitions:units": {
20+
$id: "definitions/units",
21+
pressure: {
22+
enum: ["kbar", "pa"],
23+
},
24+
},
25+
"core:abstract::d-data": {
26+
$id: "core/abstract/2d-data",
27+
$schema: "http://json-schema.org/draft-04/schema#",
28+
title: "2 dimension data schema",
29+
type: "object",
30+
properties: {
31+
xDataArray: {
32+
description: "array containing values of x Axis",
33+
type: "array",
34+
},
35+
yDataSeries: {
36+
$ref: "#/definitions/core:primitive::d-data-series",
37+
},
38+
},
39+
required: ["xDataArray", "yDataSeries"],
40+
},
41+
"core:primitive::d-data-series": {
42+
$id: "core/primitive/1d-data-series",
43+
$schema: "http://json-schema.org/draft-04/schema#",
44+
title: "1 dimension data series schema",
45+
type: "array",
46+
items: {
47+
type: "array",
48+
minItems: 1,
49+
items: {
50+
type: ["number", "string"],
51+
},
52+
},
53+
},
54+
"methods-directory:physical:pw": {
55+
$id: "methods-directory/physical/pw",
56+
$schema: "http://json-schema.org/draft-04/schema#",
57+
title: "Plane wave method unit schema",
58+
description: "Approximating the electronic wave function with a plane wave basis",
59+
type: "object",
60+
properties: {
61+
name: {
62+
type: "string",
63+
},
64+
categories: {
65+
properties: {
66+
tier1: {
67+
description: "top-level category",
68+
type: "string",
69+
},
70+
tier2: {
71+
description: "second level category",
72+
type: "string",
73+
},
74+
tier3: {
75+
description: "third level category",
76+
type: "string",
77+
},
78+
type: {
79+
description: "general type of the entity",
80+
type: "string",
81+
},
82+
subtype: {
83+
description: "general subtype of the entity",
84+
type: "string",
85+
},
86+
},
87+
},
88+
tags: {
89+
type: "array",
90+
items: {
91+
type: "string",
92+
},
93+
},
94+
},
95+
},
96+
},
97+
};
File renamed without changes.

tests/utils/schemas.tests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TREE_SIMPLE,
1414
TREE_STATIC_TERMINAL,
1515
UNEVEN_TREE,
16-
} from "../fixtures/schemas";
16+
} from "../fixtures/rjsf_schemas";
1717

1818
describe("RJSF schema", () => {
1919
it("dependencies block can be created from tree", () => {

tests/utils/yaml.combine.tests.ts

+5-21
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ import fs from "fs";
55
import yaml from "js-yaml";
66
import lodash from "lodash";
77

8+
import { JSONSchemasInterface } from "../../src/JSONSchemasInterface";
89
import { combineType, esseType } from "../../src/utils/yaml";
910
import { YAML_COMBINE_FILE } from "../enums";
11+
import { MOCK_GLOBAL_SCHEMA } from "../fixtures/mock_esse_schema";
1012

1113
const combineSchema = yaml.DEFAULT_SCHEMA.extend([combineType, esseType]);
1214

1315
describe("YAML tag: !combine", () => {
1416
let yamlFixture;
17+
let parsed;
1518

1619
before(() => {
20+
JSONSchemasInterface.registerGlobalSchema(MOCK_GLOBAL_SCHEMA);
1721
yamlFixture = fs.readFileSync(YAML_COMBINE_FILE, "utf8");
22+
parsed = yaml.load(yamlFixture, { schema: combineSchema });
1823
});
1924

2025
it("should correctly parse a custom !combine tag with forEach and config keys", () => {
21-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
2226
const expectedResult = [
2327
{ name: "mytest", a: 1, b: 3, c: 5 },
2428
{ name: "mytest", a: 1, b: 4, c: 5 },
@@ -30,72 +34,57 @@ describe("YAML tag: !combine", () => {
3034
});
3135

3236
it("should correctly parse a custom !combine tag with only a name key", () => {
33-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
3437
const expectedResult = [{ name: "mytest" }];
35-
3638
expect(parsed.case2).to.have.deep.members(expectedResult);
3739
});
3840

3941
it("should correctly parse a custom !combine tag with forEach key and no values", () => {
40-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
4142
const expectedResult = [{ name: "mytest" }];
42-
4343
expect(parsed.case3).to.have.deep.members(expectedResult);
4444
});
4545

4646
it("should correctly parse a custom !combine tag with an empty forEach key and a config key", () => {
47-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
4847
const expectedResult = [{ name: "mytest", c: 5 }];
49-
5048
expect(parsed.case4).to.have.deep.members(expectedResult);
5149
});
5250

5351
it("should correctly generate name based on template", () => {
54-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
5552
const expectedResult = [
5653
{ name: "A1 with B2 and C5", a: 1, b: "two", c: 5 },
5754
{ name: "A1 with B4 and C5", a: 1, b: "four", c: 5 },
5855
];
59-
6056
expect(parsed.case5).to.have.deep.members(expectedResult);
6157
});
6258

6359
it("should correctly parse a custom !combine tag with additional property", () => {
64-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
6560
const expectedResult = [
6661
{ name: "mytest", a: 1, b: 3 },
6762
{ name: "mytest", a: 1, b: 4 },
6863
{ name: "additional property", x: 7 },
6964
];
70-
7165
expect(parsed.case6).to.have.deep.members(expectedResult);
7266
});
7367

7468
it("should correctly parse a custom !combine tag with additional property from !combine tag", () => {
75-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
7669
const expectedResult = [
7770
{ name: "mytest", a: 1, b: 3 },
7871
{ name: "mytest", a: 1, b: 4 },
7972
{ name: "additional property", x: 7, y: 9 },
8073
{ name: "additional property", x: 8, y: 9 },
8174
];
82-
8375
expect(parsed.case7).to.have.deep.members(expectedResult);
8476
});
8577

8678
it("should create an additional config when falsy parameter is provided", () => {
87-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
8879
const expectedResult = [
8980
{ name: "A1 with B2", a: 1, b: "two" },
9081
{ name: "A1 with B4", a: 1, b: "four" },
9182
{ name: "A1", a: 1 },
9283
];
93-
9484
expect(parsed.case8).to.have.deep.members(expectedResult);
9585
});
9686

9787
it("should create all combinations of n optional parameters", () => {
98-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
9988
const expectedResult = [
10089
{ name: "optional params", a: 1 },
10190
{ name: "optional params", a: 1, b: 2 },
@@ -104,19 +93,15 @@ describe("YAML tag: !combine", () => {
10493
{ name: "optional params", a: 1, b: 2, c: 4 },
10594
{ name: "optional params", a: 1, b: 3, c: 4 },
10695
];
107-
10896
expect(parsed.case9).to.have.deep.members(expectedResult);
10997
});
11098

11199
it("should allow to exclude certain parameter-specified combinations", () => {
112-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
113100
const expectedResult = [{ name: "ignore test", a: { c: 3 }, d: 4 }];
114-
115101
expect(parsed.case10).to.have.deep.members(expectedResult);
116102
});
117103

118104
it("should use the push action to add value to an array parameter", () => {
119-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
120105
const expectedResult = [
121106
{ name: "push test", units: [{ a: 1 }, { b: 4 }] },
122107
{ name: "push test", units: [{ a: 2 }, { b: 4 }] },
@@ -134,7 +119,6 @@ describe("YAML tag: !combine", () => {
134119
});
135120

136121
it("should use cloned objects when pushing to array", () => {
137-
const parsed = yaml.load(yamlFixture, { schema: combineSchema });
138122
const [config1, config2] = parsed.case12;
139123

140124
// deleting property in one should not affect the other

tests/utils/yaml.esse.tests.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ import { expect } from "chai";
33
import fs from "fs";
44
import yaml from "js-yaml";
55

6+
import { JSONSchemasInterface } from "../../src/JSONSchemasInterface";
67
import { esseType } from "../../src/utils/yaml";
78
import { YAML_ESSE_FILE } from "../enums";
9+
import { MOCK_GLOBAL_SCHEMA } from "../fixtures/mock_esse_schema";
810

911
const yamlSchema = yaml.DEFAULT_SCHEMA.extend([esseType]);
1012

1113
describe("YAML tag: !esse", () => {
12-
const yamlFixture = fs.readFileSync(YAML_ESSE_FILE, "utf8");
14+
let yamlFixture;
15+
let parsed;
16+
17+
before(() => {
18+
JSONSchemasInterface.registerGlobalSchema(MOCK_GLOBAL_SCHEMA);
19+
yamlFixture = fs.readFileSync(YAML_ESSE_FILE, "utf8");
20+
parsed = yaml.load(yamlFixture, { schema: yamlSchema });
21+
});
1322

1423
it("should correctly parse a custom !esse tag and return ESSE schema", () => {
15-
const parsed = yaml.load(yamlFixture, { schema: yamlSchema });
1624
const expected = {
1725
$id: "core/primitive/scalar",
1826
$schema: "http://json-schema.org/draft-04/schema#",
@@ -29,24 +37,20 @@ describe("YAML tag: !esse", () => {
2937
});
3038

3139
it("should return the original data when an error occurs", () => {
32-
const parsed = yaml.load(yamlFixture, { schema: yamlSchema });
3340
expect(parsed.case2).to.be.equal("non-existent-schema-id");
3441
});
3542

3643
it("should parse a custom !esse tag and return a value from the ESSE schema", () => {
37-
const parsed = yaml.load(yamlFixture, { schema: yamlSchema });
3844
const expected = ["kbar", "pa"];
3945
expect(parsed.case3).to.have.deep.members(expected);
4046
});
4147

4248
it("should correctly return nested value from esse schema", () => {
43-
const parsed = yaml.load(yamlFixture, { schema: yamlSchema });
4449
const expected = "array containing values of x Axis";
4550
expect(parsed.case4).to.be.eql(expected);
4651
});
4752

4853
it("should correctly return array item from esse schema", () => {
49-
const parsed = yaml.load(yamlFixture, { schema: yamlSchema });
5054
const expected = "yDataSeries";
5155
expect(parsed.case5).to.be.eql(expected);
5256
});

0 commit comments

Comments
 (0)