Skip to content

Commit

Permalink
indexing service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Idokah committed Dec 6, 2022
1 parent 714eaaf commit 83b7423
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
81 changes: 81 additions & 0 deletions libs/velo-external-db-core/src/service/indexing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Chance = require('chance')
import { Uninitialized } from '@wix-velo/test-commons'
import { DomainIndex } from '@wix-velo/velo-external-db-types'
import * as gen from '../../test/gen'
import IndexService from './indexing'
import * as driver from '../../test/drivers/index_provider_test_support'
import { Index as SpiIndex, IndexField, IndexStatus } from '../spi-model/indexing'
const chance = new Chance()

describe('Index Service', () => {
describe('transformers', () => {
test('domainIndexToSpiIndex', () => {
expect(env.indexService['domainIndexToSpiIndex'](ctx.index)).toEqual({
name: ctx.index.name,
fields: ctx.index.columns.map(column => ({ path: column, order: ctx.index.order })) as IndexField[],
unique: ctx.index.isUnique,
caseInsensitive: ctx.index.caseInsensitive,
status: IndexStatus[ctx.index.status as keyof typeof IndexStatus],
})
})

test('spiIndexToDomainIndex', () => {
expect(env.indexService['spiIndexToDomainIndex'](ctx.spiIndex)).toEqual({
name: ctx.spiIndex.name,
columns: ctx.spiIndex.fields.map(field => field.path),
isUnique: ctx.spiIndex.unique,
caseInsensitive: ctx.spiIndex.caseInsensitive,
order: ctx.spiIndex.fields[0].order,
})
})
})

test('list will issue a call to list and translate data to spi format', () => {
driver.givenListResult(ctx.indexes, ctx.collectionName)

return expect(env.indexService.list(ctx.collectionName)).resolves.toEqual(ctx.indexes.map(env.indexService['domainIndexToSpiIndex']))
})

test('create will issue a call to create and translate data to spi format', () => {
driver.givenCreateResult(ctx.index, ctx.collectionName)

return expect(env.indexService.create(ctx.collectionName, env.indexService['domainIndexToSpiIndex'](ctx.index)))
.resolves.toEqual(env.indexService['domainIndexToSpiIndex'](ctx.index))
})

test('remove will issue a call to remove', () => {
driver.givenRemoveResult(ctx.collectionName, ctx.index.name)

return expect(env.indexService.remove(ctx.collectionName, ctx.index.name)).resolves.toEqual({})
})


const ctx: {
collectionName: string
indexes: DomainIndex[],
index: DomainIndex,
spiIndex: SpiIndex
} = {
collectionName: Uninitialized,
indexes: Uninitialized,
index: Uninitialized,
spiIndex: Uninitialized,
}
const env: {
indexService: IndexService
} = {
indexService: Uninitialized
}

beforeAll(() => {
env.indexService = new IndexService(driver.indexProvider)
ctx.collectionName = chance.word()
ctx.indexes = gen.randomArrayOf(gen.randomDomainIndex)
ctx.index = gen.randomDomainIndex()
ctx.spiIndex = gen.randomSpiIndex()
})

afterEach(() => {
driver.reset()
})
})
2 changes: 1 addition & 1 deletion libs/velo-external-db-core/src/service/indexing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DomainIndex, IIndexProvider } from '@wix-velo/velo-external-db-types'
import { Index as SpiIndex, IndexField, IndexFieldOrder, IndexStatus } from '../spi-model/indexing'
import { Index as SpiIndex, IndexField, IndexStatus } from '../spi-model/indexing'

export default class IndexService {
storage: IIndexProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DomainIndex } from '@wix-velo/velo-external-db-types'
import { when } from 'jest-when'

export const indexProvider = {
list: jest.fn(),
create: jest.fn(),
remove: jest.fn(),
}

export const givenListResult = (indexes: DomainIndex[], collectionName: string) => {
when(indexProvider.list).calledWith(collectionName).mockResolvedValue(indexes)
}

export const givenCreateResult = (index: DomainIndex, collectionName: string) => {
const {status, ...indexWithoutStatus} = index
when(indexProvider.create).calledWith(collectionName, indexWithoutStatus).mockResolvedValue(index)
}

export const reset = () => {
indexProvider.list.mockReset()
indexProvider.create.mockReset()
indexProvider.remove.mockReset()
}


export function givenRemoveResult(collectionName: string, name: string) {
when(indexProvider.remove).calledWith(collectionName, name).mockResolvedValue({})
}
21 changes: 21 additions & 0 deletions libs/velo-external-db-core/test/gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as Chance from 'chance'
import { AdapterOperators } from '@wix-velo/velo-external-db-commons'
import { gen as genCommon } from '@wix-velo/test-commons'
import { DomainIndex, DomainIndexStatus } from '@wix-velo/velo-external-db-types'
import { Index as SpiIndex } from '../src/spi-model/indexing'

const chance = Chance()

Expand Down Expand Up @@ -70,3 +72,22 @@ export const randomBodyWith = (obj: any) => ({
...genCommon.randomObject(),
...obj
})

export const randomDomainIndex = (): DomainIndex => ({
name: chance.word(),
columns: randomArrayOf(() => chance.word()),
isUnique: chance.bool(),
caseInsensitive: chance.bool(),
order: chance.pickone(['ASC', 'DESC']),
status: DomainIndexStatus.ACTIVE,
})

export const randomSpiIndex = (): SpiIndex => ({
name: chance.word(),
fields: randomArrayOf(() => ({
name: chance.word(),
order: chance.pickone(['ASC', 'DESC']),
})),
unique: chance.bool(),
caseInsensitive: chance.bool(),
})

0 comments on commit 83b7423

Please sign in to comment.