-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import Driver from '../driver'; | ||
import { destroyDriver, initDriver } from '../test-utils'; | ||
import { | ||
AlterTableDescription, | ||
AlterTableSettings, | ||
Column, | ||
OperationParams, | ||
TableDescription, | ||
TableIndex, | ||
} from '../table'; | ||
import { Types } from '../types'; | ||
|
||
const getTableName = () => `table_alter_${Math.trunc(1000 * Math.random())}`; | ||
|
||
describe('Alter table', () => { | ||
let driver: Driver; | ||
let TABLE_NAME: string; | ||
|
||
beforeAll(async () => { | ||
driver = await initDriver(); | ||
TABLE_NAME = getTableName(); | ||
}); | ||
|
||
afterAll(async () => await destroyDriver(driver)); | ||
|
||
it('Create table', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
await session.createTable( | ||
TABLE_NAME, | ||
new TableDescription() | ||
.withColumn(new Column('id', Types.optional(Types.UINT64))) | ||
.withColumn(new Column('title', Types.optional(Types.UTF8))) | ||
.withPrimaryKey('id') | ||
); | ||
|
||
const createdTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(createdTableDescription.primaryKey).toStrictEqual(['id']); | ||
expect(JSON.stringify(createdTableDescription.columns)).toStrictEqual( | ||
JSON.stringify([ | ||
{ name: 'id', type: { optionalType: { item: { typeId: 'UINT64' } } } }, | ||
{ name: 'title', type: { optionalType: { item: { typeId: 'UTF8' } } } }, | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
it('Alter table - add columns', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
const alterTableDescription = new AlterTableDescription(); | ||
alterTableDescription.addColumns = [ | ||
new Column('testBool', Types.optional(Types.BOOL)), | ||
new Column('testDate', Types.optional(Types.DATE)), | ||
]; | ||
|
||
await session.alterTable(TABLE_NAME, alterTableDescription); | ||
const alteredTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(JSON.stringify(alteredTableDescription.columns)).toBe( | ||
JSON.stringify([ | ||
{ name: 'id', type: { optionalType: { item: { typeId: 'UINT64' } } } }, | ||
{ name: 'title', type: { optionalType: { item: { typeId: 'UTF8' } } } }, | ||
{ name: 'testBool', type: { optionalType: { item: { typeId: 'BOOL' } } } }, | ||
{ name: 'testDate', type: { optionalType: { item: { typeId: 'DATE' } } } }, | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
it('Alter table - add indexes sync', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
const alterTableDescription = new AlterTableDescription(); | ||
|
||
const idxOverTestBool = new TableIndex('data_index_over_testBool') | ||
.withIndexColumns('testDate', 'title') | ||
.withDataColumns('testBool') | ||
.withGlobalAsync(false); | ||
alterTableDescription.addIndexes = [idxOverTestBool]; | ||
|
||
await session.alterTable( | ||
TABLE_NAME, | ||
alterTableDescription, | ||
new AlterTableSettings().withOperationParams(new OperationParams().withSyncMode()) | ||
); | ||
const alteredTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(JSON.stringify(alteredTableDescription.indexes)).toBe( | ||
JSON.stringify([ | ||
{ | ||
name: 'data_index_over_testBool', | ||
indexColumns: ['testDate', 'title'], | ||
globalIndex: {}, | ||
status: 'STATUS_READY', | ||
dataColumns: ['testBool'], | ||
}, | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
it('Alter table - drop indexes sync', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
const alterTableDescription = new AlterTableDescription(); | ||
|
||
alterTableDescription.dropIndexes = ['data_index_over_testBool']; | ||
|
||
await session.alterTable( | ||
TABLE_NAME, | ||
alterTableDescription, | ||
new AlterTableSettings().withOperationParams(new OperationParams().withSyncMode()) | ||
); | ||
const alteredTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(alteredTableDescription.indexes).toStrictEqual([]); | ||
}); | ||
}); | ||
|
||
it('Alter table - add indexes async', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
const alterTableDescription = new AlterTableDescription(); | ||
|
||
const idxOverTestBool = new TableIndex('data_index_over_testBool') | ||
.withIndexColumns('testDate', 'title') | ||
.withDataColumns('testBool') | ||
.withGlobalAsync(false); | ||
alterTableDescription.addIndexes = [idxOverTestBool]; | ||
|
||
await session.alterTable(TABLE_NAME, alterTableDescription); | ||
await new Promise((resolve) => setTimeout(resolve, 200)); // wait 200ms | ||
const alteredTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(JSON.stringify(alteredTableDescription.indexes)).toBe( | ||
JSON.stringify([ | ||
{ | ||
name: 'data_index_over_testBool', | ||
indexColumns: ['testDate', 'title'], | ||
globalIndex: {}, | ||
status: 'STATUS_READY', | ||
dataColumns: ['testBool'], | ||
}, | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
it('Alter table - drop indexes async', async () => { | ||
await driver.tableClient.withSession(async (session) => { | ||
const alterTableDescription = new AlterTableDescription(); | ||
|
||
alterTableDescription.dropIndexes = ['data_index_over_testBool']; | ||
|
||
await session.alterTable(TABLE_NAME, alterTableDescription); | ||
const alteredTableDescription = await session.describeTable(TABLE_NAME); | ||
|
||
expect(alteredTableDescription.indexes).toStrictEqual([]); | ||
}); | ||
}); | ||
}); |