fix: backend tests break every year

refactor: update some backend unit tests
This commit is contained in:
2024-01-03 12:57:24 +01:00
parent d65e6351e9
commit 123c50d1af
9 changed files with 77 additions and 52 deletions

View File

@@ -0,0 +1,5 @@
---
"@openstapps/backend": patch
---
Backend unit tests break every year

View File

@@ -17,7 +17,7 @@ export const backend = {
hiddenTypes: [SCThingType.DateSeries, SCThingType.Diff, SCThingType.Floor],
mappingIgnoredTags: ['minlength', 'pattern', 'see', 'tjs-format'],
maxMultiSearchRouteQueries: 5,
maxRequestBodySize: 2 * 10 ** 6,
maxRequestBodySize: 2e6,
name: 'Goethe-Universität Frankfurt am Main',
namespace: '909a8cbc-8520-456c-b474-ef1525f14209',
sortableFields: [

View File

@@ -14,6 +14,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCBook,
SCBulkAddRoute,
SCBulkDoneRoute,
SCBulkRequest,
@@ -23,29 +24,30 @@ import {
import {expect} from 'chai';
import {bulk, DEFAULT_TEST_TIMEOUT} from '../common.js';
import {testApp} from '../tests-setup.js';
import {readFile} from 'fs/promises';
import {v4} from 'uuid';
import bookFile from '@openstapps/core/test/resources/indexable/Book.2.json' assert {type: 'json'};
const book = JSON.parse(
await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.2.json', 'utf8'),
).instance;
const book = bookFile.instance as SCBook;
describe('Bulk routes', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const request: SCBulkRequest = {
expiration: bulk.expiration,
source: bulk.source,
type: bulk.type,
};
const bulkRoute = new SCBulkRoute();
const bulkAddRoute = new SCBulkAddRoute();
const bulkDoneRoute = new SCBulkDoneRoute();
let request: SCBulkRequest;
let bulkRoute: SCBulkRoute;
let bulkAddRoute: SCBulkAddRoute;
let bulkDoneRoute: SCBulkDoneRoute;
// afterEach(async function() {
// TODO: Delete saved bulks
// });
before(function () {
request = {
expiration: bulk.expiration,
source: bulk.source,
type: bulk.type,
};
bulkRoute = new SCBulkRoute();
bulkAddRoute = new SCBulkAddRoute();
bulkDoneRoute = new SCBulkDoneRoute();
});
it('should create bulk', async function () {
const {status, body, error} = await testApp

View File

@@ -21,7 +21,12 @@ import {expect} from 'chai';
describe('Index route', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const indexRoute = new SCIndexRoute();
let indexRoute: SCIndexRoute;
before(function () {
indexRoute = new SCIndexRoute();
});
it('should respond with both app and backend configuration', async function () {
const request: SCIndexRequest = {};

View File

@@ -30,15 +30,11 @@ import chaiAsPromised from 'chai-as-promised';
import {DEFAULT_TEST_TIMEOUT} from '../common.js';
import {testApp} from '../tests-setup.js';
import {backendConfig} from '../../src/config.js';
import {readFile} from 'fs/promises';
import registerRequest from '@openstapps/core/test/resources/PluginRegisterRequest.1.json' assert {type: 'json'};
// for using promises in expectations (to.eventually.be...)
use(chaiAsPromised);
const registerRequest = JSON.parse(
await readFile('node_modules/@openstapps/core/test/resources/PluginRegisterRequest.1.json', 'utf8'),
);
// cast it because of "TS2322: Type 'string' is not assignable to type '"add"'"
export const registerAddRequest: SCPluginAdd = registerRequest.instance as SCPluginAdd;

View File

@@ -47,8 +47,8 @@ describe('Create route', async function () {
const statusCodeSuccess = 222;
const bodySuccess = {foo: true};
const sandbox = sinon.createSandbox();
const validationError = new SCValidationErrorResponse([]);
const internalServerError = new SCInternalServerErrorResponse();
let validationError: SCValidationErrorResponse;
let internalServerError: SCInternalServerErrorResponse;
beforeEach(function () {
app = express();
@@ -64,6 +64,9 @@ describe('Create route', async function () {
handler = (_request, _app) => {
return Promise.resolve(bodySuccess);
};
validationError = new SCValidationErrorResponse([]);
internalServerError = new SCInternalServerErrorResponse();
});
afterEach(function () {

View File

@@ -29,11 +29,19 @@ import {backendConfig} from '../../src/config.js';
describe('Search route', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const searchRoute = new SCSearchRoute();
const multiSearchRoute = new SCMultiSearchRoute();
const syntaxError = new SCSyntaxErrorResponse('Foo Message');
const methodNotAllowedError = new SCMethodNotAllowedErrorResponse();
const tooManyRequestsError = new SCTooManyRequestsErrorResponse();
let searchRoute: SCSearchRoute;
let multiSearchRoute: SCMultiSearchRoute;
let syntaxError: SCSyntaxErrorResponse;
let methodNotAllowedError: SCMethodNotAllowedErrorResponse;
let tooManyRequestsError: SCTooManyRequestsErrorResponse;
before(function () {
searchRoute = new SCSearchRoute();
multiSearchRoute = new SCMultiSearchRoute();
syntaxError = new SCSyntaxErrorResponse('Foo Message');
methodNotAllowedError = new SCMethodNotAllowedErrorResponse();
tooManyRequestsError = new SCTooManyRequestsErrorResponse();
});
it('should reject GET, PUT with a valid search query', async function () {
// const expectedParams = JSON.parse(JSON.stringify(defaultParams));

View File

@@ -13,23 +13,25 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThingUpdateRoute} from '@openstapps/core';
import {SCBook, SCThingUpdateRoute} from '@openstapps/core';
import chaiAsPromised from 'chai-as-promised';
import {bulkStorageMock, DEFAULT_TEST_TIMEOUT} from '../common.js';
import {expect, use} from 'chai';
import {testApp} from '../tests-setup.js';
import {readFile} from 'fs/promises';
import bookFile from '@openstapps/core/test/resources/indexable/Book.1.json' assert {type: 'json'};
use(chaiAsPromised);
const book = JSON.parse(
await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.1.json', 'utf8'),
).instance;
const book = bookFile.instance as SCBook;
describe('Thing update route', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const thingUpdateRoute = new SCThingUpdateRoute();
let thingUpdateRoute: SCThingUpdateRoute;
before(function () {
thingUpdateRoute = new SCThingUpdateRoute();
});
it('should update a thing', async function () {
const thingUpdateRouteurlPath = thingUpdateRoute.urlPath

View File

@@ -39,7 +39,6 @@ import {Elasticsearch} from '../../../src/storage/elasticsearch/elasticsearch.js
import {bulk, DEFAULT_TEST_TIMEOUT, getTransport, getIndex} from '../../common.js';
import fs from 'fs';
import {backendConfig} from '../../../src/config.js';
import {readFile} from 'fs/promises';
import {
ACTIVE_INDICES_ALIAS,
getIndexUID,
@@ -50,6 +49,11 @@ import {
} from '../../../src/storage/elasticsearch/util/index.js';
import cron from 'node-cron';
import {query} from './query.js';
import messageFile from '@openstapps/core/test/resources/indexable/Message.1.json' assert {type: 'json'};
import bookFile from '@openstapps/core/test/resources/indexable/Book.1.json' assert {type: 'json'};
const message = messageFile.instance as SCMessage;
const book = bookFile.instance as SCBook;
use(chaiAsPromised);
@@ -60,13 +64,6 @@ function searchResponse<T>(...hits: SearchHit<T>[]): SearchResponse<T> {
return {hits: {hits}, took: 0, timed_out: false, _shards: {total: 1, failed: 0, successful: 1}};
}
const message = JSON.parse(
await readFile('node_modules/@openstapps/core/test/resources/indexable/Message.1.json', 'utf8'),
);
const book = JSON.parse(
await readFile('node_modules/@openstapps/core/test/resources/indexable/Book.1.json', 'utf8'),
);
describe('Elasticsearch', function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
@@ -74,8 +71,15 @@ describe('Elasticsearch', function () {
before(function () {
// eslint-disable-next-line no-console
console.log('before');
sandbox.stub(fs, 'readFileSync').returns('{}');
sandbox.stub(backendConfig.internal.boostings.default[0], 'fields').value({
'academicTerms.acronym': {
'SS 2023': 1.05,
'WS 2023/24': 1.1,
'SoSe 2023': 1.05,
'WiSe 2023/24': 1.1,
},
});
});
after(function () {
sandbox.restore();
@@ -445,7 +449,7 @@ describe('Elasticsearch', function () {
_id: '',
_index: '',
_score: 0,
_source: message as SCMessage,
_source: message,
};
sandbox.stub(es.client, 'search').resolves(searchResponse(foundObject));
@@ -475,7 +479,7 @@ describe('Elasticsearch', function () {
const object: SearchHit<SCMessage> = {
_id: '',
_index: oldIndex,
_source: message as SCMessage,
_source: message,
};
sandbox.stub(es.client, 'search').resolves(searchResponse<SCMessage>(object));
sandbox.stub(es, 'prepareBulkWrite').resolves(index);
@@ -489,7 +493,7 @@ describe('Elasticsearch', function () {
sandbox.stub(es.client, 'create').resolves({result: 'not_found'} as CreateResponse);
await es.init();
return expect(es.post(message as SCMessage, bulk)).to.rejectedWith('creation');
return expect(es.post(message, bulk)).to.rejectedWith('creation');
});
it('should create a new object', async function () {
@@ -502,7 +506,7 @@ describe('Elasticsearch', function () {
});
await es.init();
await es.post(message as SCMessage, bulk);
await es.post(message, bulk);
expect(createStub.called).to.be.true;
expect(caughtParameter.document).to.be.eql({
@@ -527,7 +531,7 @@ describe('Elasticsearch', function () {
_id: '',
_index: getIndex(),
_score: 0,
_source: message as SCMessage,
_source: message,
};
sandbox.stub(es.client, 'search').resolves(searchResponse());
@@ -541,7 +545,7 @@ describe('Elasticsearch', function () {
_id: '',
_index: getIndex(),
_score: 0,
_source: message as SCMessage,
_source: message,
};
sandbox.stub(es.client, 'search').resolves(searchResponse(object));
// @ts-expect-error unused
@@ -564,13 +568,13 @@ describe('Elasticsearch', function () {
_id: '123',
_index: getIndex(),
_score: 0,
_source: message as SCMessage,
_source: message,
};
const objectBook: SearchHit<SCBook> = {
_id: '321',
_index: getIndex(),
_score: 0,
_source: book as SCBook,
_source: book,
};
const fakeEsAggregations = {
'@all': {