Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
Removed the requestOptions from a deprecated call
  • Loading branch information
Auke Bruinsma authored and benhutchins committed Oct 13, 2023
1 parent d74b44a commit 90544fc
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 8 deletions.
21 changes: 20 additions & 1 deletion src/batch-get.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, should } from 'chai'
import { sortBy } from 'lodash'
import { BatchGet } from './batch-get'
import { PrimaryKey } from './query/primary-key'
Expand Down Expand Up @@ -143,6 +143,25 @@ describe('BatchGet', () => {
expect(results[0].id).eq(1)
})

it('should not return records when aborted', async () => {
const abortController = new AbortController()
const batch = new BatchGet<TestTable1>()
batch.get(TestTable1.primaryKey.fromKey(1))
batch.get(TestTable1.primaryKey.fromKey(42))
abortController.abort()

let exception
try {
await batch.retrieve({
abortSignal: abortController.signal,
})
} catch (ex) {
exception = ex
}

should().exist(exception)
})

it('should accept projection expressions', async () => {
const batch = new BatchGet<TestTable1>()
const item = TestTable1.primaryKey.fromKey(1)
Expand Down
31 changes: 31 additions & 0 deletions src/query/global-secondary-index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ describe('Query/GlobalSecondaryIndex', () => {
expect(res.records[1].id).to.eq(11)
})

it('should not return items when aborted', async () => {
const abortController = new AbortController()
await Card.new({ id: 10, title: 'abc' }).save()
await Card.new({ id: 11, title: 'abd' }).save()
await Card.new({ id: 12, title: 'abd' }).save()
abortController.abort()

let exception
try {
await Card.hashTitleIndex.query({ title: 'abd' }, undefined, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})

it('should return an empty array when no items match', async () => {
const res = await Card.hashTitleIndex.query({ title: '404' })
expect(res[0]).to.not.eq(null)
Expand Down Expand Up @@ -174,6 +191,20 @@ describe('Query/GlobalSecondaryIndex', () => {
expect(cardIds).to.include.members(res2.records.map((r) => r.id))
expect(cardIds).to.include.members(res3.records.map((r) => r.id))
})

it('should not return results when aborted', async () => {
const abortController = new AbortController()
abortController.abort()

let exception
try {
await Card.hashTitleIndex.scan(null, undefined, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})
})
})

Expand Down
28 changes: 27 additions & 1 deletion src/query/local-secondary-index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, should } from 'chai'
import * as Decorator from '../decorator'
import { DocumentClient } from '../document-client'
import { Table } from '../table'
Expand Down Expand Up @@ -54,5 +54,31 @@ describe('Query/LocalSecondaryIndex', () => {
expect(res.records[0].count).to.eq(4)
expect(res.records[1].count).to.eq(3)
})

it('should not find items when aborted', async () => {
const abortController = new AbortController()
await Card.documentClient.batchPut([
Card.new({ id: 10, title: 'a', count: 4 }),
Card.new({ id: 10, title: 'b', count: 3 }),
Card.new({ id: 10, title: 'c', count: 2 }),
Card.new({ id: 10, title: 'd', count: 1 }),
])

abortController.abort()

let exception
try {
await Card.countIndex.query({
id: 10,
count: ['>', 2],
}, {
rangeOrder: 'DESC',
}, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})
})
})
54 changes: 53 additions & 1 deletion src/query/primary-key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, should } from 'chai'
import { Table } from '../table'
import { PrimaryKey } from './primary-key'

Expand Down Expand Up @@ -74,6 +74,22 @@ describe('Query/PrimaryKey', () => {
expect(item).to.eq(undefined)
})

