feat: add method to remove references from a thing

Fixes #6
This commit is contained in:
Karl-Philipp Wulfert
2019-04-02 18:04:38 +02:00
parent a3b16b8a37
commit 9cf6fde050
4 changed files with 142 additions and 2 deletions

View File

@@ -12,7 +12,9 @@
* 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';
import {
isThing,
SCBulkAddResponse,
SCBulkAddRoute,
SCBulkDoneResponse,
@@ -20,17 +22,22 @@ import {
SCBulkResponse,
SCBulkRoute,
SCMessage,
SCThing,
SCThingOriginType,
SCThingType,
SCThingUpdateResponse,
SCThingUpdateRoute,
SCThingType,
SCThingOriginType,
} from '@openstapps/core';
import * as chai from 'chai';
import {expect} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies';
import {readdir, readFile} from 'fs';
import {suite, test} from 'mocha-typescript';
import * as moment from 'moment';
import {join, resolve} from 'path';
import * as traverse from 'traverse';
import {promisify} from 'util';
import {ConnectorClient} from '../src/connectorClient';
import {EmptyBulkError, NamespaceNotDefinedError} from '../src/errors';
import {HttpClient} from '../src/httpClient';
@@ -47,8 +54,27 @@ const bulkDoneRoute = new SCBulkDoneRoute();
const bulkRoute = new SCBulkRoute();
const thingUpdateRoute = new SCThingUpdateRoute();
const readdirPromisified = promisify(readdir);
const readFilePromisified = promisify(readFile);
const httpClient = new HttpClient();
/**
* Check if something contains things
*
* @param thing Thing to check
*/
function doesContainThings<T extends SCThing>(thing: T): boolean {
/* tslint:disable-next-line:only-arrow-functions */
return traverse(thing).reduce(function(sum, item) {
if (this.isRoot) {
return false;
}
return sum || (item === null) ? false : isThing(item);
}, false);
}
@suite()
export class ConnectorClientSpec {
async after() {
@@ -305,6 +331,36 @@ export class ConnectorClientSpec {
}).to.throw(NamespaceNotDefinedError);
}
@test
async removeReferences() {
const pathToTestFiles = resolve(
__dirname,
'..',
'node_modules',
'@openstapps',
'core',
'test',
'resources',
);
const testFiles = await readdirPromisified(pathToTestFiles);
const testInstances = await asyncPool(5, testFiles, async (testFile) => {
const buffer = await readFilePromisified(join(pathToTestFiles, testFile));
const content = JSON.parse(buffer.toString());
return content.instance;
});
for (const testInstance of testInstances) {
const testInstanceWithoutReferences = ConnectorClient.removeReferences(testInstance);
expect(doesContainThings(testInstanceWithoutReferences)).to.be.equal(false, JSON.stringify(
[testInstance, testInstanceWithoutReferences],
null,
2,
));
}
}
@test
async update() {
const message: SCMessage = {