diff --git a/.travis.yml b/.travis.yml index bfe426e152..4e90981863 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,12 +53,15 @@ cache: - bigquery/node_modules/ - computeengine/node_modules/ - datastore/node_modules/ + - debugger/node_modules/ - functions/uuid/node_modules/ - logging/node_modules/ - monitoring/node_modules/ - prediction/node_modules/ - pubsub/node_modules/ - storage/node_modules/ + - trace/node_modules/ + - vision/node_modules/ services: - redis-server diff --git a/package.json b/package.json index f281d721ee..79cddef87d 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,9 @@ "before", "beforeEach", "describe", - "it" + "it", + "assert", + "sinon" ], "ignore": [ "appengine/bower/public/bower_components/**", @@ -45,10 +47,8 @@ ] }, "scripts": { - "ava": "ava --match='!*: dependencies should install*' --timeout=2m --fail-fast --concurrency=5", - "ava:deps": "npm run deps_appengine && npm run ava", - "cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*' --timeout=2m --fail-fast --concurrency=5 --cache", - "deps_appengine": "ava --match='*: dependencies should install*'", + "mocha": "mocha -R dot -t 120000 --require intelli-espower-loader test/_setup.js test/**/*.test.js", + "cover": "nyc mocha --cache -R dot -t 180000 --require intelli-espower-loader test/_setup.js test/**/*.test.js", "deps_bigquery": "cd bigquery; npm i; cd ../", "deps_computeengine": "cd computeengine; npm i; cd ../", "deps_datastore": "cd datastore; npm i; cd ../", @@ -69,17 +69,14 @@ "report-html": "nyc report --reporter=html", "test": "npm run lint && npm run cover" }, - "ava": { - "files": [ - "test/**/*.test.js" - ] - }, "devDependencies": { "async": "^1.5.2", - "ava": "^0.15.2", "codecov": "^1.0.1", + "intelli-espower-loader": "^1.0.1", + "mocha": "^2.5.3", "nodejs-repo-tools": "git+https://github.com/GoogleCloudPlatform/nodejs-repo-tools.git#bbbb6035d77671eb053dbe6b6f0e3ff983f79639", "nyc": "^6.4.4", + "power-assert": "^1.4.1", "proxyquire": "^1.7.9", "request": "^2.72.0", "semistandard": "^8.0.0", diff --git a/test/_setup.js b/test/_setup.js new file mode 100644 index 0000000000..1b5615fe48 --- /dev/null +++ b/test/_setup.js @@ -0,0 +1,30 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var assert = require('power-assert'); +var sinon = require('sinon'); + +global.assert = assert; +global.sinon = sinon; + +beforeEach(function () { + sinon.stub(console, 'error'); + sinon.stub(console, 'log'); +}); + +afterEach(function () { + console.error.restore(); + console.log.restore(); +}); diff --git a/test/appengine/all.test.js b/test/appengine/all.test.js index 8d3532aaa3..bde97c8834 100644 --- a/test/appengine/all.test.js +++ b/test/appengine/all.test.js @@ -13,17 +13,15 @@ 'use strict'; -var test = require('ava'); var spawn = require('child_process').spawn; var request = require('request'); var fs = require('fs'); var path = require('path'); var async = require('async'); -var cwd = process.cwd(); var projectId = process.env.GCLOUD_PROJECT; function getPath (dir) { - return path.join(cwd, '/../../', dir); + return path.join(__dirname, '/../../', dir); } function changeScaling (dir) { @@ -323,36 +321,40 @@ function testRequest (url, sample, cb) { var port = 8080; sampleTests.forEach(function (sample) { - sample.env = sample.env || {}; - sample.env.PORT = port; - if (sample.dir === 'appengine/parse-server') { - sample.env.SERVER_URL = sample.env.SERVER_URL + port + '/parse'; - } - port++; - test.cb.serial(sample.dir + ': dependencies should install', function (t) { - testInstallation(sample, t.end); - }); + describe(sample.dir, function () { + sample.env = sample.env || {}; + sample.env.PORT = port; + if (sample.dir === 'appengine/parse-server') { + sample.env.SERVER_URL = sample.env.SERVER_URL + port + '/parse'; + } + port++; + it('should install dependencies', function (done) { + testInstallation(sample, done); + }); - if (sample.TRAVIS && !process.env.TRAVIS) { - return; - } + if (sample.TRAVIS && !process.env.TRAVIS) { + return; + } - if (sample.TRAVIS_NODE_VERSION && process.env.TRAVIS && - process.env.TRAVIS_NODE_VERSION !== sample.TRAVIS_NODE_VERSION) { - return; - } + if (sample.TRAVIS_NODE_VERSION && process.env.TRAVIS && + process.env.TRAVIS_NODE_VERSION !== sample.TRAVIS_NODE_VERSION) { + return; + } - test.cb.serial(sample.dir + ' should return 200 and Hello World', function (t) { - testLocalApp(sample, t.end); + it('should return 200 and Hello World', function (done) { + testLocalApp(sample, done); + }); }); }); if (process.env.TRAVIS && process.env.DEPLOY_TESTS) { - test.cb.serial('should deploy all samples', function (t) { - // 30 minutes because deployments are slow - this.timeout(30 * 60 * 1000); + describe('deployments', function () { + it('should deploy all samples', function (done) { + // 30 minutes because deployments are slow + this.timeout(30 * 60 * 1000); - testDeployments(t.end); + testDeployments(done); + }); }); } diff --git a/test/bigquery/dataset_size.test.js b/test/bigquery/dataset_size.test.js index fcfe99ce16..b98b4e0801 100644 --- a/test/bigquery/dataset_size.test.js +++ b/test/bigquery/dataset_size.test.js @@ -13,18 +13,19 @@ 'use strict'; -var test = require('ava'); var datasetSizeExample = require('../../bigquery/dataset_size'); -test.cb.serial('should return the size of a dataset', function (t) { - datasetSizeExample.main( - 'bigquery-public-data', - 'hacker_news', - function (err, size) { - t.ifError(err); - t.is(typeof size, 'string'); - t.truthy(size.indexOf(' GB') === size.length - 3); - t.end(); - } - ); +describe('bigquery:dataset_size', function () { + it('should return the size of a dataset', function (done) { + datasetSizeExample.main( + 'bigquery-public-data', + 'hacker_news', + function (err, size) { + assert(!err); + assert.equal(typeof size, 'string'); + assert(size.indexOf(' GB') === size.length - 3); + done(); + } + ); + }); }); diff --git a/test/bigquery/getting_started.test.js b/test/bigquery/getting_started.test.js index e92eeea174..930094f6d6 100644 --- a/test/bigquery/getting_started.test.js +++ b/test/bigquery/getting_started.test.js @@ -13,16 +13,17 @@ 'use strict'; -var test = require('ava'); var gettingStartedExample = require('../../bigquery/getting_started'); -test.cb.serial('should run a query', function (t) { - gettingStartedExample.main( - function (err, rows) { - t.ifError(err); - t.truthy(Array.isArray(rows)); - t.is(rows.length, 10); - t.end(); - } - ); +describe('bigquery:getting_started', function () { + it('should run a query', function (done) { + gettingStartedExample.main( + function (err, rows) { + assert(!err); + assert(Array.isArray(rows)); + assert.equal(rows.length, 10); + done(); + } + ); + }); }); diff --git a/test/bigquery/load_data_from_csv.test.js b/test/bigquery/load_data_from_csv.test.js index e298af1971..96ba7215aa 100644 --- a/test/bigquery/load_data_from_csv.test.js +++ b/test/bigquery/load_data_from_csv.test.js @@ -15,46 +15,47 @@ var async = require('async'); var path = require('path'); -var test = require('ava'); var loadDataFromCsvExample = require('../../bigquery/load_data_from_csv'); var pathToCsvFile = path.join(__dirname, '/../../bigquery/resources/data.csv'); var datasetId = 'nodejs_docs_samples'; var tableName = 'test_' + new Date().getTime() + '_' + Math.floor(Math.random() * 10000); -test.cb.serial('should load data from a csv file', function (t) { - async.series([ - function (cb) { - loadDataFromCsvExample.createTable(datasetId, tableName, cb); - }, - function (cb) { - loadDataFromCsvExample.main(pathToCsvFile, datasetId, tableName, cb); - }, - function (cb) { - loadDataFromCsvExample.deleteTable(datasetId, tableName, cb); - } - ], function (err, results) { - if (err) { - loadDataFromCsvExample.deleteTable(datasetId, tableName, function () { - t.end(err); - }); - } else { - t.ifError(err); - // metadata - t.is(results[1].status.state, 'DONE'); - t.end(); - } +describe('bigquery:load_data_from_csv', function () { + it('should load data from a csv file', function (done) { + async.series([ + function (cb) { + loadDataFromCsvExample.createTable(datasetId, tableName, cb); + }, + function (cb) { + loadDataFromCsvExample.main(pathToCsvFile, datasetId, tableName, cb); + }, + function (cb) { + loadDataFromCsvExample.deleteTable(datasetId, tableName, cb); + } + ], function (err, results) { + if (err) { + loadDataFromCsvExample.deleteTable(datasetId, tableName, function () { + done(err); + }); + } else { + assert(!err); + // metadata + assert.equal(results[1].status.state, 'DONE'); + done(); + } + }); }); -}); -test('should require correct arguments', function (t) { - t.throws(function () { - loadDataFromCsvExample.main(); - }, Error, 'pathToCsvFile is required!'); - t.throws(function () { - loadDataFromCsvExample.main(pathToCsvFile); - }, Error, 'datasetId is required!'); - t.throws(function () { - loadDataFromCsvExample.main(pathToCsvFile, datasetId); - }, Error, 'tableName is required!'); + it('should require correct arguments', function () { + assert.throws(function () { + loadDataFromCsvExample.main(); + }, Error, 'pathToCsvFile is required!'); + assert.throws(function () { + loadDataFromCsvExample.main(pathToCsvFile); + }, Error, 'datasetId is required!'); + assert.throws(function () { + loadDataFromCsvExample.main(pathToCsvFile, datasetId); + }, Error, 'tableName is required!'); + }); }); diff --git a/test/bigquery/load_data_from_gcs.test.js b/test/bigquery/load_data_from_gcs.test.js index 68c0157231..17b16937f6 100644 --- a/test/bigquery/load_data_from_gcs.test.js +++ b/test/bigquery/load_data_from_gcs.test.js @@ -14,7 +14,6 @@ 'use strict'; var async = require('async'); -var test = require('ava'); var loadDataFromCsvExample = require('../../bigquery/load_data_from_csv'); var loadDataFromGcsExample = require('../../bigquery/load_data_from_gcs'); var bucket = process.env.TEST_BUCKET_NAME; @@ -23,42 +22,44 @@ var datasetId = 'nodejs_docs_samples'; var tableName = 'test_' + new Date().getTime() + '_' + Math.floor(Math.random() * 10000); -test.cb.serial('should load data from a csv file in a GCS bucket', function (t) { - async.series([ - function (cb) { - loadDataFromCsvExample.createTable(datasetId, tableName, cb); - }, - function (cb) { - loadDataFromGcsExample.main(bucket, file, datasetId, tableName, cb); - }, - function (cb) { - loadDataFromCsvExample.deleteTable(datasetId, tableName, cb); - } - ], function (err, results) { - if (err) { - loadDataFromCsvExample.deleteTable(datasetId, tableName, function () { - t.end(err); - }); - } else { - t.ifError(err); - // metadata - t.is(results[1].status.state, 'DONE'); - t.end(); - } +describe('bigquery:load_data_from_gcs', function () { + it('should load data from a csv file in a GCS bucket', function (done) { + async.series([ + function (cb) { + loadDataFromCsvExample.createTable(datasetId, tableName, cb); + }, + function (cb) { + loadDataFromGcsExample.main(bucket, file, datasetId, tableName, cb); + }, + function (cb) { + loadDataFromCsvExample.deleteTable(datasetId, tableName, cb); + } + ], function (err, results) { + if (err) { + loadDataFromCsvExample.deleteTable(datasetId, tableName, function () { + done(err); + }); + } else { + assert(!err); + // metadata + assert.equal(results[1].status.state, 'DONE'); + done(); + } + }); }); -}); -test('should require correct arguments', function (t) { - t.throws(function () { - loadDataFromGcsExample.main(); - }, Error, 'bucket is required!'); - t.throws(function () { - loadDataFromGcsExample.main(bucket); - }, Error, 'file is required!'); - t.throws(function () { - loadDataFromGcsExample.main(bucket, file); - }, Error, 'datasetId is required!'); - t.throws(function () { - loadDataFromGcsExample.main(bucket, file, datasetId); - }, Error, 'tableName is required!'); + it('should require correct arguments', function () { + assert.throws(function () { + loadDataFromGcsExample.main(); + }, Error, 'bucket is required!'); + assert.throws(function () { + loadDataFromGcsExample.main(bucket); + }, Error, 'file is required!'); + assert.throws(function () { + loadDataFromGcsExample.main(bucket, file); + }, Error, 'datasetId is required!'); + assert.throws(function () { + loadDataFromGcsExample.main(bucket, file, datasetId); + }, Error, 'tableName is required!'); + }); }); diff --git a/test/computeengine/mailjet.test.js b/test/computeengine/mailjet.test.js index 227ea2befb..c140aae92b 100644 --- a/test/computeengine/mailjet.test.js +++ b/test/computeengine/mailjet.test.js @@ -13,40 +13,41 @@ 'use strict'; -var test = require('ava'); var proxyquire = require('proxyquire').noPreserveCache(); process.env.MAILJET_API_KEY = 'foo'; process.env.MAILJET_API_SECRET = 'bar'; -test.cb.serial('should send an email', function (t) { - proxyquire('../../computeengine/mailjet.js', { - nodemailer: { - createTransport: function (arg) { - t.is(arg, 'test'); - return { - sendMail: function (payload, cb) { - t.deepEqual(payload, { - from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', - to: 'EMAIL@EXAMPLE.COM', - subject: 'test email from Node.js on Google Cloud Platform', - text: 'Hello!\n\nThis a test email from Node.js.' - }); - cb('done'); - t.end(); +describe('computeengine:mailjet', function () { + it('should send an email', function (done) { + proxyquire('../../computeengine/mailjet.js', { + nodemailer: { + createTransport: function (arg) { + assert.equal(arg, 'test'); + return { + sendMail: function (payload, cb) { + assert.deepEqual(payload, { + from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', + to: 'EMAIL@EXAMPLE.COM', + subject: 'test email from Node.js on Google Cloud Platform', + text: 'Hello!\n\nThis a test email from Node.js.' + }); + cb('done'); + done(); + } + }; + } + }, + 'nodemailer-smtp-transport': function (options) { + assert.deepEqual(options, { + host: 'in.mailjet.com', + port: 2525, + auth: { + user: 'foo', + pass: 'bar' } - }; + }); + return 'test'; } - }, - 'nodemailer-smtp-transport': function (options) { - t.deepEqual(options, { - host: 'in.mailjet.com', - port: 2525, - auth: { - user: 'foo', - pass: 'bar' - } - }); - return 'test'; - } + }); }); }); diff --git a/test/computeengine/sendgrid.test.js b/test/computeengine/sendgrid.test.js index c8126a975a..c9f3580dca 100644 --- a/test/computeengine/sendgrid.test.js +++ b/test/computeengine/sendgrid.test.js @@ -13,26 +13,27 @@ 'use strict'; -var test = require('ava'); var proxyquire = require('proxyquire').noPreserveCache(); process.env.SENDGRID_API_KEY = 'foo'; -test.cb.serial('should send an email', function (t) { - proxyquire('../../computeengine/sendgrid.js', { - sendgrid: function (key) { - t.is(key, 'foo'); - return { - send: function (payload, cb) { - t.deepEqual(payload, { - from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', - to: 'EMAIL@EXAMPLE.COM', - subject: 'test email from Node.js on Google Cloud Platform', - text: 'Hello!\n\nThis a test email from Node.js.' - }); - cb('done'); - t.end(); - } - }; - } +describe('computeengine:sendgrid', function () { + it('should send an email', function (done) { + proxyquire('../../computeengine/sendgrid.js', { + sendgrid: function (key) { + assert.equal(key, 'foo'); + return { + send: function (payload, cb) { + assert.deepEqual(payload, { + from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', + to: 'EMAIL@EXAMPLE.COM', + subject: 'test email from Node.js on Google Cloud Platform', + text: 'Hello!\n\nThis a test email from Node.js.' + }); + cb('done'); + done(); + } + }; + } + }); }); }); diff --git a/test/computeengine/vms.test.js b/test/computeengine/vms.test.js index 5e55bec46f..e3c0315b77 100644 --- a/test/computeengine/vms.test.js +++ b/test/computeengine/vms.test.js @@ -13,14 +13,15 @@ 'use strict'; -var test = require('ava'); var vmsExample = require('../../computeengine/vms'); -test.cb.serial('should retrieve vms', function (t) { - vmsExample.main(function (err, result) { - t.ifError(err); - t.truthy(result); - t.is(Array.isArray(result), true); - t.end(); +describe('computeengine:vms', function () { + it('should retrieve vms', function (done) { + vmsExample.main(function (err, result) { + assert(!err); + assert(result); + assert(Array.isArray(result)); + done(); + }); }); }); diff --git a/test/computeengine/vms_api.test.js b/test/computeengine/vms_api.test.js index df2a73ca6b..9de04e0761 100644 --- a/test/computeengine/vms_api.test.js +++ b/test/computeengine/vms_api.test.js @@ -13,13 +13,14 @@ 'use strict'; -var test = require('ava'); var vmsExample = require('../../computeengine/vms_api'); -test.cb.serial('should retrieve vms', function (t) { - vmsExample.main(function (err, result) { - t.ifError(err); - t.truthy(result); - t.end(); +describe('computeengine:vms_api', function () { + it('should retrieve vms', function (done) { + vmsExample.main(function (err, result) { + assert(!err); + assert(result); + done(); + }); }); }); diff --git a/test/datastore/concepts.test.js b/test/datastore/concepts.test.js index 57e5d7f1d0..359a267b44 100644 --- a/test/datastore/concepts.test.js +++ b/test/datastore/concepts.test.js @@ -13,7 +13,6 @@ 'use strict'; -var test = require('ava'); var testUtil = require('./util.js'); var concepts = require('../../datastore/concepts'); @@ -29,253 +28,255 @@ var Index = concepts.Index; var Entity = concepts.Entity; var Query = concepts.Query; -test.before(function () { - var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples'; - transaction = new Transaction(projectId); - metadata = new Metadata(projectId); - index = new Index(projectId); - entity = new Entity(projectId); - query = new Query(projectId); -}); +describe('datastore:concepts', function () { + before(function () { + var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples'; + transaction = new Transaction(projectId); + metadata = new Metadata(projectId); + index = new Index(projectId); + entity = new Entity(projectId); + query = new Query(projectId); + }); -test.after.cb.serial(function (t) { - var datastore = transaction.datastore; - var query = datastore.createQuery('Task'); + after(function (done) { + var datastore = transaction.datastore; + var query = datastore.createQuery('Task'); - testUtil.deleteEntities(datastore, query, t.end); -}); + testUtil.deleteEntities(datastore, query, done); + }); -// Transactions + // Transactions -test.cb.serial('performs a transactional update', function (t) { - transaction.testTransactionalUpdate(t.end); -}); + it('performs a transactional update', function (done) { + transaction.testTransactionalUpdate(done); + }); -test.cb.serial('performs retries if necessary', function (t) { - transaction.testTransactionalRetry(t.end); -}); + it('performs retries if necessary', function (done) { + transaction.testTransactionalRetry(done); + }); -test.cb.serial('performs a get or create', function (t) { - transaction.testTransactionalGetOrCreate(t.end); -}); + it('performs a get or create', function (done) { + transaction.testTransactionalGetOrCreate(done); + }); -test.cb.serial('gets a snapshot of task list entities', function (t) { - transaction.testSingleEntityGroupReadOnly(t.end); -}); + it('gets a snapshot of task list entities', function (done) { + transaction.testSingleEntityGroupReadOnly(done); + }); -// Metadata + // Metadata -test.cb.serial('performs a namespace query', function (t) { - metadata.testNamespaceRunQuery(t.end); -}); - -test.cb.serial('performs a kind query', function (t) { - metadata.testKindRunQuery(t.end); -}); + it('performs a namespace query', function (done) { + metadata.testNamespaceRunQuery(done); + }); -test.cb.serial('performs a property query', function (t) { - metadata.testPropertyRunQuery(t.end); -}); + it('performs a kind query', function (done) { + metadata.testKindRunQuery(done); + }); -test.cb.serial('performs a property by kind query', function (t) { - metadata.testPropertyByKindRunQuery(t.end); -}); + it('performs a property query', function (done) { + metadata.testPropertyRunQuery(done); + }); -// Indexes + it('performs a property by kind query', function (done) { + metadata.testPropertyByKindRunQuery(done); + }); -test.cb.serial( - 'performs a query with a filter on an unindexed property', - function (t) { - index.testUnindexedPropertyQuery(t.end); - } -); + // Indexes -test.cb.serial('inserts arrays of data', function (t) { - index.testExplodingProperties(t.end); -}); + it( + 'performs a query with a filter on an unindexed property', + function (done) { + index.testUnindexedPropertyQuery(done); + } + ); -// Queries + it('inserts arrays of data', function (done) { + index.testExplodingProperties(done); + }); -test.cb.serial('performs a basic query', function (t) { - query.testRunQuery(t.end); -}); + // Queries -test.cb.serial('performs a query with a property filter', function (t) { - query.testPropertyFilter(t.end); -}); + it('performs a basic query', function (done) { + query.testRunQuery(done); + }); -test.cb.serial('performs a query with a composite filter', function (t) { - query.testCompositeFilter(t.end); -}); + it('performs a query with a property filter', function (done) { + query.testPropertyFilter(done); + }); -test.cb.serial('performs a query with a key filter', function (t) { - query.testKeyFilter(t.end); -}); + it('performs a query with a composite filter', function (done) { + query.testCompositeFilter(done); + }); -test.cb.serial('performs a query with ascending sort', function (t) { - query.testAscendingSort(t.end); -}); + it('performs a query with a key filter', function (done) { + query.testKeyFilter(done); + }); -test.cb.serial('performs a query with descending sort', function (t) { - query.testDescendingSort(t.end); -}); + it('performs a query with ascending sort', function (done) { + query.testAscendingSort(done); + }); -test.cb.serial('performs a query with multi sort', function (t) { - query.testMultiSort(t.end); -}); + it('performs a query with descending sort', function (done) { + query.testDescendingSort(done); + }); -test.cb.serial('performs a kindless query', function (t) { - query.testKindlessQuery(t.end); -}); + it('performs a query with multi sort', function (done) { + query.testMultiSort(done); + }); -test.cb.serial('performs a projection query', function (t) { - entity.testProperties(function (err, tasks) { - console.log(err, tasks); - t.ifError(err); - setTimeout(function () { - query.testRunQueryProjection(t.end); - }, 1000); + it('performs a kindless query', function (done) { + query.testKindlessQuery(done); }); -}); -test.cb.serial('performs a keys only query', function (t) { - query.testKeysOnlyQuery(t.end); -}); + it('performs a projection query', function (done) { + entity.testProperties(function (err, tasks) { + console.log(err, tasks); + assert(!err); + setTimeout(function () { + query.testRunQueryProjection(done); + }, 1000); + }); + }); -test.cb.serial('performs a distinct query', function (t) { - query.testDistinctQuery(t.end); -}); + it('performs a keys only query', function (done) { + query.testKeysOnlyQuery(done); + }); -test.cb.serial('performs a distinct on query', function (t) { - query.testDistinctOnQuery(t.end); -}); + it('performs a distinct query', function (done) { + query.testDistinctQuery(done); + }); -test.cb.serial('performs an array value inequality query', function (t) { - query.testArrayValueInequalityRange(t.end); -}); + it('performs a distinct on query', function (done) { + query.testDistinctOnQuery(done); + }); -test.cb.serial('performs an array value equality query', function (t) { - query.testArrayValueEquality(t.end); -}); + it('performs an array value inequality query', function (done) { + query.testArrayValueInequalityRange(done); + }); -test.cb.serial('performs an inequality range query', function (t) { - query.testInequalityRange(t.end); -}); + it('performs an array value equality query', function (done) { + query.testArrayValueEquality(done); + }); -test.cb.serial('returns an error from an invalid query', function (t) { - query.testInequalityInvalid(function (err) { - t.truthy(err); - t.end(); + it('performs an inequality range query', function (done) { + query.testInequalityRange(done); }); -}); -test.cb.serial('performs an equal and inequality range query', function (t) { - query.testEqualAndInequalityRange(t.end); -}); + it('returns an error from an invalid query', function (done) { + query.testInequalityInvalid(function (err) { + assert(err); + done(); + }); + }); -test.cb.serial('performs an equality sort query', function (t) { - query.testInequalitySort(t.end); -}); + it('performs an equal and inequality range query', function (done) { + query.testEqualAndInequalityRange(done); + }); -test.cb.serial( - 'returns an error when not sorted on filtered property', - function (t) { - query.testInequalitySortInvalidNotSame(function (err) { - t.truthy(err); - t.end(); - }); - } -); - -test.cb.serial( - 'returns an error when not sorted on first filter prop', - function (t) { - query.testInequalitySortInvalidNotFirst(function (err) { - t.truthy(err); - t.end(); - }); - } -); + it('performs an equality sort query', function (done) { + query.testInequalitySort(done); + }); -test.cb.serial('performs a query with a limit', function (t) { - query.testLimit(t.end); -}); + it( + 'returns an error when not sorted on filtered property', + function (done) { + query.testInequalitySortInvalidNotSame(function (err) { + assert(err); + done(); + }); + } + ); + + it( + 'returns an error when not sorted on first filter prop', + function (done) { + query.testInequalitySortInvalidNotFirst(function (err) { + assert(err); + done(); + }); + } + ); + + it('performs a query with a limit', function (done) { + query.testLimit(done); + }); -test.cb.serial.skip('allows manual pagination through results', function (t) { - entity.testBatchUpsert(function (err) { - t.ifError(err); - setTimeout(function () { - query.testCursorPaging(t.end); - }, 1000); + it.skip('allows manual pagination through results', function (done) { + entity.testBatchUpsert(function (err) { + assert(!err); + setTimeout(function () { + query.testCursorPaging(done); + }, 1000); + }); }); -}); -test.skip('performs an ancestor query', function (t) { - query.testEventualConsistentQuery(t.end); -}); + it.skip('performs an ancestor query', function (done) { + query.testEventualConsistentQuery(done); + }); -// Entities + // Entities -test.cb.serial('saves with an incomplete key', function (t) { - entity.testIncompleteKey(t.end); -}); + it('saves with an incomplete key', function (done) { + entity.testIncompleteKey(done); + }); -test.cb.serial('saves with a named key', function (t) { - entity.testNamedKey(t.end); -}); + it('saves with a named key', function (done) { + entity.testNamedKey(done); + }); -test.cb.serial('saves a key with a parent', function (t) { - entity.testKeyWithParent(t.end); -}); + it('saves a key with a parent', function (done) { + entity.testKeyWithParent(done); + }); -test.cb.serial('saves a key with multiple parents', function (t) { - entity.testKeyWithMultiLevelParent(t.end); -}); + it('saves a key with multiple parents', function (done) { + entity.testKeyWithMultiLevelParent(done); + }); -test.cb.serial('saves an entity with a parent', function (t) { - entity.testEntityWithParent(t.end); -}); + it('saves an entity with a parent', function (done) { + entity.testEntityWithParent(done); + }); -test.cb.serial('saves an entity with properties', function (t) { - entity.testProperties(t.end); -}); + it('saves an entity with properties', function (done) { + entity.testProperties(done); + }); -test.cb.serial('saves an entity with arrays', function (t) { - entity.testArrayValue(t.end); -}); + it('saves an entity with arrays', function (done) { + entity.testArrayValue(done); + }); -test.cb.serial('saves a basic entity', function (t) { - entity.testBasicEntity(t.end); -}); + it('saves a basic entity', function (done) { + entity.testBasicEntity(done); + }); -test.cb.serial('saves with an upsert', function (t) { - entity.testUpsert(t.end); -}); + it('saves with an upsert', function (done) { + entity.testUpsert(done); + }); -test.cb.serial('saves with an insert', function (t) { - entity.testInsert(t.end); -}); + it('saves with an insert', function (done) { + entity.testInsert(done); + }); -test.cb.serial('performs a lookup', function (t) { - entity.testLookup(t.end); -}); + it('performs a lookup', function (done) { + entity.testLookup(done); + }); -test.cb.serial('saves with an update', function (t) { - entity.testUpdate(t.end); -}); + it('saves with an update', function (done) { + entity.testUpdate(done); + }); -test.cb.serial('deletes an entity', function (t) { - entity.testDelete(t.end); -}); + it('deletes an entity', function (done) { + entity.testDelete(done); + }); -test.cb.serial('performs a batch upsert', function (t) { - entity.testBatchUpsert(t.end); -}); + it('performs a batch upsert', function (done) { + entity.testBatchUpsert(done); + }); -test.cb.serial('performs a batch lookup', function (t) { - entity.testBatchLookup(t.end); -}); + it('performs a batch lookup', function (done) { + entity.testBatchLookup(done); + }); -test.cb.serial('performs a batch delete', function (t) { - entity.testBatchDelete(t.end); + it('performs a batch delete', function (done) { + entity.testBatchDelete(done); + }); }); diff --git a/test/datastore/error.test.js b/test/datastore/error.test.js index b35cb7b37f..2c62730ac6 100644 --- a/test/datastore/error.test.js +++ b/test/datastore/error.test.js @@ -13,13 +13,14 @@ 'use strict'; -var test = require('ava'); var error = require('../../datastore/error.js'); -test.cb.serial('should have an error', function (t) { - error.runQuery(function (err) { - t.truthy(err); - t.is(err.code, 400); - t.end(); +describe('datastore:error', function () { + it('should have an error', function (done) { + error.runQuery(function (err) { + assert(err); + assert.equal(err.code, 400); + done(); + }); }); }); diff --git a/test/datastore/tasks.test.js b/test/datastore/tasks.test.js index a84b0ae492..196efa1b46 100644 --- a/test/datastore/tasks.test.js +++ b/test/datastore/tasks.test.js @@ -13,60 +13,60 @@ 'use strict'; -var test = require('ava'); var async = require('async'); - var tasks = require('../../datastore/tasks.js'); var taskIds = []; -test.after.cb.serial(function (t) { - async.parallel(taskIds.map(function (taskId) { - return function (cb) { - tasks.deleteEntity(taskId, cb); - }; - }), t.end); -}); +describe('datastore:tasks', function () { + after(function (done) { + async.parallel(taskIds.map(function (taskId) { + return function (cb) { + tasks.deleteEntity(taskId, cb); + }; + }), done); + }); -test.cb.serial('should add a task', function (t) { - getTaskId(t.end); -}); + it('should add a task', function (done) { + getTaskId(done); + }); -test.cb.serial('should mark a task as done', function (t) { - getTaskId(function (err, taskId) { - t.ifError(err); - tasks.updateEntity(taskId, t.end); + it('should mark a task as done', function (done) { + getTaskId(function (err, taskId) { + assert(!err); + tasks.updateEntity(taskId, done); + }); }); -}); -test.cb.serial('should list tasks', function (t) { - tasks.retrieveEntities(t.end); -}); + it('should list tasks', function (done) { + tasks.retrieveEntities(done); + }); -test.cb.serial('should delete a task', function (t) { - getTaskId(function (err, taskId) { - t.ifError(err); - tasks.deleteEntity(taskId, t.end); + it('should delete a task', function (done) { + getTaskId(function (err, taskId) { + assert(!err); + tasks.deleteEntity(taskId, done); + }); }); -}); -test.cb.serial('should format tasks', function (t) { - tasks.retrieveEntities(function (err, _tasks) { - t.ifError(err); - t.notThrows(function () { - tasks.formatTasks(_tasks); + it('should format tasks', function (done) { + tasks.retrieveEntities(function (err, _tasks) { + assert(!err); + assert.doesNotThrow(function () { + tasks.formatTasks(_tasks); + }); + done(); }); - t.end(); }); -}); -function getTaskId (callback) { - tasks.addEntity('description', function (err, taskKey) { - if (err) { - return callback(err); - } + function getTaskId (callback) { + tasks.addEntity('description', function (err, taskKey) { + if (err) { + return callback(err); + } - var taskId = taskKey.path.pop(); - taskIds.push(taskId); - callback(null, taskId); - }); -} + var taskId = taskKey.path.pop(); + taskIds.push(taskId); + callback(null, taskId); + }); + } +}); diff --git a/test/functions/background.test.js b/test/functions/background.test.js index d59d2ca536..91dd7f134b 100644 --- a/test/functions/background.test.js +++ b/test/functions/background.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); function getSample () { @@ -38,63 +36,55 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); +describe('functions:background', function () { + it('should echo message', function () { + var expectedMsg = 'hi'; + var context = getMockContext(); + var backgroundSample = getSample(); + backgroundSample.sample.helloWorld(context, { + message: expectedMsg + }); -test('should echo message', function (t) { - var expectedMsg = 'hi'; - var context = getMockContext(); - var backgroundSample = getSample(); - backgroundSample.sample.helloWorld(context, { - message: expectedMsg + assert(context.success.calledOnce); + assert.equal(context.failure.called, false); + assert(console.log.calledWith(expectedMsg)); }); + it('should say no message was provided', function () { + var expectedMsg = 'No message defined!'; + var context = getMockContext(); + var backgroundSample = getSample(); + backgroundSample.sample.helloWorld(context, {}); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); -}); -test('should say no message was provided', function (t) { - var expectedMsg = 'No message defined!'; - var context = getMockContext(); - var backgroundSample = getSample(); - backgroundSample.sample.helloWorld(context, {}); - - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); -test.cb.serial('should make a promise request', function (t) { - var backgroundSample = getSample(); - backgroundSample.sample.helloPromise({ - endpoint: 'foo.com' - }).then(function (result) { - t.deepEqual(backgroundSample.mocks.requestPromise.firstCall.args[0], { - uri: 'foo.com' - }); - t.is(result, 'test'); - t.end(); - }, function () { - t.fail(); + assert(context.failure.calledOnce); + assert(context.failure.firstCall.args[0] === expectedMsg); + assert.equal(context.success.called, false); }); -}); -test('should return synchronously', function (t) { - var backgroundSample = getSample(); - t.is(backgroundSample.sample.helloSynchronous({ - something: true - }), 'Something is true!'); -}); -test('should throw an error', function (t) { - var backgroundSample = getSample(); - t.throws(function () { - backgroundSample.sample.helloSynchronous({ - something: false + it('should make a promise request', function (done) { + var backgroundSample = getSample(); + backgroundSample.sample.helloPromise({ + endpoint: 'foo.com' + }).then(function (result) { + assert.deepEqual(backgroundSample.mocks.requestPromise.firstCall.args[0], { + uri: 'foo.com' + }); + assert.equal(result, 'test'); + done(); + }, function () { + assert.fail(); }); - }, Error, 'Something was not true!'); -}); - -test.after(function () { - console.error.restore(); - console.log.restore(); + }); + it('should return synchronously', function () { + var backgroundSample = getSample(); + assert(backgroundSample.sample.helloSynchronous({ + something: true + }) === 'Something is true!'); + }); + it('should throw an error', function () { + var backgroundSample = getSample(); + assert.throws(function () { + backgroundSample.sample.helloSynchronous({ + something: false + }); + }, Error, 'Something was not true!'); + }); }); diff --git a/test/functions/datastore.test.js b/test/functions/datastore.test.js index 2db401faa6..bbc78a5a8d 100644 --- a/test/functions/datastore.test.js +++ b/test/functions/datastore.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); var KEY = 'key'; @@ -51,272 +49,264 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); +describe('functions:datastore', function () { + it('set: Set fails without a value', function () { + var expectedMsg = 'Value not provided. Make sure you have a "value" ' + + 'property in your request'; + var context = getMockContext(); -test('set: Set fails without a value', function (t) { - var expectedMsg = 'Value not provided. Make sure you have a "value" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.set(context, {}); - getSample().sample.set(context, {}); + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('set: Set fails without a key', function () { + var expectedMsg = 'Key not provided. Make sure you have a "key" ' + + 'property in your request'; + var context = getMockContext(); -test('set: Set fails without a key', function (t) { - var expectedMsg = 'Key not provided. Make sure you have a "key" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.set(context, { + value: {} + }); - getSample().sample.set(context, { - value: {} + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('set: Set fails without a kind', function () { + var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + + 'property in your request'; + var context = getMockContext(); -test('set: Set fails without a kind', function (t) { - var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.set(context, { + value: {}, + key: KEY + }); - getSample().sample.set(context, { - value: {}, - key: KEY + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('set: Handles save error', function () { + var expectedMsg = 'test error'; + var context = getMockContext(); + var datastoreSample = getSample(); -test('set: Handles save error', function (t) { - var expectedMsg = 'test error'; - var context = getMockContext(); - var datastoreSample = getSample(); + datastoreSample.mocks.datastore.save = sinon.stub().callsArgWith( + 1, + expectedMsg + ); - datastoreSample.mocks.datastore.save = sinon.stub().callsArgWith( - 1, - expectedMsg - ); + datastoreSample.sample.set(context, { + value: {}, + key: KEY, + kind: KIND + }); - datastoreSample.sample.set(context, { - value: {}, - key: KEY, - kind: KIND + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(datastoreSample.mocks.datastore.save.calledOnce, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(datastoreSample.mocks.datastore.save.calledOnce, true); -}); - -test('set: Set saves an entity', function (t) { - var expectedMsg = 'Entity saved'; - var context = getMockContext(); - var datastoreSample = getSample(); - - var data = { - value: { - name: 'John' - }, - key: KEY, - kind: KIND - }; + it('set: Set saves an entity', function () { + var expectedMsg = 'Entity saved'; + var context = getMockContext(); + var datastoreSample = getSample(); - datastoreSample.sample.set(context, data); - - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedMsg); - t.is(context.failure.called, false); - t.is(datastoreSample.mocks.datastore.key.calledOnce, true); - t.deepEqual( - datastoreSample.mocks.datastore.key.firstCall.args[0], - [data.kind, data.key] - ); - t.is(datastoreSample.mocks.datastore.save.calledOnce, true); - t.deepEqual(datastoreSample.mocks.datastore.save.firstCall.args[0], { - key: { - kind: data.kind, - path: data.key - }, - data: data.value + var data = { + value: { + name: 'John' + }, + key: KEY, + kind: KIND + }; + + datastoreSample.sample.set(context, data); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedMsg); + assert.equal(context.failure.called, false); + assert.equal(datastoreSample.mocks.datastore.key.calledOnce, true); + assert.deepEqual( + datastoreSample.mocks.datastore.key.firstCall.args[0], + [data.kind, data.key] + ); + assert.equal(datastoreSample.mocks.datastore.save.calledOnce, true); + assert.deepEqual(datastoreSample.mocks.datastore.save.firstCall.args[0], { + key: { + kind: data.kind, + path: data.key + }, + data: data.value + }); }); -}); -test('get: Get fails without a key', function (t) { - var expectedMsg = 'Key not provided. Make sure you have a "key" ' + - 'property in your request'; - var context = getMockContext(); + it('get: Get fails without a key', function () { + var expectedMsg = 'Key not provided. Make sure you have a "key" ' + + 'property in your request'; + var context = getMockContext(); - getSample().sample.get(context, {}); + getSample().sample.get(context, {}); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + }); -test('get: Get fails without a kind', function (t) { - var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + - 'property in your request'; - var context = getMockContext(); + it('get: Get fails without a kind', function () { + var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + + 'property in your request'; + var context = getMockContext(); - getSample().sample.get(context, { - key: KEY + getSample().sample.get(context, { + key: KEY + }); + + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('get: Handles get error', function () { + var expectedMsg = 'test error'; + var context = getMockContext(); + var datastoreSample = getSample(); -test('get: Handles get error', function (t) { - var expectedMsg = 'test error'; - var context = getMockContext(); - var datastoreSample = getSample(); + datastoreSample.mocks.datastore.get = sinon.stub().callsArgWith( + 1, + expectedMsg + ); - datastoreSample.mocks.datastore.get = sinon.stub().callsArgWith( - 1, - expectedMsg - ); + datastoreSample.sample.get(context, { + key: KEY, + kind: KIND + }); - datastoreSample.sample.get(context, { - key: KEY, - kind: KIND + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(datastoreSample.mocks.datastore.get.calledOnce, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(datastoreSample.mocks.datastore.get.calledOnce, true); -}); + it('get: Fails when entity does not exist', function () { + var expectedMsg = 'No entity found for key key'; + var context = getMockContext(); + var datastoreSample = getSample(); -test('get: Fails when entity does not exist', function (t) { - var expectedMsg = 'No entity found for key key'; - var context = getMockContext(); - var datastoreSample = getSample(); + datastoreSample.sample.get(context, { + key: KEY, + kind: KIND + }); - datastoreSample.sample.get(context, { - key: KEY, - kind: KIND + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(datastoreSample.mocks.datastore.get.calledOnce, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(datastoreSample.mocks.datastore.get.calledOnce, true); -}); - -test('get: Finds an entity', function (t) { - var expectedResult = { - name: 'John' - }; - var context = getMockContext(); - var datastoreSample = getSample(); - - datastoreSample.mocks.datastore.get = sinon.stub().callsArgWith( - 1, - null, - expectedResult - ); - datastoreSample.sample.get(context, { - key: KEY, - kind: KIND + it('get: Finds an entity', function () { + var expectedResult = { + name: 'John' + }; + var context = getMockContext(); + var datastoreSample = getSample(); + + datastoreSample.mocks.datastore.get = sinon.stub().callsArgWith( + 1, + null, + expectedResult + ); + datastoreSample.sample.get(context, { + key: KEY, + kind: KIND + }); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedResult); + assert.equal(context.failure.called, false); + assert.equal(datastoreSample.mocks.datastore.get.calledOnce, true); + assert.deepEqual( + datastoreSample.mocks.datastore.get.firstCall.args[0], + { + path: KEY, + kind: KIND + } + ); }); - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedResult); - t.is(context.failure.called, false); - t.is(datastoreSample.mocks.datastore.get.calledOnce, true); - t.deepEqual( - datastoreSample.mocks.datastore.get.firstCall.args[0], - { - path: KEY, - kind: KIND - } - ); -}); + it('del: Delete fails without a key', function () { + var expectedMsg = 'Key not provided. Make sure you have a "key" ' + + 'property in your request'; + var context = getMockContext(); -test('del: Delete fails without a key', function (t) { - var expectedMsg = 'Key not provided. Make sure you have a "key" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.del(context, {}); - getSample().sample.del(context, {}); + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('del: Delete fails without a kind', function () { + var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + + 'property in your request'; + var context = getMockContext(); -test('del: Delete fails without a kind', function (t) { - var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.del(context, { + key: KEY + }); - getSample().sample.del(context, { - key: KEY + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('del: Handles delete error', function () { + var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + + 'property in your request'; + var context = getMockContext(); + var datastoreSample = getSample(); -test('del: Handles delete error', function (t) { - var expectedMsg = 'Kind not provided. Make sure you have a "kind" ' + - 'property in your request'; - var context = getMockContext(); - var datastoreSample = getSample(); + datastoreSample.mocks.datastore.delete = sinon.stub().callsArgWith( + 1, + expectedMsg + ); - datastoreSample.mocks.datastore.delete = sinon.stub().callsArgWith( - 1, - expectedMsg - ); + datastoreSample.sample.del(context, { + key: KEY, + kind: KIND + }); - datastoreSample.sample.del(context, { - key: KEY, - kind: KIND + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(datastoreSample.mocks.datastore.delete.calledOnce, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(datastoreSample.mocks.datastore.delete.calledOnce, true); -}); - -test('del: Deletes an entity', function (t) { - var expectedMsg = 'Entity deleted'; - var context = getMockContext(); - var datastoreSample = getSample(); - - datastoreSample.sample.del(context, { - key: KEY, - kind: KIND - }); + it('del: Deletes an entity', function () { + var expectedMsg = 'Entity deleted'; + var context = getMockContext(); + var datastoreSample = getSample(); - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedMsg); - t.is(context.failure.called, false); - t.is(datastoreSample.mocks.datastore.delete.calledOnce, true); - t.deepEqual( - datastoreSample.mocks.datastore.delete.firstCall.args[0], - { - path: KEY, + datastoreSample.sample.del(context, { + key: KEY, kind: KIND - } - ); -}); - -test.after(function () { - console.error.restore(); - console.log.restore(); + }); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedMsg); + assert.equal(context.failure.called, false); + assert.equal(datastoreSample.mocks.datastore.delete.calledOnce, true); + assert.deepEqual( + datastoreSample.mocks.datastore.delete.firstCall.args[0], + { + path: KEY, + kind: KIND + } + ); + }); }); diff --git a/test/functions/gcs.test.js b/test/functions/gcs.test.js index 67fe79d3d0..12033717c1 100644 --- a/test/functions/gcs.test.js +++ b/test/functions/gcs.test.js @@ -15,8 +15,6 @@ var fs = require('fs'); var path = require('path'); -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); function getSample () { @@ -55,55 +53,57 @@ function getMockContext () { }; } -test('Fails without a bucket', function (t) { - var expectedMsg = 'Bucket not provided. Make sure you have a "bucket" ' + - 'property in your request'; - var context = getMockContext(); +describe('functions:gcs', function () { + it('Fails without a bucket', function () { + var expectedMsg = 'Bucket not provided. Make sure you have a "bucket" ' + + 'property in your request'; + var context = getMockContext(); - getSample().sample.wordCount(context, { - file: 'file' + getSample().sample.wordCount(context, { + file: 'file' + }); + + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('Fails without a file', function () { + var expectedMsg = 'Filename not provided. Make sure you have a "file" ' + + 'property in your request'; + var context = getMockContext(); -test('Fails without a file', function (t) { - var expectedMsg = 'Filename not provided. Make sure you have a "file" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.wordCount(context, { + bucket: 'bucket' + }); - getSample().sample.wordCount(context, { - bucket: 'bucket' + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); - -test.cb.serial('Reads the file line by line', function (t) { - var expectedMsg = 'The file sample.txt has 114 words'; - var data = { - bucket: 'bucket', - file: 'sample.txt' - }; - var context = { - success: function (message) { - t.is(message, expectedMsg); - t.end(); - }, - failure: function () { - t.end('Should have succeeded!'); - } - }; + it('Reads the file line by line', function (done) { + var expectedMsg = 'The file sample.txt has 114 words'; + var data = { + bucket: 'bucket', + file: 'sample.txt' + }; + var context = { + success: function (message) { + assert.equal(message, expectedMsg); + done(); + }, + failure: function () { + done('Should have succeeded!'); + } + }; - var gcsSample = getSample(); - gcsSample.sample.wordCount(context, data); + var gcsSample = getSample(); + gcsSample.sample.wordCount(context, data); - t.is(gcsSample.mocks.storage.bucket.calledOnce, true); - t.is(gcsSample.mocks.storage.bucket.firstCall.args[0], data.bucket); - t.is(gcsSample.mocks.bucket.file.calledOnce, true); - t.is(gcsSample.mocks.bucket.file.firstCall.args[0], data.file); + assert.equal(gcsSample.mocks.storage.bucket.calledOnce, true); + assert.equal(gcsSample.mocks.storage.bucket.firstCall.args[0], data.bucket); + assert.equal(gcsSample.mocks.bucket.file.calledOnce, true); + assert.equal(gcsSample.mocks.bucket.file.firstCall.args[0], data.file); + }); }); diff --git a/test/functions/helloworld.test.js b/test/functions/helloworld.test.js index aa9ce5a104..5b42093f61 100644 --- a/test/functions/helloworld.test.js +++ b/test/functions/helloworld.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); var helloworldSample = proxyquire('../../functions/helloworld', {}); @@ -25,126 +23,119 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); - -test('helloworld:helloworld: should log a message', function (t) { - var expectedMsg = 'My Cloud Function: hi'; - var context = getMockContext(); - helloworldSample.helloWorld(context, { - message: 'hi' +describe('functions:helloworld', function () { + it('helloworld: should log a message', function () { + var expectedMsg = 'My Cloud Function: hi'; + var context = getMockContext(); + helloworldSample.helloWorld(context, { + message: 'hi' + }); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith(expectedMsg), true); }); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); -}); - -test.cb.serial('helloworld:helloGET: should print hello world', function (t) { - var expectedMsg = 'Hello World!'; - helloworldSample.helloGET({}, { - send: function (message) { - t.is(message, expectedMsg); - t.end(); - } + it('helloGET: should print hello world', function (done) { + var expectedMsg = 'Hello World!'; + helloworldSample.helloGET({}, { + send: function (message) { + assert.equal(message, expectedMsg); + done(); + } + }); }); -}); -test.cb.serial('helloworld:helloHttp: should print a name', function (t) { - var expectedMsg = 'Hello John!'; - helloworldSample.helloHttp({ - body: { - name: 'John' - } - }, { - send: function (message) { - t.is(message, expectedMsg); - t.end(); - } + it('helloHttp: should print a name', function (done) { + var expectedMsg = 'Hello John!'; + helloworldSample.helloHttp({ + body: { + name: 'John' + } + }, { + send: function (message) { + assert.equal(message, expectedMsg); + done(); + } + }); }); -}); -test.cb.serial('helloworld:helloHttp: should print hello world', function (t) { - var expectedMsg = 'Hello World!'; - helloworldSample.helloHttp({ - body: {} - }, { - send: function (message) { - t.is(message, expectedMsg); - t.end(); - } + it('helloHttp: should print hello world', function (done) { + var expectedMsg = 'Hello World!'; + helloworldSample.helloHttp({ + body: {} + }, { + send: function (message) { + assert.equal(message, expectedMsg); + done(); + } + }); }); -}); -test('helloworld:helloBackground: should print a name', function (t) { - var expectedMsg = 'Hello John!'; - var context = getMockContext(); - helloworldSample.helloBackground(context, { - name: 'John' + it('helloBackground: should print a name', function () { + var expectedMsg = 'Hello John!'; + var context = getMockContext(); + helloworldSample.helloBackground(context, { + name: 'John' + }); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedMsg); + assert.equal(context.failure.called, false); }); - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedMsg); - t.is(context.failure.called, false); -}); + it('helloBackground: should print hello world', function () { + var expectedMsg = 'Hello World!'; + var context = getMockContext(); + helloworldSample.helloBackground(context, {}); -test('helloworld:helloBackground: should print hello world', function (t) { - var expectedMsg = 'Hello World!'; - var context = getMockContext(); - helloworldSample.helloBackground(context, {}); + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedMsg); + assert.equal(context.failure.called, false); + }); - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedMsg); - t.is(context.failure.called, false); -}); + it('helloPubSub: should print a name', function () { + var expectedMsg = 'Hello Bob!'; + var context = getMockContext(); + helloworldSample.helloPubSub(context, { + name: 'Bob' + }); -test('helloworld:helloPubSub: should print a name', function (t) { - var expectedMsg = 'Hello Bob!'; - var context = getMockContext(); - helloworldSample.helloPubSub(context, { - name: 'Bob' + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith(expectedMsg), true); }); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); -}); + it('helloPubSub: should print hello world', function () { + var expectedMsg = 'Hello World!'; + var context = getMockContext(); + helloworldSample.helloPubSub(context, {}); -test('helloworld:helloPubSub: should print hello world', function (t) { - var expectedMsg = 'Hello World!'; - var context = getMockContext(); - helloworldSample.helloPubSub(context, {}); + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith(expectedMsg), true); + }); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); -}); + it('helloGCS: should print a name', function () { + var expectedMsg = 'Hello Sally!'; + var context = getMockContext(); + helloworldSample.helloGCS(context, { + name: 'Sally' + }); -test('helloworld:helloGCS: should print a name', function (t) { - var expectedMsg = 'Hello Sally!'; - var context = getMockContext(); - helloworldSample.helloGCS(context, { - name: 'Sally' + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith(expectedMsg), true); }); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); -}); - -test('helloworld:helloGCS: should print hello world', function (t) { - var expectedMsg = 'Hello World!'; - var context = getMockContext(); - helloworldSample.helloGCS(context, {}); + it('helloGCS: should print hello world', function () { + var expectedMsg = 'Hello World!'; + var context = getMockContext(); + helloworldSample.helloGCS(context, {}); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith(expectedMsg), true); + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith(expectedMsg), true); + }); }); -test.after(function () { - console.error.restore(); - console.log.restore(); -}); diff --git a/test/functions/http.test.js b/test/functions/http.test.js index cd89f06d0a..d8d64d9620 100644 --- a/test/functions/http.test.js +++ b/test/functions/http.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); function getSample () { @@ -50,141 +48,134 @@ function getMocks () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); - -test('http:helloworld: should error with no message', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.body = {}; - httpSample.sample.helloWorld(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], 'No message defined!'); -}); - -test('http:helloworld: should log message', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.body = { - message: 'hi' - }; - httpSample.sample.helloWorld(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.end.calledOnce, true); - t.is(console.log.calledWith('hi'), true); -}); - -test('http:helloHttp: should handle GET', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.method = 'GET'; - httpSample.sample.helloHttp(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], 'Hello World!'); -}); - -test('http:helloHttp: should handle PUT', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.method = 'PUT'; - httpSample.sample.helloHttp(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 403); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], 'Forbidden!'); -}); - -test('http:helloHttp: should handle other methods', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.method = 'POST'; - httpSample.sample.helloHttp(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 500); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], { error: 'Something blew up!' }); -}); - -test('http:helloContent: should handle application/json', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.headers['content-type'] = 'application/json'; - mocks.req.body = { name: 'John' }; - httpSample.sample.helloContent(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); -}); - -test('http:helloContent: should handle application/octet-stream', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.headers['content-type'] = 'application/octet-stream'; - if (typeof Buffer.from === 'function') { - mocks.req.body = Buffer.from('John'); - } else { - mocks.req.body = new Buffer('John'); - } - httpSample.sample.helloContent(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); -}); - -test('http:helloContent: should handle text/plain', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.headers['content-type'] = 'text/plain'; - mocks.req.body = 'John'; - httpSample.sample.helloContent(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); -}); - -test('http:helloContent: should handle application/x-www-form-urlencoded', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - mocks.req.headers['content-type'] = 'application/x-www-form-urlencoded'; - mocks.req.body = { name: 'John' }; - httpSample.sample.helloContent(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); -}); - -test('http:helloContent: should handle other', function (t) { - var mocks = getMocks(); - var httpSample = getSample(); - httpSample.sample.helloContent(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.deepEqual(mocks.res.send.firstCall.args[0], 'Hello World!'); +describe('functions:http', function () { + it('http:helloworld: should error with no message', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.body = {}; + httpSample.sample.helloWorld(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], 'No message defined!'); + }); + + it('http:helloworld: should log message', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.body = { + message: 'hi' + }; + httpSample.sample.helloWorld(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.end.calledOnce, true); + assert.equal(console.log.calledWith('hi'), true); + }); + + it('http:helloHttp: should handle GET', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.method = 'GET'; + httpSample.sample.helloHttp(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], 'Hello World!'); + }); + + it('http:helloHttp: should handle PUT', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.method = 'PUT'; + httpSample.sample.helloHttp(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 403); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], 'Forbidden!'); + }); + + it('http:helloHttp: should handle other methods', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.method = 'POST'; + httpSample.sample.helloHttp(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 500); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], { error: 'Something blew up!' }); + }); + + it('http:helloContent: should handle application/json', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.headers['content-type'] = 'application/json'; + mocks.req.body = { name: 'John' }; + httpSample.sample.helloContent(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); + }); + + it('http:helloContent: should handle application/octet-stream', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.headers['content-type'] = 'application/octet-stream'; + if (typeof Buffer.from === 'function') { + mocks.req.body = Buffer.from('John'); + } else { + mocks.req.body = new Buffer('John'); + } + httpSample.sample.helloContent(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); + }); + + it('http:helloContent: should handle text/plain', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.headers['content-type'] = 'text/plain'; + mocks.req.body = 'John'; + httpSample.sample.helloContent(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); + }); + + it('http:helloContent: should handle application/x-www-form-urlencoded', function () { + var mocks = getMocks(); + var httpSample = getSample(); + mocks.req.headers['content-type'] = 'application/x-www-form-urlencoded'; + mocks.req.body = { name: 'John' }; + httpSample.sample.helloContent(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], 'Hello John!'); + }); + + it('http:helloContent: should handle other', function () { + var mocks = getMocks(); + var httpSample = getSample(); + httpSample.sample.helloContent(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.deepEqual(mocks.res.send.firstCall.args[0], 'Hello World!'); + }); }); -test.after(function () { - console.error.restore(); - console.log.restore(); -}); diff --git a/test/functions/log.test.js b/test/functions/log.test.js index 28cd3c5b23..7c3eb77a6b 100644 --- a/test/functions/log.test.js +++ b/test/functions/log.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); var authClient = {}; @@ -53,76 +51,69 @@ function getSample () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); - -test('should write to log', function (t) { - var expectedMsg = 'I am a log entry!'; - getSample().sample.helloWorld({ - success: function (result) { - t.is(result, undefined); - t.is(console.log.called, true); - t.is(console.log.calledWith(expectedMsg), true); - }, - failure: t.fail +describe('functions:log', function () { + it('should write to log', function () { + var expectedMsg = 'I am a log entry!'; + getSample().sample.helloWorld({ + success: function (result) { + assert.equal(result, undefined); + assert.equal(console.log.called, true); + assert.equal(console.log.calledWith(expectedMsg), true); + }, + failure: assert.fail + }); }); -}); -test('retrieve: should retrieve logs', function (t) { - var logSample = getSample(); - logSample.sample.retrieve(); - t.is(console.log.calledWith('entries'), true); -}); + it('retrieve: should retrieve logs', function () { + var logSample = getSample(); + logSample.sample.retrieve(); + assert.equal(console.log.calledWith('entries'), true); + }); -test('retrieve: handles error', function (t) { - var expectedMsg = 'entries error'; - var logSample = getSample(); - logSample.mocks.logging.getEntries = sinon.stub().callsArgWith(1, expectedMsg); - logSample.sample.retrieve(); - t.is(console.error.calledWith(expectedMsg), true); -}); + it('retrieve: handles error', function () { + var expectedMsg = 'entries error'; + var logSample = getSample(); + logSample.mocks.logging.getEntries = sinon.stub().callsArgWith(1, expectedMsg); + logSample.sample.retrieve(); + assert.equal(console.error.calledWith(expectedMsg), true); + }); -test('getMetrics: should retrieve metrics', function (t) { - var logSample = getSample(); - logSample.sample.getMetrics(); - t.is(console.log.calledWith('series'), true); -}); + it('getMetrics: should retrieve metrics', function () { + var logSample = getSample(); + logSample.sample.getMetrics(); + assert.equal(console.log.calledWith('series'), true); + }); -test('getMetrics: creates with scope', function (t) { - var authClient = { - createScopedRequired: sinon.stub().returns(true), - createScoped: sinon.stub().returns('foo') - }; - var logSample = getSample(); - logSample.mocks.auth.getApplicationDefault = sinon.stub().callsArgWith(0, null, authClient); - logSample.sample.getMetrics(); - t.deepEqual(authClient.createScoped.firstCall.args[0], [ - 'https://www.googleapis.com/auth/cloud-platform', - 'https://www.googleapis.com/auth/monitoring', - 'https://www.googleapis.com/auth/monitoring.read', - 'https://www.googleapis.com/auth/monitoring.write' - ]); -}); + it('getMetrics: creates with scope', function () { + var authClient = { + createScopedRequired: sinon.stub().returns(true), + createScoped: sinon.stub().returns('foo') + }; + var logSample = getSample(); + logSample.mocks.auth.getApplicationDefault = sinon.stub().callsArgWith(0, null, authClient); + logSample.sample.getMetrics(); + assert.deepEqual(authClient.createScoped.firstCall.args[0], [ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/monitoring', + 'https://www.googleapis.com/auth/monitoring.read', + 'https://www.googleapis.com/auth/monitoring.write' + ]); + }); -test('getMetrics: handles auth error', function (t) { - var expectedMsg = 'auth error'; - var logSample = getSample(); - logSample.mocks.auth.getApplicationDefault = sinon.stub().callsArgWith(0, expectedMsg); - logSample.sample.getMetrics(); - t.is(console.error.calledWith('Authentication failed', expectedMsg), true); -}); + it('getMetrics: handles auth error', function () { + var expectedMsg = 'auth error'; + var logSample = getSample(); + logSample.mocks.auth.getApplicationDefault = sinon.stub().callsArgWith(0, expectedMsg); + logSample.sample.getMetrics(); + assert.equal(console.error.calledWith('Authentication failed', expectedMsg), true); + }); -test('getMetrics: handles time series error', function (t) { - var expectedMsg = 'time series error'; - var logSample = getSample(); - logSample.mocks.monitoring.projects.timeSeries.list = sinon.stub().callsArgWith(1, expectedMsg); - logSample.sample.getMetrics(); - t.is(console.error.calledWith(expectedMsg), true); + it('getMetrics: handles time series error', function () { + var expectedMsg = 'time series error'; + var logSample = getSample(); + logSample.mocks.monitoring.projects.timeSeries.list = sinon.stub().callsArgWith(1, expectedMsg); + logSample.sample.getMetrics(); + assert.equal(console.error.calledWith(expectedMsg), true); + }); }); -test.after(function () { - console.error.restore(); - console.log.restore(); -}); diff --git a/test/functions/module.test.js b/test/functions/module.test.js index b9fb6272bf..867fde6ae4 100644 --- a/test/functions/module.test.js +++ b/test/functions/module.test.js @@ -13,14 +13,15 @@ 'use strict'; -var test = require('ava'); var moduleSample = require('../../functions/module'); -test.cb.serial('should return a hello world message', function (t) { - moduleSample.helloworld({ - success: function (result) { - t.is(result, 'Hello World!'); - t.end(); - } +describe('functions:module', function () { + it('should return a hello world message', function (done) { + moduleSample.helloworld({ + success: function (result) { + assert(result === 'Hello World!'); + done(); + } + }); }); }); diff --git a/test/functions/ocr.test.js b/test/functions/ocr.test.js index c071f0ff0e..56ae389a91 100644 --- a/test/functions/ocr.test.js +++ b/test/functions/ocr.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); var bucket = 'bucket'; @@ -87,360 +85,353 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); +describe('functions:ocr', function () { + it('processImage does nothing on delete', function () { + var context = getMockContext(); -test('processImage does nothing on delete', function (t) { - var context = getMockContext(); + getSample().sample.processImage(context, { + timeDeleted: 1234 + }); - getSample().sample.processImage(context, { - timeDeleted: 1234 + assert.equal(context.done.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(context.success.called, false); }); - t.is(context.done.calledOnce, true); - t.is(context.failure.called, false); - t.is(context.success.called, false); -}); - -test('processImage fails without a bucket', function (t) { - var expectedMsg = 'Bucket not provided. Make sure you have a ' + - '"bucket" property in your request'; - var context = getMockContext(); - - getSample().sample.processImage(context, {}); + it('processImage fails without a bucket', function () { + var expectedMsg = 'Bucket not provided. Make sure you have a ' + + '"bucket" property in your request'; + var context = getMockContext(); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); - -test('processImage fails without a name', function (t) { - var expectedMsg = 'Filename not provided. Make sure you have a ' + - '"name" property in your request'; - var context = getMockContext(); + getSample().sample.processImage(context, {}); - getSample().sample.processImage(context, { - bucket: bucket + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('processImage fails without a name', function () { + var expectedMsg = 'Filename not provided. Make sure you have a ' + + '"name" property in your request'; + var context = getMockContext(); -test.cb.serial('processImage handles detectText error', function (t) { - var expectedMsg = 'error'; - var context = { - success: t.fail, - failure: function () { - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.calledWith(expectedMsg), true); - t.end(); - } - }; - - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + getSample().sample.processImage(context, { + bucket: bucket + }); - var ocrSample = getSample(); - ocrSample.mocks.vision.detectText = sinon.stub().callsArgWith(1, expectedMsg); - ocrSample.sample.processImage(context, { - bucket: bucket, - name: name + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); -}); -test.cb.serial('processImage processes an image', function (t) { - var context = { - success: function () { - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith('Processed ' + name), true); - t.end(); - }, - failure: t.fail - }; - - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); - - var ocrSample = getSample(); - ocrSample.mocks.vision.detectText = sinon.stub().callsArgWith(1, null, [ - text - ], {}); - ocrSample.mocks.translate.detect = sinon.stub().callsArgWith(1, null, { - language: 'ja' - }); - ocrSample.sample.processImage(context, { - bucket: bucket, - name: name + it('processImage handles detectText error', function (done) { + var expectedMsg = 'error'; + var context = { + success: assert.fail, + failure: function () { + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.calledWith(expectedMsg), true); + done(); + } + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.vision.detectText = sinon.stub().callsArgWith(1, expectedMsg); + ocrSample.sample.processImage(context, { + bucket: bucket, + name: name + }); }); -}); - -test('translateText fails without text', function (t) { - var expectedMsg = 'Text not provided. Make sure you have a ' + - '"text" property in your request'; - var context = getMockContext(); - - getSample().sample.translateText(context, {}); - - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); - -test('translateText fails without a filename', function (t) { - var expectedMsg = 'Filename not provided. Make sure you have a ' + - '"filename" property in your request'; - var context = getMockContext(); - getSample().sample.translateText(context, { - text: text + it('processImage processes an image', function (done) { + var context = { + success: function () { + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith('Processed ' + name), true); + done(); + }, + failure: assert.fail + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.vision.detectText = sinon.stub().callsArgWith(1, null, [ + text + ], {}); + ocrSample.mocks.translate.detect = sinon.stub().callsArgWith(1, null, { + language: 'ja' + }); + ocrSample.sample.processImage(context, { + bucket: bucket, + name: name + }); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('translateText fails without text', function () { + var expectedMsg = 'Text not provided. Make sure you have a ' + + '"text" property in your request'; + var context = getMockContext(); -test('translateText fails without a lang', function (t) { - var expectedMsg = 'Language not provided. Make sure you have a ' + - '"lang" property in your request'; - var context = getMockContext(); + getSample().sample.translateText(context, {}); - getSample().sample.translateText(context, { - text: text, - filename: filename + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('translateText fails without a filename', function () { + var expectedMsg = 'Filename not provided. Make sure you have a ' + + '"filename" property in your request'; + var context = getMockContext(); -test.cb.serial('translateText handles translation error', function (t) { - var expectedMsg = 'error'; - var context = { - success: t.fail, - failure: function () { - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.calledWith(expectedMsg), true); - t.end(); - } - }; - - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + getSample().sample.translateText(context, { + text: text + }); - var ocrSample = getSample(); - ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, expectedMsg); - ocrSample.sample.translateText(context, { - text: text, - filename: filename, - lang: lang + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); -}); -test.cb.serial('translateText handles get topic error', function (t) { - var expectedMsg = 'error'; - var context = { - success: t.fail, - failure: function () { - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.calledWith(expectedMsg), true); - t.end(); - } - }; + it('translateText fails without a lang', function () { + var expectedMsg = 'Language not provided. Make sure you have a ' + + '"lang" property in your request'; + var context = getMockContext(); - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + getSample().sample.translateText(context, { + text: text, + filename: filename + }); - var ocrSample = getSample(); - ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); - ocrSample.mocks.topic.get = sinon.stub().callsArgWith(1, expectedMsg); - ocrSample.sample.translateText(context, { - text: text, - filename: filename, - lang: lang + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); -}); - -test.cb.serial('translateText handles publish error', function (t) { - var expectedMsg = 'error'; - var context = { - success: t.fail, - failure: function () { - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.calledWith(expectedMsg), true); - t.end(); - } - }; - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); - - var ocrSample = getSample(); - ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); - ocrSample.mocks.topic.publish = sinon.stub().callsArgWith(1, expectedMsg); - ocrSample.sample.translateText(context, { - text: text, - filename: filename, - lang: lang + it('translateText handles translation error', function (done) { + var expectedMsg = 'error'; + var context = { + success: assert.fail, + failure: function () { + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.calledWith(expectedMsg), true); + done(); + } + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, expectedMsg); + ocrSample.sample.translateText(context, { + text: text, + filename: filename, + lang: lang + }); }); -}); -test.cb.serial('translateText translates and publishes text', function (t) { - var context = { - success: function () { - t.is(context.success.called, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith('Text translated to ' + lang), true); - t.end(); - }, - failure: t.fail - }; - - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); - - var ocrSample = getSample(); - ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); - ocrSample.sample.translateText(context, { - text: text, - filename: filename, - lang: lang + it('translateText handles get topic error', function (done) { + var expectedMsg = 'error'; + var context = { + success: assert.fail, + failure: function () { + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.calledWith(expectedMsg), true); + done(); + } + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); + ocrSample.mocks.topic.get = sinon.stub().callsArgWith(1, expectedMsg); + ocrSample.sample.translateText(context, { + text: text, + filename: filename, + lang: lang + }); }); -}); -test('saveResult fails without text', function (t) { - var expectedMsg = 'Text not provided. Make sure you have a ' + - '"text" property in your request'; - var context = getMockContext(); - - getSample().sample.saveResult(context, {}); - - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); - -test('saveResult fails without a filename', function (t) { - var expectedMsg = 'Filename not provided. Make sure you have a ' + - '"filename" property in your request'; - var context = getMockContext(); + it('translateText handles publish error', function (done) { + var expectedMsg = 'error'; + var context = { + success: assert.fail, + failure: function () { + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.calledWith(expectedMsg), true); + done(); + } + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); + ocrSample.mocks.topic.publish = sinon.stub().callsArgWith(1, expectedMsg); + ocrSample.sample.translateText(context, { + text: text, + filename: filename, + lang: lang + }); + }); - getSample().sample.saveResult(context, { - text: text + it('translateText translates and publishes text', function (done) { + var context = { + success: function () { + assert.equal(context.success.called, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith('Text translated to ' + lang), true); + done(); + }, + failure: assert.fail + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.translate.translate = sinon.stub().callsArgWith(2, null, translation); + ocrSample.sample.translateText(context, { + text: text, + filename: filename, + lang: lang + }); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('saveResult fails without text', function () { + var expectedMsg = 'Text not provided. Make sure you have a ' + + '"text" property in your request'; + var context = getMockContext(); -test('saveResult fails without a lang', function (t) { - var expectedMsg = 'Language not provided. Make sure you have a ' + - '"lang" property in your request'; - var context = getMockContext(); + getSample().sample.saveResult(context, {}); - getSample().sample.saveResult(context, { - text: text, - filename: filename + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); - -test.cb.serial('saveResult handles save error', function (t) { - var expectedMsg = 'error'; - var context = { - success: t.fail, - failure: function () { - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.calledWith(expectedMsg), true); - t.end(); - } - }; + it('saveResult fails without a filename', function () { + var expectedMsg = 'Filename not provided. Make sure you have a ' + + '"filename" property in your request'; + var context = getMockContext(); - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + getSample().sample.saveResult(context, { + text: text + }); - var ocrSample = getSample(); - ocrSample.mocks.file.save = sinon.stub().callsArgWith(1, expectedMsg); - ocrSample.sample.saveResult(context, { - text: text, - filename: filename, - lang: lang + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); -}); -test.cb.serial('saveResult translates and publishes text', function (t) { - var context = { - success: function () { - t.is(context.success.called, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith('Text written to ' + filename + '_to_lang.txt'), true); - t.end(); - }, - failure: t.fail - }; + it('saveResult fails without a lang', function () { + var expectedMsg = 'Language not provided. Make sure you have a ' + + '"lang" property in your request'; + var context = getMockContext(); - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + getSample().sample.saveResult(context, { + text: text, + filename: filename + }); - var ocrSample = getSample(); - ocrSample.sample.saveResult(context, { - text: text, - filename: filename, - lang: lang + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.called, true); }); -}); -test.cb.serial('saveResult translates and publishes text with dot in filename', function (t) { - var context = { - success: function () { - t.is(context.success.called, true); - t.is(context.failure.called, false); - t.is(console.log.calledWith('Text written to ' + filename + '_to_lang.txt'), true); - t.end(); - }, - failure: t.fail - }; + it('saveResult handles save error', function (done) { + var expectedMsg = 'error'; + var context = { + success: assert.fail, + failure: function () { + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert.equal(console.error.calledWith(expectedMsg), true); + done(); + } + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.mocks.file.save = sinon.stub().callsArgWith(1, expectedMsg); + ocrSample.sample.saveResult(context, { + text: text, + filename: filename, + lang: lang + }); + }); - sinon.spy(context, 'success'); - sinon.spy(context, 'failure'); + it('saveResult translates and publishes text', function (done) { + var context = { + success: function () { + assert.equal(context.success.called, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith('Text written to ' + filename + '_to_lang.txt'), true); + done(); + }, + failure: assert.fail + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.sample.saveResult(context, { + text: text, + filename: filename, + lang: lang + }); + }); - var ocrSample = getSample(); - ocrSample.sample.saveResult(context, { - text: text, - filename: filename + '.jpg', - lang: lang + it('saveResult translates and publishes text with dot in filename', function (done) { + var context = { + success: function () { + assert.equal(context.success.called, true); + assert.equal(context.failure.called, false); + assert.equal(console.log.calledWith('Text written to ' + filename + '_to_lang.txt'), true); + done(); + }, + failure: assert.fail + }; + + sinon.spy(context, 'success'); + sinon.spy(context, 'failure'); + + var ocrSample = getSample(); + ocrSample.sample.saveResult(context, { + text: text, + filename: filename + '.jpg', + lang: lang + }); }); }); -test.after(function () { - console.error.restore(); - console.log.restore(); -}); diff --git a/test/functions/pubsub.test.js b/test/functions/pubsub.test.js index d2de72ea3c..66c9638140 100644 --- a/test/functions/pubsub.test.js +++ b/test/functions/pubsub.test.js @@ -13,8 +13,6 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); function getSample () { @@ -46,108 +44,100 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); +describe('functions:pubsub', function () { + it('Publish fails without a topic', function () { + var expectedMsg = 'Topic not provided. Make sure you have a "topic" ' + + 'property in your request'; + var context = getMockContext(); -test('Publish fails without a topic', function (t) { - var expectedMsg = 'Topic not provided. Make sure you have a "topic" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.publish(context, { + message: 'message' + }); - getSample().sample.publish(context, { - message: 'message' + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); + it('Publish fails without a message', function () { + var expectedMsg = 'Message not provided. Make sure you have a "message" ' + + 'property in your request'; + var context = getMockContext(); -test('Publish fails without a message', function (t) { - var expectedMsg = 'Message not provided. Make sure you have a "message" ' + - 'property in your request'; - var context = getMockContext(); + getSample().sample.publish(context, { + topic: 'topic' + }); - getSample().sample.publish(context, { - topic: 'topic' + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); -}); - -test('Publishes the message to the topic and calls success', function (t) { - var expectedMsg = 'Message published'; - var data = { - topic: 'topic', - message: 'message' - }; - var context = getMockContext(); - - var pubsubSample = getSample(); - pubsubSample.sample.publish(context, data); - - t.is(context.success.calledOnce, true); - t.is(context.success.firstCall.args[0], expectedMsg); - t.is(context.failure.called, false); - t.is(pubsubSample.mocks.pubsub.topic.calledOnce, true); - t.deepEqual(pubsubSample.mocks.pubsub.topic.firstCall.args[0], data.topic); - t.is(pubsubSample.mocks.topic.publish.calledOnce, true); - t.deepEqual(pubsubSample.mocks.topic.publish.firstCall.args[0], { - data: { - message: data.message - } + it('Publishes the message to the topic and calls success', function () { + var expectedMsg = 'Message published'; + var data = { + topic: 'topic', + message: 'message' + }; + var context = getMockContext(); + + var pubsubSample = getSample(); + pubsubSample.sample.publish(context, data); + + assert.equal(context.success.calledOnce, true); + assert.equal(context.success.firstCall.args[0], expectedMsg); + assert.equal(context.failure.called, false); + assert.equal(pubsubSample.mocks.pubsub.topic.calledOnce, true); + assert.deepEqual(pubsubSample.mocks.pubsub.topic.firstCall.args[0], data.topic); + assert.equal(pubsubSample.mocks.topic.publish.calledOnce, true); + assert.deepEqual(pubsubSample.mocks.topic.publish.firstCall.args[0], { + data: { + message: data.message + } + }); }); -}); -test('Fails to publish the message and calls failure', function (t) { - var expectedMsg = 'error'; - var data = { - topic: 'topic', - message: 'message' - }; - var context = getMockContext(); - - var pubsubSample = getSample(); - pubsubSample.mocks.topic.publish = sinon.stub().callsArgWith(1, expectedMsg); - - pubsubSample.sample.publish(context, data); - - t.is(context.success.called, false); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(pubsubSample.mocks.pubsub.topic.calledOnce, true); - t.deepEqual(pubsubSample.mocks.pubsub.topic.firstCall.args[0], data.topic); - t.is(pubsubSample.mocks.topic.publish.calledOnce, true); - t.deepEqual(pubsubSample.mocks.topic.publish.firstCall.args[0], { - data: { - message: data.message - } + it('Fails to publish the message and calls failure', function () { + var expectedMsg = 'error'; + var data = { + topic: 'topic', + message: 'message' + }; + var context = getMockContext(); + + var pubsubSample = getSample(); + pubsubSample.mocks.topic.publish = sinon.stub().callsArgWith(1, expectedMsg); + + pubsubSample.sample.publish(context, data); + + assert.equal(context.success.called, false); + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(pubsubSample.mocks.pubsub.topic.calledOnce, true); + assert.deepEqual(pubsubSample.mocks.pubsub.topic.firstCall.args[0], data.topic); + assert.equal(pubsubSample.mocks.topic.publish.calledOnce, true); + assert.deepEqual(pubsubSample.mocks.topic.publish.firstCall.args[0], { + data: { + message: data.message + } + }); }); -}); -test('Subscribes to a message', function (t) { - var expectedMsg = 'message'; - var data = { - topic: 'topic', - message: expectedMsg - }; - var context = getMockContext(); + it('Subscribes to a message', function () { + var expectedMsg = 'message'; + var data = { + topic: 'topic', + message: expectedMsg + }; + var context = getMockContext(); - var pubsubSample = getSample(); + var pubsubSample = getSample(); - pubsubSample.sample.subscribe(context, data); + pubsubSample.sample.subscribe(context, data); - t.is(console.log.called, true); - t.is(console.log.calledWith(expectedMsg), true); - t.is(context.success.calledOnce, true); - t.is(context.failure.called, false); -}); - -test.after(function () { - console.error.restore(); - console.log.restore(); + assert.equal(console.log.called, true); + assert.equal(console.log.calledWith(expectedMsg), true); + assert.equal(context.success.calledOnce, true); + assert.equal(context.failure.called, false); + }); }); diff --git a/test/functions/sendgrid.test.js b/test/functions/sendgrid.test.js index 7aca3ad999..9c3b84cf38 100644 --- a/test/functions/sendgrid.test.js +++ b/test/functions/sendgrid.test.js @@ -13,11 +13,9 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); -const util = require('util'); -const EventEmitter = require('events'); +var util = require('util'); +var EventEmitter = require('events'); var method = 'POST'; var key = 'sengrid_key'; @@ -177,449 +175,440 @@ function getMockContext () { }; } -test.before(function () { - sinon.stub(console, 'error'); - sinon.stub(console, 'log'); -}); - -test('Send fails if not a POST request', function (t) { - var expectedMsg = 'Only POST requests are accepted'; - var mocks = getMocks(); +describe('functions:pubsub', function () { + it('Send fails if not a POST request', function () { + var expectedMsg = 'Only POST requests are accepted'; + var mocks = getMocks(); - getSample().sample.sendgridEmail(mocks.req, mocks.res); + getSample().sample.sendgridEmail(mocks.req, mocks.res); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 405); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 405); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Send fails without an API key', function (t) { - var expectedMsg = 'SendGrid API key not provided. Make sure you have a ' + - '"sg_key" property in your request querystring'; - var mocks = getMocks(); + it('Send fails without an API key', function () { + var expectedMsg = 'SendGrid API key not provided. Make sure you have a ' + + '"sg_key" property in your request querystring'; + var mocks = getMocks(); - mocks.req.method = method; - getSample().sample.sendgridEmail(mocks.req, mocks.res); + mocks.req.method = method; + getSample().sample.sendgridEmail(mocks.req, mocks.res); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 401); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 401); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Send fails without a "to"', function (t) { - var expectedMsg = 'To email address not provided. Make sure you have a ' + - '"to" property in your request'; - var mocks = getMocks(); + it('Send fails without a "to"', function () { + var expectedMsg = 'To email address not provided. Make sure you have a ' + + '"to" property in your request'; + var mocks = getMocks(); - mocks.req.method = method; - mocks.req.query.sg_key = key; - getSample().sample.sendgridEmail(mocks.req, mocks.res); + mocks.req.method = method; + mocks.req.query.sg_key = key; + getSample().sample.sendgridEmail(mocks.req, mocks.res); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Send fails without a "from"', function (t) { - var expectedMsg = 'From email address not provided. Make sure you have a ' + - '"from" property in your request'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - getSample().sample.sendgridEmail(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + it('Send fails without a "from"', function () { + var expectedMsg = 'From email address not provided. Make sure you have a ' + + '"from" property in your request'; + var mocks = getMocks(); + + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + getSample().sample.sendgridEmail(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Send fails without a "subject"', function (t) { - var expectedMsg = 'Email subject line not provided. Make sure you have a ' + - '"subject" property in your request'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - getSample().sample.sendgridEmail(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + it('Send fails without a "subject"', function () { + var expectedMsg = 'Email subject line not provided. Make sure you have a ' + + '"subject" property in your request'; + var mocks = getMocks(); + + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + getSample().sample.sendgridEmail(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Send fails without a "body"', function (t) { - var expectedMsg = 'Email content not provided. Make sure you have a ' + - '"body" property in your request'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - mocks.req.body.subject = subject; - getSample().sample.sendgridEmail(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + it('Send fails without a "body"', function () { + var expectedMsg = 'Email content not provided. Make sure you have a ' + + '"body" property in your request'; + var mocks = getMocks(); + + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + mocks.req.body.subject = subject; + getSample().sample.sendgridEmail(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); -test('Sends the email and successfully responds', function (t) { - var expectedMsg = 'success'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - mocks.req.body.subject = subject; - mocks.req.body.body = body; - getSample().sample.sendgridEmail(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); -}); + it('Sends the email and successfully responds', function () { + var expectedMsg = 'success'; + var mocks = getMocks(); + + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + mocks.req.body.subject = subject; + mocks.req.body.body = body; + getSample().sample.sendgridEmail(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + }); -test('Handles response error', function (t) { - var expectedMsg = 'failure'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - mocks.req.body.subject = subject; - mocks.req.body.body = body; - - var sendgridSample = getSample(); - sendgridSample.mocks.client.API = sinon.stub().callsArgWith(1, { - statusCode: 400, - body: 'failure', - headers: {} + it('Handles response error', function () { + var expectedMsg = 'failure'; + var mocks = getMocks(); + + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + mocks.req.body.subject = subject; + mocks.req.body.body = body; + + var sendgridSample = getSample(); + sendgridSample.mocks.client.API = sinon.stub().callsArgWith(1, { + statusCode: 400, + body: 'failure', + headers: {} + }); + sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 400); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); }); - sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 400); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); -}); + it('Handles thrown error', function () { + var mocks = getMocks(); -test('Handles thrown error', function (t) { - var mocks = getMocks(); + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + mocks.req.body.subject = subject; + mocks.req.body.body = body; - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - mocks.req.body.subject = subject; - mocks.req.body.body = body; + var sendgridSample = getSample(); + sendgridSample.mocks.mail.toJSON = sinon.stub().throws('TypeError'); + sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); - var sendgridSample = getSample(); - sendgridSample.mocks.mail.toJSON = sinon.stub().throws('TypeError'); - sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 500); + assert.equal(mocks.res.send.calledOnce, true); + }); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 500); - t.is(mocks.res.send.calledOnce, true); -}); + it('Handles emtpy response body', function () { + var mocks = getMocks(); -test('Handles emtpy response body', function (t) { - var mocks = getMocks(); + mocks.req.method = method; + mocks.req.query.sg_key = key; + mocks.req.body.to = to; + mocks.req.body.from = from; + mocks.req.body.subject = subject; + mocks.req.body.body = body; - mocks.req.method = method; - mocks.req.query.sg_key = key; - mocks.req.body.to = to; - mocks.req.body.from = from; - mocks.req.body.subject = subject; - mocks.req.body.body = body; + var sendgridSample = getSample(); + sendgridSample.mocks.client.API = sinon.stub().callsArgWith(1, { + statusCode: 200, + headers: {} + }); + sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); - var sendgridSample = getSample(); - sendgridSample.mocks.client.API = sinon.stub().callsArgWith(1, { - statusCode: 200, - headers: {} + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.calledOnce, false); }); - sendgridSample.sample.sendgridEmail(mocks.req, mocks.res); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.calledOnce, false); -}); - -test('Send fails if not a POST request', function (t) { - var expectedMsg = 'Only POST requests are accepted'; - var mocks = getMocks(); + it('Send fails if not a POST request', function () { + var expectedMsg = 'Only POST requests are accepted'; + var mocks = getMocks(); - getSample().sample.sendgridWebhook(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 405); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + getSample().sample.sendgridWebhook(mocks.req, mocks.res); -test('Throws if no basic auth', function (t) { - var expectedMsg = 'Invalid credentials'; - var mocks = getMocks(); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 405); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); - mocks.req.method = method; - mocks.req.headers.authorization = ''; - getSample().sample.sendgridWebhook(mocks.req, mocks.res); + it('Throws if no basic auth', function () { + var expectedMsg = 'Invalid credentials'; + var mocks = getMocks(); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 401); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + mocks.req.method = method; + mocks.req.headers.authorization = ''; + getSample().sample.sendgridWebhook(mocks.req, mocks.res); -test('Throws if invalid username', function (t) { - var expectedMsg = 'Invalid credentials'; - var mocks = getMocks(); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 401); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); - mocks.req.method = method; - mocks.req.headers.authorization = 'Basic d3Jvbmc6YmFy'; - getSample().sample.sendgridWebhook(mocks.req, mocks.res); + it('Throws if invalid username', function () { + var expectedMsg = 'Invalid credentials'; + var mocks = getMocks(); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 401); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + mocks.req.method = method; + mocks.req.headers.authorization = 'Basic d3Jvbmc6YmFy'; + getSample().sample.sendgridWebhook(mocks.req, mocks.res); -test('Throws if invalid password', function (t) { - var expectedMsg = 'Invalid credentials'; - var mocks = getMocks(); - - mocks.req.method = method; - mocks.req.headers.authorization = 'Basic Zm9vOndyb25n'; - getSample().sample.sendgridWebhook(mocks.req, mocks.res); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 401); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 401); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); - t.is(console.error.called, true); -}); + it('Throws if invalid password', function () { + var expectedMsg = 'Invalid credentials'; + var mocks = getMocks(); -test('Calls "end" if no events', function (t) { - var mocks = getMocks(); + mocks.req.method = method; + mocks.req.headers.authorization = 'Basic Zm9vOndyb25n'; + getSample().sample.sendgridWebhook(mocks.req, mocks.res); - mocks.req.method = method; - mocks.req.headers.authorization = auth; - mocks.req.body = undefined; - getSample().sample.sendgridWebhook(mocks.req, mocks.res); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 401); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); + assert(console.error.called); + }); - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.send.called, false); - t.is(mocks.res.end.calledOnce, true); - t.is(console.error.called, true); -}); + it('Calls "end" if no events', function () { + var mocks = getMocks(); -test('Saves files', function (t) { - var mocks = getMocks(); - - mocks.req.method = 'POST'; - mocks.req.headers.authorization = auth; - mocks.req.body = events; - var sendgridSample = getSample(); - sendgridSample.mocks.uuid.v4 = sinon.stub().returns('1357'); - sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); - - var filename = sendgridSample.mocks.bucket.file.firstCall.args[0]; - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 200); - t.is(mocks.res.end.calledOnce, true); - t.is(console.log.calledWith('Saving events to ' + filename + ' in bucket ' + sendgridSample.mocks.config.EVENT_BUCKET), true); - t.is(console.log.calledWith('JSON written to ' + filename), true); -}); + mocks.req.method = method; + mocks.req.headers.authorization = auth; + mocks.req.body = undefined; + getSample().sample.sendgridWebhook(mocks.req, mocks.res); -test('Handles save error', function (t) { - var expectedMsg = 'save_error'; - var mocks = getMocks(); - - mocks.req.method = 'POST'; - mocks.req.headers.authorization = auth; - mocks.req.body = events; - var sendgridSample = getSample(); - sendgridSample.mocks.uuid.v4 = sinon.stub().returns('2468'); - sendgridSample.mocks.file.save = sinon.stub().callsArgWith(1, expectedMsg); - sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); - - var filename = sendgridSample.mocks.bucket.file.firstCall.args[0]; - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 500); - t.is(mocks.res.end.calledOnce, true); - t.is(console.log.calledWith('Saving events to ' + filename + ' in bucket ' + sendgridSample.mocks.config.EVENT_BUCKET), true); - t.is(console.error.calledWith(expectedMsg), true); -}); + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.send.called, false); + assert.equal(mocks.res.end.calledOnce, true); + }); -test('Handles random error', function (t) { - var expectedMsg = 'random_error'; - var mocks = getMocks(); - - mocks.req.method = 'POST'; - mocks.req.headers.authorization = auth; - mocks.req.body = events; - var sendgridSample = getSample(); - sendgridSample.mocks.uuid.v4 = sinon.stub().throws(new Error(expectedMsg)); - sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); - - t.is(mocks.res.status.calledOnce, true); - t.is(mocks.res.status.firstCall.args[0], 500); - t.is(mocks.res.send.calledOnce, true); - t.is(mocks.res.send.firstCall.args[0], expectedMsg); -}); + it('Saves files', function () { + var mocks = getMocks(); + + mocks.req.method = 'POST'; + mocks.req.headers.authorization = auth; + mocks.req.body = events; + var sendgridSample = getSample(); + sendgridSample.mocks.uuid.v4 = sinon.stub().returns('1357'); + sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); + + var filename = sendgridSample.mocks.bucket.file.firstCall.args[0]; + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 200); + assert.equal(mocks.res.end.calledOnce, true); + assert.equal(console.log.calledWith('Saving events to ' + filename + ' in bucket ' + sendgridSample.mocks.config.EVENT_BUCKET), true); + assert.equal(console.log.calledWith('JSON written to ' + filename), true); + }); -test('sendgridLoad does nothing on delete', function (t) { - var context = getMockContext(); + it('Handles save error', function () { + var expectedMsg = 'save_error'; + var mocks = getMocks(); + + mocks.req.method = 'POST'; + mocks.req.headers.authorization = auth; + mocks.req.body = events; + var sendgridSample = getSample(); + sendgridSample.mocks.uuid.v4 = sinon.stub().returns('2468'); + sendgridSample.mocks.file.save = sinon.stub().callsArgWith(1, expectedMsg); + sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); + + var filename = sendgridSample.mocks.bucket.file.firstCall.args[0]; + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 500); + assert.equal(mocks.res.end.calledOnce, true); + assert.equal(console.log.calledWith('Saving events to ' + filename + ' in bucket ' + sendgridSample.mocks.config.EVENT_BUCKET), true); + assert.equal(console.error.calledWith(expectedMsg), true); + }); - getSample().sample.sendgridLoad(context, { - timeDeleted: 1234 + it('Handles random error', function () { + var expectedMsg = 'random_error'; + var mocks = getMocks(); + + mocks.req.method = 'POST'; + mocks.req.headers.authorization = auth; + mocks.req.body = events; + var sendgridSample = getSample(); + sendgridSample.mocks.uuid.v4 = sinon.stub().throws(new Error(expectedMsg)); + sendgridSample.sample.sendgridWebhook(mocks.req, mocks.res); + + assert.equal(mocks.res.status.calledOnce, true); + assert.equal(mocks.res.status.firstCall.args[0], 500); + assert.equal(mocks.res.send.calledOnce, true); + assert.equal(mocks.res.send.firstCall.args[0], expectedMsg); }); - t.is(context.done.calledOnce, true); - t.is(context.failure.called, false); - t.is(context.success.called, false); -}); + it('sendgridLoad does nothing on delete', function () { + var context = getMockContext(); -test('sendgridLoad fails without a bucket', function (t) { - var expectedMsg = 'Bucket not provided. Make sure you have a ' + - '"bucket" property in your request'; - var context = getMockContext(); + getSample().sample.sendgridLoad(context, { + timeDeleted: 1234 + }); - getSample().sample.sendgridLoad(context, {}); + assert.equal(context.done.calledOnce, true); + assert.equal(context.failure.called, false); + assert.equal(context.success.called, false); + }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('sendgridLoad fails without a bucket', function () { + var expectedMsg = 'Bucket not provided. Make sure you have a ' + + '"bucket" property in your request'; + var context = getMockContext(); -test('sendgridLoad fails without a name', function (t) { - var expectedMsg = 'Filename not provided. Make sure you have a ' + - '"name" property in your request'; - var context = getMockContext(); + getSample().sample.sendgridLoad(context, {}); - getSample().sample.sendgridLoad(context, { - bucket: 'event-bucket' + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert(console.error.called); }); - t.is(context.failure.calledOnce, true); - t.is(context.failure.firstCall.args[0], expectedMsg); - t.is(context.success.called, false); - t.is(console.error.called, true); -}); + it('sendgridLoad fails without a name', function () { + var expectedMsg = 'Filename not provided. Make sure you have a ' + + '"name" property in your request'; + var context = getMockContext(); -test.cb.serial('starts a load job', function (t) { - var name = '1234.json'; - var context = { - success: function () { - t.is(console.log.calledWith('Starting job for ' + name), true); - t.is(console.log.calledWith('Job complete for ' + name), true); - t.end(); - }, - failure: t.fail - }; + getSample().sample.sendgridLoad(context, { + bucket: 'event-bucket' + }); - var sendgridSample = getSample(); - sendgridSample.sample.sendgridLoad(context, { - bucket: 'event-bucket', - name: name + assert.equal(context.failure.calledOnce, true); + assert.equal(context.failure.firstCall.args[0], expectedMsg); + assert.equal(context.success.called, false); + assert(console.error.called); }); - setTimeout(function () { - sendgridSample.mocks.job.emit('complete', {}); - }, 10); -}); - -test.cb.serial('handles job failure', function (t) { - var name = '1234.json'; - var error = 'job_error'; - var context = { - success: t.fail, - failure: function (msg) { - t.is(msg, error); - t.is(console.log.calledWith('Starting job for ' + name), true); - t.is(console.error.calledWith('Job failed for ' + name), true); - t.is(console.error.calledWith(error), true); - t.end(); - } - }; - - var sendgridSample = getSample(); - sendgridSample.sample.sendgridLoad(context, { - bucket: 'event-bucket', - name: name + it('starts a load job', function (done) { + var name = '1234.json'; + var context = { + success: function () { + assert.equal(console.log.calledWith('Starting job for ' + name), true); + assert.equal(console.log.calledWith('Job complete for ' + name), true); + done(); + }, + failure: assert.fail + }; + + var sendgridSample = getSample(); + sendgridSample.sample.sendgridLoad(context, { + bucket: 'event-bucket', + name: name + }); + + setTimeout(function () { + sendgridSample.mocks.job.emit('complete', {}); + }, 10); }); - setTimeout(function () { - sendgridSample.mocks.job.emit('error', error); - }, 10); -}); + it('handles job failure', function (done) { + var name = '1234.json'; + var error = 'job_error'; + var context = { + success: assert.fail, + failure: function (msg) { + assert.equal(msg, error); + assert.equal(console.log.calledWith('Starting job for ' + name), true); + assert.equal(console.error.calledWith('Job failed for ' + name), true); + assert.equal(console.error.calledWith(error), true); + done(); + } + }; -test.cb.serial('handles dataset error', function (t) { - var name = '1234.json'; - var error = 'dataset_error'; - var context = { - success: t.fail, - failure: function (msg) { - t.is(msg, error); - t.is(console.error.calledWith(error), true); - t.end(); - } - }; + var sendgridSample = getSample(); + sendgridSample.sample.sendgridLoad(context, { + bucket: 'event-bucket', + name: name + }); - var sendgridSample = getSample(); - sendgridSample.mocks.dataset.get = sinon.stub().callsArgWith(1, error); - sendgridSample.sample.sendgridLoad(context, { - bucket: 'event-bucket', - name: name + setTimeout(function () { + sendgridSample.mocks.job.emit('error', error); + }, 10); }); -}); -test.cb.serial('handles table error', function (t) { - var name = '1234.json'; - var error = 'table_error'; - var context = { - success: t.fail, - failure: function (msg) { - t.is(msg, error); - t.is(console.error.calledWith(error), true); - t.end(); - } - }; - - var sendgridSample = getSample(); - sendgridSample.mocks.table.get = sinon.stub().callsArgWith(1, error); - sendgridSample.sample.sendgridLoad(context, { - bucket: 'event-bucket', - name: name + it('handles dataset error', function (done) { + var name = '1234.json'; + var error = 'dataset_error'; + var context = { + success: assert.fail, + failure: function (msg) { + assert.equal(msg, error); + assert.equal(console.error.calledWith(error), true); + done(); + } + }; + + var sendgridSample = getSample(); + sendgridSample.mocks.dataset.get = sinon.stub().callsArgWith(1, error); + sendgridSample.sample.sendgridLoad(context, { + bucket: 'event-bucket', + name: name + }); }); -}); -test.after(function () { - console.error.restore(); - console.log.restore(); + it('handles table error', function (done) { + var name = '1234.json'; + var error = 'table_error'; + var context = { + success: assert.fail, + failure: function (msg) { + assert.equal(msg, error); + assert.equal(console.error.calledWith(error), true); + done(); + } + }; + + var sendgridSample = getSample(); + sendgridSample.mocks.table.get = sinon.stub().callsArgWith(1, error); + sendgridSample.sample.sendgridLoad(context, { + bucket: 'event-bucket', + name: name + }); + }); }); diff --git a/test/functions/uuid.test.js b/test/functions/uuid.test.js index 7c3e07728c..17546ba5dc 100644 --- a/test/functions/uuid.test.js +++ b/test/functions/uuid.test.js @@ -13,15 +13,16 @@ 'use strict'; -var test = require('ava'); var uuidSample = require('../../functions/uuid'); -test.cb.serial('should generate a uuid', function (t) { - uuidSample.uuid({ - success: function (uuid) { - t.is(typeof uuid, 'string'); - t.is(uuid.length, 36); - t.end(); - } +describe('functions:uuid', function () { + it('should generate a uuid', function (done) { + uuidSample.uuid({ + success: function (uuid) { + assert.equal(typeof uuid, 'string'); + assert.equal(uuid.length, 36); + done(); + } + }); }); }); diff --git a/test/logging/export.test.js b/test/logging/export.test.js index 8d3482946f..05a9b16f9c 100644 --- a/test/logging/export.test.js +++ b/test/logging/export.test.js @@ -13,14 +13,15 @@ 'use strict'; -var test = require('ava'); var exportExample = require('../../logging/export'); -test.cb.serial('should list sinks', function (t) { - exportExample.main(function (err, sinks) { - t.ifError(err); - t.truthy(sinks, 'should have received sinks'); - t.truthy(Array.isArray(sinks), 'sinks should be an array'); - t.end(); +describe('logging:export', function () { + it('should list sinks', function (done) { + exportExample.main(function (err, sinks) { + assert(!err); + assert(sinks, 'should have received sinks'); + assert(Array.isArray(sinks), 'sinks should be an array'); + done(); + }); }); }); diff --git a/test/logging/fluent.test.js b/test/logging/fluent.test.js index 9f932c8b61..349feeea2a 100644 --- a/test/logging/fluent.test.js +++ b/test/logging/fluent.test.js @@ -13,39 +13,40 @@ 'use strict'; -var test = require('ava'); var proxyquire = require('proxyquire').noPreserveCache(); var request = require('supertest'); -test.cb.serial('should log error', function (t) { - var loggerCalled = false; +describe('logging:fluent', function () { + it('should log error', function (done) { + var loggerCalled = false; - var structuredLogger = { - emit: function (name) { - loggerCalled = true; - t.is(name, 'errors'); - } - }; + var structuredLogger = { + emit: function (name) { + loggerCalled = true; + assert(name === 'errors'); + } + }; - var app = proxyquire('../../logging/fluent.js', { - 'fluent-logger': { - createFluentSender: function (name, options) { - t.is(name, 'myapp'); - t.deepEqual(options, { - host: 'localhost', - port: 24224, - timeout: 3.0 - }); - return structuredLogger; + var app = proxyquire('../../logging/fluent.js', { + 'fluent-logger': { + createFluentSender: function (name, options) { + assert(name === 'myapp'); + assert.deepEqual(options, { + host: 'localhost', + port: 24224, + timeout: 3.0 + }); + return structuredLogger; + } } - } - }); + }); - request(app) - .get('/') - .expect(500) - .expect(function () { - t.is(loggerCalled, true, 'structuredLogger.emit should have been called'); - }) - .end(t.end); + request(app) + .get('/') + .expect(500) + .expect(function () { + assert(loggerCalled, 'structuredLogger.emit should have been called'); + }) + .end(done); + }); }); diff --git a/test/logging/list.test.js b/test/logging/list.test.js index 6c034677d7..c505625322 100644 --- a/test/logging/list.test.js +++ b/test/logging/list.test.js @@ -13,16 +13,17 @@ 'use strict'; -var test = require('ava'); var listExample = require('../../logging/list'); -test.cb.serial('should list entries', function (t) { - listExample.main(undefined, function (err, entries, nextQuery, apiResponse) { - t.ifError(err); - t.truthy(entries, 'should have received entries'); - t.truthy(Array.isArray(entries), 'entries should be an array'); - t.truthy(nextQuery, 'should have received nextQuery'); - t.truthy(apiResponse, 'should have received apiResponse'); - t.end(); +describe('logging:list', function () { + it('should list entries', function (done) { + listExample.main(undefined, function (err, entries, nextQuery, apiResponse) { + assert(!err); + assert(entries, 'should have received entries'); + assert(Array.isArray(entries), 'entries should be an array'); + assert(nextQuery, 'should have received nextQuery'); + assert(apiResponse, 'should have received apiResponse'); + done(); + }); }); }); diff --git a/test/logging/write.test.js b/test/logging/write.test.js index 1db35e158a..1465e45069 100644 --- a/test/logging/write.test.js +++ b/test/logging/write.test.js @@ -13,16 +13,17 @@ 'use strict'; -var test = require('ava'); var writeExample = require('../../logging/write'); -test.cb.serial('should write entries', function (t) { - writeExample.main(function (err, results) { - if (err && err.code === 404) { - return t.end(); - } - t.ifError(err); - t.is(results.length, 2, 'should have two results'); - t.end(); +describe('logging:write', function () { + it('should write entries', function (done) { + writeExample.main(function (err, results) { + if (err && err.code === 404) { + return done(); + } + assert(!err); + assert(results.length === 2, 'should have two results'); + done(); + }); }); }); diff --git a/test/monitoring/create_custom_metric.test.js b/test/monitoring/create_custom_metric.test.js index d110126384..44ff198ffc 100644 --- a/test/monitoring/create_custom_metric.test.js +++ b/test/monitoring/create_custom_metric.test.js @@ -13,7 +13,6 @@ 'use strict'; -var test = require('ava'); var customMetricsExample = require('../../monitoring/create_custom_metric'); /** Refactored out to keep lines shorter */ @@ -21,26 +20,33 @@ function getPointValue (timeSeries) { return timeSeries.timeSeries[0].points[0].value.int64Value; } -test.cb.serial('should create and read back a custom metric', function (t) { - customMetricsExample.main( - process.env.GCLOUD_PROJECT, - Math.random().toString(36).substring(7), - function (err, results) { - t.ifError(err); - // console.log('---------------------------------------------'); - // console.log(JSON.stringify(results, null, 2)); - // console.log('---------------------------------------------'); - t.is(results.length, 4); - // Result of creating metric - t.truthy(typeof results[0].name === 'string'); - // Result of writing time series - t.deepEqual(results[1], {}); - // Result of reading time series - t.truthy(typeof getPointValue(results[2]) === 'string'); - t.truthy(!isNaN(parseInt(getPointValue(results[2]), 10))); - // Result of deleting metric - t.deepEqual(results[3], {}); - t.end(); - } - ); +describe('monitoring:create_custom_metric', function () { + it('should create and read back a custom metric', function (done) { + customMetricsExample.main( + process.env.GCLOUD_PROJECT, + Math.random().toString(36).substring(7), + function (err, results) { + assert(!err); + // console.log('---------------------------------------------'); + // console.log(JSON.stringify(results, null, 2)); + // console.log('---------------------------------------------'); + assert(results.length === 4); + // Result of creating metric + assert(typeof results[0].name === 'string'); + // Result of writing time series + assert.deepEqual(results[1], {}); + // Result of reading time series + assert(typeof getPointValue(results[2]) === 'string'); + assert(!isNaN(parseInt(getPointValue(results[2]), 10))); + // Result of deleting metric + assert.deepEqual(results[3], {}); + assert(console.log.calledWith('Created custom metric')); + assert(console.log.calledWith('Wrote time series')); + assert(console.log.calledWith('Reading metric type')); + assert(console.log.calledWith('Time series')); + assert(console.log.calledWith('Deleted metric')); + done(); + } + ); + }); }); diff --git a/test/monitoring/list_resources.test.js b/test/monitoring/list_resources.test.js index 177415684a..b7b0c8bf91 100644 --- a/test/monitoring/list_resources.test.js +++ b/test/monitoring/list_resources.test.js @@ -13,22 +13,26 @@ 'use strict'; -var test = require('ava'); var listResourcesExample = require('../../monitoring/list_resources'); -test.cb.serial('should list a bunch of stuff', function (t) { - listResourcesExample.main( - process.env.GCLOUD_PROJECT, - function (err, results) { - t.ifError(err); - t.is(results.length, 3); - // Monitored resources - t.truthy(Array.isArray(results[0].resourceDescriptors)); - // Metric descriptors - t.truthy(Array.isArray(results[1].metricDescriptors)); - // Time series - t.truthy(Array.isArray(results[2].timeSeries)); - t.end(); - } - ); +describe('monitoring:list_resources', function () { + it('should list a bunch of stuff', function (done) { + listResourcesExample.main( + process.env.GCLOUD_PROJECT, + function (err, results) { + assert(!err); + assert(results.length === 3); + // Monitored resources + assert(Array.isArray(results[0].resourceDescriptors)); + // Metric descriptors + assert(Array.isArray(results[1].metricDescriptors)); + // Time series + assert(Array.isArray(results[2].timeSeries)); + assert(console.log.calledWith('Monitored resources')); + assert(console.log.calledWith('Metric descriptors')); + assert(console.log.calledWith('Time series')); + done(); + } + ); + }); }); diff --git a/test/prediction/hostedmodels.test.js b/test/prediction/hostedmodels.test.js index 62ae85cc96..63b9a6d262 100644 --- a/test/prediction/hostedmodels.test.js +++ b/test/prediction/hostedmodels.test.js @@ -13,13 +13,15 @@ 'use strict'; -var test = require('ava'); var hostedmodels = require('../../prediction/hostedmodels'); -test.cb.serial('should predict', function (t) { - hostedmodels.main('good night', function (err, result) { - t.ifError(err); - t.truthy(result); - t.end(); +describe('prediction:hostedmodels', function () { + it('should predict', function (done) { + hostedmodels.main('good night', function (err, result) { + assert(!err); + assert(result); + assert(console.log.calledWith('Sentiment for "good night": positive')); + done(); + }); }); }); diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js index 744379f3f0..10445c7a9a 100644 --- a/test/pubsub/iam.test.js +++ b/test/pubsub/iam.test.js @@ -13,170 +13,190 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire'); -test.cb.serial('should run the sample', function (t) { - proxyquire('../../pubsub/iam', {}).main(function (err, results) { - t.ifError(err); - t.is(results.length, 8); - // Got topic and apiResponse - t.is(results[0].length, 2); - // Got policy and apiResponse - t.is(results[1].length, 2); - // Got permissions and apiResponse - t.is(results[2].length, 2); - // Got subscription and apiResponse - t.is(results[3].length, 2); - // Got policy and apiResponse - t.is(results[4].length, 2); - // Got permissions and apiResponse - t.is(results[5].length, 2); - // Got empty apiResponse - t.deepEqual(results[6], {}); - // Got empty apiResponse - t.deepEqual(results[7], {}); - t.end(); +describe('pubsub:iam', function () { + it('should run the sample', function (done) { + proxyquire('../../pubsub/iam', {}).main(function (err, results) { + assert(!err); + assert(results.length === 8); + // Got topic and apiResponse + assert(results[0].length === 2); + // Got policy and apiResponse + assert(results[1].length === 2); + // Got permissions and apiResponse + assert(results[2].length === 2); + // Got subscription and apiResponse + assert(results[3].length === 2); + // Got policy and apiResponse + assert(results[4].length === 2); + // Got permissions and apiResponse + assert(results[5].length === 2); + // Got empty apiResponse + assert.deepEqual(results[6], {}); + // Got empty apiResponse + assert.deepEqual(results[7], {}); + assert(console.log.calledWith('Created topic messageCenter2')); + assert(console.log.calledWith('Got permissions for messageCenter2')); + assert(console.log.calledWith('Subscribed to messageCenter2')); + assert(console.log.calledWith('Got permissions for newMessages2')); + assert(console.log.calledWith('Deleted subscription newMessages2')); + assert(console.log.calledWith('Deleted topic messageCenter2')); + done(); + }); }); -}); -test('getTopicPolicyExample: handles error', function (t) { - var topic = { - iam: { - getPolicy: sinon.stub().callsArgWith(0, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - topic: sinon.stub().returns(topic) - } - } - }).getTopicPolicyExample('test-topic', function (err) { - t.is(err, 'error'); + describe('getTopicPolicyExample', function () { + it('handles error', function () { + var topic = { + iam: { + getPolicy: sinon.stub().callsArgWith(0, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + topic: sinon.stub().returns(topic) + } + } + }).getTopicPolicyExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('getSubscriptionPolicyExample: handles error', function (t) { - var subscription = { - iam: { - getPolicy: sinon.stub().callsArgWith(0, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - subscription: sinon.stub().returns(subscription) - } - } - }).getSubscriptionPolicyExample('test-subscription', function (err) { - t.is(err, 'error'); + describe('getSubscriptionPolicyExample', function () { + it('handles error', function () { + var subscription = { + iam: { + getPolicy: sinon.stub().callsArgWith(0, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + subscription: sinon.stub().returns(subscription) + } + } + }).getSubscriptionPolicyExample('test-subscription', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('setTopicPolicyExample: sets topic policy', function (t) { - var policy = {}; - var apiResponse = {}; - var topic = { - iam: { - setPolicy: sinon.stub().callsArgWith(1, null, policy, apiResponse) - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - topic: sinon.stub().returns(topic) - } - } - }).setTopicPolicyExample('test-topic', function (err, policy, apiResponse) { - t.ifError(err); - }); -}); + describe('setTopicPolicyExample', function () { + it('sets topic policy', function () { + var policy = {}; + var apiResponse = {}; + var topic = { + iam: { + setPolicy: sinon.stub().callsArgWith(1, null, policy, apiResponse) + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + topic: sinon.stub().returns(topic) + } + } + }).setTopicPolicyExample('test-topic', function (err, policy, apiResponse) { + assert(!err); + assert(console.log.calledWith('Updated policy for test-topic')); + }); + }); -test('setTopicPolicyExample: handles error', function (t) { - var topic = { - iam: { - setPolicy: sinon.stub().callsArgWith(1, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - topic: sinon.stub().returns(topic) - } - } - }).setTopicPolicyExample('test-topic', function (err) { - t.is(err, 'error'); + it('handles error', function () { + var topic = { + iam: { + setPolicy: sinon.stub().callsArgWith(1, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + topic: sinon.stub().returns(topic) + } + } + }).setTopicPolicyExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('setSubscriptionPolicyExample: sets subscription policy', function (t) { - var policy = {}; - var apiResponse = {}; - var subscription = { - iam: { - setPolicy: sinon.stub().callsArgWith(1, null, policy, apiResponse) - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - subscription: sinon.stub().returns(subscription) - } - } - }).setSubscriptionPolicyExample('test-subscription', function (err, policy, apiResponse) { - t.ifError(err); - }); -}); + describe('setSubscriptionPolicyExample', function () { + it('sets subscription policy', function () { + var policy = {}; + var apiResponse = {}; + var subscription = { + iam: { + setPolicy: sinon.stub().callsArgWith(1, null, policy, apiResponse) + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + subscription: sinon.stub().returns(subscription) + } + } + }).setSubscriptionPolicyExample('test-subscription', function (err, policy, apiResponse) { + assert(!err); + assert(console.log.calledWith('Updated policy for test-subscription')); + }); + }); -test('setSubscriptionPolicyExample: handles error', function (t) { - var subscription = { - iam: { - setPolicy: sinon.stub().callsArgWith(1, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - subscription: sinon.stub().returns(subscription) - } - } - }).setSubscriptionPolicyExample('test-subscription', function (err) { - t.is(err, 'error'); + it('handles error', function () { + var subscription = { + iam: { + setPolicy: sinon.stub().callsArgWith(1, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + subscription: sinon.stub().returns(subscription) + } + } + }).setSubscriptionPolicyExample('test-subscription', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('testTopicPermissionsExample: handles error', function (t) { - var topic = { - iam: { - testPermissions: sinon.stub().callsArgWith(1, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - topic: sinon.stub().returns(topic) - } - } - }).testTopicPermissionsExample('test-topic', function (err) { - t.is(err, 'error'); + describe('testTopicPermissionsExample', function () { + it('handles error', function () { + var topic = { + iam: { + testPermissions: sinon.stub().callsArgWith(1, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + topic: sinon.stub().returns(topic) + } + } + }).testTopicPermissionsExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('testSubscriptionPermissionsExample: handles error', function (t) { - var subscription = { - iam: { - testPermissions: sinon.stub().callsArgWith(1, 'error') - } - }; - proxyquire('../../pubsub/iam', { - './subscription': { - pubsub: { - subscription: sinon.stub().returns(subscription) - } - } - }).testSubscriptionPermissionsExample('test-subscription', function (err) { - t.is(err, 'error'); + describe('testSubscriptionPermissionsExample', function () { + it('handles error', function () { + var subscription = { + iam: { + testPermissions: sinon.stub().callsArgWith(1, 'error') + } + }; + proxyquire('../../pubsub/iam', { + './subscription': { + pubsub: { + subscription: sinon.stub().returns(subscription) + } + } + }).testSubscriptionPermissionsExample('test-subscription', function (err) { + assert(err === 'error'); + }); + }); }); }); diff --git a/test/pubsub/subscription.test.js b/test/pubsub/subscription.test.js index c57b887bb9..eab1374380 100644 --- a/test/pubsub/subscription.test.js +++ b/test/pubsub/subscription.test.js @@ -13,203 +13,224 @@ 'use strict'; -var test = require('ava'); -var sinon = require('sinon'); var proxyquire = require('proxyquire'); -test.cb.serial('should run the sample', function (t) { - proxyquire('../../pubsub/subscription', {}).main(function (err, results) { - t.ifError(err); - t.is(results.length, 8); - // Got topic and apiResponse - t.is(results[0].length, 2); - // Got subscription and apiResponse - t.is(results[1].length, 2); - // Got array of topics - t.truthy(Array.isArray(results[2])); - // Got array of subscriptions - t.truthy(Array.isArray(results[3])); - // Got messageIds and apiResponse - t.is(results[4].length, 2); - // Got array of messages - t.truthy(Array.isArray(results[5])); - // Got empty apiResponse - t.deepEqual(results[6], {}); - // Got empty apiResponse - t.deepEqual(results[7], {}); - t.end(); +describe('pubsub:subscription', function () { + it('should run the sample', function (done) { + proxyquire('../../pubsub/subscription', {}).main(function (err, results) { + assert(!err); + assert(results.length === 8); + // Got topic and apiResponse + assert(results[0].length === 2); + // Got subscription and apiResponse + assert(results[1].length === 2); + // Got array of topics + assert(Array.isArray(results[2])); + // Got array of subscriptions + assert(Array.isArray(results[3])); + // Got messageIds and apiResponse + assert(results[4].length === 2); + // Got array of messages + assert(Array.isArray(results[5])); + // Got empty apiResponse + assert.deepEqual(results[6], {}); + // Got empty apiResponse + assert.deepEqual(results[7], {}); + assert(console.log.calledWith('Created topic messageCenter')); + assert(console.log.calledWith('Subscribed to messageCenter')); + assert(console.log.calledWith('Published 1 messages')); + assert(console.log.calledWith('received message: Hello, world!')); + assert(console.log.calledWith('Pulled 1 messages')); + assert(console.log.calledWith('Deleted subscription newMessages')); + assert(console.log.calledWith('Deleted topic messageCenter')); + done(); + }); }); -}); -test('createTopicExample: handles error', function (t) { - var topic = { - get: sinon.stub().callsArgWith(1, 'error') - }; - var pubsub = { - topic: sinon.stub().returns(topic) - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).createTopicExample('test-topic', function (err) { - t.is(err, 'error'); + describe('createTopicExample', function () { + it('handles error', function () { + var topic = { + get: sinon.stub().callsArgWith(1, 'error') + }; + var pubsub = { + topic: sinon.stub().returns(topic) + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).createTopicExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('deleteTopicExample: handles error', function (t) { - var topic = { - delete: sinon.stub().callsArgWith(0, 'error') - }; - var pubsub = { - topic: sinon.stub().returns(topic) - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).deleteTopicExample('test-topic', function (err) { - t.is(err, 'error'); + describe('deleteTopicExample', function () { + it('handles error', function () { + var topic = { + delete: sinon.stub().callsArgWith(0, 'error') + }; + var pubsub = { + topic: sinon.stub().returns(topic) + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).deleteTopicExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('deleteSubscriptionExample: handles error', function (t) { - var subscription = { - delete: sinon.stub().callsArgWith(0, 'error') - }; - var pubsub = { - subscription: sinon.stub().returns(subscription) - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).deleteSubscriptionExample('test-subscription', function (err) { - t.is(err, 'error'); + describe('deleteSubscriptionExample', function () { + it('handles error', function () { + var subscription = { + delete: sinon.stub().callsArgWith(0, 'error') + }; + var pubsub = { + subscription: sinon.stub().returns(subscription) + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).deleteSubscriptionExample('test-subscription', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('publishExample: handles error', function (t) { - var topic = { - publish: sinon.stub().callsArgWith(1, 'error') - }; - var pubsub = { - topic: sinon.stub().returns(topic) - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).publishExample('test-topic', function (err) { - t.is(err, 'error'); + describe('publishExample', function () { + it('handles error', function () { + var topic = { + publish: sinon.stub().callsArgWith(1, 'error') + }; + var pubsub = { + topic: sinon.stub().returns(topic) + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).publishExample('test-topic', function (err) { + assert(err === 'error'); + }); + }); }); -}); -test('getAllTopicsExample: handles error', function (t) { - var pubsub = { - getTopics: sinon.stub().callsArgWith(1, 'error') - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllTopicsExample(function (err) { - t.is(err, 'error'); - }); -}); + describe('getAllTopicsExample', function () { + it('handles error', function () { + var pubsub = { + getTopics: sinon.stub().callsArgWith(1, 'error') + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllTopicsExample(function (err) { + assert(err === 'error'); + }); + }); -test('getAllTopicsExample: recurses', function (t) { - var pubsub = { - getTopics: sinon.stub().callsArgWith(1, 'error') - }; - pubsub.getTopics.onFirstCall().callsArgWith(1, null, [], { - token: '1234' - }); - pubsub.getTopics.onSecondCall().callsArgWith(1, null, []); - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllTopicsExample(function (err, topics) { - t.ifError(err); - t.deepEqual(topics, []); - }); -}); + it('recurses', function () { + var pubsub = { + getTopics: sinon.stub().callsArgWith(1, 'error') + }; + pubsub.getTopics.onFirstCall().callsArgWith(1, null, [], { + token: '1234' + }); + pubsub.getTopics.onSecondCall().callsArgWith(1, null, []); + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllTopicsExample(function (err, topics) { + assert(!err); + assert.deepEqual(topics, []); + }); + }); -test('getAllTopicsExample: handles deep error', function (t) { - var pubsub = { - getTopics: sinon.stub().callsArgWith(1, 'error') - }; - pubsub.getTopics.onFirstCall().callsArgWith(1, null, [], { - token: '1234' + it('handles deep error', function () { + var pubsub = { + getTopics: sinon.stub().callsArgWith(1, 'error') + }; + pubsub.getTopics.onFirstCall().callsArgWith(1, null, [], { + token: '1234' + }); + pubsub.getTopics.onSecondCall().callsArgWith(1, 'error'); + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllTopicsExample(function (err) { + assert(err === 'error'); + }); + }); }); - pubsub.getTopics.onSecondCall().callsArgWith(1, 'error'); - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllTopicsExample(function (err) { - t.is(err, 'error'); - }); -}); -test('getAllSubscriptionsExample: handles error', function (t) { - var pubsub = { - getSubscriptions: sinon.stub().callsArgWith(1, 'error') - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllSubscriptionsExample(function (err) { - t.is(err, 'error'); - }); -}); + describe('getAllSubscriptionsExample', function () { + it('handles error', function () { + var pubsub = { + getSubscriptions: sinon.stub().callsArgWith(1, 'error') + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllSubscriptionsExample(function (err) { + assert(err === 'error'); + }); + }); -test('getAllSubscriptionsExample: recurses', function (t) { - var pubsub = { - getSubscriptions: sinon.stub().callsArgWith(1, 'error') - }; - pubsub.getSubscriptions.onFirstCall().callsArgWith(1, null, [], { - token: '1234' - }); - pubsub.getSubscriptions.onSecondCall().callsArgWith(1, null, []); - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllSubscriptionsExample(function (err, subscriptions) { - t.ifError(err); - t.deepEqual(subscriptions, []); - }); -}); + it('recurses', function () { + var pubsub = { + getSubscriptions: sinon.stub().callsArgWith(1, 'error') + }; + pubsub.getSubscriptions.onFirstCall().callsArgWith(1, null, [], { + token: '1234' + }); + pubsub.getSubscriptions.onSecondCall().callsArgWith(1, null, []); + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllSubscriptionsExample(function (err, subscriptions) { + assert(!err); + assert.deepEqual(subscriptions, []); + }); + }); -test('getAllSubscriptionsExample: handles deep error', function (t) { - var pubsub = { - getSubscriptions: sinon.stub().callsArgWith(1, 'error') - }; - pubsub.getSubscriptions.onFirstCall().callsArgWith(1, null, [], { - token: '1234' + it('handles deep error', function () { + var pubsub = { + getSubscriptions: sinon.stub().callsArgWith(1, 'error') + }; + pubsub.getSubscriptions.onFirstCall().callsArgWith(1, null, [], { + token: '1234' + }); + pubsub.getSubscriptions.onSecondCall().callsArgWith(1, 'error'); + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).getAllSubscriptionsExample(function (err) { + assert(err === 'error'); + }); + }); }); - pubsub.getSubscriptions.onSecondCall().callsArgWith(1, 'error'); - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).getAllSubscriptionsExample(function (err) { - t.is(err, 'error'); - }); -}); -test('subscribeExample: handles error', function (t) { - var pubsub = { - subscribe: sinon.stub().callsArgWith(3, 'error') - }; - proxyquire('../../pubsub/subscription', { - gcloud: { - pubsub: sinon.stub().returns(pubsub) - } - }).subscribeExample('test-topic', 'test-subscription', function (err) { - t.is(err, 'error'); + describe('subscribeExample', function () { + it('handles error', function () { + var pubsub = { + subscribe: sinon.stub().callsArgWith(3, 'error') + }; + proxyquire('../../pubsub/subscription', { + gcloud: { + pubsub: sinon.stub().returns(pubsub) + } + }).subscribeExample('test-topic', 'test-subscription', function (err) { + assert(err === 'error'); + }); + }); }); }); diff --git a/test/speech/recognize.test.js b/test/speech/recognize.test.js index 1d8c6e5eb2..76b10a5057 100644 --- a/test/speech/recognize.test.js +++ b/test/speech/recognize.test.js @@ -13,20 +13,24 @@ 'use strict'; -var test = require('ava'); var path = require('path'); var recognizeExample = require('../../speech/recognize'); -test.cb.serial('should list entries', function (t) { - recognizeExample.main( - path.resolve('../../speech/resources/audio.raw'), - function (err, result) { - t.ifError(err); - t.truthy(result); - t.truthy(result.responses); - t.is(result.responses.length, 1); - t.truthy(result.responses[0].results); - t.end(); - } - ); +describe('speech:recognize', function () { + it('should list entries', function (done) { + recognizeExample.main( + path.join(__dirname, '../../speech/resources/audio.raw'), + function (err, result) { + assert(!err); + assert(result); + assert(result.responses); + assert(result.responses.length === 1); + assert(result.responses[0].results); + assert(console.log.calledWith('Got audio file!')); + assert(console.log.calledWith('Loading speech service...')); + assert(console.log.calledWith('Analyzing speech...')); + done(); + } + ); + }); }); diff --git a/test/storage/authSample.test.js b/test/storage/authSample.test.js index aac6a556d6..892e9c119a 100644 --- a/test/storage/authSample.test.js +++ b/test/storage/authSample.test.js @@ -16,19 +16,20 @@ */ 'use strict'; -var test = require('ava'); var authSampleExample = require('../../storage/authSample'); var projectId = process.env.GCLOUD_PROJECT; -test.cb.serial('should return a list of buckets', function (t) { - var bucketName = process.env.TEST_BUCKET_NAME || 'nodejs-docs-samples'; +describe('storage:authSample', function () { + it('should return a list of buckets', function (done) { + var bucketName = process.env.TEST_BUCKET_NAME || 'nodejs-docs-samples'; - authSampleExample.main(projectId, function (err, response) { - t.ifError(err); - t.truthy(response.items.length > 0, 'There should be some buckets.'); - t.is(response.items.filter(function (item) { - return item.name === bucketName; - }).length, 1, 'There should be a bucket named ' + bucketName); - t.end(); + authSampleExample.main(projectId, function (err, response) { + assert(!err); + assert(response.items.length > 0, 'There should be some buckets.'); + assert(response.items.filter(function (item) { + return item.name === bucketName; + }).length === 1, 'There should be a bucket named ' + bucketName); + done(); + }); }); }); diff --git a/test/vision/faceDetection.test.js b/test/vision/faceDetection.test.js index ac3a5d928c..f464fa722b 100644 --- a/test/vision/faceDetection.test.js +++ b/test/vision/faceDetection.test.js @@ -13,7 +13,6 @@ 'use strict'; -var test = require('ava'); var fs = require('fs'); var path = require('path'); @@ -46,20 +45,25 @@ function MockCanvas () { MockCanvas.Image = function () {}; var faceDetectionExample = require('../../vision/faceDetection'); -var inputFile = path.resolve(path.join('../../vision/resources', 'face.png')); -var outputFile = path.resolve(path.join('../../vision', 'out.png')); +var inputFile = path.join(__dirname, '../../vision/resources', 'face.png'); +var outputFile = path.join(__dirname, '../../vision', 'out.png'); -test.cb.serial('should detect faces', function (t) { - faceDetectionExample.main( - inputFile, - outputFile, - MockCanvas, - function (err, faces) { - t.ifError(err); - t.is(faces.length, 1); - var image = fs.readFileSync(outputFile); - t.is(image.toString('utf8'), 'testfoobar'); - t.end(); - } - ); +describe('vision:faceDetection', function () { + it('should detect faces', function (done) { + faceDetectionExample.main( + inputFile, + outputFile, + MockCanvas, + function (err, faces) { + assert(!err); + assert(faces.length === 1); + var image = fs.readFileSync(outputFile); + assert(image.toString('utf8') === 'testfoobar'); + assert(console.log.calledWith('Found 1 face')); + assert(console.log.calledWith('Highlighting...')); + assert(console.log.calledWith('Finished!')); + done(); + } + ); + }); }); diff --git a/test/vision/labelDetection.test.js b/test/vision/labelDetection.test.js index 565f53082b..ab237b46f5 100644 --- a/test/vision/labelDetection.test.js +++ b/test/vision/labelDetection.test.js @@ -13,19 +13,20 @@ 'use strict'; -var test = require('ava'); var path = require('path'); - var labelDetectionSample = require('../../vision/labelDetection'); -var inputFile = path.resolve(path.join('../../vision/resources', 'cat.jpg')); +var inputFile = path.join(__dirname, '../../vision/resources', 'cat.jpg'); -test.cb.serial('should detect labels', function (t) { - labelDetectionSample.main( - inputFile, - function (err, labels) { - t.ifError(err); - t.truthy(labels.length > 0); - t.end(); - } - ); +describe('vision:labelDetection', function () { + it('should detect labels', function (done) { + labelDetectionSample.main( + inputFile, + function (err, labels) { + assert(!err); + assert(labels.length > 0); + assert(console.log.calledWith('Found label: cat for ' + inputFile)); + done(); + } + ); + }); }); diff --git a/test/vision/landmarkDetection.test.js b/test/vision/landmarkDetection.test.js index 1586333823..c3e91419d7 100644 --- a/test/vision/landmarkDetection.test.js +++ b/test/vision/landmarkDetection.test.js @@ -13,17 +13,16 @@ 'use strict'; -var test = require('ava'); - var landmarkDetectionSample = require('../../vision/landmarkDetection'); +var inputFile = 'https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg'; -test.cb.serial('should detect landmarks', function (t) { - landmarkDetectionSample.main( - 'https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg', - function (err, landmarks) { - t.ifError(err); - t.truthy(landmarks.length > 0); - t.end(); - } - ); +describe('vision:landmarkDetection', function () { + it('should detect landmarks', function (done) { + landmarkDetectionSample.main(inputFile, function (err, landmarks) { + assert(!err); + assert(landmarks.length > 0); + assert(console.log.calledWith('Found landmark: Taitung, Famous Places "up the water flow" marker for ' + inputFile)); + done(); + }); + }); }); diff --git a/test/vision/textDetection.test.js b/test/vision/textDetection.test.js index e3d6baa307..9d8c04a7ef 100644 --- a/test/vision/textDetection.test.js +++ b/test/vision/textDetection.test.js @@ -13,26 +13,21 @@ 'use strict'; -var test = require('ava'); var path = require('path'); -var inputDir = path.resolve('../../vision/resources'); +var inputDir = path.join(__dirname, '../../vision/resources'); var textDetectionSample = require('../../vision/textDetection'); -test.cb.serial('should detect texts', function (t) { - textDetectionSample.main( - inputDir, - function (err, textResponse) { - t.ifError(err); - t.truthy(Object.keys(textResponse).length > 0); - textDetectionSample.lookup( - ['the', 'sunbeams', 'in'], - function (err, hits) { - t.ifError(err); - t.truthy(hits.length > 0); - t.truthy(hits[0].length > 0); - t.end(); - } - ); - } - ); +describe('vision:textDetection', function () { + it('should detect texts', function (done) { + textDetectionSample.main(inputDir, function (err, textResponse) { + assert(!err); + assert(Object.keys(textResponse).length > 0); + textDetectionSample.lookup(['the', 'sunbeams', 'in'], function (err, hits) { + assert(!err); + assert(hits.length > 0); + assert(hits[0].length > 0); + done(); + }); + }); + }); }); diff --git a/vision/package.json b/vision/package.json index 9d84e921dc..d351ae9523 100644 --- a/vision/package.json +++ b/vision/package.json @@ -9,9 +9,11 @@ }, "dependencies": { "async": "^1.5.0", - "canvas": "^1.3.15", "gcloud": "^0.32.0", "natural": "^0.4.0", "redis": "^2.6.0-2" + }, + "optionalDependencies": { + "canvas": "^1.3.15" } }