From 08565d18393368aa0aaedad5995d033d808f3d92 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Thu, 16 Jun 2016 20:59:58 +0200 Subject: [PATCH] fix graph --- models/graph.go | 2 +- models/graph_test.go | 49 +++++++++++++++++++++++++++++++++++--- models/testdata/node1.json | 25 +++++++++++++++++++ models/testdata/node2.json | 24 +++++++++++++++++++ models/testdata/node3.json | 24 +++++++++++++++++++ 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 models/testdata/node1.json create mode 100644 models/testdata/node2.json create mode 100644 models/testdata/node3.json diff --git a/models/graph.go b/models/graph.go index 4e4e2c9..0683b22 100644 --- a/models/graph.go +++ b/models/graph.go @@ -106,7 +106,7 @@ func (builder *GraphBuilder) Extract() ([]*GraphNode, []*GraphLink) { if linkPart[1] == node.NodeID { link.Target = i both++ - break + continue } } if both == 2 { diff --git a/models/graph_test.go b/models/graph_test.go index 4c03932..816e211 100644 --- a/models/graph_test.go +++ b/models/graph_test.go @@ -1,19 +1,62 @@ package models import ( + "encoding/json" + "io/ioutil" "testing" + "github.com/FreifunkBremen/respond-collector/data" "github.com/stretchr/testify/assert" ) +type TestNode struct { + Nodeinfo *data.NodeInfo `json:"nodeinfo"` + Neighbours *data.Neighbours `json:"neighbours"` +} + func TestGenerateGraph(t *testing.T) { assert := assert.New(t) + nodes := testGetNodesByFile("node1.json", "node2.json", "node3.json") + + graph := nodes.BuildGraph() + assert.NotNil(graph) + assert.Equal(1, graph.Version, "Wrong Version") + assert.NotNil(graph.Batadv, "no Batadv") + assert.Equal(false, graph.Batadv.Directed, "directed batadv") + assert.Equal(3, len(graph.Batadv.Nodes), "wrong Nodes count") + assert.Equal(2, len(graph.Batadv.Links), "wrong Links count") + // TODO more tests required +} + +func testGetNodesByFile(files ...string) *Nodes { nodes := &Nodes{ List: make(map[string]*Node), } - graph := nodes.BuildGraph() - assert.NotNil(graph) - // TODO more tests required + for _, file := range files { + nodes.List[file] = testGetNodeByFile(file) + } + + return nodes +} + +func testGetNodeByFile(filename string) *Node { + testnode := &TestNode{} + testfile(filename, testnode) + return &Node{ + Nodeinfo: testnode.Nodeinfo, + Neighbours: testnode.Neighbours, + } +} + +func testfile(name string, obj interface{}) { + file, err := ioutil.ReadFile("testdata/" + name) + if err != nil { + panic(err) + } + + if err := json.Unmarshal(file, obj); err != nil { + panic(err) + } } diff --git a/models/testdata/node1.json b/models/testdata/node1.json new file mode 100644 index 0000000..379e5b9 --- /dev/null +++ b/models/testdata/node1.json @@ -0,0 +1,25 @@ +{ + "nodeinfo":{ + "node_id":"node1.json", + "network":{ + "mesh":{ + "bat0":{ + "interfaces":{ + "wireless":["a"] + } + } + } + } + }, + "neighbours":{ + "batadv":{ + "a":{ + "neighbours":{ + "b":{"tq":250,"lastseen":0.42}, + "c":{"tq":250,"lastseen":0.42} + } + } + } + } + +} diff --git a/models/testdata/node2.json b/models/testdata/node2.json new file mode 100644 index 0000000..de1b546 --- /dev/null +++ b/models/testdata/node2.json @@ -0,0 +1,24 @@ +{ + "nodeinfo":{ + "node_id":"node2.json", + "network":{ + "mesh":{ + "bat0":{ + "interfaces":{ + "wireless":["b"] + } + } + } + } + }, + "neighbours":{ + "batadv":{ + "b":{ + "neighbours":{ + "a":{"tq":150,"lastseen":0.42} + } + } + } + } + +} diff --git a/models/testdata/node3.json b/models/testdata/node3.json new file mode 100644 index 0000000..7dcabd3 --- /dev/null +++ b/models/testdata/node3.json @@ -0,0 +1,24 @@ +{ + "nodeinfo":{ + "node_id":"node3.json", + "network":{ + "mesh":{ + "bat0":{ + "interfaces":{ + "wireless":["c"] + } + } + } + } + }, + "neighbours":{ + "batadv":{ + "c":{ + "neighbours":{ + "a":{"tq":200,"lastseen":0.42} + } + } + } + } + +}