From 52c77ff5fee556654bee7d68b531683c6fae7470 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Tue, 20 Sep 2016 17:40:45 +0300 Subject: [PATCH] Replace Karma+Jasmine with Jest test framework --- .babelrc | 7 +- .travis.yml | 2 +- karma.conf.js | 36 ----- package.json | 33 ++--- test/N1qlFormatterTest.js | 135 +++++++++++++++++ test/StandardSqlFormatterTest.js | 63 ++++++++ .../behavesLikeSqlFormatter.js | 9 +- test/languages/testN1qlFormatter.js | 136 ------------------ test/languages/testStandardSqlFormatter.js | 64 --------- test/sqlFormatterTest.js | 6 - 10 files changed, 220 insertions(+), 271 deletions(-) delete mode 100644 karma.conf.js create mode 100644 test/N1qlFormatterTest.js create mode 100644 test/StandardSqlFormatterTest.js rename test/{languages => }/behavesLikeSqlFormatter.js (98%) delete mode 100644 test/languages/testN1qlFormatter.js delete mode 100644 test/languages/testStandardSqlFormatter.js delete mode 100644 test/sqlFormatterTest.js diff --git a/.babelrc b/.babelrc index 4376c02c20..131f2cfb2b 100644 --- a/.babelrc +++ b/.babelrc @@ -10,10 +10,5 @@ "transform-object-rest-spread", "transform-es3-member-expression-literals", "transform-es3-property-literals" - ], - "env": { - "test": { - "plugins": ["istanbul"] - } - } + ] } diff --git a/.travis.yml b/.travis.yml index 43d449ec68..b4f337ebb7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/karma.conf.js b/karma.conf.js deleted file mode 100644 index 357db35572..0000000000 --- a/karma.conf.js +++ /dev/null @@ -1,36 +0,0 @@ -const webpackConfig = require("./webpack.config.js"); - -webpackConfig.plugins = []; -webpackConfig.devtool = "inline-source-map"; - -module.exports = function(config) { - config.set({ - frameworks: ["jasmine"], - browsers: ["PhantomJS"], - files: ["test/**/*Test.js"], - preprocessors: { - "test/**/*Test.js": ["webpack", "sourcemap"] - }, - reporters: [ - "progress", - "coverage", - "jasmine-diff" - ], - singleRun: true, - - webpack: webpackConfig, - // keeps webpack from spamming the console, shows only warnings and errors - webpackMiddleware: { - noInfo: true - }, - coverageReporter: { - reporters: [ - {type: "html", dir: "coverage/", subdir: "html"}, - {type: "lcov", dir: "coverage/", subdir: "lcov"} - ] - }, - jasmineDiffReporter: { - pretty: 4 // Indent the data structure with 4-space steps - } - }); -}; diff --git a/package.json b/package.json index 05341acbac..56130ef2b6 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -35,16 +36,19 @@ "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", @@ -52,22 +56,15 @@ "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 + } } diff --git a/test/N1qlFormatterTest.js b/test/N1qlFormatterTest.js new file mode 100644 index 0000000000..24404dda22 --- /dev/null +++ b/test/N1qlFormatterTest.js @@ -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" + ); + }); +}); diff --git a/test/StandardSqlFormatterTest.js b/test/StandardSqlFormatterTest.js new file mode 100644 index 0000000000..d7d4820930 --- /dev/null +++ b/test/StandardSqlFormatterTest.js @@ -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" + ); + }); +}); diff --git a/test/languages/behavesLikeSqlFormatter.js b/test/behavesLikeSqlFormatter.js similarity index 98% rename from test/languages/behavesLikeSqlFormatter.js rename to test/behavesLikeSqlFormatter.js index ee6c385627..bd97af13d0 100644 --- a/test/languages/behavesLikeSqlFormatter.js +++ b/test/behavesLikeSqlFormatter.js @@ -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: " "} ); @@ -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() { diff --git a/test/languages/testN1qlFormatter.js b/test/languages/testN1qlFormatter.js deleted file mode 100644 index 563f6f0250..0000000000 --- a/test/languages/testN1qlFormatter.js +++ /dev/null @@ -1,136 +0,0 @@ -import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter"; - -export default function testN1qlFormatter(formatter) { - describe("N1qlFormatter", function() { - behavesLikeSqlFormatter(formatter, "n1ql"); - - it("formats SELECT query with element selection expression", function() { - const result = formatter.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 = formatter.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 = formatter.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 = formatter.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 = formatter.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 = formatter.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 = formatter.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 = formatter.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" - ); - }); - }); -} diff --git a/test/languages/testStandardSqlFormatter.js b/test/languages/testStandardSqlFormatter.js deleted file mode 100644 index e970825d1b..0000000000 --- a/test/languages/testStandardSqlFormatter.js +++ /dev/null @@ -1,64 +0,0 @@ -import behavesLikeSqlFormatter from "./behavesLikeSqlFormatter"; - -export default function testStandardSqlFormatter(formatter) { - describe("StandardSqlFormatter", function() { - behavesLikeSqlFormatter(formatter); - - it("formats ALTER TABLE ... MODIFY query", function() { - const result = formatter.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 = formatter.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(formatter.format("[foo JOIN bar]")).toBe("[foo JOIN bar]\n"); - expect(formatter.format("[foo ]] JOIN bar]")).toBe("[foo ]] JOIN bar]\n"); - }); - - it("recognizes @variables", function() { - const result = formatter.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 = formatter.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" - ); - }); - }); -} diff --git a/test/sqlFormatterTest.js b/test/sqlFormatterTest.js deleted file mode 100644 index 72b87e1995..0000000000 --- a/test/sqlFormatterTest.js +++ /dev/null @@ -1,6 +0,0 @@ -import sqlFormatter from "../src/sqlFormatter"; -import testStandardSqlFormatter from "./languages/testStandardSqlFormatter"; -import testN1qlFormatter from "./languages/testN1qlFormatter"; - -testStandardSqlFormatter(sqlFormatter); -testN1qlFormatter(sqlFormatter);