Skip to content

Commit 9c9cec4

Browse files
committed
refactor e2e
1 parent cb3ea78 commit 9c9cec4

File tree

3 files changed

+61
-74
lines changed

3 files changed

+61
-74
lines changed

apps/velo-external-db/test/drivers/index_api_rest_matchers.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { IndexFieldOrder, IndexStatus, Index } from "libs/velo-external-db-core/
22

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

5+
6+
const indexWith = (index: Index, extraProps: Partial<Index>) => ({
7+
...index,
8+
fields: index.fields.map(field => ({
9+
...field,
10+
order: expect.toBeOneOf([IndexFieldOrder.ASC, IndexFieldOrder.DESC]),
11+
})),
12+
caseInsensitive: expect.any(Boolean), // TODO: remove this when we support case insensitive indexes
13+
...extraProps
14+
})
15+
16+
517
export const listIndexResponseWithDefaultIndex = () =>
618
expect.arrayContaining([toHaveDefaultIndex()])
719

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

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

31-
const indexWith = (index: Index, extraProps: Partial<Index>) => ({
32-
...index,
33-
fields: index.fields.map(field => ({
34-
...field,
35-
order: expect.toBeOneOf([IndexFieldOrder.ASC, IndexFieldOrder.DESC]),
36-
})),
37-
caseInsensitive: expect.any(Boolean), // TODO: remove this when we support case insensitive indexes
38-
...extraProps
39-
})
43+
export const listIndexResponseWithFailedIndex = (index: Index) => {
44+
return expect.arrayContaining([indexWith(index, { status: IndexStatus.FAILED })])
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { authOwner } from "@wix-velo/external-db-testkit"
2+
import { streamToArray } from "@wix-velo/test-commons"
3+
import waitUntil from "async-wait-until"
24
import { CreateIndexRequest, Index, ListIndexesRequest } from "libs/velo-external-db-core/src/spi-model/indexing"
35

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

8-
export const givenIndexes = async(collectionName: string, indexes: Index[], auth: any) => {
10+
export const givenIndexes = async (collectionName: string, indexes: Index[], auth: any) => {
911
for (const index of indexes) {
1012
await axios.post('/indexes/create', { dataCollectionId: collectionName, index } as CreateIndexRequest, auth)
1113
}
14+
await Promise.all(indexes.map(index => indexCreated(collectionName, index.name, auth)))
1215
}
1316

14-
export const retrieveIndexesFor = async(collectionName: string) => axios.post('/indexes/list', { dataCollectionId: collectionName } as ListIndexesRequest, authOwner)
17+
const indexCreated = async (collectionName: string, indexName: string, auth: any) => {
18+
await waitUntil(async () => {
19+
const indexes = await retrieveIndexesFor(collectionName) as Index[]
20+
return indexes.some(index => index.name === indexName)
21+
})
22+
}
23+
24+
export const retrieveIndexesFor = async (collectionName: string) => axios.post('/indexes/list', { dataCollectionId: collectionName }, {responseType: 'stream', ...authOwner})
25+
.then(response => streamToArray(response.data))

apps/velo-external-db/test/e2e/app_index.e2e.spec.ts

+33-63
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ const axiosServer = axios.create({
1515
})
1616

1717

18-
export const streamToArray = async (stream) => { //todo: move this to utils
19-
20-
return new Promise((resolve, reject) => {
21-
const arr = []
22-
23-
stream.on('data', data => {
24-
arr.push(JSON.parse(data.toString()))
25-
});
26-
27-
stream.on('end', () => {
28-
resolve(arr)
29-
});
30-
31-
stream.on('error', (err) => reject(err))
32-
33-
})
34-
}
35-
3618
describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
3719
beforeAll(async () => {
3820
await setupDb()
@@ -46,24 +28,14 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
4628
test('list', async () => {
4729
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
4830

49-
const response = await axiosServer.post('/indexes/list', {
50-
dataCollectionId: ctx.collectionName
51-
}, { responseType: 'stream', ...authOwner })
52-
53-
// expect(streamToArray(response.data)).
54-
expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWithDefaultIndex())
31+
expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWithDefaultIndex())
5532
})
5633

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

61-
await eventually(async () => {
62-
const response = await axiosServer.post('/indexes/list', {
63-
dataCollectionId: ctx.collectionName
64-
}, { responseType: 'stream', ...authOwner })
65-
await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
66-
})
38+
await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
6739
})
6840

