Skip to content

Commit 035e41c

Browse files
authored
Merge pull request #9 from zeroturnaround/migrate-to-jest
Replace Karma+Jasmine with Jest test framework Closes #4
2 parents 982d42b + 52c77ff commit 035e41c

10 files changed

+220
-271
lines changed

.babelrc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,5 @@
1010
"transform-object-rest-spread",
1111
"transform-es3-member-expression-literals",
1212
"transform-es3-property-literals"
13-
],
14-
"env": {
15-
"test": {
16-
"plugins": ["istanbul"]
17-
}
18-
}
13+
]
1914
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ script:
88
- npm run check
99
- npm run build
1010
after_script:
11-
- cat ./coverage/lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js
11+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js

karma.conf.js

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

package.json

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "sql-formatter",
33
"version": "0.0.3",
44
"description": "Formats whitespaces in a SQL query to make it more readable",
5+
"license": "MIT",
56
"main": "lib/sqlFormatter.js",
67
"keywords": [
78
"sql",
@@ -22,8 +23,8 @@
2223
"scripts": {
2324
"clean": "rimraf lib dist",
2425
"lint": "eslint .",
25-
"test": "cross-env NODE_ENV=test karma start",
26-
"test:watch": "npm run test -- --no-single-run",
26+
"test": "jest",
27+
"test:watch": "npm run test -- --watch",
2728
"check": "npm run lint && npm run test",
2829
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min",
2930
"build:commonjs": "babel src --out-dir lib",
@@ -35,39 +36,35 @@
3536
"type": "git",
3637
"url": "https://github.com/zeroturnaround/sql-formatter.git"
3738
},
39+
"bugs": {
40+
"url": "https://github.com/zeroturnaround/sql-formatter/issues"
41+
},
3842
"dependencies": {
39-
"lodash": "^4.14.0"
43+
"lodash": "^4.16.0"
4044
},
4145
"devDependencies": {
4246
"babel-cli": "^6.14.0",
4347
"babel-core": "^6.11.4",
4448
"babel-eslint": "^6.1.2",
49+
"babel-jest": "^15.0.0",
4550
"babel-loader": "^6.2.4",
4651
"babel-plugin-add-module-exports": "^0.2.1",
47-
"babel-plugin-istanbul": "^2.0.2",
4852
"babel-plugin-transform-class-properties": "^6.11.5",
4953
"babel-plugin-transform-es3-member-expression-literals": "^6.5.0",
5054
"babel-plugin-transform-es3-property-literals": "^6.5.0",
5155
"babel-plugin-transform-function-bind": "^6.8.0",
5256
"babel-plugin-transform-object-rest-spread": "^6.8.0",
5357
"babel-plugin-transform-runtime": "^6.8.0",
5458
"babel-preset-es2015": "^6.14.0",
55-
"cross-env": "^1.0.7",
59+
"cross-env": "^2.0.1",
5660
"eslint": "^3.1.1",
57-
"jasmine-core": "^2.4.1",
58-
"karma": "^1.1.1",
59-
"karma-coverage": "^1.1.1",
60-
"karma-jasmine": "^1.0.2",
61-
"karma-jasmine-diff-reporter": "^0.6.0",
62-
"karma-phantomjs-launcher": "^1.0.1",
63-
"karma-sourcemap-loader": "^0.3.7",
64-
"karma-webpack": "^1.7.0",
65-
"phantomjs-prebuilt": "^2.1.7",
61+
"jest": "^15.1.1",
6662
"rimraf": "^2.3.4",
6763
"webpack": "^1.13.1"
6864
},
69-
"bugs": {
70-
"url": "https://github.com/zeroturnaround/sql-formatter/issues"
71-
},
72-
"license": "MIT"
65+
"jest": {
66+
"testPathDirs": ["test"],
67+
"testRegex": ".*Test",
68+
"collectCoverage": true
69+
}
7370
}

