Skip to content

Commit 74e91a4

Browse files
better tests of copy
1 parent 2145abc commit 74e91a4

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/graph.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ export class Graph {
6161

6262
const nodecopy = nid => {
6363
if(nid in doing) {
64-
console.log('copied before', nid, 'doing', doing)
64+
// console.log('copied before', nid, 'doing', doing)
6565
return}
66-
console.log('copy start', nid, 'doing', doing)
66+
// console.log('copy start', nid, 'doing', doing)
6767
done.push(nid)
6868
const node = this.nodes[nid]
6969
doing[nid] = output.addNode(node.type,node.props)
7070
for (const rid of node.out) nodecopy(this.rels[rid].to)
7171
for (const rid of node.in) nodecopy(this.rels[rid].from)
72-
console.log('linking',nid,'to',node.out.map(rid => this.rels[rid].to))
72+
// console.log('linking',nid,'to',node.out.map(rid => this.rels[rid].to))
7373
for (const rid of node.out) output.addRel('',doing[nid],doing[this.rels[rid].to],{})
7474
}
7575

test/copy.test.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import {assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
44
import {Graph} from '../src/graph.js';
5+
const short = props => props.name.split(/\n/)[0]
6+
const nameof = (graph,nid) => short(graph.nodes[nid].props)
7+
const names = graph => graph.nodes.map(node => short(node.props)).sort()
8+
const relations = graph => graph.rels.map(rel => `${nameof(graph,rel.from)} -> ${nameof(graph,rel.to)}`).sort()
59

610
// Tests for copying subtrees from this sample input
711

@@ -24,5 +28,32 @@ const input = await Graph.fetch(url)
2428

2529
Deno.test("We have expected input", async () => {
2630
assertEquals(input.nodes.length, 7);
27-
assertEquals(input.rels.length, 5)
31+
assertEquals(input.rels.length, 5);
32+
assertEquals(names(input), ["Indexing","Mixing","Search","Sites","Solo","Transmission","Watch",]);
33+
assertEquals(relations(input), ["Mixing -> Transmission","Mixing -> Watch","Search -> Sites","Search -> Solo","Transmission -> Mixing"]);
34+
});
35+
Deno.test("We have right nodes and rels in a copy", async () => {
36+
const cluster = new Graph()
37+
input.copy(0,cluster)
38+
assertEquals(names(cluster), ["Mixing","Transmission","Watch",]);
39+
assertEquals(relations(cluster), ["Mixing -> Transmission","Mixing -> Watch","Transmission -> Mixing"]);
40+
});
41+
Deno.test("We have aliased props in the copy", async () => {
42+
const local = Graph.load(JSON.parse(input.stringify()))
43+
assertEquals(names(local), ["Indexing","Mixing","Search","Sites","Solo","Transmission","Watch"]);
44+
const cluster = new Graph()
45+
local.copy(0,cluster)
46+
cluster.nodes[0].props.name = 'Hello'
47+
assertEquals(names(cluster), ["Hello","Transmission","Watch"]);
48+
assertEquals(names(local), ["Hello","Indexing","Search","Sites","Solo","Transmission","Watch"]);
49+
});
50+
Deno.test("We don't have aliased relations in the copy", async () => {
51+
const local = Graph.load(JSON.parse(input.stringify()))
52+
const cluster = new Graph()
53+
local.copy(0,cluster)
54+
cluster.addRel("",0,cluster.addNode("Page",{name:"Good Bye"}))
55+
assertEquals(names(local), ["Indexing","Mixing","Search","Sites","Solo","Transmission","Watch"]);
56+
assertEquals(names(cluster), ["Good Bye","Mixing","Transmission","Watch"]);
57+
assertEquals(relations(local), ["Mixing -> Transmission","Mixing -> Watch","Search -> Sites","Search -> Solo","Transmission -> Mixing"]);
58+
assertEquals(relations(cluster), ["Mixing -> Good Bye","Mixing -> Transmission","Mixing -> Watch","Transmission -> Mixing"]);
2859
});

0 commit comments

Comments
 (0)