yanic/models/graph_test.go

67 lines
1.4 KiB
Go
Raw Normal View History

2016-03-20 19:00:02 +00:00
package models
import (
2016-06-16 18:59:58 +00:00
"encoding/json"
"io/ioutil"
2016-03-20 19:00:02 +00:00
"testing"
2016-06-16 18:59:58 +00:00
"github.com/FreifunkBremen/respond-collector/data"
2016-03-20 19:00:02 +00:00
"github.com/stretchr/testify/assert"
)
2016-06-16 18:59:58 +00:00
type TestNode struct {
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
Neighbours *data.Neighbours `json:"neighbours"`
}
2016-03-20 19:00:02 +00:00
func TestGenerateGraph(t *testing.T) {
assert := assert.New(t)
2016-06-16 18:59:58 +00:00
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 {
2016-03-20 19:00:02 +00:00
nodes := &Nodes{
List: make(map[string]*Node),
}
2016-06-16 18:59:58 +00:00
for _, file := range files {
node := testGetNodeByFile(file)
nodes.Update(file, &data.ResponseData{
NodeInfo: node.Nodeinfo,
Neighbours: node.Neighbours,
})
2016-06-16 18:59:58 +00:00
}
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)
}
2016-03-20 19:00:02 +00:00
}