test/N1qlFormatterTest.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import sqlFormatter from "./../src/sqlFormatter";
2+
import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter";
3+
4+
describe("N1qlFormatter", function() {
5+
behavesLikeSqlFormatter("n1ql");
6+
7+
it("formats SELECT query with element selection expression", function() {
8+
const result = sqlFormatter.format("SELECT orderlines[0].productId FROM orders;", {language: "n1ql"});
9+
expect(result).toBe(
10+
"SELECT\n" +
11+
" orderlines[0].productId\n" +
12+
"FROM\n" +
13+
" orders;\n"
14+
);
15+
});
16+
17+
it("formats SELECT query with primary key quering", function() {
18+
const result = sqlFormatter.format(
19+
"SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];",
20+
{language: "n1ql"}
21+
);
22+
expect(result).toBe(
23+
"SELECT\n" +
24+
" fname,\n" +
25+
" email\n" +
26+
"FROM\n" +
27+
" tutorial\n" +
28+
"USE KEYS\n" +
29+
" ['dave', 'ian'];\n"
30+
);
31+
});
32+
33+
it("formats INSERT with {} object literal", function() {
34+
const result = sqlFormatter.format(
35+
"INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id':1,'type':'Tarzan'});",
36+
{language: "n1ql"}
37+
);
38+
expect(result).toBe(
39+
"INSERT INTO\n" +
40+
" heroes (KEY, VALUE)\n" +
41+
"VALUES\n" +
42+
" ('123', {'id': 1, 'type': 'Tarzan'});\n"
43+
);
44+
});
45+
46+
it("formats INSERT with large object and array literals", function() {
47+
const result = sqlFormatter.format(
48+
"INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id': 1, 'type': 'Tarzan', " +
49+
"'array': [123456789, 123456789, 123456789, 123456789, 123456789], 'hello': 'world'});",
50+
{language: "n1ql"}
51+
);
52+
expect(result).toBe(
53+
"INSERT INTO\n" +
54+
" heroes (KEY, VALUE)\n" +
55+
"VALUES\n" +
56+
" (\n" +
57+
" '123',\n" +
58+
" {\n" +
59+
" 'id': 1,\n" +
60+
" 'type': 'Tarzan',\n" +
61+
" 'array': [\n" +
62+
" 123456789,\n" +
63+
" 123456789,\n" +
64+
" 123456789,\n" +
65+
" 123456789,\n" +
66+
" 123456789\n" +
67+
" ],\n" +
68+
" 'hello': 'world'\n" +
69+
" }\n" +
70+
" );\n"
71+
);
72+
});
73+
74+
it("formats SELECT query with UNNEST toplevel reserver word", function() {
75+
const result = sqlFormatter.format(
76+
"SELECT * FROM tutorial UNNEST tutorial.children c;",
77+
{language: "n1ql"}
78+
);
79+
expect(result).toBe(
80+
"SELECT\n" +
81+
" *\n" +
82+
"FROM\n" +
83+
" tutorial\n" +
84+
"UNNEST\n" +
85+
" tutorial.children c;\n"
86+
);
87+
});
88+
89+
it("formats SELECT query with NEST and USE KEYS", function() {
90+
const result = sqlFormatter.format(
91+
"SELECT * FROM usr " +
92+
"USE KEYS 'Elinor_33313792' NEST orders_with_users orders " +
93+
"ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;",
94+
{language: "n1ql"}
95+
);
96+
expect(result).toBe(
97+
"SELECT\n" +
98+
" *\n" +
99+
"FROM\n" +
100+
" usr\n" +
101+
"USE KEYS\n" +
102+
" 'Elinor_33313792'\n" +
103+
"NEST\n" +
104+
" orders_with_users orders ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;\n"
105+
);
106+
});
107+
108+
it("formats explained DELETE query with USE KEYS and RETURNING", function() {
109+
const result = sqlFormatter.format(
110+
"EXPLAIN DELETE FROM tutorial t USE KEYS 'baldwin' RETURNING t",
111+
{language: "n1ql"}
112+
);
113+
expect(result).toBe(
114+
"EXPLAIN DELETE FROM\n" +
115+
" tutorial t\n" +
116+
"USE KEYS\n" +
117+
" 'baldwin' RETURNING t\n"
118+
);
119+
});
120+
121+
it("formats UPDATE query with USE KEYS and RETURNING", function() {
122+
const result = sqlFormatter.format(
123+
"UPDATE tutorial USE KEYS 'baldwin' SET type = 'actor' RETURNING tutorial.type",
124+
{language: "n1ql"}
125+
);
126+
expect(result).toBe(
127+
"UPDATE\n" +
128+
" tutorial\n" +
129+
"USE KEYS\n" +
130+
" 'baldwin'\n" +
131+
"SET\n" +
132+
" type = 'actor' RETURNING tutorial.type\n"
133+
);
134+
});
135+
});

