diff --git a/src/backup.ts b/src/backup.ts index 949bce42f..c872746d1 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -13,13 +13,14 @@ * limitations under the License. */ -import {promisifyAll} from '@google-cloud/promisify'; +import {promisifyAll, callbackifyAll} from '@google-cloud/promisify'; import {Instance} from './instance'; import { IOperation, LongRunningCallback, RequestCallback, ResourceCallback, + NormalCallback, } from './common'; import {EnumKey, Spanner, RequestConfig, TranslateEnumKeys} from '.'; import { @@ -77,6 +78,11 @@ interface BackupRequest { ): void; (config: RequestConfig, callback: RequestCallback): void; } +export type GetStateCallback = NormalCallback< + EnumKey +>; +export type GetExpireTimeCallback = NormalCallback; +export type ExistsCallback = NormalCallback; /** * The {@link Backup} class represents a Cloud Spanner backup. * @@ -185,7 +191,6 @@ class Backup { ); } - getMetadata(): Promise; getMetadata(gaxOptions?: CallOptions): Promise; getMetadata(callback: GetMetadataCallback): void; getMetadata(gaxOptions: CallOptions, callback: GetMetadataCallback): void; @@ -248,6 +253,12 @@ class Backup { ); } + getState(): Promise< + | EnumKey + | undefined + | null + >; + getState(callback: GetStateCallback): void; /** * Retrieves the state of the backup. * @@ -256,6 +267,7 @@ class Backup { * @see {@link #getMetadata} * * @method Backup#getState + * @param {GetStateCallback} [callback] Callback function. * @returns {Promise | undefined>} * When resolved, contains the current state of the backup if it exists. * @@ -276,6 +288,8 @@ class Backup { return backupInfo.state; } + getExpireTime(): Promise; + getExpireTime(callback: GetExpireTimeCallback): void; /** * Retrieves the expiry time of the backup. * @@ -299,6 +313,8 @@ class Backup { return new PreciseDate(backupInfo.expireTime as DateStruct); } + exists(): Promise; + exists(callback: ExistsCallback): void; /** * Checks whether the backup exists. * @@ -492,6 +508,15 @@ promisifyAll(Backup, { exclude: ['getState', 'getExpireTime', 'exists'], }); +/*! Developer Documentation + * + * All async methods (except for streams) will return a Promise in the event + * that a callback is omitted. + */ +callbackifyAll(Backup, { + exclude: ['create', 'getMetadata', 'updateExpireTime', 'delete'], +}); + /** * Reference to the {@link Backup} class. * @name module:@google-cloud/spanner.Backup diff --git a/test/backup.ts b/test/backup.ts index b9a9e30f6..9c48cabcb 100644 --- a/test/backup.ts +++ b/test/backup.ts @@ -29,6 +29,7 @@ import {GetMetadataResponse} from '../src/backup'; import {grpc} from 'google-gax'; let promisified = false; +// let callbackified = false; const fakePfy = extend({}, pfy, { promisifyAll(klass, options) { if (klass.name !== 'Backup') { @@ -41,6 +42,18 @@ const fakePfy = extend({}, pfy, { 'exists', ]); }, + // callbackifyAll(klass, options) { + // if (klass.name !== 'Backup') { + // return; + // } + // callbackified = true; + // assert.deepStrictEqual(options.exclude, [ + // 'create', + // 'getMetadata', + // 'updateExpireTime', + // 'delete', + // ]); + // }, }); class FakeGrpcServiceObject extends EventEmitter { @@ -106,6 +119,10 @@ describe('Backup', () => { assert(promisified); }); + // it('should callbackify all that needs to be callbackified', () => { + // assert(callbackified); + // }); + it('should localize the request function', () => { assert.strictEqual(backup.request, INSTANCE.request); }); @@ -309,6 +326,18 @@ describe('Backup', () => { await backup.getMetadata(options, assert.ifError); }); + it('should accept gaxOpts and a callback2', async () => { + const options = { + timeout: 1000, + }; + + backup.request = config => { + assert.deepStrictEqual(config.gaxOpts, options); + }; + + await backup.getMetadata(options); + }); + it('should get backup info', done => { const INFO = { name: 'backup-name', @@ -364,6 +393,35 @@ describe('Backup', () => { assert.deepStrictEqual(thrown, err); } }); + + it('should accept callback and return state', done => { + const BACKUP_INFO_RESPONSE: GetMetadataResponse = [ + { + state: 'CREATING', + }, + ]; + backup.getMetadata = async () => BACKUP_INFO_RESPONSE; + + backup.getState((err, state) => { + if (err) { + assert.ifError(err); + } + assert.strictEqual(state, 'CREATING'); + done(); + }); + }); + + it('should accept callback and return errors', done => { + const err = {code: grpc.status.INTERNAL}; + backup.getMetadata = async () => { + throw err; + }; + + backup.getState(error => { + assert.strictEqual(error, err); + done(); + }); + }); }); describe('getExpireTime', () => { @@ -392,6 +450,32 @@ describe('Backup', () => { assert.deepStrictEqual(thrown, err); } }); + + it('should accept callback and return the expire time from backup info', done => { + const BACKUP_INFO_RESPONSE: GetMetadataResponse = [ + { + expireTime: EXP_BACKUP_EXPIRE_TIME.toStruct(), + }, + ]; + backup.getMetadata = async () => BACKUP_INFO_RESPONSE; + + backup.getExpireTime((err, result) => { + assert.deepStrictEqual(result, EXP_BACKUP_EXPIRE_TIME); + done(); + }); + }); + + it('should accept callback and return errors', done => { + const err = {code: grpc.status.INTERNAL}; + backup.getMetadata = async () => { + throw err; + }; + + backup.getExpireTime(error => { + assert.deepStrictEqual(error, err); + done(); + }); + }); }); describe('exists', () => { @@ -425,6 +509,45 @@ describe('Backup', () => { assert.deepStrictEqual(thrown, err); } }); + + it('should accept backup and return true when backup info indicates backup exists', done => { + const BACKUP_INFO_RESPONSE: GetMetadataResponse = [{}]; + backup.getMetadata = async () => BACKUP_INFO_RESPONSE; + + backup.exists((err, result) => { + if (err) { + assert.ifError(err); + } + assert.strictEqual(result, true); + done(); + }); + }); + + it('should accept callback and return false when backup does not exist', done => { + backup.getMetadata = async () => { + throw {code: grpc.status.NOT_FOUND}; + }; + + backup.exists((err, result) => { + if (err) { + assert.ifError(err); + } + assert.strictEqual(result, false); + done(); + }); + }); + + it('should accept callback and return other errors', done => { + const err = {code: grpc.status.INTERNAL}; + backup.getMetadata = async () => { + throw err; + }; + + backup.exists(error => { + assert.strictEqual(error, err); + done(); + }); + }); }); describe('updateExpireTime', () => {