Skip to content

#4 Replace Karma+Jasmine with Jest test framework #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ script:
- npm run check
- npm run build
after_script:
- cat ./coverage/lcov/lcov.info | ./node_modules/coveralls/bin/coveralls.js
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
36 changes: 0 additions & 36 deletions karma.conf.js

This file was deleted.

33 changes: 15 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "sql-formatter",
"version": "0.0.3",
"description": "Formats whitespaces in a SQL query to make it more readable",
"license": "MIT",
"main": "lib/sqlFormatter.js",
"keywords": [
"sql",
Expand All @@ -22,8 +23,8 @@
"scripts": {
"clean": "rimraf lib dist",
"lint": "eslint .",
"test": "cross-env NODE_ENV=test karma start",
"test:watch": "npm run test -- --no-single-run",
"test": "jest",
"test:watch": "npm run test -- --watch",
"check": "npm run lint && npm run test",
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min",
"build:commonjs": "babel src --out-dir lib",
Expand All @@ -35,39 +36,35 @@
"type": "git",
"url": "https://github.com/zeroturnaround/sql-formatter.git"
},
"bugs": {
"url": "https://github.com/zeroturnaround/sql-formatter/issues"
},
"dependencies": {
"lodash": "^4.14.0"
"lodash": "^4.16.0"
},
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-core": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-jest": "^15.0.0",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-istanbul": "^2.0.2",
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-plugin-transform-es3-member-expression-literals": "^6.5.0",
"babel-plugin-transform-es3-property-literals": "^6.5.0",
"babel-plugin-transform-function-bind": "^6.8.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-runtime": "^6.8.0",
"babel-preset-es2015": "^6.14.0",
"cross-env": "^1.0.7",
"cross-env": "^2.0.1",
"eslint": "^3.1.1",
"jasmine-core": "^2.4.1",
"karma": "^1.1.1",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-diff-reporter": "^0.6.0",
"karma-phantomjs-launcher": "^1.0.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.7.0",
"phantomjs-prebuilt": "^2.1.7",
"jest": "^15.1.1",
"rimraf": "^2.3.4",
"webpack": "^1.13.1"
},
"bugs": {
"url": "https://github.com/zeroturnaround/sql-formatter/issues"
},
"license": "MIT"
"jest": {
"testPathDirs": ["test"],
"testRegex": ".*Test",
"collectCoverage": true
}
}
135 changes: 135 additions & 0 deletions test/N1qlFormatterTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import sqlFormatter from "./../src/sqlFormatter";
import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter";

describe("N1qlFormatter", function() {
behavesLikeSqlFormatter("n1ql");

it("formats SELECT query with element selection expression", function() {
const result = sqlFormatter.format("SELECT orderlines[0].productId FROM orders;", {language: "n1ql"});
expect(result).toBe(
"SELECT\n" +
" orderlines[0].productId\n" +
"FROM\n" +
" orders;\n"
);
});

it("formats SELECT query with primary key quering", function() {
const result = sqlFormatter.format(
"SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];",
{language: "n1ql"}
);
expect(result).toBe(
"SELECT\n" +
" fname,\n" +
" email\n" +
"FROM\n" +
" tutorial\n" +
"USE KEYS\n" +
" ['dave', 'ian'];\n"
);
});

it("formats INSERT with {} object literal", function() {
const result = sqlFormatter.format(
"INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id':1,'type':'Tarzan'});",
{language: "n1ql"}
);
expect(result).toBe(
"INSERT INTO\n" +
" heroes (KEY, VALUE)\n" +
"VALUES\n" +
" ('123', {'id': 1, 'type': 'Tarzan'});\n"
);
});

it("formats INSERT with large object and array literals", function() {
const result = sqlFormatter.format(
"INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id': 1, 'type': 'Tarzan', " +
"'array': [123456789, 123456789, 123456789, 123456789, 123456789], 'hello': 'world'});",
{language: "n1ql"}
);
expect(result).toBe(
"INSERT INTO\n" +
" heroes (KEY, VALUE)\n" +
"VALUES\n" +
" (\n" +
" '123',\n" +
" {\n" +
" 'id': 1,\n" +
" 'type': 'Tarzan',\n" +
" 'array': [\n" +
" 123456789,\n" +
" 123456789,\n" +
" 123456789,\n" +
" 123456789,\n" +
" 123456789\n" +
" ],\n" +
" 'hello': 'world'\n" +
" }\n" +
" );\n"
);
});

it("formats SELECT query with UNNEST toplevel reserver word", function() {
const result = sqlFormatter.format(
"SELECT * FROM tutorial UNNEST tutorial.children c;",
{language: "n1ql"}
);
expect(result).toBe(
"SELECT\n" +
" *\n" +
"FROM\n" +
" tutorial\n" +
"UNNEST\n" +
" tutorial.children c;\n"
);
});

it("formats SELECT query with NEST and USE KEYS", function() {
const result = sqlFormatter.format(
"SELECT * FROM usr " +
"USE KEYS 'Elinor_33313792' NEST orders_with_users orders " +
"ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;",
{language: "n1ql"}
);
expect(result).toBe(
"SELECT\n" +
" *\n" +
"FROM\n" +
" usr\n" +
"USE KEYS\n" +
" 'Elinor_33313792'\n" +
"NEST\n" +
" orders_with_users orders ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;\n"
);
});

it("formats explained DELETE query with USE KEYS and RETURNING", function() {
const result = sqlFormatter.format(
"EXPLAIN DELETE FROM tutorial t USE KEYS 'baldwin' RETURNING t",
{language: "n1ql"}
);
expect(result).toBe(
"EXPLAIN DELETE FROM\n" +
" tutorial t\n" +
"USE KEYS\n" +
" 'baldwin' RETURNING t\n"
);
});

it("formats UPDATE query with USE KEYS and RETURNING", function() {
const result = sqlFormatter.format(
"UPDATE tutorial USE KEYS 'baldwin' SET type = 'actor' RETURNING tutorial.type",
{language: "n1ql"}
);
expect(result).toBe(
"UPDATE\n" +
" tutorial\n" +
"USE KEYS\n" +
" 'baldwin'\n" +
"SET\n" +
" type = 'actor' RETURNING tutorial.type\n"
);
});
});
63 changes: 63 additions & 0 deletions test/StandardSqlFormatterTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sqlFormatter from "./../src/sqlFormatter";
import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter";