test/StandardSqlFormatterTest.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import sqlFormatter from "./../src/sqlFormatter";
2+
import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter";
3+
4+
describe("StandardSqlFormatter", function() {
5+
behavesLikeSqlFormatter();
6+
7+
it("formats ALTER TABLE ... MODIFY query", function() {
8+
const result = sqlFormatter.format(
9+
"ALTER TABLE supplier MODIFY supplier_name char(100) NOT NULL;"
10+
);
11+
expect(result).toBe(
12+
"ALTER TABLE\n" +
13+
" supplier\n" +
14+
"MODIFY\n" +
15+
" supplier_name char(100) NOT NULL;\n"
16+
);
17+
});
18+
19+
it("formats ALTER TABLE ... ALTER COLUMN query", function() {
20+
const result = sqlFormatter.format(
21+
"ALTER TABLE supplier ALTER COLUMN supplier_name VARCHAR(100) NOT NULL;"
22+
);
23+
expect(result).toBe(
24+
"ALTER TABLE\n" +
25+
" supplier\n" +
26+
"ALTER COLUMN\n" +
27+
" supplier_name VARCHAR(100) NOT NULL;\n"
28+
);
29+
});
30+
31+
it("recognizes [] strings", function() {
32+
expect(sqlFormatter.format("[foo JOIN bar]")).toBe("[foo JOIN bar]\n");
33+
expect(sqlFormatter.format("[foo ]] JOIN bar]")).toBe("[foo ]] JOIN bar]\n");
34+
});
35+
36+
it("recognizes @variables", function() {
37+
const result = sqlFormatter.format(
38+
"SELECT @variable, @'var name', @\"var name\", @`var name`, @[var name];"
39+
);
40+
expect(result).toBe(
41+
"SELECT\n" +
42+
" @variable,\n" +
43+
" @'var name',\n" +
44+
" @\"var name\",\n" +
45+
" @`var name`,\n" +
46+
" @[var name];\n"
47+
);
48+
});
49+
50+
it("recognizes :variables", function() {
51+
const result = sqlFormatter.format(
52+
"SELECT :variable, :'var name', :\"var name\", :`var name`, :[var name];"
53+
);
54+
expect(result).toBe(
55+
"SELECT\n" +
56+
" :variable,\n" +
57+
" :'var name',\n" +
58+
" :\"var name\",\n" +
59+
" :`var name`,\n" +
60+
" :[var name];\n"
61+
);
62+
});
63+
});

test/languages/behavesLikeSqlFormatter.js renamed to test/behavesLikeSqlFormatter.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import sqlFormatter from "./../src/sqlFormatter";
2+
13
/**
24
* Core tests for all SQL formatters
3-
* @param {sqlFormatter} formatter
45
* @param {String} language
56
*/
6-
export default function behavesLikeSqlFormatter(formatter, language) {
7+
export default function behavesLikeSqlFormatter(language) {
78
it("uses given indent config for indention", function() {
8-
const result = formatter.format(
9+
const result = sqlFormatter.format(
910
"SELECT count(*),Column1 FROM Table1;",
1011
{language, indent: " "}
1112
);
@@ -20,7 +21,7 @@ export default function behavesLikeSqlFormatter(formatter, language) {
2021
});
2122

2223
function format(query) {
23-
return formatter.format(query, {language});
24+
return sqlFormatter.format(query, {language});
2425
}
2526

2627
it("formats simple SELECT query", function() {

0 commit comments

Comments
 (0)