From 61ddfc0b55bda2948049fa21dd265eaad4a8dd9e Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 19 May 2016 15:50:24 +0200 Subject: [PATCH 1/6] getVersion test now checks a regex --- tests/all.js | 12 ++++++------ tests/tests/classes/Serverless.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/all.js b/tests/all.js index 53f5b8d93..09435d1f2 100644 --- a/tests/all.js +++ b/tests/all.js @@ -8,13 +8,13 @@ describe('All Tests', function() { after(function() {}); + // require('./tests/classes/Serverless'); + // require('./tests/classes/PluginManager'); + // require('./tests/classes/Utils'); + // require('./tests/classes/Config'); + // require('./tests/classes/Service'); require('./tests/classes/Serverless'); - require('./tests/classes/PluginManager'); - require('./tests/classes/Utils'); - require('./tests/classes/Config'); - require('./tests/classes/Service'); - require('./tests/classes/Serverless'); - require('./tests/classes/YamlParser'); + // require('./tests/classes/YamlParser'); // require('./tests/classes/Project'); // require('./tests/classes/ProviderAws'); // require('./tests/classes/Function'); diff --git a/tests/tests/classes/Serverless.js b/tests/tests/classes/Serverless.js index c21da1937..4a9eb2931 100644 --- a/tests/tests/classes/Serverless.js +++ b/tests/tests/classes/Serverless.js @@ -13,7 +13,7 @@ describe('Serverless', () => { 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+)$/); }); }); From 6fc76e7fcbd63a1f201b43f7d31c28114049f526 Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 19 May 2016 15:58:51 +0200 Subject: [PATCH 2/6] commented in tests in all.js --- tests/all.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/all.js b/tests/all.js index 09435d1f2..8ddbf6800 100644 --- a/tests/all.js +++ b/tests/all.js @@ -8,13 +8,12 @@ describe('All Tests', function() { after(function() {}); - // require('./tests/classes/Serverless'); - // require('./tests/classes/PluginManager'); - // require('./tests/classes/Utils'); - // require('./tests/classes/Config'); - // require('./tests/classes/Service'); + require('./tests/classes/PluginManager'); + require('./tests/classes/Utils'); + require('./tests/classes/Config'); + require('./tests/classes/Service'); require('./tests/classes/Serverless'); - // require('./tests/classes/YamlParser'); + require('./tests/classes/YamlParser'); // require('./tests/classes/Project'); // require('./tests/classes/ProviderAws'); // require('./tests/classes/Function'); From e759fe1761b55b29d02365659217f3e1dae4cc4f Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 19 May 2016 21:13:50 +0200 Subject: [PATCH 3/6] added functions and events to Service and tested it --- lib/classes/Service.js | 28 ++++++----- tests/tests/classes/Service.js | 86 ++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/lib/classes/Service.js b/lib/classes/Service.js index 5c7503b98..8f17b70e5 100644 --- a/lib/classes/Service.js +++ b/lib/classes/Service.js @@ -41,9 +41,7 @@ class Service { _this.custom = serverlessYaml.custom; _this.plugins = serverlessYaml.plugins; _this.resources = serverlessYaml.resources; - - // TODO load functions - + _this.functions = serverlessYaml.functions; }) .then(() => { return this.S.instances.yamlParser.parse(path.join(servicePath, 'serverless.env.yaml')); @@ -60,9 +58,6 @@ class Service { toObjectPopulated(options) { options = options || {}; - - // TODO populate Function classes first - return this.S.instances.utils.populate(this, this.toObject(), options); } @@ -71,19 +66,27 @@ class Service { } getAllFunctions() { - + return Object.keys(this.functions); } getFunction(functionName) { - + if (functionName in this.functions) { + return this.functions[functionName]; + } else { + 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]; + } else { + throw new SError(`event ${eventName} doesn't exist in function ${functionName}`); + } } - getAllEventsInFunction() { - + getAllEventsInFunction(functionName) { + return Object.keys(this.getFunction(functionName).events); } getStage(stageName) { @@ -110,7 +113,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/tests/tests/classes/Service.js b/tests/tests/classes/Service.js index 00af2fcfd..d50198aee 100644 --- a/tests/tests/classes/Service.js +++ b/tests/tests/classes/Service.js @@ -222,6 +222,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 a region object based on provided stage', () => { + 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; @@ -283,6 +368,7 @@ describe('Service', () => { }); }); + describe('#getRegionInStage()', () => { let serviceInstance; From bed5b7993f67d56032434046a9035d55276d203f Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 19 May 2016 21:16:14 +0200 Subject: [PATCH 4/6] fixed test case name --- tests/tests/classes/Service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/classes/Service.js b/tests/tests/classes/Service.js index d50198aee..85ea9254f 100644 --- a/tests/tests/classes/Service.js +++ b/tests/tests/classes/Service.js @@ -277,7 +277,7 @@ describe('Service', () => { }; }); - it('should return a region object based on provided stage', () => { + it('should return an event object based on provided function', () => { expect(serviceInstance.getEventInFunction('schedule', 'create')).to.be.equal('rate(5 minutes)'); }); From b84b3b12b48f53dfcb9b9f9ae764fbf7bb0ee94c Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Fri, 20 May 2016 08:29:03 +0200 Subject: [PATCH 5/6] fixed shortId test --- tests/tests/classes/Utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/classes/Utils.js b/tests/tests/classes/Utils.js index ea7557d4f..d4b8a453a 100644 --- a/tests/tests/classes/Utils.js +++ b/tests/tests/classes/Utils.js @@ -42,7 +42,7 @@ describe('Utils', () => { 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.at.least(4); }); }); From 590fc4dee0b428f34b4133960b8a98cacb856866 Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Fri, 20 May 2016 08:43:41 +0200 Subject: [PATCH 6/6] Utils: shortId is now consistent --- lib/classes/Utils.js | 4 ++-- tests/tests/classes/Utils.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/classes/Utils.js b/lib/classes/Utils.js index 9578b8c9e..bdd864451 100644 --- a/lib/classes/Utils.js +++ b/lib/classes/Utils.js @@ -117,8 +117,8 @@ class Utils { return data.data; } - generateShortId(maxLen) { - return (Math.round((Math.random() * Math.pow(36, maxLen)))).toString(36); + generateShortId(length) { + return Math.random().toString(36).substr(2, length); } populate(service, data, options) { diff --git a/tests/tests/classes/Utils.js b/tests/tests/classes/Utils.js index d4b8a453a..ae1416d12 100644 --- a/tests/tests/classes/Utils.js +++ b/tests/tests/classes/Utils.js @@ -36,13 +36,13 @@ 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.be.at.least(4); + expect(id.length).to.be.equal(6); }); });