Skip to content

Commit

Permalink
refactor e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
Idokah committed Dec 4, 2022
1 parent 97c1dbc commit b75dbe5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 74 deletions.
24 changes: 15 additions & 9 deletions apps/velo-external-db/test/drivers/index_api_rest_matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { IndexFieldOrder, IndexStatus, Index } from "libs/velo-external-db-core/

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


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
})


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

Expand All @@ -28,12 +40,6 @@ export const createIndexResponseWith = (index: Index) => responseWith(({ index:

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
})
export const listIndexResponseWithFailedIndex = (index: Index) => {
return expect.arrayContaining([indexWith(index, { status: IndexStatus.FAILED })])
}
15 changes: 13 additions & 2 deletions apps/velo-external-db/test/drivers/index_api_rest_test_support.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { authOwner } from "@wix-velo/external-db-testkit"
import { streamToArray } from "@wix-velo/test-commons"
import waitUntil from "async-wait-until"
import { CreateIndexRequest, Index, ListIndexesRequest } from "libs/velo-external-db-core/src/spi-model/indexing"

const axios = require('axios').create({
baseURL: 'http://localhost:8080'
})

export const givenIndexes = async(collectionName: string, indexes: Index[], auth: any) => {
export const givenIndexes = async (collectionName: string, indexes: Index[], auth: any) => {
for (const index of indexes) {
await axios.post('/indexes/create', { dataCollectionId: collectionName, index } as CreateIndexRequest, auth)
}
await Promise.all(indexes.map(index => indexCreated(collectionName, index.name, auth)))
}

export const retrieveIndexesFor = async(collectionName: string) => axios.post('/indexes/list', { dataCollectionId: collectionName } as ListIndexesRequest, authOwner)
const indexCreated = async (collectionName: string, indexName: string, auth: any) => {
await waitUntil(async () => {
const indexes = await retrieveIndexesFor(collectionName) as Index[]
return indexes.some(index => index.name === indexName)
})
}

export const retrieveIndexesFor = async (collectionName: string) => axios.post('/indexes/list', { dataCollectionId: collectionName }, {responseType: 'stream', ...authOwner})
.then(response => streamToArray(response.data))
96 changes: 33 additions & 63 deletions apps/velo-external-db/test/e2e/app_index.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ const axiosServer = axios.create({
})


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))

})
}

describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
beforeAll(async () => {
await setupDb()
Expand All @@ -46,24 +28,14 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
test('list', async () => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)

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

// expect(streamToArray(response.data)).
expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWithDefaultIndex())
expect(index.retrieveIndexesFor(ctx.collectionName)).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)

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(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
})

test('create', async () => {
Expand All @@ -76,52 +48,48 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
}, 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]))
})
await eventually(async () =>
await expect(index.retrieveIndexesFor(ctx.collectionName)).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
})
await expect(axiosServer.post('/indexes/create', {
dataCollectionId: ctx.collectionName,
index: ctx.index
}, authOwner)).rejects.toThrow()
})

test.only('remove', async() => {
test('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]))

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

await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.not.toEqual(matchers.listIndexResponseWith([ctx.index]))
})


test('get failed indexes', async () => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)

await axiosServer.post('/indexes/create', {
dataCollectionId: ctx.collectionName,
index: ctx.invalidIndex
}, authOwner).catch(e=>{})


await eventually(async () =>
await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWithFailedIndex(ctx.invalidIndex))
)
})

afterAll(async () => {
await teardownApp()
})
Expand All @@ -130,11 +98,13 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
collectionName: Uninitialized,
column: Uninitialized,
index: Uninitialized,
invalidIndex: Uninitialized,
}

beforeEach(() => {
ctx.collectionName = chance.word()
ctx.column = gen.randomColumn()
ctx.index = gen.spiIndexFor(ctx.collectionName, [ctx.column.name])
ctx.invalidIndex = gen.spiIndexFor(ctx.collectionName, ['wrongColumn'])
})
});

0 comments on commit b75dbe5

Please sign in to comment.