Skip to content

Commit e18c4a4

Browse files
committed
test(various): cleanup, add tests for abstract request class
1 parent 5826b7f commit e18c4a4

File tree

8 files changed

+117
-33
lines changed

8 files changed

+117
-33
lines changed

src/decorator/metadata/metadata.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export class Metadata<T> {
7272
* @throws Throws an error if an indexName was delivered but no index was found for given name or the found index has no sort key defined
7373
*/
7474
getSortKey(indexName?: string): keyof T | null {
75-
// todo: is this ok?! it returns null when no sortKey is defined but throws when no sortKey when index was given
7675
if (indexName) {
7776
const index = this.getIndex(indexName)
7877
if (index) {

src/dynamo/dynamo-rx.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,4 @@ describe('dynamo rx', () => {
7777
expect(dynamoRx.dynamoDb.config.credentials).toBe(credentials)
7878
})
7979

80-
xit('should use the given aws region', () => {})
81-
8280
})

src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import { of } from 'rxjs'
22
// tslint:disable:no-unnecessary-class
33
// tslint:disable:no-unused-expression
44
import { getTableName } from '../../../../test/helper'
5-
import {
6-
Organization,
7-
SimpleWithCompositePartitionKeyModel,
8-
SimpleWithPartitionKeyModel,
9-
} from '../../../../test/models'
5+
import { Organization, SimpleWithPartitionKeyModel } from '../../../../test/models'
106
import { updateDynamoEasyConfig } from '../../../config'
117
import { BatchGetSingleTableRequest } from './batch-get-single-table.request'
128

@@ -18,12 +14,6 @@ describe('batch get', () => {
1814
expect(() => new BatchGetSingleTableRequest(<any>null, <any>null, 'tableName', [])).toThrow()
1915
})
2016

21-
xit('should throw when a class without metadata was given', () => {
22-
class NoModelClass {}
23-
24-
expect(() => new BatchGetSingleTableRequest(<any>null, NoModelClass, 'tableName', [])).toThrow()
25-
})
26-
2717
it('should throw when adding a key without partition key', () => {
2818
expect(() => {
2919
new BatchGetSingleTableRequest(<any>null, SimpleWithPartitionKeyModel, 'tableName', [{}])
@@ -33,12 +23,6 @@ describe('batch get', () => {
3323
}).toThrow()
3424
})
3525

36-
xit('should throw when sort key is missing', () => {
37-
expect(() => {
38-
new BatchGetSingleTableRequest(<any>null, SimpleWithCompositePartitionKeyModel, 'tableName', [{ partitionKey: 'aKey' }])
39-
}).toThrow()
40-
})
41-
4226
})
4327

4428
describe('correct params', () => {

src/dynamo/request/query/query.request.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,6 @@ describe('query request', () => {
107107
})
108108
})
109109

110-
// describe('calls endpoint with correct params', () => {
111-
// const dynamoRx: DynamoRx = DYNAMO_RX_MOCK as DynamoRx
112-
// let querySpy: jasmine.Spy
113-
// let request: QueryRequest<ComplexModel>
114-
//
115-
// beforeEach(() => {
116-
// request = new QueryRequest(<any>null, ComplexModel, getTableName(ComplexModel))
117-
// querySpy = spyOn(dynamoRx, 'query').and.callThrough()
118-
// })
119-
// })
120-
121110
describe('scan direction', () => {
122111
let req: QueryRequest<SimpleWithPartitionKeyModel>
123112
beforeEach(() => req = new QueryRequest(<any>null, SimpleWithPartitionKeyModel, 'tableName'))

src/dynamo/request/query/query.request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class QueryRequest<T> extends Request<T, QueryRequest<T>, QueryInput, Que
137137
}
138138

139139
execSingle(): Observable<T | null> {
140-
// fixme, copy params, don't add limit on member (too implicit)
140+
// fixme, copy params, don't add limit on member (too implicit, --> request instance can't be reused to fetch many)
141141
this.limit(1)
142142
this.logger.debug('single request', this.params)
143143
return this.dynamoRx.query(this.params).pipe(
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { getTableName } from '../../../test/helper'
2+
import {
3+
ModelWithABunchOfIndexes,
4+
SimpleWithCompositePartitionKeyModel,
5+
SimpleWithPartitionKeyModel,
6+
} from '../../../test/models'
7+
import { INDEX_ACTIVE_CREATED_AT, INDEX_COUNT } from '../../../test/models/model-with-indexes.model'
8+
import { ModelConstructor } from '../../model/model-constructor'
9+
import { Request } from './request.model'
10+
11+
12+
class TestRequest<T> extends Request<T, any, any, any> {
13+
14+
constructor(modelClazz: ModelConstructor<T>) {
15+
super(<any>null, modelClazz, getTableName(modelClazz))
16+
}
17+
18+
exec() {return <any>null}
19+
20+
execCount() {return <any>null}
21+
22+
execFullResponse() {return <any>null}
23+
24+
execNoMap() {return <any>null}
25+
26+
execSingle() {return <any>null}
27+
28+
}
29+
30+
31+
describe('Request', () => {
32+
let request: TestRequest<any>
33+
34+
describe('constructor', () => {
35+
beforeEach(() => {
36+
request = new TestRequest(SimpleWithPartitionKeyModel)
37+
})
38+
it('should set the default limit to params', () => {
39+
expect(request.params).toBeDefined()
40+
expect(request.params.Limit).toBe(Request.DEFAULT_LIMIT)
41+
})
42+
})
43+
44+
describe('exclusiveStartKey', () => {
45+
beforeEach(() => {
46+
request = new TestRequest(SimpleWithPartitionKeyModel)
47+
})
48+
49+
it('should add ExclusiveStartKey to params', () => {
50+
request.exclusiveStartKey({ id: { S: 'myId' } })
51+
expect(request.params.ExclusiveStartKey).toEqual({ id: { S: 'myId' } })
52+
})
53+
54+
it('should remove ExclusiveStartKey when providing null', () => {
55+
request.exclusiveStartKey({ id: { S: 'myId' } })
56+
expect(request.params.ExclusiveStartKey).toEqual({ id: { S: 'myId' } })
57+
58+
request.exclusiveStartKey(null)
59+
expect(request.params.ExclusiveStartKey).toBeUndefined()
60+
expect(Object.keys(request.params).includes('ExclusiveStartKey')).toBeFalsy()
61+
})
62+
})
63+
64+
describe('index', () => {
65+
beforeEach(() => {
66+
request = new TestRequest(ModelWithABunchOfIndexes)
67+
})
68+
69+
it('[GSI] should set index on params ', () => {
70+
request.index(INDEX_ACTIVE_CREATED_AT)
71+
expect(request.params.IndexName).toBe(INDEX_ACTIVE_CREATED_AT)
72+
})
73+
74+
it('[LSI] should set index on params ', () => {
75+
request.index(INDEX_COUNT)
76+
expect(request.params.IndexName).toBe(INDEX_COUNT)
77+
})
78+
79+
it('should throw when index does not exist on model', () => {
80+
expect(() => request.index('NON_EXISTENT_INDEX')).toThrow()
81+
})
82+
83+
})
84+
85+
describe('limit', () => {
86+
beforeEach(() => {
87+
request = new TestRequest(SimpleWithCompositePartitionKeyModel)
88+
})
89+
90+
it('should throw when a number was given less than 1 (except -1, which means INFINITE)', () => {
91+
expect(() => request.limit(-100)).toThrow()
92+
expect(() => request.limit(0)).toThrow()
93+
})
94+
95+
it('should set limit to params and be overridable', () => {
96+
request.limit(5)
97+
expect(request.params.Limit).toBe(5)
98+
99+
request.limit(10)
100+
expect(request.params.Limit).toBe(10)
101+
})
102+
103+
it('should remove limit when INFINITE_LIMIT was given as arg', () => {
104+
request.limit(5)
105+
expect(request.params.Limit).toBe(5)
106+
request.limit(Request.INFINITE_LIMIT)
107+
expect(request.params.Limit).toBeUndefined()
108+
expect(Object.keys(request.params).includes('Limit')).toBeFalsy()
109+
})
110+
111+
})
112+
113+
})

src/dynamo/request/scan/scan.request.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('scan request', () => {
8686

8787
it('execSingle', async () => {
8888
const res = await scanRequest.execSingle().toPromise()
89-
// fixme
89+
// todo: uncomment when todo in scanRequest::execSingle was done
9090
// expect(scanSpy).toHaveBeenCalled()
9191
// expect(scanSpy.calls.mostRecent().args[0]).toBeDefined()
9292
// expect(scanSpy.calls.mostRecent().args[0].Limit).toBe(1)

src/dynamo/request/scan/scan.request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class ScanRequest<T> extends Request<T, ScanRequest<T>, ScanInput, ScanRe
6666
}
6767

6868
execSingle(): Observable<T | null> {
69+
// todo: add Limit=1 to params
6970
delete this.params.Select
7071
this.logger.debug('single request', this.params)
7172
return this.dynamoRx.scan(this.params).pipe(

0 commit comments

Comments
 (0)