Skip to content

Commit

Permalink
Prepare second describe block for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljbruce committed Jun 3, 2024
1 parent 316c651 commit 974117b
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions test/gapic-mocks/handwritten-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,60 @@ import {RunQueryOptions} from '../../src/query';
const async = require('async');

describe('HandwrittenLayerErrors', () => {
describe('Can only specify one of transaction, consistency, readTime', () => {
const clientName = 'DatastoreClient';
const datastore = getInitializedDatastoreClient();
const clientName = 'DatastoreClient';

/**
* Returns a callback function that expects an error with a particular
* message. This is used for testing all client library functions that accept
* a callback in order to ensure the callback receives a particular error.
*
* @param {mocha.Done} done The mocha done function which is called when the
* test finishes.
* @param {string} message The expected error message in the test.
*
*/
function getCallbackExpectingError(done: mocha.Done, message: string) {
return (error?: Error | null) => {
try {
if (error) {
assert.strictEqual(error.message, message);
done();
return;
}
done(new Error('The callback should have received an error'));
} catch (err: unknown) {
done(err);
/**
* Returns a callback function that expects an error with a particular
* message. This is used for testing all client library functions that accept
* a callback in order to ensure the callback receives a particular error.
*
* @param {mocha.Done} done The mocha done function which is called when the
* test finishes.
* @param {string} message The expected error message in the test.
*
*/
function getCallbackExpectingError(done: mocha.Done, message: string) {
return (error?: Error | null) => {
try {
if (error) {
assert.strictEqual(error.message, message);
done();
return;
}
done(new Error('The callback should have received an error'));
} catch (err: unknown) {
done(err);
}
};
}

/**
* This function ends the test with an error if a call reaches the gapic
* layer. Using this function in a test makes the test fail if any outgoing
* grpc calls are made in that test. This allows the test to ensure that no
* grpc calls happen, which is typically desired behaviour when an error is
* sent back to the user from the handwritten layer.
*
* @param {mocha.Done} done The mocha done function which is called when the
* test finishes.
*/
function errorOnGapicCall(datastore: Datastore, done: mocha.Done) {
const dataClient = datastore.clients_.get(clientName);
if (dataClient) {
dataClient.runQuery = () => {
done(new Error('The gapic layer should not have received a call'));
};
dataClient.runAggregationQuery = () => {
done(new Error('The gapic layer should not have received a call'));
};
dataClient.lookup = () => {
done(new Error('The gapic layer should not have received a call'));
};
}
}

/**
* This function ends the test with an error if a call reaches the gapic
* layer. Using this function in a test makes the test fail if any outgoing
* grpc calls are made in that test. This allows the test to ensure that no
* grpc calls happen, which is typically desired behaviour when an error is
* sent back to the user from the handwritten layer.
*
* @param {mocha.Done} done The mocha done function which is called when the
* test finishes.
*/
function errorOnGapicCall(done: mocha.Done) {
const dataClient = datastore.clients_.get(clientName);
if (dataClient) {
dataClient.runQuery = () => {
done(new Error('The gapic layer should not have received a call'));
};
dataClient.runAggregationQuery = () => {
done(new Error('The gapic layer should not have received a call'));
};
dataClient.lookup = () => {
done(new Error('The gapic layer should not have received a call'));
};
}
}
describe('Can only specify one of transaction, consistency, readTime', () => {
const datastore = getInitializedDatastoreClient();
async.each(
[
{
Expand Down Expand Up @@ -92,7 +93,7 @@ describe('HandwrittenLayerErrors', () => {
it('should error when runQuery is used', done => {
const transaction = datastore.transaction();
const query = datastore.createQuery('Task');
errorOnGapicCall(done); // Test fails if Gapic layer receives a call.
errorOnGapicCall(datastore, done); // Test fails if Gapic layer receives a call.
transaction.runQuery(
query,
testParameters.options,
Expand All @@ -105,7 +106,7 @@ describe('HandwrittenLayerErrors', () => {
const aggregate = datastore
.createAggregationQuery(query)
.addAggregation(AggregateField.sum('appearances'));
errorOnGapicCall(done); // Test fails if Gapic layer receives a call.
errorOnGapicCall(datastore, done); // Test fails if Gapic layer receives a call.
transaction.runAggregationQuery(
aggregate,
testParameters.options,
Expand All @@ -115,7 +116,7 @@ describe('HandwrittenLayerErrors', () => {
it('should error when get is used', done => {
const transaction = datastore.transaction();
const keys = datastore.key(['Company', 'Google']);
errorOnGapicCall(done); // Test fails if Gapic layer receives a call.
errorOnGapicCall(datastore, done); // Test fails if Gapic layer receives a call.
transaction.get(
keys,
testParameters.options,
Expand All @@ -126,4 +127,7 @@ describe('HandwrittenLayerErrors', () => {
}
);
});
describe('Must report that the transaction has expired', () => {

Check failure on line 130 in test/gapic-mocks/handwritten-errors.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎⏎··`

});
});

0 comments on commit 974117b

Please sign in to comment.