Skip to content

Commit

Permalink
Fixes #748, tests for prql-js (#833)
Browse files Browse the repository at this point in the history
* For #748 , tests for javascript package
  • Loading branch information
qharlie committed Jul 23, 2022
1 parent 560794e commit 216eab3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ jobs:
node-version: "18.x"
registry-url: "https://registry.npmjs.org"

- name: Run prql tests for node
run: npm i && npm run test
working-directory: prql-js/tests/

- name: Publish package on npm
run: npm publish
working-directory: prql-js/
Expand Down
20 changes: 20 additions & 0 deletions prql-js/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"description": "Javascript tests for prql-js",
"files": [
"package.json"
],
"license": "Apache-2.0",
"name": "prql-js",
"repository": {
"type": "git",
"url": "https://github.com/prql/prql"
},
"scripts": {
"test": "mocha ."
},
"version": "0.2.2",
"dependencies": {
"chai": "^4.3.6",
"mocha": "^10.0.0"
}
}
66 changes: 66 additions & 0 deletions prql-js/tests/test_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const assert = require("assert");
const expect = require("chai").expect;
const prql = require("../dist/node");
const print = console.log;
const employee_prql = `from employees
join salaries [emp_no]
group [emp_no, gender] (
aggregate [
emp_salary = average salary
]
)
join de=dept_emp [emp_no]
join dm=dept_manager [
(dm.dept_no == de.dept_no) and s"(de.from_date, de.to_date) OVERLAPS (dm.from_date, dm.to_date)"
]
group [dm.emp_no, gender] (
aggregate [
salary_avg = average emp_salary,
salary_sd = stddev emp_salary
]
)
derive mng_no = dm.emp_no
join managers=employees [emp_no]
derive mng_name = s"managers.first_name || ' ' || managers.last_name"
select [mng_name, managers.gender, salary_avg, salary_sd]`;

describe("prql-js", () => {
describe("to_sql", () => {
it("should return valid sql from valid prql", () => {
const sql = prql.to_sql(employee_prql);
assert(
sql.trim().toLowerCase().startsWith("with") ||
sql.trim().toLowerCase().startsWith("select")
);
});

it("should throw an error on invalid prql", () => {
expect(() =>
prql.to_sql("Mississippi has four S’s and four I’s.")
).to.throw("Error");
});
});

describe("compile", () => {
it("should return valid compile result for compile", () => {
const res = prql.compile(employee_prql);
expect(res.error).to.be.undefined;
});

it("should return compile result with errors for compile", () => {
const res = prql.compile("Can you spell that without using S or I?");
expect(res.error).to.not.be.null;
});
});

describe("to_json", () => {
it("should return valid json from valid prql", () => {
const js = JSON.parse(prql.to_json(employee_prql));
assert.equal(js.nodes.length, 1);
});

it("should throw an error on invalid prql", () => {
expect(() => prql.to_json("Answer: T-H-A-T!"));
});
});
});

0 comments on commit 216eab3

Please sign in to comment.