Skip to content

Commit

Permalink
Add index provider test support and generate random domain and SPI in…
Browse files Browse the repository at this point in the history
…dexes
  • Loading branch information
MXPOL committed Mar 6, 2024
1 parent 8001e1d commit cf1f1c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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({})
}
22 changes: 22 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 } from '../src/spi-model/indexing'
import {
CollectionCapabilities,
CollectionOperation,
Expand Down Expand Up @@ -108,3 +110,23 @@ 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 = (): Index => ({
name: chance.word(),
fields: randomArrayOf(() => ({
name: chance.word(),
order: chance.pickone(['ASC', 'DESC']),
})),
unique: chance.bool(),
caseInsensitive: chance.bool(),
})

0 comments on commit cf1f1c2

Please sign in to comment.