Setup world and basic steps
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/f80e448a Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/f80e448a Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/f80e448a Branch: refs/heads/TINKERPOP-1489 Commit: f80e448ab3b51835221aa4ce4dec60e5cffbf3e2 Parents: b5d1580 Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> Authored: Thu Nov 23 15:06:17 2017 +0100 Committer: Jorge Bay Gondra <jorgebaygon...@gmail.com> Committed: Mon Jan 8 11:30:46 2018 +0100 ---------------------------------------------------------------------- .../lib/driver/driver-remote-connection.js | 4 +- .../test/cucumber/feature-steps.js | 59 +++++++++++++++--- .../gremlin-javascript/test/cucumber/world.js | 64 +++++++++++++++++++- .../test/integration/traversal-test.js | 6 +- 4 files changed, 119 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f80e448a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js index 18c551c..31f27cd 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js @@ -72,11 +72,11 @@ function DriverRemoteConnection(url, options) { this._responseHandlers = {}; this._reader = options.reader || new serializer.GraphSONReader(); this._writer = options.writer || new serializer.GraphSONWriter(); - this._traversalSource = options.traversalSource || 'g'; this._openPromise = null; this._openCallback = null; this._closePromise = null; this.isOpen = false; + this.traversalSource = options.traversalSource || 'g'; } inherits(DriverRemoteConnection, RemoteConnection); @@ -127,7 +127,7 @@ DriverRemoteConnection.prototype._getRequest = function (id, bytecode) { 'processor': 'traversal', 'args': { 'gremlin': this._writer.adaptObject(bytecode), - 'aliases': { 'g': this._traversalSource } + 'aliases': { 'g': this.traversalSource } } }); }; http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f80e448a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js index bea7321..5270b39 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js @@ -23,13 +23,24 @@ 'use strict'; const defineSupportCode = require('cucumber').defineSupportCode; +const assert = require('assert'); const vm = require('vm'); - +const graphModule = require('../../lib/structure/graph'); +const graphTraversalModule = require('../../lib/process/graph-traversal'); +const traversalModule = require('../../lib/process/traversal'); +const Graph = graphModule.Graph; +const __ = graphTraversalModule.statics; defineSupportCode(function(methods) { methods.Given(/^the (.+) graph$/, function (graphName) { - //TODO: Set context g + if (graphName === 'empty') { + //TODO + } + const data = this.getDataByGraphName(graphName); + this.graphName = graphName; + this.g = new Graph().traversal().withRemote(data.connection) }); + methods.Given('the graph initializer of', function () { //TODO }); @@ -37,7 +48,7 @@ defineSupportCode(function(methods) { methods.Given('an unsupported test', () => {}); methods.Given('the traversal of', function (traversalText) { - //TODO: make traversal + this.traversal = vm.runInNewContext(translate(traversalText), getSandbox(this.g)); }); methods.Given(/^$/, function (paramName, stringValue) { @@ -45,16 +56,24 @@ defineSupportCode(function(methods) { }); methods.When('iterated to list', function () { - //TODO + return this.traversal.toList().then(list => this.result = list); }); methods.When('iterated next', function () { - //TODO + return this.traversal.next().then(it => this.result = it.value); }); methods.Then(/^the result should be (\w+)$/, function (characterizedAs, resultTable) { + switch (characterizedAs) { + case 'empty': + assert.strictEqual(0, result.length); + break; + case 'ordered': + const expectedResult = resultTable.rows().map(parseRow); + console.log('--- ordered', expectedResult); + break; + } //TODO - //console.log('--resultTable', resultTable.rows()); if (typeof resultTable === 'function'){ return resultTable(); } @@ -69,4 +88,30 @@ defineSupportCode(function(methods) { }); methods.Then('nothing should happen because', () => {}); -}); \ No newline at end of file +}); + +function getSandbox(g) { + return { + g: g, + __: __, + Cardinality: traversalModule.cardinality, + Column: traversalModule.column, + Direction: traversalModule.direction, + Order: traversalModule.order, + P: traversalModule.P, + Pick: traversalModule.pick, + Scope: traversalModule.scope, + Operator: traversalModule.operator, + T: traversalModule.t, + }; +} + +function translate(traversalText) { + traversalText = traversalText.replace('.in(', '.in_('); + traversalText = traversalText.replace('.from(', '.from_('); + return traversalText; +} + +function parseRow(row) { + return row[0]; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f80e448a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js index 2e4f6e1..8f72c58 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js @@ -23,17 +23,77 @@ 'use strict'; const defineSupportCode = require('cucumber').defineSupportCode; +const helper = require('../helper'); +const graphModule = require('../../lib/structure/graph'); +const graphTraversalModule = require('../../lib/process/graph-traversal'); +const Graph = graphModule.Graph; +const __ = graphTraversalModule.statics; defineSupportCode(function (methods) { + const cache = {}; + function TinkerPopWorld(){ this.g = null; this.traversal = null; + this.result = null; + this.cache = null; } + + TinkerPopWorld.prototype.getDataByGraphName = function (name) { + return this.cache[name]; + }; + methods.setWorldConstructor(TinkerPopWorld); + methods.BeforeAll(function () { // load all traversals + const promises = ['modern', 'classic', 'crew', 'grateful'].map(graphName => { + const connection = helper.getConnection('g' + graphName); + return connection.open() + .then(() => Promise.all([getVertices(connection), getEdges(connection)])) + .then(values => { + cache[graphName] = { + connection: connection, + vertices: values[0], + edges: values[1] + }; + }); + }); + return Promise.all(promises); }); - methods.Before(function () { + methods.AfterAll(function () { + return Promise.all(Object.keys(cache).map(graphName => cache[graphName].connection.close())); }); -}); \ No newline at end of file + + methods.Before(function () { + this.cache = cache; + }); +}); + +function getVertices(connection) { + const g = new Graph().traversal().withRemote(connection); + return g.V().group().by('name').by(__.tail()).next().then(it => it.value); +} + +function getEdges(connection) { + const g = new Graph().traversal().withRemote(connection); + return g.E().group() + .by(__.project("o", "l", "i").by(__.outV().values("name")).by(__.inV().values("name"))) + .by(__.tail()) + .next() + .then(it => { + const edges = {}; + Object.keys(it.value).map(key => { + edges[getEdgeKey(key)] = it.value[key]; + }); + return edges; + }); +} + +function getEdgeKey(key) { + const o = /o=(.+?)[,}]/.exec(key)[1]; + const l = /l=(.+?)[,}]/.exec(key)[1]; + const i = /i=(.+?)[,}]/.exec(key)[1]; + return o + "-" + l + "->" + i; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f80e448a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js index 96de7c7..adb7d6c 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js @@ -42,10 +42,10 @@ describe('Traversal', function () { describe('#toList()', function () { it('should submit the traversal and return a list', function () { var g = new Graph().traversal().withRemote(connection); - return g.addV('user').toList().then(function (list) { + return g.V().toList().then(function (list) { assert.ok(list); - assert.strictEqual(list.length, 1); - assert.ok(list[0] instanceof Vertex); + assert.strictEqual(list.length, 7); + list.forEach(v => assert.ok(v instanceof Vertex)); }); }); });