Skip to content

Commit 83b7423

Browse files
committed
indexing service tests
1 parent 714eaaf commit 83b7423

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Chance = require('chance')
2+
import { Uninitialized } from '@wix-velo/test-commons'
3+
import { DomainIndex } from '@wix-velo/velo-external-db-types'
4+
import * as gen from '../../test/gen'
5+
import IndexService from './indexing'
6+
import * as driver from '../../test/drivers/index_provider_test_support'
7+
import { Index as SpiIndex, IndexField, IndexStatus } from '../spi-model/indexing'
8+
const chance = new Chance()
9+
10+
describe('Index Service', () => {
11+
describe('transformers', () => {
12+
test('domainIndexToSpiIndex', () => {
13+
expect(env.indexService['domainIndexToSpiIndex'](ctx.index)).toEqual({
14+
name: ctx.index.name,
15+
fields: ctx.index.columns.map(column => ({ path: column, order: ctx.index.order })) as IndexField[],
16+
unique: ctx.index.isUnique,
17+
caseInsensitive: ctx.index.caseInsensitive,
18+
status: IndexStatus[ctx.index.status as keyof typeof IndexStatus],
19+
})
20+
})
21+
22+
test('spiIndexToDomainIndex', () => {
23+
expect(env.indexService['spiIndexToDomainIndex'](ctx.spiIndex)).toEqual({
24+
name: ctx.spiIndex.name,
25+
columns: ctx.spiIndex.fields.map(field => field.path),
26+
isUnique: ctx.spiIndex.unique,
27+
caseInsensitive: ctx.spiIndex.caseInsensitive,
28+
order: ctx.spiIndex.fields[0].order,
29+
})
30+
})
31+
})
32+
33+
test('list will issue a call to list and translate data to spi format', () => {
34+
driver.givenListResult(ctx.indexes, ctx.collectionName)
35+
36+
return expect(env.indexService.list(ctx.collectionName)).resolves.toEqual(ctx.indexes.map(env.indexService['domainIndexToSpiIndex']))
37+
})
38+
39+
test('create will issue a call to create and translate data to spi format', () => {
40+
driver.givenCreateResult(ctx.index, ctx.collectionName)
41+
42+
return expect(env.indexService.create(ctx.collectionName, env.indexService['domainIndexToSpiIndex'](ctx.index)))
43+
.resolves.toEqual(env.indexService['domainIndexToSpiIndex'](ctx.index))
44+
})
45+
46+
test('remove will issue a call to remove', () => {
47+
driver.givenRemoveResult(ctx.collectionName, ctx.index.name)
48+
49+
return expect(env.indexService.remove(ctx.collectionName, ctx.index.name)).resolves.toEqual({})
50+
})
51+
52+
53+
const ctx: {
54+
collectionName: string
55+
indexes: DomainIndex[],
56+
index: DomainIndex,
57+
spiIndex: SpiIndex
58+
} = {
59+
collectionName: Uninitialized,
60+
indexes: Uninitialized,
61+
index: Uninitialized,
62+
spiIndex: Uninitialized,
63+
}
64+
const env: {
65+
indexService: IndexService
66+
} = {
67+
indexService: Uninitialized
68+
}
69+
70+
beforeAll(() => {
71+
env.indexService = new IndexService(driver.indexProvider)
72+
ctx.collectionName = chance.word()
73+
ctx.indexes = gen.randomArrayOf(gen.randomDomainIndex)
74+
ctx.index = gen.randomDomainIndex()
75+
ctx.spiIndex = gen.randomSpiIndex()
76+
})
77+
78+
afterEach(() => {
79+
driver.reset()
80+
})
81+
})

libs/velo-external-db-core/src/service/indexing.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DomainIndex, IIndexProvider } from '@wix-velo/velo-external-db-types'
2-
import { Index as SpiIndex, IndexField, IndexFieldOrder, IndexStatus } from '../spi-model/indexing'
2+
import { Index as SpiIndex, IndexField, IndexStatus } from '../spi-model/indexing'
33

44
export default class IndexService {
55
storage: IIndexProvider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { DomainIndex } from '@wix-velo/velo-external-db-types'
2+
import { when } from 'jest-when'
3+
4+
export const indexProvider = {
5+
list: jest.fn(),
6+
create: jest.fn(),
7+
remove: jest.fn(),
8+
}
9+
10+
export const givenListResult = (indexes: DomainIndex[], collectionName: string) => {
11+
when(indexProvider.list).calledWith(collectionName).mockResolvedValue(indexes)
12+
}
13+
14+
export const givenCreateResult = (index: DomainIndex, collectionName: string) => {
15+
const {status, ...indexWithoutStatus} = index
16+
when(indexProvider.create).calledWith(collectionName, indexWithoutStatus).mockResolvedValue(index)
17+
}
18+
19+
export const reset = () => {
20+
indexProvider.list.mockReset()
21+
indexProvider.create.mockReset()
22+
indexProvider.remove.mockReset()
23+
}
24+
25+
26+
export function givenRemoveResult(collectionName: string, name: string) {
27+
when(indexProvider.remove).calledWith(collectionName, name).mockResolvedValue({})
28+
}

libs/velo-external-db-core/test/gen.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Chance from 'chance'
22
import { AdapterOperators } from '@wix-velo/velo-external-db-commons'
33
import { gen as genCommon } from '@wix-velo/test-commons'
4+
import { DomainIndex, DomainIndexStatus } from '@wix-velo/velo-external-db-types'
5+
import { Index as SpiIndex } from '../src/spi-model/indexing'
46

57
const chance = Chance()
68

@@ -70,3 +72,22 @@ export const randomBodyWith = (obj: any) => ({
7072
...genCommon.randomObject(),
7173
...obj
7274
})
75+
76+
export const randomDomainIndex = (): DomainIndex => ({
77+
name: chance.word(),
78+
columns: randomArrayOf(() => chance.word()),
79+
isUnique: chance.bool(),
80+
caseInsensitive: chance.bool(),
81+
order: chance.pickone(['ASC', 'DESC']),
82+
status: DomainIndexStatus.ACTIVE,
83+
})
84+
85+
export const randomSpiIndex = (): SpiIndex => ({
86+
name: chance.word(),
87+
fields: randomArrayOf(() => ({
88+
name: chance.word(),
89+
order: chance.pickone(['ASC', 'DESC']),
90+
})),
91+
unique: chance.bool(),
92+
caseInsensitive: chance.bool(),
93+
})

0 commit comments

Comments
 (0)