From bd50d5c5f000648889454e373d1ae0ccffa642e2 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Fri, 17 May 2013 17:16:32 +0200 Subject: [PATCH] GraphViewer: Added Tests for joining of community nodes --- .../js/graphViewer/graph/arangoAdapter.js | 17 +++- .../admin/js/graphViewer/graph/nodeReducer.js | 15 ++-- .../specAdapter/arangoAdapterSpec.js | 77 ++++++++++++++++--- .../jasmine_test/specAdapter/interfaceSpec.js | 2 +- 4 files changed, 91 insertions(+), 20 deletions(-) diff --git a/html/admin/js/graphViewer/graph/arangoAdapter.js b/html/admin/js/graphViewer/graph/arangoAdapter.js index d52ec8efa0..a47a2f058c 100644 --- a/html/admin/js/graphViewer/graph/arangoAdapter.js +++ b/html/admin/js/graphViewer/graph/arangoAdapter.js @@ -54,6 +54,7 @@ function ArangoAdapter(nodes, edges, config) { queries = {}, nodeCollection, edgeCollection, + limit, reducer, arangodb, width, @@ -267,7 +268,11 @@ function ArangoAdapter(nodes, edges, config) { }); }); if (callback) { - callback(result[0].vertex); + var n = insertNode(result[0].vertex); + if (limit < nodes.length) { + reducer.getCommunity(limit, n); + } + callback(n); } }, @@ -534,8 +539,14 @@ function ArangoAdapter(nodes, edges, config) { api.edge = api.base + "edge?collection=" + edgeCollection; }; - self.setNodeLimit = function (limit) { - + self.setNodeLimit = function (pLimit, callback) { + limit = pLimit; + if (limit < nodes.length) { + reducer.getCommunity(limit); + if (callback !== undefined) { + callback(); + } + } }; } \ No newline at end of file diff --git a/html/admin/js/graphViewer/graph/nodeReducer.js b/html/admin/js/graphViewer/graph/nodeReducer.js index f0ae7421db..53b5e5e884 100644 --- a/html/admin/js/graphViewer/graph/nodeReducer.js +++ b/html/admin/js/graphViewer/graph/nodeReducer.js @@ -287,18 +287,21 @@ function NodeReducer(nodes, edges) { populateValues(dQ, a, heap); var max = 0; while (communityDetectionStep(dQ, a, heap, coms)) { + // InfiniteLoopCheck should be removed! max++; if (max > 100) { break; - //console.log(max); } } - //console.log(max); res = _.pluck(_.values(coms), "com"); - dist = floatDist(focus._id); - res = res.filter(function(e) {return !_.contains(e, focus._id)}); - res = res.filter(function(e) {return e.length > 1}); - res.sort(sortByDistance); + if (focus !== undefined) { + dist = floatDist(focus._id); + res = res.filter(function(e) {return !_.contains(e, focus._id)}); + res = res.filter(function(e) {return e.length > 1}); + res.sort(sortByDistance); + } else { + res = res.filter(function(e) {return e.length > 1}); + } return res[0]; }; } diff --git a/html/admin/js/graphViewer/jasmine_test/specAdapter/arangoAdapterSpec.js b/html/admin/js/graphViewer/jasmine_test/specAdapter/arangoAdapterSpec.js index a636195a27..2692f2a5c3 100644 --- a/html/admin/js/graphViewer/jasmine_test/specAdapter/arangoAdapterSpec.js +++ b/html/admin/js/graphViewer/jasmine_test/specAdapter/arangoAdapterSpec.js @@ -39,12 +39,12 @@ "use strict"; describe('Arango Adapter', function () { - /* + describeInterface(new ArangoAdapter([], [], { nodeCollection: "", edgeCollection: "" })); - */ + var adapter, nodes, edges, @@ -271,7 +271,20 @@ loadGraph, requests; - beforeEach(function() { + beforeEach(function() { + var self = this; + self.fakeReducerRequest = function() {}; + spyOn(window, "NodeReducer").andCallFake(function(v, e) { + return { + getCommunity: function(limit, focus) { + if (focus !== undefined) { + self.fakeReducerRequest(limit, focus); + } else { + self.fakeReducerRequest(limit); + } + } + }; + }); adapter = new ArangoAdapter( nodes, edges, @@ -420,7 +433,6 @@ }); }); - it('should be able to load a tree node from ArangoDB' + ' by internal attribute and value', function() { @@ -613,11 +625,6 @@ beforeEach(function() { runs(function() { - var self = this; - this.fakeReducerRequest = function() {}; - spyOn(window, "NodeReducer").andCallFake(function(v, e) { - this.getCommunity = self.fakeReducerRequest; - }); spyOn($, "ajax").andCallFake(function(request) { if (spyHook !== undefined) { if(!spyHook(request)) { @@ -850,16 +857,66 @@ adapter.setNodeLimit(6); spyOn(this, "fakeReducerRequest"); adapter.loadNodeFromTreeById(c1, checkCallbackFunction); - expect(this.fakeReducerRequest).toHaveBeenCalledWith(6, c1); + expect(this.fakeReducerRequest).toHaveBeenCalledWith(6, nodeWithID(c1)); }); }); + + it('should render a reduced set of nodes if too many nodes are added', function() { + runs(function() { + adapter.setNodeLimit(6); + spyOn(this, "fakeReducerRequest").andCallFake(function() { + return [0, 1, 2, 3]; + }); + adapter.loadNodeFromTreeById(c1, checkCallbackFunction); + }); + + waitsFor(function() { + return callbackCheck; + }); + + runs(function() { + notExistNodes([c0, c1, c2, c3]); + existNodes([c4, c5, c6, c7]); + expect(nodes.length).toEqual(5); + + }); + + }); + it('should not trigger the reducer if the limit is set large enough', function() { spyOn(this, "fakeReducerRequest"); adapter.setNodeLimit(10); expect(this.fakeReducerRequest).not.toHaveBeenCalled(); }); + + it('should trigger the reducer if the limit is set too small', function() { + spyOn(this, "fakeReducerRequest"); + adapter.setNodeLimit(2); + expect(this.fakeReducerRequest).toHaveBeenCalledWith(2); + }); + + it('should reshape all objects if limit is set too small', function() { + var called = false; + var callback = function() { + called = true; + }; + spyOn(this, "fakeReducerRequest").andCallFake(function() { + return [0, 1, 2]; + }); + adapter.setNodeLimit(2, callback); + + notExistNodes([c0, c1, c2]); + existNodes([c3]); + expect(nodes.length).toEqual(2); + + expect(called).toBeTruthy(); + + + }); + + describe('that has loaded several queries', function() { var c8, c9, e2_8; diff --git a/html/admin/js/graphViewer/jasmine_test/specAdapter/interfaceSpec.js b/html/admin/js/graphViewer/jasmine_test/specAdapter/interfaceSpec.js index 7c0a0be1aa..eefd4b42e3 100644 --- a/html/admin/js/graphViewer/jasmine_test/specAdapter/interfaceSpec.js +++ b/html/admin/js/graphViewer/jasmine_test/specAdapter/interfaceSpec.js @@ -65,7 +65,7 @@ var describeInterface = function (testee) { expect(testee).toHaveFunction("createNode", 2); expect(testee).toHaveFunction("deleteNode", 2); expect(testee).toHaveFunction("patchNode", 3); - expect(testee).toHaveFunction("setNodeLimit", 1); + expect(testee).toHaveFunction("setNodeLimit", 2); }); };