Skip to content

Commit

Permalink
e2e - add eventually tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Idokah committed Dec 4, 2022
1 parent 352a388 commit 97c1dbc
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 39 deletions.
24 changes: 24 additions & 0 deletions apps/velo-external-db/test/drivers/eventually.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as trier from 'trier-promise'

const defaults = {
timeout: 5000,
interval: 200
}

export const eventually = async (fn: any, opts?: { timeout?: number; interval?: number }) => {
return Promise.resolve().then(() => {
let error = null
const action = () => Promise.resolve().then(fn).catch(err => {
error = err
throw err
})
const options = Object.assign({ action }, defaults, opts)

return trier(options).catch(() => {
if (error !== null) {
error.message = `Timeout of ${options.timeout} ms with: ` + error.message
}
throw error
})
})
}
35 changes: 17 additions & 18 deletions apps/velo-external-db/test/drivers/index_api_rest_matchers.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import { IndexFieldOrder, IndexStatus } from "libs/velo-external-db-core/src/spi-model/indexing";

// const responseWith = (matcher: any) => expect.objectContaining({ data: matcher })
import { IndexFieldOrder, IndexStatus, Index } from "libs/velo-external-db-core/src/spi-model/indexing";

const responseWith = (matcher: any) => expect.objectContaining({ data: matcher })

export const listIndexResponseWithDefaultIndex = () =>
expect.arrayContaining([toHaveDefaultIndex()])

// export const listIndexResponseWith = (indexes: any) =>
// expect.arrayContaining(
// [...indexes.map((index: any) => expect.objectContaining({
// ...index,
// status: IndexStatus.ACTIVE
// }))]
// )
export const listIndexResponseWith = (indexes: any) =>
export const listIndexResponseWith = (indexes: Index[]) =>
expect.arrayContaining(
[...indexes.map((index: any) => ({
...index,
status: IndexStatus.ACTIVE,
caseInsensitive: expect.any(Boolean), // TODO: remove this when we support case insensitive indexes
}))]
[...indexes.map((index: Index) => indexWith(index, { status: IndexStatus.ACTIVE }))]
)

export const toHaveDefaultIndex = () => ({
Expand All @@ -36,5 +24,16 @@ export const toHaveDefaultIndex = () => ({
})


// [{"caseInsensitive": false, "fields": [{"order": "ASC", "path": "cebi"}], "name": "dak", "status": 2, "unique": true}]
// {"caseInsensitive": true, "fields": [{"order": "ASC", "path": "cebi"}], "name": "dak", "status": 2, "unique": true}]
export const createIndexResponseWith = (index: Index) => responseWith(({ index: indexWith(index, { status: IndexStatus.BUILDING }) }))

export const removeIndexResponse = () => responseWith(({}))

const indexWith = (index: Index, extraProps: Partial<Index>) => ({
...index,
fields: index.fields.map(field => ({
...field,
order: expect.toBeOneOf([IndexFieldOrder.ASC, IndexFieldOrder.DESC]),
})),
caseInsensitive: expect.any(Boolean), // TODO: remove this when we support case insensitive indexes
...extraProps
})
78 changes: 68 additions & 10 deletions apps/velo-external-db/test/e2e/app_index.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as index from '../drivers/index_api_rest_test_support'
import * as gen from '../gen'
import axios from 'axios'
const chance = new Chance()
import { InputField } from '@wix-velo/velo-external-db-types'
import { eventually } from '../drivers/eventually'

const axiosServer = axios.create({
baseURL: 'http://localhost:8080'
Expand All @@ -19,17 +19,17 @@ export const streamToArray = async (stream) => { //todo: move this to utils

return new Promise((resolve, reject) => {
const arr = []

stream.on('data', data => {
arr.push(JSON.parse(data.toString()))
});

stream.on('end', () => {
resolve(arr)
});

stream.on('error', (err) => reject(err))

})
}

Expand All @@ -48,22 +48,80 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {

const response = await axiosServer.post('/indexes/list', {
dataCollectionId: ctx.collectionName
}, {responseType: 'stream', ...authOwner})
}, { responseType: 'stream', ...authOwner })

await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWithDefaultIndex())
// expect(streamToArray(response.data)).
expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWithDefaultIndex())
})