describe("StandardSqlFormatter", function() {
behavesLikeSqlFormatter();

it("formats ALTER TABLE ... MODIFY query", function() {
const result = sqlFormatter.format(
"ALTER TABLE supplier MODIFY supplier_name char(100) NOT NULL;"
);
expect(result).toBe(
"ALTER TABLE\n" +
" supplier\n" +
"MODIFY\n" +
" supplier_name char(100) NOT NULL;\n"
);
});

it("formats ALTER TABLE ... ALTER COLUMN query", function() {
const result = sqlFormatter.format(
"ALTER TABLE supplier ALTER COLUMN supplier_name VARCHAR(100) NOT NULL;"
);
expect(result).toBe(
"ALTER TABLE\n" +
" supplier\n" +
"ALTER COLUMN\n" +
" supplier_name VARCHAR(100) NOT NULL;\n"
);
});

it("recognizes [] strings", function() {
expect(sqlFormatter.format("[foo JOIN bar]")).toBe("[foo JOIN bar]\n");
expect(sqlFormatter.format("[foo ]] JOIN bar]")).toBe("[foo ]] JOIN bar]\n");
});

it("recognizes @variables", function() {
const result = sqlFormatter.format(
"SELECT @variable, @'var name', @\"var name\", @`var name`, @[var name];"
);
expect(result).toBe(
"SELECT\n" +
" @variable,\n" +
" @'var name',\n" +
" @\"var name\",\n" +
" @`var name`,\n" +
" @[var name];\n"
);
});

it("recognizes :variables", function() {
const result = sqlFormatter.format(
"SELECT :variable, :'var name', :\"var name\", :`var name`, :[var name];"
);
expect(result).toBe(
"SELECT\n" +
" :variable,\n" +
" :'var name',\n" +
" :\"var name\",\n" +
" :`var name`,\n" +
" :[var name];\n"
);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import sqlFormatter from "./../src/sqlFormatter";

/**
* Core tests for all SQL formatters
* @param {sqlFormatter} formatter
* @param {String} language
*/
export default function behavesLikeSqlFormatter(formatter, language) {
export default function behavesLikeSqlFormatter(language) {
it("uses given indent config for indention", function() {
const result = formatter.format(
const result = sqlFormatter.format(
"SELECT count(*),Column1 FROM Table1;",
{language, indent: " "}
);
Expand All @@ -20,7 +21,7 @@ export default function behavesLikeSqlFormatter(formatter, language) {
});

function format(query) {
return formatter.format(query, {language});
return sqlFormatter.format(query, {language});
}

it("formats simple SELECT query", function() {
Expand Down
Loading