Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix patching when used without pre-computed NodeSelections. Close #40 #41

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/common/JSONImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ define([
}

async patch(node, diffs, resolvedSelectors=new NodeSelections()) {
const diffIds = diffs.map(diff => {
return {id: diff.nodeId};
});
await Promise.all(diffIds.map(async diffId => await this.resolveSelectorsForExistingNodes(node, diffId, resolvedSelectors)));
await this.resolveSelectorsFromDiffs(node, diffs, resolvedSelectors);
await this._patch(diffs, resolvedSelectors);
}

Expand Down Expand Up @@ -372,6 +369,17 @@ define([
await this.resolveSelectors(node, state, resolvedSelectors, false);
}

async resolveSelectorsFromDiffs(node, diffs, resolvedSelectors) {
await Promise.all(diffs.map(async diff => {
const selector = new NodeSelector(diff.nodeId);
const parent = await this.core.loadByPath(this.rootNode, diff.parentPath);
const node = await selector.findNode(this.core, this.rootNode, parent, resolvedSelectors);
if(node) {
resolvedSelectors.record(diff.parentPath, selector, node);
}
}));
}

async resolveSelectors(node, state, resolvedSelectors, create=true) {
const parent = this.core.getParent(node);
if(parent) {
Expand Down
86 changes: 85 additions & 1 deletion test/common/JSONImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('JSONImporter', function () {
const _ = testFixture.requirejs('underscore');
const Core = testFixture.requirejs('common/core/coreQ');
const Importer = testFixture.requirejs('webgme-json-importer/JSONImporter');
const {OmittedProperties, NodeSelections} = Importer;
const {OmittedProperties, NodeSelections, NodeChangeSet} = Importer;
const assert = require('assert');
const gmeConfig = testFixture.getGmeConfig();
const path = testFixture.path;
Expand Down Expand Up @@ -1366,6 +1366,90 @@ describe('JSONImporter', function () {
}));
});
});

describe('patch', function () {
let node1, children;
beforeEach(async function () {
node1 = core.createNode({
parent: root,
base: fco
});
children = range(5).map(idx => {
const node = core.createNode({
parent: node1,
base: fco
});
core.setAttribute(node, 'name', `child${idx}`);
return node;
});

});

it('should apply attribute changeset to appropriate node in the subtree (@path)', async () => {
const changeSets = [
new NodeChangeSet(
core.getPath(node1),
core.getPath(children[0]),
'put',
['attributes', 'name'],
'changedNameChild0'
),
new NodeChangeSet(
core.getPath(node1),
core.getPath(children[3]),
'put',
['attributes', 'name'],
'changedNameChild3'
),
];
await importer.patch(node1, changeSets);
assert.equal(core.getAttribute(children[0], 'name'), 'changedNameChild0');
assert.equal(core.getAttribute(children[3], 'name'), 'changedNameChild3');
});

it('should apply attribute changeset to appropriate node in the subtree (@guid)', async () => {
const changeSets = [
new NodeChangeSet(
core.getPath(node1),
core.getGuid(children[0]),
'put',
['attributes', 'name'],
'changedNameChild0'
),
new NodeChangeSet(
core.getPath(node1),
core.getGuid(children[3]),
'put',
['attributes', 'name'],
'changedNameChild3'
),
];

await importer.patch(node1, changeSets);
assert.equal(core.getAttribute(children[0], 'name'), 'changedNameChild0');
assert.equal(core.getAttribute(children[3], 'name'), 'changedNameChild3');
});

it('should apply base pointer changeset to appropriate node in the subtree (@guid)', async () => {
const node2 = core.createNode({
parent: node1,
base: fco
});

core.setAttribute(node2, 'name', 'newName');

const changeSets = [new NodeChangeSet(
core.getPath(node1),
core.getGuid(children[3]),
'put',
['pointers', 'base'],
core.getGuid(node2)
)];

await importer.patch(node1, changeSets);
assert(core.getPointerPath(children[3], 'base') === core.getPath(node2));
});
});
});

function range(size, startAt = 0) {
Expand Down