test('list with multiple indexes', async () => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await index.givenIndexes(ctx.collectionName, [ctx.index], authOwner)

const response = await axiosServer.post('/indexes/list', {
dataCollectionId: ctx.collectionName
}, {responseType: 'stream', ...authOwner})
await eventually(async () => {
const response = await axiosServer.post('/indexes/list', {
dataCollectionId: ctx.collectionName
}, { responseType: 'stream', ...authOwner })
await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
})
})

await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
test('create', async () => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)

// in-progress
await expect(axiosServer.post('/indexes/create', {
dataCollectionId: ctx.collectionName,
index: ctx.index
}, authOwner)).resolves.toEqual(matchers.createIndexResponseWith(ctx.index))

// active
await eventually(async () => {
const response = await axiosServer.post('/indexes/list', {
dataCollectionId: ctx.collectionName
}, { responseType: 'stream', ...authOwner })
await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
})
})

test('create with existing index', async () => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await index.givenIndexes(ctx.collectionName, [ctx.index], authOwner)

eventually(async () => {
await expect(axiosServer.post('/indexes/create', {
dataCollectionId: ctx.collectionName,
index: ctx.index
}, authOwner)).rejects.toThrow()
}, {
timeout: 5000,
interval: 1000
})
})

test.only('remove', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await index.givenIndexes(ctx.collectionName, [ctx.index], authOwner)

await eventually(async () => {
await expect(axiosServer.post('/indexes/remove', {
dataCollectionId: ctx.collectionName,
indexName: ctx.index.name
}, authOwner)).resolves.toEqual(matchers.removeIndexResponse()).catch()
})

// await expect(axiosServer.post('/indexes/remove', {
// dataCollectionId: ctx.collectionName,
// index: ctx.index
// }, authOwner)).resolves.toEqual(matchers.removeIndexResponse())

// const response = await axiosServer.post('/indexes/list', {
// dataCollectionId: ctx.collectionName
// }, { responseType: 'stream', ...authOwner })
// await expect(streamToArray(response.data)).resolves.not.toEqual(matchers.listIndexResponseWith([ctx.index]))
})


afterAll(async () => {
await teardownApp()
})
Expand Down
14 changes: 3 additions & 11 deletions libs/velo-external-db-core/src/service/indexing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ export default class IndexService {

async create(collectionName: string, index: SpiIndex) {
const domainIndex = this.spiIndexToDomainIndex(index)
try {
const createdIndex = await this.storage.create(collectionName, domainIndex)
return this.domainIndexToSpiIndex(createdIndex)
} catch (e: any) {
return {
...index,
status: IndexStatus.FAILED,
error: e.message,
} as SpiIndex
}
const createdIndex = await this.storage.create(collectionName, domainIndex)
return this.domainIndexToSpiIndex(createdIndex)
}

async remove(collectionName: string, indexName: string) {
Expand All @@ -41,7 +33,7 @@ export default class IndexService {
}
}

private spiIndexToDomainIndex(spiIndex: SpiIndex): DomainIndex{
private spiIndexToDomainIndex(spiIndex: SpiIndex): DomainIndex {
return {
name: spiIndex.name,
columns: spiIndex.fields.map(field => field.path),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"pg": "^8.7.3",
"pg-escape": "^0.2.0",
"sqlstring": "^2.3.3",
"trier-promise": "^1.0.1",
"tslib": "^2.3.0",
"tsqlstring": "^1.0.1",
"uuid": "^8.3.2"
Expand Down

0 comments on commit 97c1dbc

Please sign in to comment.