Skip to content

Commit f47c1f6

Browse files
committed
Add pytest, node, github action
1 parent 7d94f60 commit f47c1f6

7 files changed

Lines changed: 117 additions & 95 deletions

File tree

.github/workflows/tests.yml

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"scripts": {
6-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"test": "node --test"
77
},
88
"keywords": [],
99
"author": "",

pythonBuilder/builderTest.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

pythonBuilder/test_builder.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import json
2+
from builder import layout, visualization, encoding
3+
4+
5+
def build_test1_spec():
6+
test1 = (
7+
visualization()
8+
.mark("bar")
9+
.add_encoding(
10+
encoding()
11+
.channel("x").field("x")
12+
.channel("y").field("y")
13+
.channel("color").field("grey")
14+
)
15+
)
16+
17+
test1 = test1.visualization
18+
test1["encoding"] = test1["encoding"].encoding
19+
20+
return test1
21+
22+
def build_test2_spec():
23+
24+
test2 = layout().direction("vertical").gap("5px").add_child(
25+
visualization()
26+
.mark("line")
27+
.add_encoding(
28+
encoding()
29+
.channel("x").field("x")
30+
.channel("y").field("y")
31+
.channel("color").field("blue")
32+
)
33+
).add_child(
34+
layout().direction("horizontal").gap("5px")
35+
.add_child(
36+
visualization()
37+
.mark("point")
38+
.add_encoding(
39+
encoding()
40+
.channel("x").field("x")
41+
.channel("y").field("y")
42+
.channel("color").field("green")
43+
)
44+
).add_child(
45+
visualization()
46+
.mark("area")
47+
.add_encoding(
48+
encoding()
49+
.channel("x").field("x")
50+
.channel("y").field("y")
51+
.channel("color").field("purple")
52+
)
53+
)
54+
)
55+
test2 = test2.layout
56+
test2["children"][0] = test2["children"][0].visualization
57+
test2["children"][0]["encoding"] = test2["children"][0]["encoding"].encoding
58+
test2["children"][1] = test2["children"][1].layout
59+
test2["children"][1]["children"][0] = test2["children"][1]["children"][0].visualization
60+
test2["children"][1]["children"][0]["encoding"] = test2["children"][1]["children"][0]["encoding"].encoding
61+
test2["children"][1]["children"][1] = test2["children"][1]["children"][1].visualization
62+
test2["children"][1]["children"][1]["encoding"] = test2["children"][1]["children"][1]["encoding"].encoding
63+
64+
return test2
65+
66+
def test_example1_matches_json():
67+
test1 = build_test1_spec()
68+
with open("schemaExamples/example1.json", "r") as f:
69+
example1 = json.load(f)
70+
assert test1 == example1
71+
72+
73+
def test_example2_matches_json():
74+
test2 = build_test2_spec()
75+
with open("schemaExamples/example2.json", "r") as f:
76+
example2 = json.load(f)
77+
assert test2 == example2
78+

schemaTest/schema.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const test = require('node:test');
2+
const assert = require('assert');
3+
const fs = require('fs');
4+
const Ajv = require('ajv');
5+
6+
const ajv = new Ajv({ allErrors: true });
7+
8+
const schema = JSON.parse(
9+
fs.readFileSync('schema/visualization.schema.json', 'utf8')
10+
);
11+
12+
const example1 = JSON.parse(
13+
fs.readFileSync('schemaExamples/example1.json', 'utf8')
14+
);
15+
16+
const example2 = JSON.parse(
17+
fs.readFileSync('schemaExamples/example2.json', 'utf8')
18+
);
19+
20+
test('example1 is valid', () => {
21+
const validate = ajv.compile(schema);
22+
const valid = validate(example1);
23+
assert.ok(
24+
valid,
25+
'example1.json should be valid. Errors:\n' +
26+
JSON.stringify(validate.errors, null, 2)
27+
);
28+
});
29+
30+
test('example2 is valid', () => {
31+
const validate = ajv.compile(schema);
32+
const valid = validate(example2);
33+
assert.ok(
34+
valid,
35+
'example2.json should be valid. Errors:\n' +
36+
JSON.stringify(validate.errors, null, 2)
37+
);
38+
});

schemaTest/schemaTest.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)