it('should not find item using a query filter object when aborted', async () => {
const abortController = new AbortController()

await Card.new({ id: 10, title: 'abc' }).save()
abortController.abort()

let exception
try {
await primaryKey.get({ id: 10, title: 'abc' }, undefined, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})

it('should find item using a query filter object', async () => {
await Card.new({ id: 10, title: 'abc' }).save()
const item = await primaryKey.get({ id: 10, title: 'abc' })
Expand All @@ -94,6 +110,22 @@ describe('Query/PrimaryKey', () => {
}
})

it('should not find item using hash and range arguments when aborted', async () => {
const abortController = new AbortController()
await Card.new({ id: 10, title: 'abc' }).save()

abortController.abort()

let exception
try {
await primaryKey.get(10, 'abc', undefined, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})

it('should allow date type to be the range', async () => {
const now = new Date()
await TableWithDateRange.new({ id: 1, date: now }).save()
Expand Down Expand Up @@ -188,6 +220,26 @@ describe('Query/PrimaryKey', () => {
expect(res.records[0].title).to.eq('aba')
expect(res.records[1].title).to.eq('abc')
})

it('should not find items when aborted', async () => {
const abortController = new AbortController()

await Card.new({ id: 10, title: 'abc' }).save()
await Card.new({ id: 10, title: 'abd' }).save()
await Card.new({ id: 10, title: 'aba' }).save()
abortController.abort()

let exception
try {
await primaryKey.scan(null, {
limit: 2,
}, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})
})

describe('#update', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/query/primary-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ export class PrimaryKey<T extends Table, HashKeyType extends PrimaryKeyType, Ran
*
* @deprecated
*/
public async batchGet(inputs: Array<PrimaryKeyBatchInput<HashKeyType, RangeKeyType>>, requestOptions?: IRequestOptions): Promise<T[]> {
public async batchGet(inputs: Array<PrimaryKeyBatchInput<HashKeyType, RangeKeyType>>): Promise<T[]> {
const batch = new BatchGet<T>()

for (const input of inputs) {
batch.get(this.fromKey(input[0], input[1]))
}

return await batch.retrieve(requestOptions)
return await batch.retrieve()
}

/**
Expand Down
59 changes: 58 additions & 1 deletion src/query/search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, should } from 'chai'
import { TestableTable } from '../setup-tests.spec'
import { MagicSearch } from './search'

Expand Down Expand Up @@ -27,6 +27,24 @@ describe('Query/Search', () => {
expect(result.records[0].lowercaseString).to.eq('table search 0')
})

it('should not search when aborted', async () => {
const abortController = new AbortController()

const search = new MagicSearch<TestableTable>(TestableTable, { title: 'Table.search 0' })
const input = search.getInput()
expect(input.IndexName).to.eq('titleIndex')
abortController.abort()

let exception
try {
await search.exec({ abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})

it('should ignore index if you are using a special condition', async () => {
const search = new MagicSearch<TestableTable>(TestableTable, {
title: ['contains', 'Table.search'],
Expand Down Expand Up @@ -237,6 +255,26 @@ describe('Query/Search', () => {
const output = await search.all()
expect(output.length).to.eq(8)
})

it('should not return results when aborted', async () => {
const abortController = new AbortController()
const search = new MagicSearch<TestableTable>(TestableTable)
.filter('title').contains('Table.search')
.limit(2)

// we set a limit and then called .all(), so it should page automatically until all results are found
// this is stupid and slow, it would be faster to remove the limit, but we are testing the paging logic of .all
abortController.abort()

let exception
try {
await search.all({ abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})
})

describe('#minimum', () => {
Expand All @@ -249,5 +287,24 @@ describe('Query/Search', () => {
const output = await search.minimum(5)
expect(output.length).to.be.at.least(5)
})

it('should not return results when aborted', async () => {
const abortController = new AbortController()

const search = new MagicSearch<TestableTable>(TestableTable)
.filter('title').contains('Table.search')
.limit(2)

abortController.abort()

let exception
try {
await search.minimum(5, { abortSignal: abortController.signal })
} catch (ex) {
exception = ex
}

should().exist(exception)
})
})
})
5 changes: 3 additions & 2 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { migrateTable } from './tables/migrate-table'
import { type TablePropertyValue, type TableProperties, type TableProperty } from './tables/properties'
import { Schema } from './tables/schema'
import { isTrulyEmpty } from './utils/truly-empty'
import { type IRequestOptions } from './connections'

type StaticThis<T> = new() => T

Expand Down Expand Up @@ -142,8 +143,8 @@ export class Table {
return await deleteTable(this.schema)
}

public static async describeTable(): Promise<TableDescription> {
return await describeTable(this.schema)
public static async describeTable(requestOptions?: IRequestOptions): Promise<TableDescription> {
return await describeTable(this.schema, requestOptions)
}
// #endregion static methods
// #endregion static
Expand Down

0 comments on commit 90544fc

Please sign in to comment.