Skip to content

Commit

Permalink
Integration tests for Multi-GSI table / entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed Sep 24, 2023
1 parent 3e3f87f commit 6881b38
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 11 deletions.
26 changes: 26 additions & 0 deletions test/examples/chickenTypeAndEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ export const CHICKEN_ENTITY: Entity<
}
}

export const TWO_GSI_CHICKEN_ENTITY: Entity<
Chicken,
Pick<Chicken, 'breed'>,
Pick<Chicken, 'name' | 'dateOfBirth'>
> = {
...CHICKEN_ENTITY,
gsis: {
gsi1: {
pk({ coop }: Pick<Chicken, 'coop'>): string {
return `COOP#${coop}`
},
sk({ breed, dateOfBirth }: Pick<Chicken, 'breed' | 'dateOfBirth'>): string {
return gsiSk(breed, dateOfBirth)
}
},
gsi2: {
pk(): string {
return `CHICKEN`
},
sk({ dateOfBirth }: Pick<Chicken, 'dateOfBirth'>): string {
return `DATEOFBIRTH#${dateOfBirth}`
}
}
}
}

function sk(name: string, dateOfBirth: string) {
return `DATEOFBIRTH#${dateOfBirth}#NAME#${name}`
}
Expand Down
46 changes: 46 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

TwoGSITableName:
Value: !Ref TwoGSITable

CustomTableName:
Value: !Ref CustomTable

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

TwoGSITable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSI1PK
AttributeType: S
- AttributeName: GSI1SK
AttributeType: S
- AttributeName: GSI2PK
AttributeType: S
- AttributeName: GSI2SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: "GSI1"
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: GSI1PK
KeyType: HASH
- AttributeName: GSI1SK
KeyType: RANGE
- IndexName: "GSI2"
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: GSI2PK
KeyType: HASH
- AttributeName: GSI2SK
KeyType: RANGE

CustomTable:
Type: AWS::DynamoDB::Table
Properties:
Expand Down
52 changes: 50 additions & 2 deletions test/integration/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
CHICKEN_ENTITY,
findOlderThan,
findYoungerThan,
gsiBreed
gsiBreed,
TWO_GSI_CHICKEN_ENTITY
} from '../examples/chickenTypeAndEntity'
import { FARM_ENTITY } from '../examples/farmTypeAndEntity'
import { FakeClock } from '../unit/testSupportCode/fakes/fakeClock'
Expand All @@ -46,6 +47,7 @@ import { CAT_ENTITY } from '../examples/catTypeAndEntity'

let documentClient: DynamoDBDocumentClient
let testTableName: string
let twoGSITableName: string
let customTableName: string
let farmTableName: string
const clock = new FakeClock()
Expand All @@ -62,6 +64,7 @@ beforeAll(async () => {
const awsEnv = await initAWSResources()
documentClient = awsEnv.documentClient
testTableName = awsEnv.testTableName
twoGSITableName = awsEnv.twoGSITableName
customTableName = awsEnv.customTableName
farmTableName = awsEnv.farmTableName
})
Expand Down Expand Up @@ -103,7 +106,7 @@ describe('standard single table', () => {
Pick<Chicken, 'breed'>,
Pick<Chicken, 'name' | 'dateOfBirth'>
>
beforeAll(async () => {
beforeAll(() => {
tableName = testTableName
// store = defineStandardSingleTableStore(documentClient, tableName)
// storeWithScans = defineStandardSingleTableStore(documentClient, tableName, { allowScans: true })
Expand Down Expand Up @@ -1080,6 +1083,51 @@ describe('minimal single table', () => {
})
})

describe('Multiple GSI Single Table', () => {
let store: AllEntitiesStore
beforeAll(async () => {
const config = {
...createStandardSingleTableConfig(twoGSITableName),
gsiNames: { gsi1: 'GSI1', gsi2: 'GSI2' }
}
config.metaAttributeNames.gsisById = {
gsi1: { pk: 'GSI1PK', sk: 'GSI1SK' },
gsi2: { pk: 'GSI2PK', sk: 'GSI2SK' }
}

store = createStore(config, createStoreContext({ clock, logger: noopLogger }, documentClient))
})

test('query with multiple GSIs', async () => {
await emptyTable(twoGSITableName)
await store.for(TWO_GSI_CHICKEN_ENTITY).put(ginger)
// NB: Different GSI fields
expect(await scanWithDocClient(twoGSITableName)).toEqual([
{
PK: 'CHICKEN#BREED#sussex',
SK: 'DATEOFBIRTH#2021-07-01#NAME#ginger',
GSI1PK: 'COOP#bristol',
GSI1SK: 'CHICKEN#BREED#sussex#DATEOFBIRTH#2021-07-01',
GSI2PK: 'CHICKEN',
GSI2SK: 'DATEOFBIRTH#2021-07-01',
_et: 'chicken',
_lastUpdated: '2023-07-01T19:00:00.000Z',
breed: 'sussex',
coop: 'bristol',
dateOfBirth: '2021-07-01',
name: 'ginger'
}
])
await store.for(TWO_GSI_CHICKEN_ENTITY).put(babs)
await store.for(TWO_GSI_CHICKEN_ENTITY).put(yolko)

const result = await store
.for(TWO_GSI_CHICKEN_ENTITY)
.queryAllWithGsiByPk({ coop: 'bristol' }, { gsiId: 'gsi1' })
expect(result).toEqual([ginger, babs])
})
})

describe('multi table', () => {
let store: AllEntitiesStore
beforeAll(() => {
Expand Down
17 changes: 8 additions & 9 deletions test/integration/testSupportCode/integrationTestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ export async function initAWSResources() {
return tableName ?? throwError('Unable to find table name')()
}

const newTestTableName = findTableName('TableName')
const newCustomTableName = findTableName('CustomTableName')
const newFarmTableName = findTableName('FarmTableName')
const tableNames = {
testTableName: findTableName('TableName'),
twoGSITableName: findTableName('TwoGSITableName'),
customTableName: findTableName('CustomTableName'),
farmTableName: findTableName('FarmTableName')
}

console.log(
`testTableName = ${newTestTableName}, customTableName = ${newCustomTableName}, farmTableName = ${newFarmTableName}`
)
console.log(JSON.stringify(tableNames))

return {
documentClient,
testTableName: newTestTableName,
customTableName: newCustomTableName,
farmTableName: newFarmTableName
...tableNames
}
}

0 comments on commit 6881b38

Please sign in to comment.