Skip to content

Commit 38931c8

Browse files
danieljbruceGautamSharda
authored andcommitted
test: Add tests and comments to better document any typed data structures (#1498)
* Add a test for the mutate function * Comment out the tests * Fixed problems with the test to reproduce issue * Modify the cosmetic aspects of the new system test * Add a comment about the shape of Entry types * Clarify the comment describing the expected type * Add a comment that describes the type required Type for mutation that is * Uncomment again * Remove only
1 parent 0efc6de commit 38931c8

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

handwritten/bigtable/src/mutation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ export type ISetCell = btTypes.bigtable.v2.Mutation.ISetCell;
2525
export type Bytes = string | Buffer;
2626
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2727
export type Data = any;
28+
/*
29+
The Data type is expected to be in the following format:
30+
{
31+
columnFamily1: {
32+
column1: Cell,
33+
column2: Cell
34+
},
35+
columnFamily2: {
36+
otherColumn1: Cell,
37+
otherColumn2: Cell
38+
}
39+
}
40+
Where the Cell data type has the following structure:
41+
Uint8Array | string | {
42+
value: Uint8Array|string,
43+
timestamp: number|Long|string,
44+
}
45+
*/
2846
export interface JsonObj {
2947
[k: string]: string | JsonObj;
3048
}

handwritten/bigtable/src/table.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ export interface MutateOptions {
287287

288288
// eslint-disable-next-line @typescript-eslint/no-explicit-any
289289
export type Entry = any;
290+
/*
291+
The Entry type is expected to be in the following format:
292+
{
293+
columnFamily: {
294+
column: Data // Data is the expected type passed into Mutation.encodeSetCell
295+
}
296+
}
297+
*/
290298

291299
export type DeleteTableCallback = (
292300
err: ServiceError | null,

handwritten/bigtable/system-test/bigtable.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {Row} from '../src/row.js';
3333
import {Table} from '../src/table.js';
3434
import {RawFilter} from '../src/filter';
3535
import {generateId, PREFIX} from './common';
36+
import {Mutation} from '../src/mutation';
3637

3738
describe('Bigtable', () => {
3839
const bigtable = new Bigtable();
@@ -1712,6 +1713,90 @@ describe('Bigtable', () => {
17121713
});
17131714
});
17141715
});
1716+
1717+
describe('mutateRows entries tests', () => {
1718+
const table = INSTANCE.table(generateId('table'));
1719+
1720+
afterEach(async () => {
1721+
await table.delete();
1722+
});
1723+
1724+
it('should only insert one row in the table with mutate', async () => {
1725+
// Create table
1726+
const tableOptions = {
1727+
families: ['columnFamily'],
1728+
};
1729+
await table.create(tableOptions);
1730+
// Add entries
1731+
const entry = {
1732+
columnFamily: {
1733+
column: 1,
1734+
},
1735+
};
1736+
const mutation = {
1737+
key: 'rowKey',
1738+
data: entry,
1739+
method: Mutation.methods.INSERT,
1740+
};
1741+
const gaxOptions = {maxRetries: 4};
1742+
await table.mutate(mutation, {gaxOptions});
1743+
// Get rows and compare
1744+
const [rows] = await table.getRows();
1745+
assert.strictEqual(rows.length, 1);
1746+
});
1747+
1748+
it('should insert one row in the table using mutate in a similar way to how the documentation says to use insert', async () => {
1749+
// Create table
1750+
const tableOptions = {
1751+
families: ['columnFamily'],
1752+
};
1753+
await table.create(tableOptions);
1754+
// Add entries
1755+
const mutation = {
1756+
key: 'rowKey',
1757+
data: {
1758+
columnFamily: {
1759+
column: 1,
1760+
},
1761+
},
1762+
method: Mutation.methods.INSERT,
1763+
};
1764+
const gaxOptions = {maxRetries: 4};
1765+
await table.mutate(mutation, {gaxOptions});
1766+
// Get rows and compare
1767+
const [rows] = await table.getRows();
1768+
assert.strictEqual(rows.length, 1);
1769+
});
1770+
1771+
it('should only insert one row in the table with insert as described by the GCP documentation', async () => {
1772+
// Create table
1773+
const tableOptions = {
1774+
families: ['follows'],
1775+
};
1776+
await table.create(tableOptions);
1777+
// Add entries
1778+
const greetings = ['Hello World!', 'Hello Bigtable!', 'Hello Node!'];
1779+
const rowsToInsert = greetings.map((greeting, index) => ({
1780+
key: `greeting${index}`,
1781+
data: {
1782+
follows: {
1783+
// 'follows' is the column family
1784+
someColumn: {
1785+
// Setting the timestamp allows the client to perform retries. If
1786+
// server-side time is used, retries may cause multiple cells to
1787+
// be generated.
1788+
timestamp: new Date(),
1789+
value: greeting,
1790+
},
1791+
},
1792+
},
1793+
}));
1794+
await table.insert(rowsToInsert);
1795+
// Get rows and compare
1796+
const [rows] = await table.getRows();
1797+
assert.strictEqual(rows.length, 3);
1798+
});
1799+
});
17151800
});
17161801

17171802
function createInstanceConfig(

0 commit comments

Comments
 (0)