6941
test('create', async () => {
@@ -76,52 +48,48 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
7648
}, authOwner)).resolves.toEqual(matchers.createIndexResponseWith(ctx.index))
7749

7850
// active
79-
await eventually(async () => {
80-
const response = await axiosServer.post('/indexes/list', {
81-
dataCollectionId: ctx.collectionName
82-
}, { responseType: 'stream', ...authOwner })
83-
await expect(streamToArray(response.data)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
84-
})
51+
await eventually(async () =>
52+
await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWith([ctx.index]))
53+
)
8554
})
8655

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

91-
eventually(async () => {
92-
await expect(axiosServer.post('/indexes/create', {
93-
dataCollectionId: ctx.collectionName,
94-
index: ctx.index
95-
}, authOwner)).rejects.toThrow()
96-
}, {
97-
timeout: 5000,
98-
interval: 1000
99-
})
60+
await expect(axiosServer.post('/indexes/create', {
61+
dataCollectionId: ctx.collectionName,
62+
index: ctx.index
63+
}, authOwner)).rejects.toThrow()
10064
})
10165

102-
test.only('remove', async() => {
66+
test('remove', async () => {
10367
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
10468
await index.givenIndexes(ctx.collectionName, [ctx.index], authOwner)
105-
106-
await eventually(async () => {
107-
await expect(axiosServer.post('/indexes/remove', {
108-
dataCollectionId: ctx.collectionName,
109-
indexName: ctx.index.name
110-
}, authOwner)).resolves.toEqual(matchers.removeIndexResponse()).catch()
111-
})
112-
113-
// await expect(axiosServer.post('/indexes/remove', {
114-
// dataCollectionId: ctx.collectionName,
115-
// index: ctx.index
116-
// }, authOwner)).resolves.toEqual(matchers.removeIndexResponse())
117-
118-
// const response = await axiosServer.post('/indexes/list', {
119-
// dataCollectionId: ctx.collectionName
120-
// }, { responseType: 'stream', ...authOwner })
121-
// await expect(streamToArray(response.data)).resolves.not.toEqual(matchers.listIndexResponseWith([ctx.index]))
69+
70+
await expect(axiosServer.post('/indexes/remove', {
71+
dataCollectionId: ctx.collectionName,
72+
indexName: ctx.index.name
73+
}, authOwner)).resolves.toEqual(matchers.removeIndexResponse()).catch()
74+
75+
await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.not.toEqual(matchers.listIndexResponseWith([ctx.index]))
12276
})
12377

12478

79+
test('get failed indexes', async () => {
80+
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
81+
82+
await axiosServer.post('/indexes/create', {
83+
dataCollectionId: ctx.collectionName,
84+
index: ctx.invalidIndex
85+
}, authOwner).catch(e=>{})
86+
87+
88+
await eventually(async () =>
89+
await expect(index.retrieveIndexesFor(ctx.collectionName)).resolves.toEqual(matchers.listIndexResponseWithFailedIndex(ctx.invalidIndex))
90+
)
91+
})
92+
12593
afterAll(async () => {
12694
await teardownApp()
12795
})
@@ -130,11 +98,13 @@ describe(`Velo External DB Index API: ${currentDbImplementationName()}`, () => {
13098
collectionName: Uninitialized,
13199
column: Uninitialized,
132100
index: Uninitialized,
101+
invalidIndex: Uninitialized,
133102
}
134103

135104
beforeEach(() => {
136105
ctx.collectionName = chance.word()
137106
ctx.column = gen.randomColumn()
138107
ctx.index = gen.spiIndexFor(ctx.collectionName, [ctx.column.name])
108+
ctx.invalidIndex = gen.spiIndexFor(ctx.collectionName, ['wrongColumn'])
139109
})
140110
});

0 commit comments

Comments
 (0)