Skip to content

Commit

Permalink
feat: support callbacks for exists(), getState(), getExpireTime() met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
AVaksman committed May 28, 2020
1 parent 1626274 commit 3129c6b
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -77,6 +78,11 @@ interface BackupRequest {
): void;
<T>(config: RequestConfig, callback: RequestCallback<T>): void;
}
export type GetStateCallback = NormalCallback<
EnumKey<typeof databaseAdmin.spanner.admin.database.v1.Backup.State>
>;
export type GetExpireTimeCallback = NormalCallback<PreciseDate>;
export type ExistsCallback = NormalCallback<boolean>;
/**
* The {@link Backup} class represents a Cloud Spanner backup.
*
Expand Down Expand Up @@ -185,7 +191,6 @@ class Backup {
);
}

getMetadata(): Promise<GetMetadataResponse>;
getMetadata(gaxOptions?: CallOptions): Promise<GetMetadataResponse>;
getMetadata(callback: GetMetadataCallback): void;
getMetadata(gaxOptions: CallOptions, callback: GetMetadataCallback): void;
Expand Down Expand Up @@ -248,6 +253,12 @@ class Backup {
);
}

getState(): Promise<
| EnumKey<typeof databaseAdmin.spanner.admin.database.v1.Backup.State>
| undefined
| null
>;
getState(callback: GetStateCallback): void;
/**
* Retrieves the state of the backup.
*
Expand All @@ -256,6 +267,7 @@ class Backup {
* @see {@link #getMetadata}
*
* @method Backup#getState
* @param {GetStateCallback} [callback] Callback function.
* @returns {Promise<EnumKey<typeof, databaseAdmin.spanner.admin.database.v1.Backup.State> | undefined>}
* When resolved, contains the current state of the backup if it exists.
*
Expand All @@ -276,6 +288,8 @@ class Backup {
return backupInfo.state;
}

getExpireTime(): Promise<PreciseDate | undefined>;
getExpireTime(callback: GetExpireTimeCallback): void;
/**
* Retrieves the expiry time of the backup.
*
Expand All @@ -299,6 +313,8 @@ class Backup {
return new PreciseDate(backupInfo.expireTime as DateStruct);
}

exists(): Promise<boolean>;
exists(callback: ExistsCallback): void;
/**
* Checks whether the backup exists.
*
Expand Down Expand Up @@ -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
Expand Down
123 changes: 123 additions & 0 deletions test/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3129c6b

Please sign in to comment.