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} + } + } + } + } + +}