Skip to content

Commit

Permalink
fix(search): update our flakey search tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed Nov 26, 2024
1 parent 9b814d5 commit 966a752
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .docker/aws-resources/elasticsearch/esindex_corpus_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1,
"number_of_replicas": 0,
"knn": true
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1,
"number_of_shards": 1,
"number_of_replicas": 0,
"knn": true
}
},
Expand Down
2 changes: 1 addition & 1 deletion .docker/aws-resources/elasticsearch/esindex_corpus_es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1,
"number_of_replicas": 0,
"knn": true
}
},
Expand Down
2 changes: 1 addition & 1 deletion .docker/aws-resources/elasticsearch/esindex_corpus_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1,
"number_of_replicas": 0,
"knn": true
}
},
Expand Down
2 changes: 1 addition & 1 deletion .docker/aws-resources/elasticsearch/esindex_corpus_it.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1,
"number_of_replicas": 0,
"knn": true
}
},
Expand Down
4 changes: 2 additions & 2 deletions .docker/aws-resources/elasticsearch/esindex_list.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"settings": {
"index": {
"number_of_shards": 10,
"number_of_replicas": 2
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ services:
start_period: 80s

localstack:
image: localstack/localstack:3.7.1@sha256:7f4f51bbfcfb42c661b9c02716a3cfcb53433f3c066905329aa911178835b300
image: localstack/localstack:4.0.2
ports:
- '4566:4566'
volumes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Corpus search - keyword', () => {
beforeAll(async () => {
await deleteDocuments();
await seedCorpus();
await unleash(
unleash(
mockFlags([
{ name: config.unleash.flags.semanticSearch.name, enabled: false },
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Corpus search - semantic', () => {
const clientMock: any = jest.spyOn(Client.prototype, 'search');

beforeAll(async () => {
await unleash(
unleash(
mockFlags([
{ name: config.unleash.flags.semanticSearch.name, enabled: true },
]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { advancedSearch } from './elasticsearchSearch';
import { bulkDocument } from './elasticsearchBulk';
import { client } from './index';
import { config } from '../../config';
import { faker } from '@faker-js/faker';
import {
SearchItemsContentType,
SearchItemsSortBy,
SearchItemsSortOrder,
SearchItemsStatusFilter,
} from '../../__generated__/types';
import { deleteDocuments } from '../../test/utils/searchIntegrationTestHelpers';
import { client } from '.';
import { config } from '../../config';

const defaultDocProps = {
resolved_id: 1,
Expand All @@ -25,20 +26,7 @@ const defaultDocProps = {

describe('Elasticsearch Search Query', () => {
beforeEach(async () => {
await client.deleteByQuery({
index: config.aws.elasticsearch.list.index,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});

await deleteDocuments();
await bulkDocument([
{
...defaultDocProps,
Expand Down Expand Up @@ -100,9 +88,14 @@ describe('Elasticsearch Search Query', () => {
},
]);

// wait for 1 second. I noticed test wasn't passing if run took quickly after inserting. There may be more to do
// here to make sure this does not become brittle.
await new Promise((resolve) => setTimeout(resolve, 1000));
// Wait for index to finish
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
});

afterAll(async () => {
await deleteDocuments();
});
it.each([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { client } from './index';

import { bulkDocument } from './elasticsearchBulk';
import { getDocument } from './elasticsearchSearch';
import { deleteDocuments } from '../../test/utils/searchIntegrationTestHelpers';

const defaultDocProps = {
resolved_id: 1,
Expand All @@ -19,19 +20,7 @@ const defaultDocProps = {

describe('Elasticsearch Bulk', () => {
beforeAll(async () => {
await client.deleteByQuery({
index: config.aws.elasticsearch.list.index,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
await deleteDocuments();

await bulkDocument([
{
Expand All @@ -51,6 +40,10 @@ describe('Elasticsearch Bulk', () => {
});
});

afterAll(async () => {
await deleteDocuments();
});

it('can bulk index a document', async () => {
const document = (await getDocument('1-12345')) as any;
expect(document).not.toBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../__generated__/types';
import { client } from './index';
import { config } from '../../config';
import { deleteDocuments } from '../../test/utils/searchIntegrationTestHelpers';

const defaultDocProps = {
resolved_id: 1,
Expand All @@ -23,19 +24,7 @@ const defaultDocProps = {

describe('Elasticsearch Search Query', () => {
beforeEach(async () => {
await client.deleteByQuery({
index: config.aws.elasticsearch.list.index,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
await deleteDocuments();

await bulkDocument([
{
Expand Down Expand Up @@ -139,6 +128,10 @@ describe('Elasticsearch Search Query', () => {
});
});

afterAll(async () => {
await deleteDocuments();
});

// For now this gives us confidence that basic search is not
// broken when we make updates
it('can search for a document', async () => {
Expand Down Expand Up @@ -330,8 +323,8 @@ describe('Elasticsearch Search Query', () => {
expect(response.pageInfo.hasPreviousPage).toBeFalse();
expect(response.edges.length).toBe(2);
expect(response.edges[0].cursor).toBe('MA==');
expect(response.edges[0].node.savedItem.id).toBe(456);
expect(response.edges[1].node.savedItem.id).toBe(123);
expect(response.edges[0].node.savedItem.id).toBe(123);
expect(response.edges[1].node.savedItem.id).toBe(789);
expect(response.edges[1].cursor).toBe('MQ==');
}, 10000);

Expand Down
15 changes: 2 additions & 13 deletions servers/user-list-search/src/saves/elasticsearch.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { client } from '../datasource/elasticsearch';
import { config } from '../config';
import { bulkDocument } from '../datasource/elasticsearch/elasticsearchBulk';
import { deleteSearchIndexByUserId } from './elasticsearch';
import { deleteDocuments } from '../test/utils/searchIntegrationTestHelpers';

const defaultDocProps = {
resolved_id: 1,
Expand All @@ -17,19 +18,7 @@ const defaultDocProps = {

describe('Elasticsearch - Integration', () => {
beforeEach(async () => {
await client.deleteByQuery({
index: config.aws.elasticsearch.list.index,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
await deleteDocuments();

await bulkDocument([
{
Expand Down
22 changes: 5 additions & 17 deletions servers/user-list-search/src/saves/premiumSearch.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../datasource/clients/knexClient';
import { Knex } from 'knex';
import {
deleteDocuments,
loadItemExtended,
loadList,
} from '../test/utils/searchIntegrationTestHelpers';
Expand Down Expand Up @@ -76,7 +77,6 @@ async function loadUserItem(
}

describe('premium search functional test', () => {
const testEsClient = client;
let app: Application;
let server: ApolloServer<ContextManager>;
let url: string;
Expand Down Expand Up @@ -129,20 +129,7 @@ describe('premium search functional test', () => {

beforeAll(async () => {
({ app, server, url } = await startServer(0));
await testEsClient.deleteByQuery({
index: config.aws.elasticsearch.list.index,
type: config.aws.elasticsearch.list.type,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await testEsClient.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
await deleteDocuments();

await db('readitla_ril-tmp.list').truncate();
await db('readitla_b.items_extended').truncate();
Expand Down Expand Up @@ -236,7 +223,7 @@ describe('premium search functional test', () => {
await Promise.all(items);

// Takes a hot sec for the data to be available, otherwise test flakes
await testEsClient.indices.refresh({
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
});
Expand All @@ -245,7 +232,8 @@ describe('premium search functional test', () => {
await server.stop();
await db.destroy();
await knexDbWriteClient().destroy();
testEsClient.close();
await deleteDocuments();
client.close();
});

it('should search for Items under User entity', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../datasource/clients/knexClient';
import { Knex } from 'knex';
import {
deleteDocuments,
loadItemExtended,
loadList,
} from '../test/utils/searchIntegrationTestHelpers';
Expand Down Expand Up @@ -78,7 +79,6 @@ async function loadUserItem(
}

describe('premium search functional test (offset pagination)', () => {
const testEsClient = client;
let app: Application;
let server: ApolloServer<ContextManager>;
let url: string;
Expand Down Expand Up @@ -124,19 +124,7 @@ describe('premium search functional test (offset pagination)', () => {

beforeAll(async () => {
({ app, server, url } = await startServer(0));
await testEsClient.deleteByQuery({
index: config.aws.elasticsearch.list.index,
body: {
query: {
match_all: {},
},
},
});

// Wait for delete to finish
await testEsClient.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
await deleteDocuments();

await db('readitla_ril-tmp.list').truncate();
await db('readitla_b.items_extended').truncate();
Expand Down Expand Up @@ -228,7 +216,7 @@ describe('premium search functional test (offset pagination)', () => {
];
await Promise.all(items);
// Takes a hot sec for the data to be available, otherwise test flakes
await testEsClient.indices.refresh({
await client.indices.refresh({
index: config.aws.elasticsearch.list.index,
});
});
Expand All @@ -237,7 +225,8 @@ describe('premium search functional test (offset pagination)', () => {
await server.stop();
await db.destroy();
await knexDbWriteClient().destroy();
testEsClient.close();
await deleteDocuments();
client.close();
});

it('should handle response that returns hits with no highlights object', async () => {
Expand Down
Loading

0 comments on commit 966a752

Please sign in to comment.