From c353efff3607768fbd2cd4a811e40e3be9f32049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Mon, 18 Feb 2019 13:10:55 -0700 Subject: [PATCH] Add a test helper function for converged graphs (#1093) Really minor refactor, adds a `convergedPagerankGraph` helper method which provides a converged pagerank graph. :) Test plan: `yarn test` suffices. --- src/core/pagerankGraph.test.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/core/pagerankGraph.test.js b/src/core/pagerankGraph.test.js index 5385388..0286ece 100644 --- a/src/core/pagerankGraph.test.js +++ b/src/core/pagerankGraph.test.js @@ -17,10 +17,15 @@ describe("core/pagerankGraph", () => { const nonEmptyGraph = () => new Graph().addNode(NodeAddress.fromParts(["hi"])); - function examplePagerankGraph() { + function examplePagerankGraph(): PagerankGraph { const g = advancedGraph().graph1(); return new PagerankGraph(g, defaultEvaluator); } + async function convergedPagerankGraph(): Promise { + const pg = examplePagerankGraph(); + await pg.runPagerank({maxIterations: 100, convergenceThreshold: 1e-4}); + return pg; + } it("cannot construct PagerankGraph with empty Graph", () => { const eg1 = new Graph(); @@ -50,9 +55,7 @@ describe("core/pagerankGraph", () => { expect(new Set(graphNodes)).toEqual(new Set(pgNodes)); }); it("node and nodes both return consistent scores", async () => { - const g = advancedGraph().graph1(); - const pg = new PagerankGraph(g, defaultEvaluator); - await pg.runPagerank({maxIterations: 1, convergenceThreshold: 0.001}); + const pg = await convergedPagerankGraph(); for (const {node, score} of pg.nodes()) { expect(score).toEqual(NullUtil.get(pg.node(node)).score); } @@ -289,15 +292,13 @@ describe("core/pagerankGraph", () => { describe("to/from JSON", () => { it("to->fro is identity", async () => { - const pg = examplePagerankGraph(); - await pg.runPagerank({maxIterations: 1, convergenceThreshold: 0.01}); + const pg = await convergedPagerankGraph(); const pgJSON = pg.toJSON(); const pg_ = PagerankGraph.fromJSON(pgJSON); expect(pg.equals(pg_)).toBe(true); }); it("fro->to is identity", async () => { - const pg = examplePagerankGraph(); - await pg.runPagerank({maxIterations: 1, convergenceThreshold: 0.01}); + const pg = await convergedPagerankGraph(); const pgJSON = pg.toJSON(); const pg_ = PagerankGraph.fromJSON(pgJSON); const pgJSON_ = pg_.toJSON();