|
| 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 | +}) |
0 commit comments