Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add tests and comments to better document any typed data structures #1498

18 changes: 18 additions & 0 deletions src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export type ISetCell = btTypes.bigtable.v2.Mutation.ISetCell;
export type Bytes = string | Buffer;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Data = any;
/*
The Data type is expected to be in the following format:
{
columnFamily1: {
column1: Cell,
column2: Cell
},
columnFamily2: {
otherColumn1: Cell,
otherColumn2: Cell
}
}
Where the Cell data type has the following structure:
Uint8Array | string | {
value: Uint8Array|string,
timestamp: number|Long|string,
}
*/
export interface JsonObj {
[k: string]: string | JsonObj;
}
Expand Down
8 changes: 8 additions & 0 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Entry = any;
/*
The Entry type is expected to be in the following format:
{
columnFamily: {
column: Data // Data is the expected type passed into Mutation.encodeSetCell
}
}
*/

export type DeleteTableCallback = (
err: ServiceError | null,
Expand Down Expand Up @@ -830,7 +838,7 @@
// Handling retries in this client. Specify the retry options to
// make sure nothing is retried in retry-request.
noResponseRetries: 0,
shouldRetryFn: (_: any) => {

Check warning on line 841 in src/table.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is defined but never used

Check warning on line 841 in src/table.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return false;
},
};
Expand Down Expand Up @@ -988,7 +996,7 @@
userStream.emit('error', error);
}
})
.on('data', _ => {

Check warning on line 999 in src/table.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is defined but never used
// Reset error count after a successful read so the backoff
// time won't keep increasing when as stream had multiple errors
numConsecutiveErrors = 0;
Expand Down Expand Up @@ -1611,7 +1619,7 @@
// Handling retries in this client. Specify the retry options to
// make sure nothing is retried in retry-request.
noResponseRetries: 0,
shouldRetryFn: (_: any) => {

Check warning on line 1622 in src/table.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is defined but never used

Check warning on line 1622 in src/table.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return false;
},
};
Expand Down
85 changes: 85 additions & 0 deletions system-test/bigtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {Row} from '../src/row.js';
import {Table} from '../src/table.js';
import {RawFilter} from '../src/filter';
import {generateId, PREFIX} from './common';
import {Mutation} from '../src/mutation';

describe('Bigtable', () => {
const bigtable = new Bigtable();
Expand Down Expand Up @@ -1712,6 +1713,90 @@ describe('Bigtable', () => {
});
});
});

describe('mutateRows entries tests', () => {
const table = INSTANCE.table(generateId('table'));

afterEach(async () => {
await table.delete();
});

it('should only insert one row in the table with mutate', async () => {
// Create table
const tableOptions = {
families: ['columnFamily'],
};
await table.create(tableOptions);
// Add entries
const entry = {
columnFamily: {
column: 1,
},
};
const mutation = {
key: 'rowKey',
data: entry,
method: Mutation.methods.INSERT,
};
const gaxOptions = {maxRetries: 4};
await table.mutate(mutation, {gaxOptions});
// Get rows and compare
const [rows] = await table.getRows();
assert.strictEqual(rows.length, 1);
});

it('should insert one row in the table using mutate in a similar way to how the documentation says to use insert', async () => {
// Create table
const tableOptions = {
families: ['columnFamily'],
};
await table.create(tableOptions);
// Add entries
const mutation = {
key: 'rowKey',
data: {
columnFamily: {
column: 1,
},
},
method: Mutation.methods.INSERT,
};
const gaxOptions = {maxRetries: 4};
await table.mutate(mutation, {gaxOptions});
// Get rows and compare
const [rows] = await table.getRows();
assert.strictEqual(rows.length, 1);
});

it('should only insert one row in the table with insert as described by the GCP documentation', async () => {
// Create table
const tableOptions = {
families: ['follows'],
};
await table.create(tableOptions);
// Add entries
const greetings = ['Hello World!', 'Hello Bigtable!', 'Hello Node!'];
const rowsToInsert = greetings.map((greeting, index) => ({
key: `greeting${index}`,
data: {
follows: {
// 'follows' is the column family
someColumn: {
// Setting the timestamp allows the client to perform retries. If
// server-side time is used, retries may cause multiple cells to
// be generated.
timestamp: new Date(),
value: greeting,
},
},
},
}));
await table.insert(rowsToInsert);
// Get rows and compare
const [rows] = await table.getRows();
assert.strictEqual(rows.length, 3);
});
});
});

function createInstanceConfig(
Expand Down
Loading