mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 01:22:54 +00:00
Resolve "Transition to ESLint"
This commit is contained in:
committed by
Rainer Killinger
parent
ca1d2444e0
commit
418ba67d15
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any,unicorn/no-null */
|
||||
/*
|
||||
* Copyright (C) 2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -14,7 +15,15 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {ApiResponse, Client} from '@elastic/elasticsearch';
|
||||
import {SCBook, SCBulkResponse, SCConfigFile, SCMessage, SCSearchQuery, SCThings, SCThingType} from '@openstapps/core';
|
||||
import {
|
||||
SCBook,
|
||||
SCBulkResponse,
|
||||
SCConfigFile,
|
||||
SCMessage,
|
||||
SCSearchQuery,
|
||||
SCThings,
|
||||
SCThingType,
|
||||
} from '@openstapps/core';
|
||||
import {instance as book} from '@openstapps/core/test/resources/indexable/Book.1.json';
|
||||
import {instance as message} from '@openstapps/core/test/resources/indexable/Message.1.json';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
@@ -43,6 +52,7 @@ describe('Elasticsearch', function () {
|
||||
const sandbox = sinon.createSandbox();
|
||||
|
||||
before(function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('before');
|
||||
sandbox.stub(fs, 'readFileSync').returns('{}');
|
||||
});
|
||||
@@ -54,7 +64,7 @@ describe('Elasticsearch', function () {
|
||||
it('should provide custom elasticsearch URL if defined', function () {
|
||||
const customAddress = 'http://foo-address:9200';
|
||||
const restore = mockedEnv({
|
||||
ES_ADDR: customAddress
|
||||
ES_ADDR: customAddress,
|
||||
});
|
||||
|
||||
expect(Elasticsearch.getElasticsearchUrl()).to.be.equal(customAddress);
|
||||
@@ -64,7 +74,7 @@ describe('Elasticsearch', function () {
|
||||
|
||||
it('should provide local URL as fallback', function () {
|
||||
const restore = mockedEnv({
|
||||
ES_ADDR: undefined
|
||||
ES_ADDR: undefined,
|
||||
});
|
||||
|
||||
expect(Elasticsearch.getElasticsearchUrl()).to.match(/(https?:\/\/)?localhost(:\d+)?/);
|
||||
@@ -81,7 +91,7 @@ describe('Elasticsearch', function () {
|
||||
source: '',
|
||||
state: 'in progress',
|
||||
type: SCThingType.Semester,
|
||||
uid: 'bulk-uid-123-123-123'
|
||||
uid: 'bulk-uid-123-123-123',
|
||||
};
|
||||
|
||||
it('should provide index UID from the provided UID', function () {
|
||||
@@ -94,8 +104,9 @@ describe('Elasticsearch', function () {
|
||||
});
|
||||
|
||||
it('should provide index name from the provided data', function () {
|
||||
expect(Elasticsearch.getIndex(type as SCThingType, source, bulk))
|
||||
.to.be.equal(`stapps_${type.split(' ').join('_')}_${source}_${Elasticsearch.getIndexUID(bulk.uid)}`);
|
||||
expect(Elasticsearch.getIndex(type as SCThingType, source, bulk)).to.be.equal(
|
||||
`stapps_${type.split(' ').join('_')}_${source}_${Elasticsearch.getIndexUID(bulk.uid)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -109,7 +120,7 @@ describe('Elasticsearch', function () {
|
||||
});
|
||||
|
||||
it('should remove invalid characters', function () {
|
||||
expect(Elasticsearch.removeAliasChars('f,o#o\\b|ar/<?al\ias>* ', 'bulk-uid')).to.be.equal('foobaralias');
|
||||
expect(Elasticsearch.removeAliasChars('f,o#o\\b|ar/<?alias>* ', 'bulk-uid')).to.be.equal('foobaralias');
|
||||
});
|
||||
|
||||
it('should remove invalid starting characters', function () {
|
||||
@@ -124,10 +135,12 @@ describe('Elasticsearch', function () {
|
||||
});
|
||||
|
||||
it('should work with common cases', function () {
|
||||
expect(Elasticsearch.removeAliasChars('the-quick-brown-fox-jumps-over-the-lazy-dog-1234567890', 'bulk-uid'))
|
||||
.to.be.equal('the-quick-brown-fox-jumps-over-the-lazy-dog-1234567890');
|
||||
expect(Elasticsearch.removeAliasChars('THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG', 'bulk-uid'))
|
||||
.to.be.equal('THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG');
|
||||
expect(
|
||||
Elasticsearch.removeAliasChars('the-quick-brown-fox-jumps-over-the-lazy-dog-1234567890', 'bulk-uid'),
|
||||
).to.be.equal('the-quick-brown-fox-jumps-over-the-lazy-dog-1234567890');
|
||||
expect(
|
||||
Elasticsearch.removeAliasChars('THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG', 'bulk-uid'),
|
||||
).to.be.equal('THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG');
|
||||
});
|
||||
|
||||
it('should warn in case of characters that are invalid in future elasticsearch versions', function () {
|
||||
@@ -158,16 +171,16 @@ describe('Elasticsearch', function () {
|
||||
...configFile.internal,
|
||||
database: {
|
||||
name: 'foo',
|
||||
version: 123
|
||||
}
|
||||
}
|
||||
version: 123,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(() => new Elasticsearch(config)).to.throw(Error);
|
||||
});
|
||||
|
||||
it('should log an error in case of there is one when getting response from the elasticsearch client', async function () {
|
||||
const error = Error('Foo Error');
|
||||
const error = new Error('Foo Error');
|
||||
const loggerErrorStub = sandbox.stub(Logger, 'error').resolves('foo');
|
||||
sandbox.stub(Client.prototype, 'on').yields(error);
|
||||
|
||||
@@ -185,7 +198,7 @@ describe('Elasticsearch', function () {
|
||||
expect(loggerLogStub.calledWith(fakeResponse)).to.be.false;
|
||||
|
||||
const restore = mockedEnv({
|
||||
'ES_DEBUG': 'true',
|
||||
ES_DEBUG: 'true',
|
||||
});
|
||||
new Elasticsearch(configFile);
|
||||
|
||||
@@ -208,8 +221,8 @@ describe('Elasticsearch', function () {
|
||||
monitoring: {
|
||||
actions: [],
|
||||
watchers: [],
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const es = new Elasticsearch(config);
|
||||
@@ -225,8 +238,8 @@ describe('Elasticsearch', function () {
|
||||
monitoring: {
|
||||
actions: [],
|
||||
watchers: [],
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
const monitoringSetUpStub = sandbox.stub(Monitoring, 'setUp');
|
||||
|
||||
@@ -245,22 +258,21 @@ describe('Elasticsearch', function () {
|
||||
|
||||
beforeEach(function () {
|
||||
es = new Elasticsearch(configFile);
|
||||
// @ts-ignore
|
||||
es.client.indices = {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
getAlias: () => Promise.resolve({body: [{[oldIndex]: {aliases: {[SCThingType.Book]: {}}}}]}),
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
putTemplate: () => Promise.resolve({}),
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
create: () => Promise.resolve({}),
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
delete: () => Promise.resolve({}),
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
exists: () => Promise.resolve({}),
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
refresh: () => Promise.resolve({}),
|
||||
// @ts-ignore
|
||||
updateAliases: () => Promise.resolve({})
|
||||
// @ts-expect-error not assignable
|
||||
updateAliases: () => Promise.resolve({}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -340,13 +352,13 @@ describe('Elasticsearch', function () {
|
||||
},
|
||||
{
|
||||
remove: {index: oldIndex, alias: SCThingType.Book},
|
||||
}
|
||||
},
|
||||
];
|
||||
sandbox.stub(Elasticsearch, 'getIndex').returns(index);
|
||||
sandbox.stub(es, 'aliasMap').value({
|
||||
[SCThingType.Book]: {
|
||||
[bulk.source]: oldIndex,
|
||||
}
|
||||
},
|
||||
});
|
||||
const refreshStub = sandbox.stub(es.client.indices, 'refresh');
|
||||
const updateAliasesStub = sandbox.stub(es.client.indices, 'updateAliases');
|
||||
@@ -357,11 +369,12 @@ describe('Elasticsearch', function () {
|
||||
await es.bulkUpdated(bulk);
|
||||
|
||||
expect(refreshStub.calledWith({index})).to.be.true;
|
||||
expect(updateAliasesStub.calledWith({
|
||||
expect(
|
||||
updateAliasesStub.calledWith({
|
||||
body: {
|
||||
actions: expectedRefreshActions
|
||||
}
|
||||
})
|
||||
actions: expectedRefreshActions,
|
||||
},
|
||||
}),
|
||||
).to.be.true;
|
||||
expect(deleteStub.called).to.be.true;
|
||||
});
|
||||
@@ -392,7 +405,7 @@ describe('Elasticsearch', function () {
|
||||
_index: '',
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: [foundObject]}}});
|
||||
|
||||
@@ -420,7 +433,7 @@ describe('Elasticsearch', function () {
|
||||
_index: oldIndex,
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: [object]}}});
|
||||
sandbox.stub(Elasticsearch, 'getIndex').returns(index);
|
||||
@@ -434,7 +447,7 @@ describe('Elasticsearch', function () {
|
||||
_index: getIndex(),
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: [object]}}});
|
||||
// return index name with different generated UID (see getIndex method)
|
||||
@@ -451,18 +464,21 @@ describe('Elasticsearch', function () {
|
||||
});
|
||||
|
||||
it('should create a new object', async function () {
|
||||
let caughtParam: any;
|
||||
let caughtParameter: any;
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: []}}});
|
||||
// @ts-ignore
|
||||
let createStub = sandbox.stub(es.client, 'create').callsFake((param) => {
|
||||
caughtParam = param;
|
||||
// @ts-expect-error call
|
||||
const createStub = sandbox.stub(es.client, 'create').callsFake(parameter => {
|
||||
caughtParameter = parameter;
|
||||
return Promise.resolve({body: {created: true}});
|
||||
});
|
||||
|
||||
await es.post(message as SCMessage, bulk);
|
||||
|
||||
expect(createStub.called).to.be.true;
|
||||
expect(caughtParam.body).to.be.eql({...message, creation_date: caughtParam.body.creation_date});
|
||||
expect(caughtParameter.body).to.be.eql({
|
||||
...message,
|
||||
creation_date: caughtParameter.body.creation_date,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -482,32 +498,34 @@ describe('Elasticsearch', function () {
|
||||
_index: getIndex(),
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: []}}});
|
||||
|
||||
return expect(es.put(object._source)).to.rejectedWith('exist');
|
||||
});
|
||||
|
||||
// noinspection JSUnusedLocalSymbols
|
||||
it('should update the object if it already exists', async function () {
|
||||
let caughtParam: any;
|
||||
let caughtParameter: any;
|
||||
const object: ElasticsearchObject<SCMessage> = {
|
||||
_id: '',
|
||||
_index: getIndex(),
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
sandbox.stub(es.client, 'search').resolves({body: {hits: {hits: [object]}}});
|
||||
// @ts-ignore
|
||||
const stubUpdate = sandbox.stub(es.client, 'update').callsFake((params) => {
|
||||
caughtParam = params;
|
||||
// @ts-expect-error unused
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const stubUpdate = sandbox.stub(es.client, 'update').callsFake(parameters => {
|
||||
caughtParameter = parameters;
|
||||
return Promise.resolve({body: {created: true}});
|
||||
});
|
||||
|
||||
await es.put(object._source);
|
||||
|
||||
expect(caughtParam.body.doc).to.be.eql(object._source);
|
||||
expect(caughtParameter.body.doc).to.be.eql(object._source);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -519,14 +537,14 @@ describe('Elasticsearch', function () {
|
||||
_index: getIndex(),
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: message as SCMessage
|
||||
_source: message as SCMessage,
|
||||
};
|
||||
const objectBook: ElasticsearchObject<SCBook> = {
|
||||
_id: '321',
|
||||
_index: getIndex(),
|
||||
_score: 0,
|
||||
_type: '',
|
||||
_source: book as SCBook
|
||||
_source: book as SCBook,
|
||||
};
|
||||
const fakeEsAggregations = {
|
||||
'@all': {
|
||||
@@ -537,40 +555,36 @@ describe('Elasticsearch', function () {
|
||||
buckets: [
|
||||
{
|
||||
key: 'person',
|
||||
doc_count: 13
|
||||
doc_count: 13,
|
||||
},
|
||||
{
|
||||
key: 'catalog',
|
||||
doc_count: 4
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
doc_count: 4,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const fakeSearchResponse: Partial<ApiResponse<SearchResponse<SCThings>>> = {
|
||||
// @ts-ignore
|
||||
body: {
|
||||
took: 12,
|
||||
timed_out: false,
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
_shards: {},
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
hits: {
|
||||
hits: [
|
||||
objectMessage,
|
||||
objectBook,
|
||||
],
|
||||
total: 123
|
||||
hits: [objectMessage, objectBook],
|
||||
total: 123,
|
||||
},
|
||||
aggregations: fakeEsAggregations
|
||||
aggregations: fakeEsAggregations,
|
||||
},
|
||||
headers: {},
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
meta: {},
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
statusCode: {},
|
||||
// @ts-ignore
|
||||
warnings: {}
|
||||
// @ts-expect-error not assignable
|
||||
warnings: {},
|
||||
};
|
||||
let searchStub: sinon.SinonStub;
|
||||
before(function () {
|
||||
@@ -593,11 +607,11 @@ describe('Elasticsearch', function () {
|
||||
},
|
||||
{
|
||||
count: 4,
|
||||
key: 'catalog'
|
||||
}
|
||||
key: 'catalog',
|
||||
},
|
||||
],
|
||||
field: 'type',
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
const {data, facets} = await es.search({});
|
||||
@@ -613,7 +627,7 @@ describe('Elasticsearch', function () {
|
||||
expect(pagination).to.be.eql({
|
||||
count: fakeSearchResponse.body!.hits.hits.length,
|
||||
offset: from,
|
||||
total: fakeSearchResponse.body!.hits.total
|
||||
total: fakeSearchResponse.body!.hits.total,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -624,7 +638,7 @@ describe('Elasticsearch', function () {
|
||||
});
|
||||
|
||||
it('should build the search request properly', async function () {
|
||||
const params: SCSearchQuery = {
|
||||
const parameters: SCSearchQuery = {
|
||||
query: 'mathematics',
|
||||
from: 30,
|
||||
size: 5,
|
||||
@@ -632,42 +646,37 @@ describe('Elasticsearch', function () {
|
||||
{
|
||||
type: 'ducet',
|
||||
order: 'desc',
|
||||
arguments:
|
||||
{
|
||||
field: 'name'
|
||||
}
|
||||
}
|
||||
arguments: {
|
||||
field: 'name',
|
||||
},
|
||||
},
|
||||
],
|
||||
filter: {
|
||||
type: 'value',
|
||||
arguments: {
|
||||
field: 'type',
|
||||
value: SCThingType.AcademicEvent
|
||||
}
|
||||
}
|
||||
value: SCThingType.AcademicEvent,
|
||||
},
|
||||
},
|
||||
};
|
||||
const fakeResponse = {foo: 'bar'};
|
||||
const fakeBuildSortResponse = [fakeResponse];
|
||||
// @ts-ignore
|
||||
// @ts-expect-error not assignable
|
||||
sandbox.stub(query, 'buildQuery').returns(fakeResponse);
|
||||
// @ts-ignore
|
||||
sandbox.stub(query, 'buildSort').returns(fakeBuildSortResponse);
|
||||
|
||||
await es.search(params);
|
||||
await es.search(parameters);
|
||||
|
||||
sandbox.assert
|
||||
.calledWithMatch(searchStub,
|
||||
{
|
||||
body: {
|
||||
aggs: aggregations,
|
||||
query: fakeResponse,
|
||||
sort: fakeBuildSortResponse
|
||||
},
|
||||
from: params.from,
|
||||
index: Elasticsearch.getListOfAllIndices(),
|
||||
size: params.size,
|
||||
}
|
||||
);
|
||||
sandbox.assert.calledWithMatch(searchStub, {
|
||||
body: {
|
||||
aggs: aggregations,
|
||||
query: fakeResponse,
|
||||
sort: fakeBuildSortResponse,
|
||||
},
|
||||
from: parameters.from,
|
||||
index: Elasticsearch.getListOfAllIndices(),
|
||||
size: parameters.size,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user