Skip to content

Commit

Permalink
[TINKERPOP-2872] Change JS tests to compare elements by ID for consis…
Browse files Browse the repository at this point in the history
…tency with other GLVs (#2422)

Passes an ID comparator for deep-eql to use when object is type Element, then overwrites chai's members() and isSubsetOf() to use this comparator.
  • Loading branch information
ryn5 authored Jan 11, 2024
1 parent 3a071af commit d2eaf93
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This release also includes changes from <<release-3-6-6, 3.6.6>> and <<release-3
* Fixed bug in `union()` as a start step where the `Path` was including the starting dummy traverser.
* Moved some TinkerGraph specific transaction tests from `TransactionMultiThreadedTest` to `TinkerTransactionGraphTest`
* Fixed incorrect read operations in some cases for `TinkerTransactionGraph`.
* Updated JavaScript tests to check equality on only id and class when comparing elements for consistency with other GLVs.
* Improved performance for Element comparison by comparing hashCode() prior to doing more expensive checks.
==== Bugs
Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,5 @@ The Apache TinkerPop project bundles the following components under the MIT Lice
bootstrap 5.0.0 (http://getbootstrap.com/) - for details, see licenses/bootstrap
jquery 1.11.0 (https://jquery.com/) - for details, see licenses/jquery
normalize.css 2.1.2 (https://necolas.github.io/normalize.css/) - for details, see licenses/normalize
prism.css/js 1.27.0 (https://prismjs.com) - for details, see licenses/prism
wow.js 1.1.2 (https://wowjs.uk/) - for details, see licenses/wow
prism.css/js 1.27.0 (https://prismjs.com/) - for details, see licenses/prism
wow.js 1.1.2 (https://wowjs.uk/) - for details, see licenses/wow
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,12 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.

========================================================================
MIT Licenses
========================================================================

The Apache TinkerPop project bundles the following components under the MIT License:

chai 5.0.0 (https://www.chaijs.com/) - for details, see licenses/chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Chai.js Assertion Library

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Portions of this code are based on the Chai Assertion Library
* at https://www.chaijs.com/, which is licensed under the MIT License.
* The functions deepMembersById, flag, and isSubsetOf are adapted from
* Chai's source code.
* See licenses/chai for full license.
*/

const chai = require('chai');
const deepEqual = require('deep-eql');
const { Edge, Vertex, VertexProperty } = require('../../lib/structure/graph');

function isElement(obj) {
return obj instanceof Edge || obj instanceof Vertex || obj instanceof VertexProperty;
}

const opt = {comparator: compareElements};

function isSubsetOf(subset, superset, cmp, contains, ordered) {
if (!contains) {
if (subset.length !== superset.length) return false;
superset = superset.slice();
}

return subset.every(function(elem, idx) {
if (ordered) return cmp ? cmp(elem, superset[idx], opt) : elem === superset[idx];

if (!cmp) {
var matchIdx = superset.indexOf(elem);
if (matchIdx === -1) return false;

// Remove match from superset so not counted twice if duplicate in subset.
if (!contains) superset.splice(matchIdx, 1);
return true;
}

return superset.some(function(elem2, matchIdx) {
if (!cmp(elem, elem2, opt)) return false;

// Remove match from superset so not counted twice if duplicate in subset.
if (!contains) superset.splice(matchIdx, 1);
return true;
});
});
}

function flag(obj, key, value) {
var flags = obj.__flags || (obj.__flags = Object.create(null));
if (arguments.length === 3) {
flags[key] = value;
} else {
return flags[key];
}
};

function deepMembersById (subset, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new chai.Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
new chai.Assertion(subset, flagMsg, ssfi, true).to.be.an('array');

var contains = flag(this, 'contains');
var ordered = flag(this, 'ordered');

var subject, failMsg, failNegateMsg;

if (contains) {
subject = ordered ? 'an ordered superset' : 'a superset';
failMsg = 'expected #{this} to be ' + subject + ' of #{exp}';
failNegateMsg = 'expected #{this} to not be ' + subject + ' of #{exp}';
} else {
subject = ordered ? 'ordered members' : 'members';
failMsg = 'expected #{this} to have the same ' + subject + ' as #{exp}';
failNegateMsg = 'expected #{this} to not have the same ' + subject + ' as #{exp}';
}

var cmp = flag(this, 'deep') ? deepEqual : undefined;

this.assert(
isSubsetOf(subset, obj, cmp, contains, ordered)
, failMsg
, failNegateMsg
, subset
, obj
, true
);
}

function compareElements(a, b) {
if (!isElement(a) || !isElement(b)) {
return null;
} else {
return a.constructor === b.constructor && a.id === b.id;
}
}

module.exports = {
deepMembersById,
compareElements,
opt
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const t = traversalModule.t;
const P = traversalModule.P;
const direction = traversalModule.direction;
const merge = traversalModule.merge;

const deepMembersById = require('./element-comparison').deepMembersById;
const parsers = [
[ 'str\\[(.*)\\]', (stringValue) => stringValue ], //returns the string value as is
[ 'vp\\[(.+)\\]', toVertexProperty ],
Expand All @@ -64,6 +64,12 @@ const parsers = [
[ 'M\\[(.+)\\]', toMerge ]
].map(x => [ new RegExp('^' + x[0] + '$'), x[1] ]);

chai.use(function (chai, chaiUtils) {
chai.Assertion.overwriteMethod('members', function (_super) {
return deepMembersById;
});
});

const ignoreReason = {
nullKeysInMapNotSupportedWell: "Javascript does not nicely support 'null' as a key in Map instances",
setNotSupported: "There is no Set support in gremlin-javascript",
Expand Down Expand Up @@ -240,7 +246,7 @@ Then(/^the result should be (\w+)$/, function assertResult(characterizedAs, resu
expect(toCompare(this.result)).to.have.deep.ordered.members(expectedResult);
break;
case 'unordered':
expect(toCompare(this.result)).to.have.deep.members(expectedResult);
expect(toCompare(this.result)).to.have.deep.members(toCompare(expectedResult));
break;
case 'of':
// result is a subset of the expected
Expand Down
Loading

0 comments on commit d2eaf93

Please sign in to comment.