From 4a024d662e50e7376df9718804faaa2fe2eb545d Mon Sep 17 00:00:00 2001 From: nicolaslabbe Date: Fri, 30 Sep 2016 11:28:31 +0200 Subject: [PATCH] remove unused variables + eslint + add UT --- src/cli/helpers/abe-sql.js | 174 ++++++--------------------- src/cli/helpers/abe-utils.js | 47 ++++---- src/tasks/nodemon.js | 1 - test/fixtures/data/article-1.json | 3 +- test/fixtures/data/homepage-1.json | 3 +- test/fixtures/reference/test.json | 5 + test/fixtures/templates/article.html | 2 +- test/request.js | 57 ++++++++- 8 files changed, 127 insertions(+), 165 deletions(-) create mode 100644 test/fixtures/reference/test.json diff --git a/src/cli/helpers/abe-sql.js b/src/cli/helpers/abe-sql.js index 5eb7e550..668949fe 100755 --- a/src/cli/helpers/abe-sql.js +++ b/src/cli/helpers/abe-sql.js @@ -1,22 +1,10 @@ -import extend from 'extend' -import loremIpsum from 'lorem-ipsum' -import clc from 'cli-color' import {parse} from 'node-sqlparser' -import fse from 'fs-extra' -import requestAjax from 'request' -import ajaxRequest from 'ajax-request' import {Promise} from 'es6-promise' import path from 'path' import { config, - cli, - log, Manager, - folderUtils, - fileUtils, FileParser, - Util, - fileAttr, getAttr } from '../' @@ -29,8 +17,6 @@ export default class Sql { static recurseWhere(where, operator = '') { var arr = [] var obj = {} - var operatorLeft = operator - var operatorRight = operator if(typeof where.left !== 'undefined' && where.left !== null && typeof where.right !== 'undefined' && where.right !== null @@ -76,7 +62,6 @@ export default class Sql { static cleanRequest(str, jsonPage) { var matchFrom = /from .(.*?) / var matchVariable = /{{([a-zA-Z]*)}}/ - var match var matchFromExec = matchFrom.exec(str) if(typeof matchFromExec !== 'undefined' && matchFromExec !== null @@ -85,19 +70,15 @@ export default class Sql { var fromMatch var toReplace = matchFromExec[1] while (fromMatch = matchVariable.exec(toReplace)) { - if(typeof fromMatch !== 'undefined' && fromMatch !== null - && typeof fromMatch[1] !== 'undefined' && fromMatch[1] !== null) { - - try { - var value = eval('jsonPage.' + fromMatch[1]) - if(typeof value !== 'undefined' && value !== null) { - toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value) - }else { - toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '') - } - }catch(e) { - + try { + var value = eval('json.' + fromMatch[1]) + if(typeof value !== 'undefined' && value !== null) { + toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value) + }else { + toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '') } + }catch(e) { + } } @@ -313,23 +294,17 @@ export default class Sql { } static executeFromClause(statement, pathFromClause){ - var files = [] - var recursive = 99 - var fileRegex = /(.*(-abe-).*Z\.json)/ var from = Sql.sanitizeFromStatement(statement) // if the from clause ends with a dot, we won't recurse the directory analyze if(from.slice(-1) === '.'){ - recursive = 0 from = from.slice(0, -1) } var fromDirectory = Sql.getFromDirectory(from, pathFromClause) - var dateStart = new Date() - var list = Manager.instance.getList() - var files_array = list.filter((element, index, arr) => { + var files_array = list.filter((element, index) => { if(element.publish) { if (element.path.indexOf(fromDirectory) > -1) { return true @@ -357,7 +332,7 @@ export default class Sql { } static executeQuery(pathexecuteQuery, match, jsonPage) { - var p = new Promise((resolve, reject) => { + var p = new Promise((resolve) => { var res = Sql.execQuery(pathexecuteQuery, match, jsonPage) resolve(res) }).catch(function(e) { @@ -367,6 +342,17 @@ export default class Sql { return p } + /** + * check if a given string an url, string json, file url, abe sql request + * + * Sql.get('http://google.com') + * Sql.get('{"test":"test"}') + * Sql.get('select * from ../') + * Sql.get('test') + * + * @param {String} str + * @return {String} url | request | value | file | other + */ static getSourceType(str) { if(/http:\/\/|https:\/\//.test(str)) { return 'url' @@ -376,8 +362,11 @@ export default class Sql { return 'request' } - if(/[\{|\[][[\S\s]*?[\{|\]]/.test(str)) { + try { + JSON.parse(str) return 'value' + }catch(e) { + } if(/\.json/.test(str)) { @@ -406,7 +395,7 @@ export default class Sql { jsonValues[column] = json[column] } }) - jsonValues["abe_meta"] = json["abe_meta"] + jsonValues['abe_meta'] = json['abe_meta'] }else { jsonValues = json } @@ -423,137 +412,48 @@ export default class Sql { } static whereEquals(where, value, compare, json) { - var shouldAdd = true + var shouldAdd = json if(where.left === 'template' || where.left === 'abe_meta.template') { if(value.indexOf('/') > -1 && value !== compare) { shouldAdd = false }else if(value.indexOf('/') === -1 && compare.indexOf(value) === -1) { shouldAdd = false } - }else { - - // if both entries are Array - var foundOne = false - if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]' - && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { - - Array.prototype.forEach.call(value, (v) => { - if(compare.includes(v)) { - foundOne = true - } - }) - }else if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]') { // only "compare" is Array - if(compare.includes(value)) { - foundOne = true - } - }else if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { // only "value" is Array - if(value.includes(compare)) { - foundOne = true - } - }else if(value === compare) { // only none is Array - foundOne = true - } - - if(!foundOne) { + if(value !== compare) { // only none is Array shouldAdd = false } } - if (shouldAdd) { - return json - } return shouldAdd } static whereNotEquals(where, value, compare, json) { - var shouldAdd = true + var shouldAdd = json if(where.left === 'template' || where.left === 'abe_meta.template') { - if (value.indexOf('/') > -1 && value === compare) { shouldAdd = false } else if (value.indexOf('/') === -1 && compare.indexOf(value) !== -1) { shouldAdd = false } - }else { - - // if both entries are Array - var foundOne = false - if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]' - && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { - - Array.prototype.forEach.call(value, (v) => { - if(compare.includes(v)) { - foundOne = true - } - }) - }else if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]') { // only "compare" is Array - if(compare.includes(value)) { - foundOne = true - } - }else if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { // only "value" is Array - if(value.includes(compare)) { - foundOne = true - } - }else if(value === compare) { // only none is Array - foundOne = true - } - - if(foundOne) { + if(value === compare) { // only none is Array shouldAdd = false } } - if (shouldAdd) { - return json - } return shouldAdd } static whereLike(where, value, compare, json) { - var shouldAdd = true + var shouldAdd = json if(where.left === 'template' || where.left === 'abe_meta.template') { - if(value.indexOf(compare) === -1) { shouldAdd = false } - }else { - - // if both entries are Array - var foundOne = false - if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]' - && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { - - Array.prototype.forEach.call(compare, (v) => { - Array.prototype.forEach.call(value, (v2) => { - if(v.indexOf(v2) !== -1) { - foundOne = true - } - }) - }) - }else if(typeof compare === 'object' && Object.prototype.toString.call(compare) === '[object Array]') { // only "compare" is Array - Array.prototype.forEach.call(compare, (v) => { - if(v.indexOf(value) !== -1) { - foundOne = true - } - }) - }else if(typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]') { // only "value" is Array - Array.prototype.forEach.call(value, (v) => { - if(compare.indexOf(v) !== -1) { - foundOne = true - } - }) - }else if(value.indexOf(compare) === -1) { - foundOne = true - } - - if(foundOne) { + if(value.indexOf(compare) === -1) { shouldAdd = false } } - if (shouldAdd) { - return json - } return shouldAdd } @@ -566,8 +466,8 @@ export default class Sql { var compare if((where.left === 'template' || where.left === 'abe_meta.template') - && typeof jsonDoc["abe_meta"] !== 'undefined' && jsonDoc["abe_meta"] !== null) { - value = FileParser.getTemplate(jsonDoc["abe_meta"].template) + && typeof jsonDoc['abe_meta'] !== 'undefined' && jsonDoc['abe_meta'] !== null) { + value = FileParser.getTemplate(jsonDoc['abe_meta'].template) }else { try { value = eval('jsonDoc.' + where.left) @@ -595,13 +495,13 @@ export default class Sql { if(typeof value !== 'undefined' && value !== null) { switch(where.compare) { case '=': - shouldAdd = Sql.whereEquals(where, value, compare, shouldAdd) + shouldAdd = Sql.whereEquals(where.left, value, compare, shouldAdd) break case '!=': - shouldAdd = Sql.whereNotEquals(where, value, compare, shouldAdd) + shouldAdd = Sql.whereNotEquals(where.left, value, compare, shouldAdd) break case 'LIKE': - shouldAdd = Sql.whereLike(where, value, compare, shouldAdd) + shouldAdd = Sql.whereLike(where.left, value, compare, shouldAdd) break default: break diff --git a/src/cli/helpers/abe-utils.js b/src/cli/helpers/abe-utils.js index 73767525..0c171cb7 100755 --- a/src/cli/helpers/abe-utils.js +++ b/src/cli/helpers/abe-utils.js @@ -306,11 +306,14 @@ export default class Utils { return obj } - static requestList(obj, sourceAttr, tplPath, match, jsonPage) { + static requestList(obj, tplPath, match, jsonPage) { var p = new Promise((resolve, reject) => { Sql.executeQuery(tplPath, match, jsonPage) .then((data) => { - jsonPage[sourceAttr][obj.key] = data + if (!jsonPage['abe_source']) { + jsonPage['abe_source'] = {} + } + jsonPage['abe_source'][obj.key] = data if (!obj.editable) { if (obj['max-length']) { jsonPage[obj.key] = data.slice(0, obj['max-length']) @@ -336,7 +339,7 @@ export default class Utils { return p } - static valueList(obj, sourceAttr, tplPath, match, jsonPage) { + static valueList(obj, match, jsonPage) { var p = new Promise((resolve, reject) => { var value = Sql.getDataSource(match) @@ -344,9 +347,9 @@ export default class Utils { try{ value = JSON.parse(value) - jsonPage[sourceAttr][obj.key] = value + jsonPage['abe_source'][obj.key] = value }catch(e){ - jsonPage[sourceAttr][obj.key] = null + jsonPage['abe_source'][obj.key] = null console.log(clc.red(`Error ${value}/is not a valid JSON`), `\n${e}`) } } @@ -356,7 +359,7 @@ export default class Utils { return p } - static urlList(obj, sourceAttr, tplPath, match, jsonPage) { + static urlList(obj, tplPath, match, jsonPage) { var p = new Promise((resolve, reject) => { if(obj.autocomplete !== true && obj.autocomplete !== 'true') { var host = obj.sourceString @@ -400,14 +403,14 @@ export default class Utils { if(typeof body === 'string') { var parsedBody = JSON.parse(body) if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Array]') { - jsonPage[sourceAttr][obj.key] = parsedBody + jsonPage['abe_source'][obj.key] = parsedBody }else if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Object]') { - jsonPage[sourceAttr][obj.key] = [parsedBody] + jsonPage['abe_source'][obj.key] = [parsedBody] } }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Array]') { - jsonPage[sourceAttr][obj.key] = body + jsonPage['abe_source'][obj.key] = body }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Object]') { - jsonPage[sourceAttr][obj.key] = body + jsonPage['abe_source'][obj.key] = body } } catch(e) { console.log(clc.red(`Error ${obj.sourceString} is not a valid JSON`), `\n${e}`) @@ -425,7 +428,7 @@ export default class Utils { localReq.end() }else { - jsonPage[sourceAttr][obj.key] = obj.sourceString + jsonPage['abe_source'][obj.key] = obj.sourceString resolve() } }) @@ -433,22 +436,20 @@ export default class Utils { return p } - static fileList(obj, sourceAttr, tplPath, match, jsonPage) { + static fileList(obj, tplPath, match, jsonPage) { var p = new Promise((resolve, reject) => { - jsonPage[sourceAttr][obj.key] = FileParser.getJson(path.join(config.root, obj.sourceString)) + jsonPage['abe_source'][obj.key] = FileParser.getJson(path.join(config.root, obj.sourceString)) resolve() }) return p } - static nextDataList(tplPath, text, jsonPage, match) { + static nextDataList(tplPath, jsonPage, match) { var p = new Promise((resolve, reject) => { // var t = new TimeMesure() - var sourceAttr = config.source.name - - if(typeof jsonPage[sourceAttr] === 'undefined' || jsonPage[sourceAttr] === null) { - jsonPage[sourceAttr] = {} + if(typeof jsonPage['abe_source'] === 'undefined' || jsonPage['abe_source'] === null) { + jsonPage['abe_source'] = {} } var obj = Utils.getAllAttributes(match, jsonPage) @@ -458,7 +459,7 @@ export default class Utils { switch (type) { case 'request': - Utils.requestList(obj, sourceAttr, tplPath, match, jsonPage) + Utils.requestList(obj, tplPath, match, jsonPage) .then(() => { // t.duration(match) resolve() @@ -467,7 +468,7 @@ export default class Utils { }) break case 'value': - Utils.valueList(obj, sourceAttr, tplPath, match, jsonPage) + Utils.valueList(obj, match, jsonPage) .then(() => { resolve() }).catch((e) => { @@ -475,7 +476,7 @@ export default class Utils { }) break case 'url': - Utils.urlList(obj, sourceAttr, tplPath, match, jsonPage) + Utils.urlList(obj, tplPath, match, jsonPage) .then(() => { resolve() }).catch((e) => { @@ -483,7 +484,7 @@ export default class Utils { }) break case 'file': - Utils.fileList(obj, sourceAttr, tplPath, match, jsonPage) + Utils.fileList(obj, tplPath, match, jsonPage) .then(() => { resolve() }).catch((e) => { @@ -507,7 +508,7 @@ export default class Utils { let util = new Utils() var matches = util.dataRequest(text) Array.prototype.forEach.call(matches, (match) => { - promises.push(Utils.nextDataList(tplPath, text, jsonPage, match[0])) + promises.push(Utils.nextDataList(tplPath, jsonPage, match[0])) }) Promise.all(promises) diff --git a/src/tasks/nodemon.js b/src/tasks/nodemon.js index 7cbee0c6..43de67c7 100755 --- a/src/tasks/nodemon.js +++ b/src/tasks/nodemon.js @@ -33,7 +33,6 @@ nodemon({ process.env.ROOT + '/plugins/**/**/*.js', process.env.ROOT + '/abe.json', process.env.ROOT + '/locales/*', - process.env.ROOT + '/test/*', process.env.ROOT + '/hooks/**/*.js', process.env.ROOT + '/reference/**/*.json' ], diff --git a/test/fixtures/data/article-1.json b/test/fixtures/data/article-1.json index b2499a62..6bf14e29 100644 --- a/test/fixtures/data/article-1.json +++ b/test/fixtures/data/article-1.json @@ -4,5 +4,6 @@ "link": "/article-1.json", "status": "publish", "date": "2016-08-11T16:40:41.974Z" - } + }, + "title":"test" } \ No newline at end of file diff --git a/test/fixtures/data/homepage-1.json b/test/fixtures/data/homepage-1.json index 6c1a23d9..dd0e3162 100644 --- a/test/fixtures/data/homepage-1.json +++ b/test/fixtures/data/homepage-1.json @@ -4,5 +4,6 @@ "link": "/homepage-1.json", "status": "publish", "date": "2016-07-11T16:40:41.974Z" - } + }, + "title":"test" } \ No newline at end of file diff --git a/test/fixtures/reference/test.json b/test/fixtures/reference/test.json new file mode 100644 index 00000000..e6cffbda --- /dev/null +++ b/test/fixtures/reference/test.json @@ -0,0 +1,5 @@ +{ + "ref1": "ref1", + "ref2": "ref2", + "ref3": "ref3" +} \ No newline at end of file diff --git a/test/fixtures/templates/article.html b/test/fixtures/templates/article.html index d983d8cb..aa3934e9 100644 --- a/test/fixtures/templates/article.html +++ b/test/fixtures/templates/article.html @@ -1,4 +1,4 @@ -{{abe type='data' key='top_things_slider_highlight' desc='Automatic slider' source='select abe_meta from ./' editable='false'}} +{{abe type='data' key='title' desc='select title' source='select title,abe_meta from ./' editable='false'}} {{abe type='import' file='title.html'}} diff --git a/test/request.js b/test/request.js index 14172fdf..a233b117 100644 --- a/test/request.js +++ b/test/request.js @@ -1,4 +1,5 @@ var chai = require('chai'); +var path = require('path'); var config = require('../src/cli').config config.set({root: __dirname + '/fixtures'}) @@ -28,7 +29,7 @@ describe('Request', function() { */ it('Util.getAllAttributes()', function(done) { var attributes = Util.getAllAttributes(this.fixture.tag, this.fixture.json) - chai.assert.equal(attributes.sourceString, 'select abe_meta from ./', 'sourceString is ok') + chai.expect(attributes.sourceString).to.contain('select'); done(); }); @@ -153,4 +154,58 @@ describe('Request', function() { ) ); }); + + /** + * Sql.whereEquals + * + */ + it('Sql.whereOr()', function() { + var article = fileAttr.getDocumentRevision('article-1.json') + + var res = Sql.handleSqlRequest('select title from ./ where template=`article` AND template=`test` AND template=`test`', {}) + var res = Sql.handleSqlRequest('select title from ./ where template=`article` OR template=`test`', {}) + + // chai.expect(article) + // .to.deep.equal( + // Sql.whereOr( + // [{ left: 'template' }], + // article.abe_meta.template, + // "article", + // article + // ) + // ); + }); + + /** + * Sql.whereLike + * + */ + it('Sql.getSourceType()', function() { + chai.expect(Sql.getSourceType('http://google.com')).to.equal('url'); + chai.expect(Sql.getSourceType('select * from test')).to.equal('request'); + chai.expect(Sql.getSourceType('{"test":"test"}')).to.equal('value'); + chai.expect(Sql.getSourceType('references.json')).to.equal('file'); + chai.expect(Sql.getSourceType('test')).to.equal('other'); + }); + + /** + * Sql.requestList + * + */ + it('Sql.requestList()', function(done) { + let util = new Util() + var matches = util.dataRequest(this.fixture.tag) + + chai.expect(matches[0][0]).to.not.be.null + + var attributes = Util.getAllAttributes(matches[0][0], {}) + chai.expect(matches[0][0]).to.not.be.null + + var jsonPage = {} + Util.requestList(attributes, '', matches[0][0], jsonPage) + .then(function () { + chai.expect(jsonPage.abe_source).to.not.be.undefined + done() + }) + }); });