diff --git a/lib/classes/Service.js b/lib/classes/Service.js index 99a0c9baa..284c5c95f 100644 --- a/lib/classes/Service.js +++ b/lib/classes/Service.js @@ -41,8 +41,7 @@ class Service { that.custom = serverlessYaml.custom; that.plugins = serverlessYaml.plugins; that.resources = serverlessYaml.resources; - - // TODO load functions + that.functions = serverlessYaml.functions; }) .then(() => { that.S.instances.yamlParser.parse(path.join(servicePath, 'serverless.env.yaml')); @@ -128,19 +127,25 @@ class Service { } getAllFunctions() { - + return Object.keys(this.functions); } getFunction(functionName) { - + if (functionName in this.functions) { + return this.functions[functionName]; + } + throw new SError(`function ${functionName} doesn't exist in this Service`); } - getEventInFunction(eventName) { - + getEventInFunction(eventName, functionName) { + if (eventName in this.getFunction(functionName).events) { + return this.getFunction(functionName).events[eventName]; + } + throw new SError(`event ${eventName} doesn't exist in function ${functionName}`); } - getAllEventsInFunction() { - + getAllEventsInFunction(functionName) { + return Object.keys(this.getFunction(functionName).events); } getStage(stageName) { @@ -165,7 +170,6 @@ class Service { return Object.keys(this.getStage(stageName).regions); } - // return object getVariables(stageName, regionName) { if (stageName && regionName) { return this.getRegionInStage(stageName, regionName).vars; diff --git a/lib/classes/Utils.js b/lib/classes/Utils.js index 5f32a9b15..e00d336eb 100644 --- a/lib/classes/Utils.js +++ b/lib/classes/Utils.js @@ -82,8 +82,8 @@ class Utils { }); } - generateShortId(maxLen) { - return (Math.round((Math.random() * Math.pow(36, maxLen)))).toString(36); + generateShortId(length) { + return Math.random().toString(36).substr(2, length); } } diff --git a/tests/all.js b/tests/all.js index c82f9a395..1ad28540d 100644 --- a/tests/all.js +++ b/tests/all.js @@ -9,3 +9,4 @@ require('./tests/classes/Utils'); require('./tests/classes/Config'); require('./tests/classes/Service'); require('./tests/classes/YamlParser'); + diff --git a/tests/tests/classes/Serverless.js b/tests/tests/classes/Serverless.js index 97eeea82f..47616055a 100644 --- a/tests/tests/classes/Serverless.js +++ b/tests/tests/classes/Serverless.js @@ -7,7 +7,7 @@ describe('Serverless', () => { describe('#getVersion()', () => { it('should get the correct serverless version', () => { const serverless = new Serverless(); - expect(serverless.getVersion()).to.be.equal('0.5.5'); + expect(serverless.getVersion()).to.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/); }); }); }); diff --git a/tests/tests/classes/Service.js b/tests/tests/classes/Service.js index cffe908e9..9c24c35b8 100644 --- a/tests/tests/classes/Service.js +++ b/tests/tests/classes/Service.js @@ -213,6 +213,91 @@ describe('Service', () => { }); }); + describe('#getFunction()', () => { + + let serviceInstance; + before(() => { + const S = new Serverless(); + serviceInstance = new Service(S); + serviceInstance.functions = { + create: { + handler: 'users.create' + } + }; + }); + + it('should return function object', () => { + expect(serviceInstance.getFunction('create')).to.deep.equal({ handler: 'users.create' }); + }); + + it('should throw error if function does not exist', () => { + expect(()=> { serviceInstance.getFunction('random') }).to.throw(Error); + }); + }); + + describe('#getAllFunctions()', () => { + + it('should return an array of function names in Service', () => { + const S = new Serverless(); + const serviceInstance = new Service(S); + serviceInstance.functions = { + create: { + handler: 'users.create' + }, + list: { + handler: 'users.list' + } + }; + + expect(serviceInstance.getAllFunctions()).to.deep.equal(['create', 'list']); + }); + }); + + describe('#getEventInFunction()', () => { + + let serviceInstance; + before(() => { + const S = new Serverless(); + serviceInstance = new Service(S); + serviceInstance.functions = { + create: { + events: { + schedule: 'rate(5 minutes)' + } + } + }; + }); + + it('should return an event object based on provided function', () => { + expect(serviceInstance.getEventInFunction('schedule', 'create')).to.be.equal('rate(5 minutes)'); + }); + + it('should throw error if function does not exist in service', () => { + expect(() => {serviceInstance.getEventInFunction(null, 'list')}).to.throw(Error); + }); + + it('should throw error if event doesnt exist in function', () => { + expect(() => {serviceInstance.getEventInFunction('randomEvent', 'create')}).to.throw(Error); + }); + }); + + describe('#getAllEventsInFunction()', () => { + it('should return an array of events in a specified function', () => { + const S = new Serverless(); + const serviceInstance = new Service(S); + serviceInstance.functions = { + create: { + events: { + schedule: 'rate(5 minutes)', + bucket: 'my_bucket' + } + } + }; + + expect(serviceInstance.getAllEventsInFunction('create')).to.deep.equal(['schedule', 'bucket']); + }); + }); + describe('#getStage()', () => { let serviceInstance; before(() => { @@ -272,6 +357,7 @@ describe('Service', () => { }); }); + describe('#getRegionInStage()', () => { let serviceInstance; before(() => { diff --git a/tests/tests/classes/Utils.js b/tests/tests/classes/Utils.js index e0fb41252..24008b1c4 100644 --- a/tests/tests/classes/Utils.js +++ b/tests/tests/classes/Utils.js @@ -14,13 +14,13 @@ const yamlParser = new YamlParser(S); describe('Utils', () => { describe('#generateShortId()', () => { it('should generate a shortId', () => { - const id = SUtils.generateShortId(6); + const id = SUtils.generateShortId(); expect(id).to.be.a('string'); }); it('should generate a shortId for the given length', () => { const id = SUtils.generateShortId(6); - expect(id.length).to.equal(6); + expect(id.length).to.be.equal(6); }); });