fix graph

This commit is contained in:
Martin Geno 2016-06-16 20:59:58 +02:00
parent fb2f0bda27
commit 08565d1839
5 changed files with 120 additions and 4 deletions

View File

@ -106,7 +106,7 @@ func (builder *GraphBuilder) Extract() ([]*GraphNode, []*GraphLink) {
if linkPart[1] == node.NodeID {
link.Target = i
both++
break
continue
}
}
if both == 2 {

View File

@ -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)
}
}

25
models/testdata/node1.json vendored Normal file
View File

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

24
models/testdata/node2.json vendored Normal file
View File

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

24
models/testdata/node3.json vendored Normal file
View File

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