Module remove: fix serverlessstate tests

This commit is contained in:
Austen Collins 2016-02-03 19:00:49 -08:00
parent 741bfd15c9
commit da479d231b
3 changed files with 86 additions and 104 deletions

View File

@ -5,10 +5,10 @@
*/
const SError = require('./ServerlessError'),
SUtils = require('./utils/index'),
_ = require('lodash'),
path = require('path'),
fs = require('fs');
SUtils = require('./utils/index'),
_ = require('lodash'),
path = require('path'),
fs = require('fs');
class ServerlessState {
@ -35,9 +35,9 @@ class ServerlessState {
let _this = this;
return _this.project.load()
.then(function() {
return _this.meta.load();
});
.then(function() {
return _this.meta.load();
});
}
/**
@ -50,9 +50,9 @@ class ServerlessState {
let _this = this;
return _this.project.save({ deep: true })
.then(function() {
return _this.meta.save({ deep: true });
});
.then(function() {
return _this.meta.save({ deep: true });
});
}
/**
@ -186,8 +186,8 @@ class ServerlessState {
getComponents(options) {
let _this = this,
allComponents = [],
foundComponents = [];
allComponents = [],
foundComponents = [];
// Get all
for (let i = 0; i < Object.keys(_this.project.components).length; i++) {
@ -220,15 +220,15 @@ class ServerlessState {
/**
* Get Functions
* - Returns an array of this state's function instances
* - Options: paths, component, function
* - Options: paths
* - options.paths is an array of Serverless paths like this: ['component/functionOne', 'component/subfolder1/subfolder2/functionTwo']
*/
getFunctions(options) {
let _this = this,
allFunctions = [],
foundFunctions = [];
allFunctions = [],
foundFunctions = [];
// Get all
for (let i = 0; i < Object.keys(_this.project.components).length; i++) {
@ -242,31 +242,23 @@ class ServerlessState {
if (!options) return allFunctions;
if (options && Object.keys(options).length === 1 && options.returnPaths === true) return allFunctions.map(function(d) { return d._config.sPath });
// If component, module or functions convert to sPath
// TODO: Eventually remove and support sPath only
if (options.component) {
options.paths = [options.component];
if (options.module) options.paths[0] = options.paths[0] + '/' + options.module;
if (options.function) options.paths[0] = options.paths[0] + '/' + options.function;
}
// If options specified, loop through and find the ones specified
for (let i = 0; i < allFunctions.length; i++) {
let func = allFunctions[i];
if (options.component && options.cPath && options.function) {
if (func._config.component == options.component && func._config.module == options.module && func.name == options.function) {
foundFunctions.push(options.returnPaths ? func._config.sPath : func);
let func = allFunctions[i];
for (let j = 0; j < options.paths.length; j++) {
if (func._config.sPath.indexOf(options.paths[j]) !== -1) {
foundFunctions.push(func);
break;
}
continue;
}
if (options.component && options.module) {
if (func._config.component == options.component && func._config.module == options.module) {
foundFunctions.push(options.returnPaths ? func._config.sPath : func);
}
continue;
}
if (options.component) {
if (func._config.component == options.component) {
foundFunctions.push(options.returnPaths ? func._config.sPath : func);
}
continue;
}
if (options.paths && options.paths.indexOf(func._config.sPath) !== -1) {
foundFunctions.push(func);
continue;
}
}
@ -276,28 +268,24 @@ class ServerlessState {
/**
* Get Endpoints
* - Returns an array of this state's function instances
* - Options: paths, component, module, function, endpointPath, endpointMethod
* - options.paths is an array of Serverless paths like this: ['component/moduleOne/functionOne@moduleOne/functionOne~GET']
* - Options: paths, endpointPath, endpointMethod
* - options.paths is an array of Serverless paths like this: ['component/groupOne/functionOne@moduleOne/functionOne~GET']
*/
getEndpoints(options) {
let _this = this,
allEndpoints = [],
foundEndpoints = [];
let _this = this,
allEndpoints = [],
foundEndpoints = [];
// Get all functions
for (let i = 0; i < Object.keys(_this.project.components).length; i++) {
let component = _this.project.components[Object.keys(_this.project.components)[i]];
if (!component.modules) continue;
for (let j = 0; j < Object.keys(component.modules).length; j++) {
let module = component.modules[Object.keys(component.modules)[j]];
if (!module.functions) continue;
for (let k = 0; k < Object.keys(module.functions).length; k++) {
let func = module.functions[Object.keys(module.functions)[k]];
for (let l = 0; l < func.endpoints.length; l++) {
allEndpoints.push(func.endpoints[l]);
}
if (!component.functions) continue;
for (let k = 0; k < Object.keys(component.functions).length; k++) {
let func = component.functions[Object.keys(component.functions)[k]];
for (let l = 0; l < func.endpoints.length; l++) {
allEndpoints.push(func.endpoints[l]);
}
}
}
@ -306,61 +294,39 @@ class ServerlessState {
if (!options) return allEndpoints;
if (options && Object.keys(options).length === 1 && options.returnPaths === true) return allEndpoints.map(function(d) { return d._config.sPath });
// If component, module or functions convert to sPath
// TODO: Eventually remove and support sPath only
if (options.component) {
options.paths = [options.component];
if (options.module) options.paths[0] = options.paths[0] + '/' + options.module;
if (options.function) options.paths[0] = options.paths[0] + '/' + options.function;
if (options.endpointPath) options.paths[0] = options.paths[0] + '@' + options.endpointPath;
if (options.endpointMethod) options.paths[0] = options.paths[0] + '~' + options.endpointMethod;
console.log("HERE")
}
// If options specified, loop through functions and find the ones specified
for (let i = 0; i < allEndpoints.length; i++) {
let endpoint = allEndpoints[i];
if (options.component && options.module && options.function && options.endpointPath && options.endpointMethod) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module && endpoint._config.function == options.function && endpoint.path == options.endpointPath && endpoint.method == options.endpointMethod) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
for (let j = 0; j < options.paths.length; j++) {
console.log(endpoint._config.sPath, options.paths[j], endpoint._config.sPath.indexOf(options.paths[j]));
if (endpoint._config.sPath.indexOf(options.paths[j]) !== -1) {
foundEndpoints.push(endpoint);
break;
}
continue;
}
if (options.component && options.module && options.function && options.endpointPath && !options.endpointMethod) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module && endpoint._config.function == options.function && endpoint.path == options.endpointPath) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
// Filter if endpointPath or endpointMethod is specified
if (options.endpointPath) {
for (let i = 0; i < foundEndpoints.length; i++) {
if (foundEndpoints[i].path !== options.endpointPath) foundEndpoints.splice(i, 1);
}
if (options.component && options.module && options.function && options.endpointMethod && !options.endpointPath) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module && endpoint._config.function == options.function && endpoint.method == options.endpointMethod) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.component && options.module && options.function && !options.endpointPath && !options.endpointMethod) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module && endpoint._config.function == options.function) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.component && options.module && options.endpointMethod && !options.function && !options.endpointPath) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module && endpoint.method == options.endpointMethod) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.component && options.module && !options.function && !options.endpointPath && !options.endpointMethod) {
if (endpoint._config.component == options.component && endpoint._config.module == options.module) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.component && options.endpointMethod && !options.module && !options.function && !options.endpointPath) {
if (endpoint._config.component == options.component && endpoint.method == options.endpointMethod) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.component && !options.module && !options.function && !options.endpointPath && !options.endpointMethod) {
if (endpoint._config.component == options.component) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
}
continue;
}
if (options.paths && options.paths.indexOf(endpoint._config.sPath) !== -1) {
foundEndpoints.push(endpoint);
continue;
}
if (options.endpointMethod) {
for (let i = 0; i < foundEndpoints.length; i++) {
if (foundEndpoints[i].method !== options.endpointMethods) foundEndpoints.splice(i, 1);
}
}

View File

@ -33,7 +33,7 @@
}
},
{
"path": "module1/function3",
"path": "group1/function3",
"method": "POST",
"authorizationType": "none",
"apiKeyRequired": false,

View File

@ -144,7 +144,7 @@ describe('Test Serverless State Class', function() {
it('Get functions w/o paths', function(done) {
let functions = instance.getFunctions();
assert.equal(true, functions.length === 3);
assert.equal(true, functions.length === 5);
done();
});
@ -154,15 +154,24 @@ describe('Test Serverless State Class', function() {
done();
});
it('Get functions w/ partial paths', function(done) {
let functions = instance.getFunctions({ paths: ['nodejscomponent/group1/group2'] });
assert.equal(true, functions.length === 1);
done();
});
it('Get functions by component, module and function', function(done) {
let functions = instance.getFunctions({ component: 'nodejscomponent', module: 'group1', function: 'function1' });
let functions = instance.getFunctions({
component: 'nodejscomponent',
module: 'group1',
function: 'function1' });
assert.equal(true, functions.length === 1);
done();
});
it('Get endpoints w/o paths', function(done) {
let endpoints = instance.getEndpoints();
assert.equal(true, endpoints.length === 4);
assert.equal(true, endpoints.length === 7);
done();
});
@ -172,8 +181,15 @@ describe('Test Serverless State Class', function() {
done();
});
it('Get endpoints w/ partial paths', function(done) {
let endpoints = instance.getEndpoints({ paths: ['nodejscomponent/group1/group2'] });
assert.equal(true, endpoints.length === 2);
done();
});
it('Get endpoints by component, module, function, path and method', function(done) {
let endpoints = instance.getEndpoints({ component: 'nodejscomponent', module: 'group1', function: 'function3', endpointPath: 'group1/function3', endpointMethod: 'POST' });
console.log(endpoints)
assert.equal(true, endpoints.length === 1);
done();
});
@ -184,8 +200,8 @@ describe('Test Serverless State Class', function() {
done();
});
it('Get endpoints by component and method', function(done) {
let endpoints = instance.getEndpoints({ component: 'nodejscomponent', endpointMethod: 'GET' });
it('Get endpoints by method', function(done) {
let endpoints = instance.getEndpoints({ paths: ['nodejscomponent/group1'], endpointMethod: 'GET' });
assert.equal(true, endpoints.length === 3);
done();
});