feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-null */
/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -12,7 +13,6 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {asyncPool} from '@krlwlfrt/async-pool/lib/async-pool';
import {
isThing,
SCBulkAddResponse,
@@ -33,16 +33,12 @@ import {expect} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSpies from 'chai-spies';
import clone = require('rfdc');
import {readdir, readFile} from 'fs';
import {suite, test} from '@testdeck/mocha';
import moment from 'moment';
import {join, resolve} from 'path';
import traverse from 'traverse';
import {promisify} from 'util';
import {ConnectorClient} from '../src/connector-client.js';
import {EmptyBulkError, NamespaceNotDefinedError} from '../src/errors.js';
import {HttpClient} from '../src/http-client.js';
import {HttpClientRequest, HttpClientResponse} from '../src/http-client-interface.js';
import {ConnectorClient, EmptyBulkError, NamespaceNotDefinedError, HttpClient, HttpClientRequest, HttpClientResponse} from '../src/index.js';
import path from "path";
import {fileURLToPath} from "url";
import {readdir, readFile} from "fs/promises";
chai.should();
chai.use(chaiSpies);
@@ -55,9 +51,6 @@ const bulkDoneRoute = new SCBulkDoneRoute();
const bulkRoute = new SCBulkRoute();
const thingUpdateRoute = new SCThingUpdateRoute();
const readdirPromisified = promisify(readdir);
const readFilePromisified = promisify(readFile);
const httpClient = new HttpClient();
/**
@@ -76,14 +69,12 @@ function doesContainThings<T extends SCThingWithoutReferences>(thing: T): boolea
}, false);
}
@suite()
export class ConnectorClientSpec {
async after() {
describe('ConnectorClient', function () {
afterEach(function () {
sandbox.restore();
}
});
@test
async bulk() {
it('should bulk', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
return {
body: {
@@ -115,10 +106,9 @@ export class ConnectorClientSpec {
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlPath()),
});
}
});
@test
async bulkWithoutTimeout() {
it('should bulk without timeout', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
return {
body: {
@@ -150,10 +140,9 @@ export class ConnectorClientSpec {
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlPath()),
});
}
});
@test
async index() {
it('should index', async function () {
const messages: SCMessage[] = [
{
audiences: ['employees'],
@@ -240,16 +229,14 @@ export class ConnectorClientSpec {
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlPath()),
});
}
});
@test
async indexFails() {
it('should fail to index', async function () {
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
return connectorClient.index([]).should.be.rejectedWith(EmptyBulkError);
}
await connectorClient.index([]).should.be.rejectedWith(EmptyBulkError);
});
@test
async indexWithoutSource() {
it('should index without source', async function () {
const messages: SCMessage[] = [
{
audiences: ['employees'],
@@ -336,28 +323,25 @@ export class ConnectorClientSpec {
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlPath()),
});
}
});
@test
makeUuid() {
it('should make uuid', async function () {
const uuid = ConnectorClient.makeUUID('foo', 'b-tu');
expect(uuid).to.be.equal('abad271e-d9e9-5802-b7bc-96d8a647b451');
expect(ConnectorClient.makeUUID('bar', 'b-tu')).not.to.be.equal(uuid);
expect(ConnectorClient.makeUUID('foo', 'f-u')).not.to.be.equal(uuid);
}
});
@test
makeUuidFails() {
it('should fail making a uuid', async function (){
expect(() => {
ConnectorClient.makeUUID('foo', 'b-u');
}).to.throw(NamespaceNotDefinedError);
}
});
@test
async removeReferences() {
const pathToTestFiles = resolve(
__dirname,
it('should remove references', async function () {
const pathToTestFiles = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..',
'node_modules',
'@openstapps',
@@ -367,14 +351,14 @@ export class ConnectorClientSpec {
'indexable',
);
const testFiles = await readdirPromisified(pathToTestFiles);
const testFiles = await readdir(pathToTestFiles);
const testInstances = await asyncPool(5, testFiles, async testFile => {
const buffer = await readFilePromisified(join(pathToTestFiles, testFile));
const testInstances = await Promise.all(testFiles.map(async testFile => {
const buffer = await readFile(path.join(pathToTestFiles, testFile));
const content = JSON.parse(buffer.toString());
return content.instance;
});
}));
for (const testInstance of testInstances) {
const checkInstance = clone()(testInstance);
@@ -384,6 +368,7 @@ export class ConnectorClientSpec {
false,
JSON.stringify([testInstance, testInstanceWithoutReferences], null, 2),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((testInstanceWithoutReferences as any).origin).to.be.equal(
undefined,
JSON.stringify([testInstance, testInstanceWithoutReferences], null, 2),
@@ -393,10 +378,9 @@ export class ConnectorClientSpec {
'Removing the references of a thing could have side effects because no deep copy is used',
);
}
}
});
@test
async removeUndefinedProperties() {
it('should remove undefined properties', async function () {
const objectWithUndefinedProperties = {
value: 'foo',
novalue: undefined,
@@ -417,10 +401,9 @@ export class ConnectorClientSpec {
objectWithoutUndefinedProperties,
JSON.stringify([objectWithUndefinedProperties, objectWithoutUndefinedProperties], null, 2),
);
}
});
@test
async update() {
it('should update', async function () {
const message: SCMessage = {
audiences: ['employees'],
categories: ['news'],
@@ -462,5 +445,5 @@ export class ConnectorClientSpec {
}),
),
});
}
}
});
});