From dbdbe7d5d794e2362c84f6b8b0e1736cf0ec00f9 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Tue, 20 Nov 2018 23:27:30 +0530 Subject: [PATCH 1/5] refactor(samples): convert sample tests from ava to mocha --- samples/concepts.js | 145 +++++++----------- samples/package.json | 7 +- samples/system-test/.eslintrc.yml | 3 +- samples/system-test/concepts.test.js | 198 ++++++++++--------------- samples/system-test/quickstart.test.js | 65 ++++---- samples/system-test/tasks.test.js | 47 +++--- 6 files changed, 194 insertions(+), 271 deletions(-) diff --git a/samples/concepts.js b/samples/concepts.js index cb43a0027..1b4c65e2e 100644 --- a/samples/concepts.js +++ b/samples/concepts.js @@ -21,7 +21,7 @@ const sinon = require('sinon'); // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use // the project specified by the GCLOUD_PROJECT environment variable. See // https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -const {Datastore} = require('@google-cloud/datastore'); +const Datastore = require('@google-cloud/datastore'); function makeStub() { return sinon.stub().returns(Promise.resolve([])); @@ -117,40 +117,35 @@ class Entity extends TestHelper { return task; } - testIncompleteKey(t) { - t.plan(0); + testIncompleteKey() { return this.datastore.save({ key: this.incompleteKey, data: {}, }); } - testNamedKey(t) { - t.plan(0); + testNamedKey() { return this.datastore.save({ key: this.namedKey, data: {}, }); } - testKeyWithParent(t) { - t.plan(0); + testKeyWithParent() { return this.datastore.save({ key: this.keyWithParent, data: {}, }); } - testKeyWithMultiLevelParent(t) { - t.plan(0); + testKeyWithMultiLevelParent() { return this.datastore.save({ key: this.keyWithMultiLevelParent, data: {}, }); } - testEntityWithParent(t) { - t.plan(0); + testEntityWithParent() { const taskKey = this.keyWithParent; // [START datastore_entity_with_parent] @@ -205,8 +200,7 @@ class Entity extends TestHelper { }); } - testArrayValue(t) { - t.plan(0); + testArrayValue() { // [START datastore_array_value] const task = { tags: ['fun', 'programming'], @@ -220,16 +214,14 @@ class Entity extends TestHelper { }); } - testBasicEntity(t) { - t.plan(0); + testBasicEntity() { return this.datastore.save({ key: this.getIncompleteKey(), data: this.getTask(), }); } - testUpsert(t) { - t.plan(0); + testUpsert() { const taskKey = this.getIncompleteKey(); const task = this.getTask(); @@ -250,8 +242,7 @@ class Entity extends TestHelper { }); } - testInsert(t) { - t.plan(0); + testInsert() { const taskKey = this.getIncompleteKey(); const task = this.getTask(); @@ -273,8 +264,7 @@ class Entity extends TestHelper { }); } - testLookup(t) { - t.plan(0); + testLookup() { const taskKey = this.getIncompleteKey(); // [START datastore_lookup] @@ -301,8 +291,7 @@ class Entity extends TestHelper { .then(() => this.datastore.get(taskKey)); } - testUpdate(t) { - t.plan(0); + testUpdate() { const taskKey = this.getIncompleteKey(); const task = this.getTask(); @@ -326,8 +315,7 @@ class Entity extends TestHelper { .then(() => this.datastore.update({key: taskKey, data: task})); } - testDelete(t) { - t.plan(0); + testDelete() { const taskKey = this.getIncompleteKey(); // [START datastore_delete] @@ -392,8 +380,7 @@ class Entity extends TestHelper { ]); } - testBatchLookup(t) { - t.plan(0); + testBatchLookup() { const taskKey1 = this.datastore.key(['Task', 1]); const taskKey2 = this.datastore.key(['Task', 2]); @@ -411,8 +398,7 @@ class Entity extends TestHelper { return this.datastore.get([taskKey1, taskKey2]); } - testBatchDelete(t) { - t.plan(0); + testBatchDelete() { const taskKey1 = this.datastore.key(['Task', 1]); const taskKey2 = this.datastore.key(['Task', 2]); @@ -429,8 +415,7 @@ class Entity extends TestHelper { } class Index extends TestHelper { - testUnindexedPropertyQuery(t) { - t.plan(0); + testUnindexedPropertyQuery() { const datastore = this.datastore; // [START datastore_unindexed_property_query] @@ -442,7 +427,7 @@ class Index extends TestHelper { return this.datastore.runQuery(query); } - testExplodingProperties(t) { + testExplodingProperties(assert) { const original = datastore.key; datastore.key = this.datastore.key; @@ -461,14 +446,14 @@ class Index extends TestHelper { datastore.key = original; return this.datastore.save(task).then(() => { - t.truthy(task.key); - t.truthy(task.key.id); + assert.ok(task.key); + assert.ok(task.key.id); }); } } class Metadata extends TestHelper { - testNamespaceRunQuery(t) { + testNamespaceRunQuery(assert) { const datastore = this.datastore; const startNamespace = 'Animals'; @@ -511,11 +496,11 @@ class Metadata extends TestHelper { return runNamespaceQuery(startNamespace, endNamespace); }) .then(namespaces => { - t.true(namespaces.includes('Animals')); + assert.strictEqual(namespaces.includes('Animals'), true); }); } - testKindRunQuery(t) { + testKindRunQuery(assert) { const datastore = this.datastore; // [START datastore_kind_run_query] @@ -535,11 +520,11 @@ class Metadata extends TestHelper { // [END datastore_kind_run_query] return runKindQuery().then(kinds => { - t.true(kinds.includes('Account')); + assert.strictEqual(kinds.includes('Account'), true); }); } - testPropertyRunQuery(t) { + testPropertyRunQuery(assert) { const datastore = this.datastore; // [START datastore_property_run_query] @@ -570,11 +555,11 @@ class Metadata extends TestHelper { // [END datastore_property_run_query] return runPropertyQuery().then(propertiesByKind => { - t.deepEqual(propertiesByKind.Account, ['balance']); + assert.deepStrictEqual(propertiesByKind.Account, ['balance']); }); } - testPropertyByKindRunQuery(t) { + testPropertyByKindRunQuery(assert) { const datastore = this.datastore; // [START datastore_property_by_kind_run_query] @@ -609,7 +594,7 @@ class Metadata extends TestHelper { // [END datastore_property_by_kind_run_query] return runPropertyByKindQuery().then(propertiesByKind => { - t.deepEqual(propertiesByKind, { + assert.deepStrictEqual(propertiesByKind, { balance: ['INT64'], }); }); @@ -665,8 +650,7 @@ class Query extends TestHelper { return query; } - testRunQuery(t) { - t.plan(0); + testRunQuery() { const query = this.basicQuery; // [START datastore_run_query] @@ -682,8 +666,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testPropertyFilter(t) { - t.plan(0); + testPropertyFilter() { const datastore = this.datastore; // [START datastore_property_filter] @@ -693,8 +676,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testCompositeFilter(t) { - t.plan(0); + testCompositeFilter() { const datastore = this.datastore; // [START datastore_composite_filter] @@ -707,8 +689,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testKeyFilter(t) { - t.plan(0); + testKeyFilter() { const datastore = this.datastore; // [START datastore_key_filter] @@ -720,8 +701,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testAscendingSort(t) { - t.plan(0); + testAscendingSort() { const datastore = this.datastore; // [START datastore_ascending_sort] @@ -731,8 +711,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testDescendingSort(t) { - t.plan(0); + testDescendingSort() { const datastore = this.datastore; // [START datastore_descending_sort] @@ -744,8 +723,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testMultiSort(t) { - t.plan(0); + testMultiSort() { const datastore = this.datastore; // [START datastore_multi_sort] @@ -760,8 +738,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testKindlessQuery(t) { - t.plan(0); + testKindlessQuery() { const datastore = this.datastore; const lastSeenKey = this.datastore.key(['Task', Date.now()]); @@ -803,8 +780,7 @@ class Query extends TestHelper { return runProjectionQuery(); } - testKeysOnlyQuery(t) { - t.plan(0); + testKeysOnlyQuery() { const datastore = this.datastore; // [START datastore_keys_only_query] @@ -817,8 +793,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testDistinctOnQuery(t) { - t.plan(0); + testDistinctOnQuery() { const datastore = this.datastore; // [START datastore_distinct_on_query] @@ -832,8 +807,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testArrayValueInequalityRange(t) { - t.plan(0); + testArrayValueInequalityRange() { const datastore = this.datastore; // [START datastore_array_value_inequality_range] @@ -846,8 +820,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testArrayValueEquality(t) { - t.plan(0); + testArrayValueEquality() { const datastore = this.datastore; // [START datastore_array_value_equality] @@ -860,8 +833,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testInequalityRange(t) { - t.plan(0); + testInequalityRange() { const datastore = this.datastore; // [START datastore_inequality_range] @@ -887,8 +859,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testEqualAndInequalityRange(t) { - t.plan(0); + testEqualAndInequalityRange() { const datastore = this.datastore; // [START datastore_equal_and_inequality_range] @@ -903,8 +874,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testInequalitySort(t) { - t.plan(0); + testInequalitySort() { const datastore = this.datastore; // [START datastore_inequality_sort] @@ -945,8 +915,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testLimit(t) { - t.plan(0); + testLimit() { const datastore = this.datastore; // [START datastore_limit] @@ -956,7 +925,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testCursorPaging(t) { + testCursorPaging(assert) { const datastore = this.datastore; const pageSize = 1; @@ -993,7 +962,7 @@ class Query extends TestHelper { return runPageQuery().then(results => { const entities = results[0]; - t.true(Array.isArray(entities)); + assert.strictEqual(Array.isArray(entities), true); const info = results[1]; if (!info || !info.endCursor) { throw new Error('An `info` with an `endCursor` is not present.'); @@ -1001,8 +970,7 @@ class Query extends TestHelper { }); } - testEventualConsistentQuery(t) { - t.plan(0); + testEventualConsistentQuery() { const datastoreMock = datastore; datastore = this.datastore; // [START datastore_eventual_consistent_query] @@ -1078,7 +1046,7 @@ class Transaction extends TestHelper { return this.datastore.save(entities); } - testTransactionalUpdate(t) { + testTransactionalUpdate(assert) { const fromKey = this.fromKey; const toKey = this.toKey; const originalBalance = this.originalBalance; @@ -1100,8 +1068,8 @@ class Transaction extends TestHelper { const accounts = results.map(result => result[0]); // Restore `datastore` to the mock API. datastore = datastoreMock; - t.is(accounts[0].balance, originalBalance - amountToTransfer); - t.is(accounts[1].balance, originalBalance + amountToTransfer); + assert.strictEqual(accounts[0].balance, originalBalance - amountToTransfer); + assert.strictEqual(accounts[1].balance, originalBalance + amountToTransfer); }) .catch(err => { // Restore `datastore` to the mock API. @@ -1110,8 +1078,7 @@ class Transaction extends TestHelper { }); } - testTransactionalRetry(t) { - t.plan(0); + testTransactionalRetry() { // Overwrite so the real Datastore instance is used in `transferFunds`. const datastoreMock = datastore; datastore = this.datastore; @@ -1161,7 +1128,7 @@ class Transaction extends TestHelper { }); } - testTransactionalGetOrCreate(t) { + testTransactionalGetOrCreate(assert) { const taskKey = this.datastore.key(['Task', Date.now()]); // Overwrite so the real Datastore instance is used in `transferFunds`. @@ -1198,11 +1165,11 @@ class Transaction extends TestHelper { return getOrCreate(taskKey, {}) .then(task => { - t.truthy(task, 'Should have a task.'); + assert.ok(task, 'Should have a task.'); return getOrCreate(taskKey, {}); }) .then(task => { - t.truthy(task, 'Should have a task.'); + assert.ok(task, 'Should have a task.'); // Restore `datastore` to the mock API. datastore = datastoreMock; }) @@ -1213,7 +1180,7 @@ class Transaction extends TestHelper { }); } - testSingleEntityGroupReadOnly(t) { + testSingleEntityGroupReadOnly(assert) { // Overwrite so the real Datastore instance is used in `transferFunds`. const datastoreMock = datastore; datastore = this.datastore; @@ -1246,8 +1213,8 @@ class Transaction extends TestHelper { results => { // Restore `datastore` to the mock API. datastore = datastoreMock; - t.is(results.length, 2); - t.true(Array.isArray(results[1])); + assert.strictEqual(results.length, 2); + assert.strictEqual(Array.isArray(results[1]), true); }, err => { // Restore `datastore` to the mock API. diff --git a/samples/package.json b/samples/package.json index 3df4cace8..8debb5594 100644 --- a/samples/package.json +++ b/samples/package.json @@ -9,9 +9,7 @@ "node": ">=8" }, "scripts": { - "test": "npm run cover", - "ava": "ava -T 20s --verbose test/*.test.js system-test/*.test.js", - "cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js system-test/*.test.js && nyc report" + "test": "mocha system-test/*.test.js --timeout=600000" }, "dependencies": { "@google-cloud/datastore": "^2.0.0", @@ -20,8 +18,7 @@ }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "^0.25.0", - "nyc": "^13.0.0", + "mocha": "^5.2.0", "proxyquire": "^2.0.1" } } diff --git a/samples/system-test/.eslintrc.yml b/samples/system-test/.eslintrc.yml index c0289282a..0ab526f52 100644 --- a/samples/system-test/.eslintrc.yml +++ b/samples/system-test/.eslintrc.yml @@ -1,5 +1,6 @@ --- +env: + mocha: true rules: node/no-unpublished-require: off - node/no-unsupported-features: off no-empty: off diff --git a/samples/system-test/concepts.test.js b/samples/system-test/concepts.test.js index a2a4aa12f..eb945bcf3 100644 --- a/samples/system-test/concepts.test.js +++ b/samples/system-test/concepts.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2018, 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 @@ -15,9 +15,9 @@ 'use strict'; -const concepts = require(`../concepts`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const concepts = require('../concepts'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); let transaction; let metadata; @@ -31,10 +31,10 @@ const Index = concepts.Index; const Entity = concepts.Entity; const Query = concepts.Query; -test.before(tools.checkCredentials); -test.before(t => { +before(() => { + tools.checkCredentials(); const projectId = process.env.GCLOUD_PROJECT; - t.truthy(projectId, `You must set the GCLOUD_PROJECT env var!`); + assert.ok(projectId, 'You must set the GCLOUD_PROJECT env var!'); transaction = new Transaction(projectId); metadata = new Metadata(projectId); index = new Index(projectId); @@ -42,150 +42,114 @@ test.before(t => { query = new Query(projectId); }); -test.after.always(async () => { +after(async () => { const datastore = transaction.datastore; - const query = datastore.createQuery(`Task`).select(`__key__`); + const query = datastore.createQuery('Task').select('__key__'); const [entities] = await datastore.runQuery(query); await datastore.delete(entities.map(entity => entity[datastore.KEY])); }); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); // Transactions -test.serial(`performs a transactional update`, t => - transaction.testTransactionalUpdate(t) -); -test.serial(`performs retries if necessary`, t => - transaction.testTransactionalRetry(t) -); -test.serial(`performs a get or create`, t => - transaction.testTransactionalGetOrCreate(t) -); -test.serial(`gets a snapshot of task list entities`, t => - transaction.testSingleEntityGroupReadOnly(t) -); +it('performs a transactional update', () => + transaction.testTransactionalUpdate(assert)); +it('performs retries if necessary', () => transaction.testTransactionalRetry()); +it('performs a get or create', () => + transaction.testTransactionalGetOrCreate(assert)); +it('gets a snapshot of task list entities', () => + transaction.testSingleEntityGroupReadOnly(assert)); // Metadata -test.serial(`performs a namespace query`, t => - metadata.testNamespaceRunQuery(t) -); -test.serial(`performs a kind query`, t => metadata.testKindRunQuery(t)); -test.serial(`performs a property query`, t => metadata.testPropertyRunQuery(t)); -test.serial(`performs a property by kind query`, t => - metadata.testPropertyByKindRunQuery(t) -); +it('performs a namespace query', () => metadata.testNamespaceRunQuery(assert)); +it('performs a kind query', () => metadata.testKindRunQuery(assert)); +it('performs a property query', () => metadata.testPropertyRunQuery(assert)); +it('performs a property by kind query', () => + metadata.testPropertyByKindRunQuery(assert)); // Indexes -test.serial(`performs a query with a filter on an unindexed property`, t => - index.testUnindexedPropertyQuery(t) -); -test.serial(`inserts arrays of data`, t => index.testExplodingProperties(t)); +it('performs a query with a filter on an unindexed property', () => + index.testUnindexedPropertyQuery()); +it('inserts arrays of data', () => index.testExplodingProperties(assert)); // Queries -test.serial(`performs a basic query`, t => query.testRunQuery(t)); -test.serial(`performs a query with a property filter`, t => - query.testPropertyFilter(t) -); -test.serial(`performs a query with a composite filter`, t => - query.testCompositeFilter(t) -); -test.serial(`performs a query with a key filter`, t => query.testKeyFilter(t)); -test.serial(`performs a query with ascending sort`, t => - query.testAscendingSort(t) -); -test.serial(`performs a query with descending sort`, t => - query.testDescendingSort(t) -); -test.serial(`performs a query with multi sort`, t => query.testMultiSort(t)); -test.serial(`performs a kindless query`, t => query.testKindlessQuery(t)); -test.serial('performs a projection query', t => { +it('performs a basic query', async () => await query.testRunQuery()); +it('performs a query with a property filter', () => query.testPropertyFilter()); +it('performs a query with a composite filter', () => + query.testCompositeFilter()); +it('performs a query with a key filter', () => query.testKeyFilter()); +it('performs a query with ascending sort', () => query.testAscendingSort()); +it('performs a query with descending sort', () => query.testDescendingSort()); +it('performs a query with multi sort', () => query.testMultiSort()); +it('performs a kindless query', () => query.testKindlessQuery()); +it('performs a projection query', () => { return entity - .testProperties(t) + .testProperties() .then(() => { return new Promise((resolve, reject) => { setTimeout(() => { - query.testRunQueryProjection(t).then(resolve, reject); + query.testRunQueryProjection().then(resolve, reject); }, 1000); }); }) .then(results => { - t.deepEqual(results, { + assert.deepStrictEqual(results, { priorities: [4], percentCompletes: [10], }); }); }); -test.serial(`performs a keys only query`, t => query.testKeysOnlyQuery(t)); -test.serial(`performs a distinct on query`, t => query.testDistinctOnQuery(t)); -test.serial(`performs an array value inequality query`, t => - query.testArrayValueInequalityRange(t) -); -test.serial(`performs an array value equality query`, t => - query.testArrayValueEquality(t) -); -test.serial(`performs an inequality range query`, t => - query.testInequalityRange(t) -); -test.serial(`returns an error from an invalid query`, async t => { - await t.throws(query.testInequalityInvalid(t)); +it('performs a keys only query', () => query.testKeysOnlyQuery()); +it('performs a distinct on query', () => query.testDistinctOnQuery()); +it('performs an array value inequality query', () => + query.testArrayValueInequalityRange()); +it('performs an array value equality query', () => + query.testArrayValueEquality()); +it('performs an inequality range query', () => query.testInequalityRange()); +it('returns an error from an invalid query', async () => { + await assert.throws(query.testInequalityInvalid()); }); -test.serial(`performs an equal and inequality range query`, t => - query.testEqualAndInequalityRange(t) -); -test.serial(`performs an equality sort query`, t => - query.testInequalitySort(t) -); -test.serial( - `returns an error when not sorted on filtered property`, - async t => { - await t.throws(query.testInequalitySortInvalidNotSame(t)); - } -); -test.serial( - `returns an error when not sorted on first filter prop`, - async t => { - await t.throws(query.testInequalitySortInvalidNotFirst(t)); - } -); -test.serial(`performs a query with a limit`, t => query.testLimit(t)); -test.serial(`allows manual pagination through results`, t => { - return entity.testBatchUpsert(t).then(() => { +it('performs an equal and inequality range query', () => + query.testEqualAndInequalityRange()); +it('performs an equality sort query', () => query.testInequalitySort()); +it('returns an error when not sorted on filtered property', async () => { + await assert.throws(query.testInequalitySortInvalidNotSame()); +}); +it('returns an error when not sorted on first filter prop', async () => { + await assert.throws(query.testInequalitySortInvalidNotFirst()); +}); +it('performs a query with a limit', () => query.testLimit()); +it('allows manual pagination through results', () => { + return entity.testBatchUpsert().then(() => { return new Promise((resolve, reject) => { setTimeout(() => { - query.testCursorPaging(t).then(resolve, reject); + query.testCursorPaging(assert).then(resolve, reject); }, 1000); }); }); }); -test.serial(`performs an ancestor query`, t => - query.testEventualConsistentQuery(t) -); +it('performs an ancestor query', () => query.testEventualConsistentQuery()); // Entities -test.serial(`saves with an incomplete key`, t => entity.testIncompleteKey(t)); -test.serial(`saves with a named key`, t => entity.testNamedKey(t)); -test.serial(`saves a key with a parent`, t => entity.testKeyWithParent(t)); -test.serial(`saves a key with multiple parents`, t => - entity.testKeyWithMultiLevelParent(t) -); -test.serial(`saves an entity with a parent`, t => - entity.testEntityWithParent(t) -); -test.serial(`saves an entity with properties`, t => { - t.plan(0); - return entity.testProperties(t); +it('saves with an incomplete key', () => entity.testIncompleteKey()); +it('saves with a named key', () => entity.testNamedKey()); +it('saves a key with a parent', () => entity.testKeyWithParent()); +it('saves a key with multiple parents', () => + entity.testKeyWithMultiLevelParent()); +it('saves an entity with a parent', () => entity.testEntityWithParent()); +it('saves an entity with properties', () => { + return entity.testProperties(); }); -test.serial(`saves an entity with arrays`, t => entity.testArrayValue(t)); -test.serial(`saves a basic entity`, t => entity.testBasicEntity(t)); -test.serial(`saves with an upsert`, t => entity.testUpsert(t)); -test.serial(`saves with an insert`, t => entity.testInsert(t)); -test.serial(`performs a lookup`, t => entity.testLookup(t)); -test.serial(`saves with an update`, t => entity.testUpdate(t)); -test.serial(`deletes an entity`, t => entity.testDelete(t)); -test.serial(`performs a batch upsert`, t => { - t.plan(0); - return entity.testBatchUpsert(t); +it('saves an entity with arrays', () => entity.testArrayValue()); +it('saves a basic entity', () => entity.testBasicEntity()); +it('saves with an upsert', () => entity.testUpsert()); +it('saves with an insert', () => entity.testInsert()); +it('performs a lookup', () => entity.testLookup()); +it('saves with an update', () => entity.testUpdate()); +it('deletes an entity', () => entity.testDelete()); +it('performs a batch upsert', () => { + return entity.testBatchUpsert(); }); -test.serial(`performs a batch lookup`, t => entity.testBatchLookup(t)); -test.serial(`performs a batch delete`, t => entity.testBatchDelete(t)); +it('performs a batch lookup', () => entity.testBatchLookup()); +it('performs a batch delete', () => entity.testBatchDelete()); diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index d579cda66..82c45346a 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2018, 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 @@ -15,62 +15,61 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noPreserveCache(); -const sinon = require(`sinon`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const proxyquire = require('proxyquire').noPreserveCache(); +const sinon = require('sinon'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const {Datastore} = proxyquire(`@google-cloud/datastore`, {}); +const Datastore = proxyquire('@google-cloud/datastore', {}); const datastore = new Datastore(); -const entity = {description: `Buy milk`}; -const kind = `Task`; -const name = `sampletask1`; +const entity = {description: 'Buy milk'}; +const kind = 'Task'; +const name = 'sampletask1'; const key = datastore.key([kind, name]); const datastoreEntity = Object.assign({}, entity); datastoreEntity[datastore.KEY] = key; -test.before(async () => { +before(async () => { try { await datastore.delete(key); } catch (err) {} // ignore error }); -test.after.always(async () => { +after(async () => { try { await datastore.delete(key); } catch (err) {} // ignore error }); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); -test.cb(`should get a task from Datastore`, t => { +it('should get a task from Datastore', () => { const datastoreMock = { key: (...args) => datastore.key(...args), - save: _task => { - t.is(_task.key.kind, kind); - t.is(_task.key.name, name); - t.deepEqual(_task.data, entity); + save: async _task => { + assert.strictEqual(_task.key.kind, kind); + assert.strictEqual(_task.key.name, name); + assert.deepStrictEqual(_task.data, entity); - return datastore.save(_task).then(() => { - setTimeout(() => { - datastore - .get(key) - .then(([task]) => { - t.deepEqual(task, datastoreEntity); - t.true( - console.log.calledWith(`Saved ${name}: ${entity.description}`) - ); - t.end(); - }) - .catch(t.end); - }, 200); - }, t.end); + return await datastore.save(_task).then(async () => { + await new Promise(r => setTimeout(r, 200)); + await datastore + .get(key) + .then(([task]) => { + assert.deepStrictEqual(task, datastoreEntity); + assert.strictEqual( + console.log.calledWith(`Saved ${name}: ${entity.description}`), + true + ); + }) + .catch(); + }); }, }; - proxyquire(`../quickstart`, { + proxyquire('../quickstart', { '@google-cloud/datastore': { Datastore: sinon.stub().returns(datastoreMock), }, diff --git a/samples/system-test/tasks.test.js b/samples/system-test/tasks.test.js index 953f02c02..21376dd36 100644 --- a/samples/system-test/tasks.test.js +++ b/samples/system-test/tasks.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2018, 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 @@ -15,64 +15,59 @@ 'use strict'; -const {Datastore} = require('@google-cloud/datastore'); +const Datastore = require('@google-cloud/datastore'); const datastore = new Datastore(); const path = require('path'); -const test = require('ava'); +const assert = require('assert'); const tools = require('@google-cloud/nodejs-repo-tools'); -const cmd = `node tasks.js`; -const cwd = path.join(__dirname, `..`); +const cmd = 'node tasks.js'; +const cwd = path.join(__dirname, '..'); -const description = `description`; +const description = 'description'; let key; -test.after.always(async () => { +after(async () => { try { await datastore.delete(key); } catch (err) {} // ignore error }); -test.before(tools.checkCredentials); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +before(tools.checkCredentials); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); -test.serial(`should add a task`, async t => { - t.plan(2); +it('should add a task', async () => { const expected = /^Task (\d+) created successfully.$/; const parts = tools.run(`${cmd} new "${description}"`, cwd).match(expected); - t.true(expected.test(parts[0])); + assert.strictEqual(expected.test(parts[0]), true); const [task] = await datastore.get( datastore.key([`Task`, parseInt(parts[1], 10)]) ); key = task[datastore.KEY]; - t.is(task.description, description); + assert.strictEqual(task.description, description); }); -test.serial(`should mark a task as done`, async t => { - t.plan(2); +it('should mark a task as done', async () => { const expected = `Task ${key.id} updated successfully.`; const output = await tools.runAsync(`${cmd} done ${key.id}`, cwd); - t.is(output, expected); + assert.strictEqual(output, expected); const [task] = await datastore.get(key); - t.true(task.done); + assert.strictEqual(task.done, true); }); -test.serial(`should list tasks`, async t => { - t.plan(0); +it('should list tasks', async () => await tools .tryTest(async assert => { const output = await tools.runAsync(`${cmd} list`, cwd); assert(output.includes(key.id)); }) - .start(); -}); + .start()); -test.serial(`should delete a task`, async t => { - t.plan(2); +it('should delete a task', async () => { const expected = `Task ${key.id} deleted successfully.`; const output = await tools.runAsync(`${cmd} delete ${key.id}`, cwd); - t.is(output, expected); + assert.strictEqual(output, expected); const [task] = await datastore.get(key); - t.is(task, undefined); + assert.strictEqual(task, undefined); }); From 6dee8696b64bc1b702d56c4511a921ab091542e8 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Wed, 21 Nov 2018 18:26:21 +0530 Subject: [PATCH 2/5] Refactor the code --- samples/concepts.js | 19 ++++++++++--------- samples/system-test/concepts.test.js | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/samples/concepts.js b/samples/concepts.js index 1b4c65e2e..e650fbfac 100644 --- a/samples/concepts.js +++ b/samples/concepts.js @@ -16,6 +16,7 @@ 'use strict'; const sinon = require('sinon'); +const assert = require('assert'); // By default, the client will authenticate using the service account file // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use @@ -427,7 +428,7 @@ class Index extends TestHelper { return this.datastore.runQuery(query); } - testExplodingProperties(assert) { + testExplodingProperties() { const original = datastore.key; datastore.key = this.datastore.key; @@ -453,7 +454,7 @@ class Index extends TestHelper { } class Metadata extends TestHelper { - testNamespaceRunQuery(assert) { + testNamespaceRunQuery() { const datastore = this.datastore; const startNamespace = 'Animals'; @@ -500,7 +501,7 @@ class Metadata extends TestHelper { }); } - testKindRunQuery(assert) { + testKindRunQuery() { const datastore = this.datastore; // [START datastore_kind_run_query] @@ -524,7 +525,7 @@ class Metadata extends TestHelper { }); } - testPropertyRunQuery(assert) { + testPropertyRunQuery() { const datastore = this.datastore; // [START datastore_property_run_query] @@ -559,7 +560,7 @@ class Metadata extends TestHelper { }); } - testPropertyByKindRunQuery(assert) { + testPropertyByKindRunQuery() { const datastore = this.datastore; // [START datastore_property_by_kind_run_query] @@ -925,7 +926,7 @@ class Query extends TestHelper { return this.datastore.runQuery(query); } - testCursorPaging(assert) { + testCursorPaging() { const datastore = this.datastore; const pageSize = 1; @@ -1046,7 +1047,7 @@ class Transaction extends TestHelper { return this.datastore.save(entities); } - testTransactionalUpdate(assert) { + testTransactionalUpdate() { const fromKey = this.fromKey; const toKey = this.toKey; const originalBalance = this.originalBalance; @@ -1128,7 +1129,7 @@ class Transaction extends TestHelper { }); } - testTransactionalGetOrCreate(assert) { + testTransactionalGetOrCreate() { const taskKey = this.datastore.key(['Task', Date.now()]); // Overwrite so the real Datastore instance is used in `transferFunds`. @@ -1180,7 +1181,7 @@ class Transaction extends TestHelper { }); } - testSingleEntityGroupReadOnly(assert) { + testSingleEntityGroupReadOnly() { // Overwrite so the real Datastore instance is used in `transferFunds`. const datastoreMock = datastore; datastore = this.datastore; diff --git a/samples/system-test/concepts.test.js b/samples/system-test/concepts.test.js index eb945bcf3..45ced3613 100644 --- a/samples/system-test/concepts.test.js +++ b/samples/system-test/concepts.test.js @@ -54,24 +54,24 @@ afterEach(tools.restoreConsole); // Transactions it('performs a transactional update', () => - transaction.testTransactionalUpdate(assert)); + transaction.testTransactionalUpdate()); it('performs retries if necessary', () => transaction.testTransactionalRetry()); it('performs a get or create', () => - transaction.testTransactionalGetOrCreate(assert)); + transaction.testTransactionalGetOrCreate()); it('gets a snapshot of task list entities', () => - transaction.testSingleEntityGroupReadOnly(assert)); + transaction.testSingleEntityGroupReadOnly()); // Metadata -it('performs a namespace query', () => metadata.testNamespaceRunQuery(assert)); -it('performs a kind query', () => metadata.testKindRunQuery(assert)); -it('performs a property query', () => metadata.testPropertyRunQuery(assert)); +it('performs a namespace query', () => metadata.testNamespaceRunQuery()); +it('performs a kind query', () => metadata.testKindRunQuery()); +it('performs a property query', () => metadata.testPropertyRunQuery()); it('performs a property by kind query', () => - metadata.testPropertyByKindRunQuery(assert)); + metadata.testPropertyByKindRunQuery()); // Indexes it('performs a query with a filter on an unindexed property', () => index.testUnindexedPropertyQuery()); -it('inserts arrays of data', () => index.testExplodingProperties(assert)); +it('inserts arrays of data', () => index.testExplodingProperties()); // Queries it('performs a basic query', async () => await query.testRunQuery()); @@ -108,23 +108,23 @@ it('performs an array value equality query', () => query.testArrayValueEquality()); it('performs an inequality range query', () => query.testInequalityRange()); it('returns an error from an invalid query', async () => { - await assert.throws(query.testInequalityInvalid()); + await assert.throws(query.testInequalityInvalid); }); it('performs an equal and inequality range query', () => query.testEqualAndInequalityRange()); it('performs an equality sort query', () => query.testInequalitySort()); it('returns an error when not sorted on filtered property', async () => { - await assert.throws(query.testInequalitySortInvalidNotSame()); + await assert.throws(query.testInequalitySortInvalidNotSame); }); it('returns an error when not sorted on first filter prop', async () => { - await assert.throws(query.testInequalitySortInvalidNotFirst()); + await assert.throws(query.testInequalitySortInvalidNotFirst); }); it('performs a query with a limit', () => query.testLimit()); it('allows manual pagination through results', () => { return entity.testBatchUpsert().then(() => { return new Promise((resolve, reject) => { setTimeout(() => { - query.testCursorPaging(assert).then(resolve, reject); + query.testCursorPaging().then(resolve, reject); }, 1000); }); }); From 891e731d7d8af4535446e814d7e795d296e7a5fa Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Wed, 21 Nov 2018 18:44:11 +0530 Subject: [PATCH 3/5] lint error resolved --- samples/concepts.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/samples/concepts.js b/samples/concepts.js index e650fbfac..ca2a208b0 100644 --- a/samples/concepts.js +++ b/samples/concepts.js @@ -1069,8 +1069,14 @@ class Transaction extends TestHelper { const accounts = results.map(result => result[0]); // Restore `datastore` to the mock API. datastore = datastoreMock; - assert.strictEqual(accounts[0].balance, originalBalance - amountToTransfer); - assert.strictEqual(accounts[1].balance, originalBalance + amountToTransfer); + assert.strictEqual( + accounts[0].balance, + originalBalance - amountToTransfer + ); + assert.strictEqual( + accounts[1].balance, + originalBalance + amountToTransfer + ); }) .catch(err => { // Restore `datastore` to the mock API. From 77503dc2710f310692d2087f8e4411807746efed Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Thu, 22 Nov 2018 20:56:33 +0530 Subject: [PATCH 4/5] Refactor the code --- samples/tasks.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/tasks.js b/samples/tasks.js index 9cfa4f955..5f97d5955 100644 --- a/samples/tasks.js +++ b/samples/tasks.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2018, 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 @@ -20,7 +20,7 @@ // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use // the project specified by the GCLOUD_PROJECT environment variable. See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/latest/guides/authentication -const {Datastore} = require('@google-cloud/datastore'); +const Datastore = require('@google-cloud/datastore'); // Creates a client const datastore = new Datastore(); From a9a010906c675442f2e438d79f79ad1384d32bf4 Mon Sep 17 00:00:00 2001 From: Naresh Kumar Date: Fri, 23 Nov 2018 13:27:03 +0530 Subject: [PATCH 5/5] Refactor the code --- samples/concepts.js | 2 +- samples/system-test/quickstart.test.js | 2 +- samples/system-test/tasks.test.js | 2 +- samples/tasks.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/concepts.js b/samples/concepts.js index ca2a208b0..ec2f9f932 100644 --- a/samples/concepts.js +++ b/samples/concepts.js @@ -22,7 +22,7 @@ const assert = require('assert'); // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use // the project specified by the GCLOUD_PROJECT environment variable. See // https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -const Datastore = require('@google-cloud/datastore'); +const {Datastore} = require('@google-cloud/datastore'); function makeStub() { return sinon.stub().returns(Promise.resolve([])); diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 82c45346a..80f85ed04 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -20,7 +20,7 @@ const sinon = require('sinon'); const assert = require('assert'); const tools = require('@google-cloud/nodejs-repo-tools'); -const Datastore = proxyquire('@google-cloud/datastore', {}); +const {Datastore} = proxyquire('@google-cloud/datastore', {}); const datastore = new Datastore(); const entity = {description: 'Buy milk'}; diff --git a/samples/system-test/tasks.test.js b/samples/system-test/tasks.test.js index 21376dd36..400234b0e 100644 --- a/samples/system-test/tasks.test.js +++ b/samples/system-test/tasks.test.js @@ -15,7 +15,7 @@ 'use strict'; -const Datastore = require('@google-cloud/datastore'); +const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const path = require('path'); const assert = require('assert'); diff --git a/samples/tasks.js b/samples/tasks.js index 5f97d5955..7fa3449e6 100644 --- a/samples/tasks.js +++ b/samples/tasks.js @@ -20,7 +20,7 @@ // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use // the project specified by the GCLOUD_PROJECT environment variable. See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/latest/guides/authentication -const Datastore = require('@google-cloud/datastore'); +const {Datastore} = require('@google-cloud/datastore'); // Creates a client const datastore = new Datastore();