From b75dbe57fffe78e796a7731ea5327fc314f93ffb Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Sun, 4 Dec 2022 16:50:12 +0200 Subject: [PATCH] refactor e2e --- .../test/drivers/index_api_rest_matchers.ts | 24 +++-- .../drivers/index_api_rest_test_support.ts | 15 ++- .../test/e2e/app_index.e2e.spec.ts | 96 +++++++------------ 3 files changed, 61 insertions(+), 74 deletions(-) diff --git a/apps/velo-external-db/test/drivers/index_api_rest_matchers.ts b/apps/velo-external-db/test/drivers/index_api_rest_matchers.ts index 95fab4768..4c04ce0be 100644 --- a/apps/velo-external-db/test/drivers/index_api_rest_matchers.ts +++ b/apps/velo-external-db/test/drivers/index_api_rest_matchers.ts @@ -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, + 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()]) @@ -28,12 +40,6 @@ export const createIndexResponseWith = (index: Index) => responseWith(({ index: export const removeIndexResponse = () => responseWith(({})) -const indexWith = (index: Index, extraProps: Partial) => ({ - ...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 })]) +} diff --git a/apps/velo-external-db/test/drivers/index_api_rest_test_support.ts b/apps/velo-external-db/test/drivers/index_api_rest_test_support.ts index ff7a1f8ea..691bdb471 100644 --- a/apps/velo-external-db/test/drivers/index_api_rest_test_support.ts +++ b/apps/velo-external-db/test/drivers/index_api_rest_test_support.ts @@ -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) \ No newline at end of file +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)) \ No newline at end of file diff --git a/apps/velo-external-db/test/e2e/app_index.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_index.e2e.spec.ts index e307502a9..6ca17d1f1 100644 --- a/apps/velo-external-db/test/e2e/app_index.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_index.e2e.spec.ts @@ -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() @@ -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 () => { @@ -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() }) @@ -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']) }) }); \ No newline at end of file