Skip to content

Commit

Permalink
More multi-table tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed Sep 24, 2023
1 parent a7e07c0 commit 54ae66d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/support/setupSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function createStandardSingleTableConfig(tableName: string): TableConfig
}

/**
* Same configuration as createStandardSingleTableConfig but for multiple entities
* Same configuration as createStandardSingleTableConfig but for multiple tables
* @param tablesToEntityTypes Map of underlying Dynamo Table Names to the entities stored in each table
* @param defaultTableName Which table to use if an operation is performed on an entity not explicitly configured in tablesToEntityTypes. Default - no default table is used, and all entities must be explicitly configured.
* @throws if `defaultTableName` isn't included in keys of `tablesToEntityTypes`
Expand Down
34 changes: 34 additions & 0 deletions test/examples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Outputs:
TableName:
Value: !Ref TestTable

TableTwoName:
Value: !Ref TestTableTwo

TwoGSITableName:
Value: !Ref TwoGSITable

Expand Down Expand Up @@ -46,6 +49,37 @@ Resources:
- AttributeName: GSISK
KeyType: RANGE

TestTableTwo:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSIPK
AttributeType: S
- AttributeName: GSISK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: "GSI"
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: GSIPK
KeyType: HASH
- AttributeName: GSISK
KeyType: RANGE

TwoGSITable:
Type: AWS::DynamoDB::Table
Properties:
Expand Down
56 changes: 55 additions & 1 deletion test/integration/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DeleteCommand, DynamoDBDocumentClient, ScanCommand } from '@aws-sdk/lib
import {
AllEntitiesStore,
createMinimumSingleTableConfig,
createStandardMultiTableConfig,
createStandardSingleTableConfig,
createStore,
createStoreContext,
Expand All @@ -47,6 +48,7 @@ import { CAT_ENTITY } from '../examples/catTypeAndEntity'

let documentClient: DynamoDBDocumentClient
let testTableName: string
let testTableTwoName: string
let twoGSITableName: string
let customTableName: string
let farmTableName: string
Expand All @@ -64,6 +66,7 @@ beforeAll(async () => {
const awsEnv = await initAWSResources()
documentClient = awsEnv.documentClient
testTableName = awsEnv.testTableName
testTableTwoName = awsEnv.testTableTwoName
twoGSITableName = awsEnv.twoGSITableName
customTableName = awsEnv.customTableName
farmTableName = awsEnv.farmTableName
Expand Down Expand Up @@ -1128,7 +1131,7 @@ describe('Multiple GSI Single Table', () => {
})
})

describe('multi table', () => {
describe('multi table custom config', () => {
let store: AllEntitiesStore
beforeAll(() => {
const config: TablesConfig = {
Expand Down Expand Up @@ -1206,3 +1209,54 @@ describe('multi table', () => {
})
})
})

describe('multi table standard config', () => {
let store: AllEntitiesStore
beforeAll(() => {
// NB - no default table for this example
store = createStore(
createStandardMultiTableConfig({
[testTableName]: [SHEEP_ENTITY.type],
[testTableTwoName]: [DOG_ENTITY.type]
}),
createStoreContext({ clock: new FakeClock(), logger: noopLogger }, documentClient)
)
})

test('basic operations', async () => {
await emptyTable(testTableName)
await emptyTable(testTableTwoName)
await store.for(SHEEP_ENTITY).put(shaunTheSheep)
await store.for(DOG_ENTITY).put(chesterDog)

expect(await store.for(SHEEP_ENTITY).getOrThrow(shaunIdentifier)).toEqual(shaunTheSheep)
expect(await store.for(DOG_ENTITY).getOrThrow(chesterDog)).toEqual(chesterDog)
expect(async () => await store.for(CHICKEN_ENTITY).getOrThrow(ginger)).rejects.toThrowError(
'Unable to locate table that supports entity type chicken'
)

// Should have been written to correct tables
expect(await scanWithDocClient(testTableName)).toEqual([
{
PK: 'SHEEP#BREED#merino',
SK: 'NAME#shaun',
_et: 'sheep',
_lastUpdated: '2023-07-01T19:00:00.000Z',
ageInYears: 3,
breed: 'merino',
name: 'shaun'
}
])
expect(await scanWithDocClient(testTableTwoName)).toEqual([
{
PK: 'FARM#Sunflower Farm',
SK: 'DOG#NAME#Chester',
_et: 'dog',
_lastUpdated: '2023-07-01T19:00:00.000Z',
ageInYears: 4,
farm: 'Sunflower Farm',
name: 'Chester'
}
])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export async function initAWSResources() {

const tableNames = {
testTableName: findTableName('TableName'),
testTableTwoName: findTableName('TableTwoName'),
twoGSITableName: findTableName('TwoGSITableName'),
customTableName: findTableName('CustomTableName'),
farmTableName: findTableName('FarmTableName')
Expand Down

0 comments on commit 54ae66d

Please sign in to comment.