Skip to content

Commit

Permalink
added functions and events to Service and tested it
Browse files Browse the repository at this point in the history
  • Loading branch information
eahefnawy committed May 19, 2016
1 parent 6435d39 commit e759fe1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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;
Expand Down
86 changes: 86 additions & 0 deletions tests/tests/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -283,6 +368,7 @@ describe('Service', () => {
});
});


describe('#getRegionInStage()', () => {

let serviceInstance;
Expand Down

0 comments on commit e759fe1

Please sign in to comment.