Skip to content

Commit

Permalink
refactor: clean up types for tests (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 10, 2019
1 parent 9b39431 commit 8a99d56
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 111 deletions.
14 changes: 11 additions & 3 deletions src/index.ts
Expand Up @@ -26,7 +26,7 @@

import * as arrify from 'arrify';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import {GrpcClient, GrpcClientOptions} from 'google-gax';
import {GrpcClient} from 'google-gax';
import {ChannelCredentials} from 'grpc';
import * as is from 'is';

Expand Down Expand Up @@ -631,9 +631,15 @@ class Datastore extends DatastoreRequest {
* const datastore = new Datastore();
* const query = datastore.createQuery('Company');
*/
createQuery(namespace: string, kind?: string) {
createQuery(kind?: string): Query;
createQuery(kind?: string[]): Query;
createQuery(namespace: string, kind: string): Query;
createQuery(namespace: string, kind: string[]): Query;
createQuery(namespaceOrKind?: string|string[], kind?: string|string[]):
Query {
let namespace = namespaceOrKind as string;
if (arguments.length < 2) {
kind = namespace;
kind = namespaceOrKind;
namespace = this.namespace!;
}
return new Query(this, namespace, arrify(kind));
Expand Down Expand Up @@ -851,6 +857,8 @@ export interface TransactionOptions {
readOnly?: boolean;
}

export {DatastoreRequest, Query, Transaction};

export interface DatastoreOptions extends GoogleAuthOptions {
namespace?: string;
apiEndpoint?: string;
Expand Down
6 changes: 3 additions & 3 deletions src/query.ts
Expand Up @@ -70,11 +70,11 @@ class Query {
limitVal: number;
offsetVal: number;

constructor(scope?: Datastore|Transaction, kinds?: string[]);
constructor(scope?: Datastore|Transaction, kinds?: string[]|null);
constructor(
scope?: Datastore|Transaction, namespace?: string, kinds?: string[]);
scope?: Datastore|Transaction, namespace?: string|null, kinds?: string[]);
constructor(
scope?: Datastore|Transaction, namespaceOrKinds?: string|string[],
scope?: Datastore|Transaction, namespaceOrKinds?: string|string[]|null,
kinds?: string[]) {
let namespace = namespaceOrKinds as string | null;
if (!kinds) {
Expand Down
10 changes: 5 additions & 5 deletions src/request.ts
Expand Up @@ -25,7 +25,7 @@ import * as streamEvents from 'stream-events';
import * as through from 'through2';
import {google} from '../proto/datastore';
import {CallOptions} from 'google-gax';
import {Stream} from 'stream';
import {Transform} from 'stream';

// Import the clients for each version supported by this package.
const gapic = Object.freeze({
Expand Down Expand Up @@ -232,7 +232,7 @@ class DatastoreRequest {
* });
*/
createReadStream(keys: Entities, options: CreateReadStreamOptions = {}):
Stream {
Transform {
keys = arrify(keys).map(entity.keyToKeyProto);
if (keys.length === 0) {
throw new Error('At least one Key object is required.');
Expand Down Expand Up @@ -467,12 +467,12 @@ class DatastoreRequest {
* });
*/
get(keys: Entities,
options?: CreateReadStreamOptions): Promise<Entity|Stream>;
options?: CreateReadStreamOptions): Promise<Entity|Transform>;
get(keys: Entities, callback: GetCallback): void;
get(keys: Entities, options: CreateReadStreamOptions,
callback: GetCallback): void;
get(keys: Entities, optionsOrCallback?: CreateReadStreamOptions|GetCallback,
cb?: GetCallback): void|Promise<Entity|Stream> {
cb?: GetCallback): void|Promise<Entity|Transform> {
const options =
typeof optionsOrCallback === 'object' && optionsOrCallback !== null ?
optionsOrCallback :
Expand Down Expand Up @@ -663,7 +663,7 @@ class DatastoreRequest {
* this.end();
* });
*/
runQueryStream(query: Query, options: RunQueryStreamOptions = {}): Stream {
runQueryStream(query: Query, options: RunQueryStreamOptions = {}): Transform {
query = extend(true, new Query(), query);

const makeRequest = (query: Query) => {
Expand Down
4 changes: 2 additions & 2 deletions src/transaction.ts
Expand Up @@ -296,8 +296,8 @@ class Transaction extends DatastoreRequest {
* });
* });
*/
createQuery(namespace: string, kind?: string): Query {
return this.datastore.createQuery.call(this, namespace, kind);
createQuery(namespace: string, kind?: string|string[]): Query {
return this.datastore.createQuery.call(this, namespace, kind as string[]);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions system-test/datastore.ts
Expand Up @@ -24,14 +24,15 @@ describe('Datastore', () => {
// Override the Key method so we can track what keys are created during the
// tests. They are then deleted in the `after` hook.
const key = datastore.key;
datastore.key = function(options) {
// tslint:disable-next-line no-any
datastore.key = function(options: any) {
const keyObject = key.call(this, options);
testKinds.push(keyObject.kind);
return keyObject;
};

after(async () => {
async function deleteEntities(kind) {
async function deleteEntities(kind: string) {
const query = datastore.createQuery(kind).select('__key__');
const [entities] = await datastore.runQuery(query);
const keys = entities.map(entity => {
Expand Down
31 changes: 16 additions & 15 deletions test/entity.ts
Expand Up @@ -17,9 +17,10 @@
import * as assert from 'assert';
import * as extend from 'extend';
import {Datastore} from '../src';
import {Entity, entity} from '../src/entity';

describe('entity', () => {
let entity;
let entity: Entity;

beforeEach(() => {
delete require.cache[require.resolve('../src/entity.js')];
Expand Down Expand Up @@ -188,7 +189,7 @@ describe('entity', () => {
let run = false;

const decodeValueProto = entity.decodeValueProto;
entity.decodeValueProto = (valueProto) => {
entity.decodeValueProto = (valueProto: {}) => {
if (!run) {
run = true;
return decodeValueProto(valueProto);
Expand Down Expand Up @@ -256,7 +257,7 @@ describe('entity', () => {
entityValue: expectedValue,
};

entity.entityFromEntityProto = (entityProto) => {
entity.entityFromEntityProto = (entityProto: {}) => {
assert.strictEqual(entityProto, expectedValue);
return expectedValue;
};
Expand All @@ -272,7 +273,7 @@ describe('entity', () => {
keyValue: expectedValue,
};

entity.keyFromKeyProto = (keyProto) => {
entity.keyFromKeyProto = (keyProto: {}) => {
assert.strictEqual(keyProto, expectedValue);
return expectedValue;
};
Expand Down Expand Up @@ -340,7 +341,7 @@ describe('entity', () => {
integerValue: value,
};

entity.Int = function(value_) {
entity.Int = function(value_: {}) {
assert.strictEqual(value_, value);
this.value = value_;
};
Expand All @@ -365,7 +366,7 @@ describe('entity', () => {
doubleValue: value,
};

entity.Double = function(value_) {
entity.Double = function(value_: {}) {
assert.strictEqual(value_, value);
this.value = value_;
};
Expand Down Expand Up @@ -439,7 +440,7 @@ describe('entity', () => {
let run = false;

const encodeValue = entity.encodeValue;
entity.encodeValue = (value_) => {
entity.encodeValue = (value_: {}) => {
if (!run) {
run = true;
return encodeValue(value_);
Expand All @@ -462,7 +463,7 @@ describe('entity', () => {
keyValue: value,
};

entity.keyToKeyProto = (key) => {
entity.keyToKeyProto = (key: {}) => {
assert.strictEqual(key, value);
return value;
};
Expand All @@ -486,7 +487,7 @@ describe('entity', () => {
let run = false;

const encodeValue = entity.encodeValue;
entity.encodeValue = (value_) => {
entity.encodeValue = (value_: {}) => {
if (!run) {
run = true;
return encodeValue(value_);
Expand Down Expand Up @@ -570,7 +571,7 @@ describe('entity', () => {
properties: entityObject.data,
};

entity.encodeValue = (value_) => {
entity.encodeValue = (value_: {}) => {
assert.strictEqual(value_, value);
return value;
};
Expand Down Expand Up @@ -1021,12 +1022,12 @@ describe('entity', () => {

const expectedResults = entityProto;

entity.keyFromKeyProto = (key_) => {
entity.keyFromKeyProto = (key_: {}) => {
assert.strictEqual(key_, key);
return key;
};

entity.entityFromEntityProto = (entityProto_) => {
entity.entityFromEntityProto = (entityProto_: {}) => {
assert.strictEqual(entityProto_, entityProto);
return entityProto;
};
Expand All @@ -1044,7 +1045,7 @@ describe('entity', () => {
path: ['Kind', 123],
});

entity.keyToKeyProto = (key_) => {
entity.keyToKeyProto = (key_: {}) => {
assert.strictEqual(key_, key);
setImmediate(done);
return key;
Expand Down Expand Up @@ -1102,7 +1103,7 @@ describe('entity', () => {

it('should set the namespace', (done) => {
entity.Key = class {
constructor(keyOptions) {
constructor(keyOptions: entity.KeyOptions) {
assert.strictEqual(keyOptions.namespace, NAMESPACE);
done();
}
Expand All @@ -1112,7 +1113,7 @@ describe('entity', () => {

it('should create a proper Key', (done) => {
entity.Key = class {
constructor(keyOptions) {
constructor(keyOptions: entity.KeyOptions) {
assert.deepStrictEqual(keyOptions, {
namespace: NAMESPACE,
path: ['Kind', new entity.Int(111), 'Kind2', 'name'],
Expand Down

0 comments on commit 8a99d56

Please sign in to comment.