Skip to content

Commit

Permalink
Added test of function to upload Zip to Lambda (#309)
Browse files Browse the repository at this point in the history
* Modify `_mockSetting` returns `AWS`

* Add test for _uploadNew

* Add linefeed

* Add _awsRestore()

this will restore all the methods and services

* Modifies that commonize the same process

* Add test fro _uploadExisting

* Refactoring

Modify mainly to Arrow function
  • Loading branch information
abetomo authored and DeviaVir committed Jun 7, 2017
1 parent 185688d commit 5f176ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
19 changes: 8 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,12 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) {
}
}

Lambda.prototype._uploadExisting = function (lambda, params, cb) {
var request = lambda.updateFunctionCode({
Lambda.prototype._uploadExisting = (lambda, params, cb) => {
const request = lambda.updateFunctionCode({
'FunctionName': params.FunctionName,
'ZipFile': params.Code.ZipFile,
'Publish': params.Publish
}, function (err, data) {
}, (err, data) => {
if (err) {
return cb(err, data)
}
Expand All @@ -442,25 +442,22 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) {
'Environment': params.Environment,
'DeadLetterConfig': params.DeadLetterConfig,
'TracingConfig': params.TracingConfig
}, function (err, data) {
}, (err, data) => {
return cb(err, data)
})
})

request.on('retry', function (response) {
request.on('retry', (response) => {
console.log(response.error.message)
console.log('=> Retrying')
})

return request
}

Lambda.prototype._uploadNew = function (lambda, params, cb) {
var request = lambda.createFunction(params, function (err, data) {
return cb(err, data)
})

request.on('retry', function (response) {
Lambda.prototype._uploadNew = (lambda, params, cb) => {
const request = lambda.createFunction(params, (err, data) => cb(err, data))
request.on('retry', (response) => {
console.log(response.error.message)
console.log('=> Retrying')
})
Expand Down
60 changes: 40 additions & 20 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ const _mockSetting = () => {
callback(null, lambdaMockSettings[method])
})
})

return require('aws-sdk')
}

const _awsRestore = () => {
awsMock.restore('CloudWatchEvents')
awsMock.restore('Lambda')
}

/* global before, after, beforeEach, afterEach, describe, it */
Expand All @@ -105,6 +112,14 @@ describe('lib/main', function () {
this.timeout(60000)
}

let aws = null // mock
let awsLambda = null // mock
before(() => {
aws = _mockSetting()
awsLambda = new aws.Lambda({ apiVersion: '2015-03-31' })
})
after(() => _awsRestore())

beforeEach(function () {
program = Hoek.clone(originalProgram)
})
Expand Down Expand Up @@ -834,25 +849,14 @@ describe('lib/main', function () {
}]
}

let awsLambda = null

before(() => {
fs.writeFileSync(
'event_sources.json',
JSON.stringify(eventSourcesJsonValue)
)
_mockSetting()

awsLambda = new (require('aws-sdk')).Lambda({
apiVersion: '2015-03-31'
})
})

after(() => {
fs.unlinkSync('event_sources.json')
awsMock.restore('CloudWatchEvents')
awsMock.restore('Lambda')
})
after(() => fs.unlinkSync('event_sources.json'))

it('program.eventSourceFile is empty value', (done) => {
program.eventSourceFile = ''
Expand Down Expand Up @@ -935,16 +939,10 @@ describe('lib/main', function () {
'event_sources.json',
JSON.stringify(eventSourcesJsonValue)
)

_mockSetting()
schedule = new ScheduleEvents(require('aws-sdk'))
schedule = new ScheduleEvents(aws)
})

after(() => {
fs.unlinkSync('event_sources.json')
awsMock.restore('CloudWatchEvents')
awsMock.restore('Lambda')
})
after(() => fs.unlinkSync('event_sources.json'))

it('program.eventSourceFile is empty value', (done) => {
program.eventSourceFile = ''
Expand Down Expand Up @@ -985,6 +983,28 @@ describe('lib/main', function () {
})
})

describe('_uploadNew', () => {
it('simple test with mock', (done) => {
const params = lambda._params(program, null)
lambda._uploadNew(awsLambda, params, (err, results) => {
assert.isNull(err)
assert.deepEqual(results, lambdaMockSettings.createFunction)
done()
})
})
})

describe('_uploadExisting', () => {
it('simple test with mock', (done) => {
const params = lambda._params(program, null)
lambda._uploadExisting(awsLambda, params, (err, results) => {
assert.isNull(err)
assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration)
done()
})
})
})

describe('check env vars before create sample files', function () {
const filesCreatedBySetup = [
'.env',
Expand Down

0 comments on commit 5f176ec

Please sign in to comment.