autopilot+test: testify betweenness centrality tests

The commit also reindents the source to conform with ts=8 guideline.
This commit is contained in:
Andras Banki-Horvath 2020-06-17 19:23:42 +02:00
parent ccabad8607
commit afbbeae4f2
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

View File

@ -3,6 +3,8 @@ package autopilot
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func TestBetweennessCentralityMetricConstruction(t *testing.T) { func TestBetweennessCentralityMetricConstruction(t *testing.T) {
@ -11,50 +13,46 @@ func TestBetweennessCentralityMetricConstruction(t *testing.T) {
for _, workers := range failing { for _, workers := range failing {
m, err := NewBetweennessCentralityMetric(workers) m, err := NewBetweennessCentralityMetric(workers)
if m != nil || err == nil { require.Error(
t.Fatalf("construction must fail with <= 0 workers") t, err, "construction must fail with <= 0 workers",
} )
require.Nil(t, m)
} }
for _, workers := range ok { for _, workers := range ok {
m, err := NewBetweennessCentralityMetric(workers) m, err := NewBetweennessCentralityMetric(workers)
if m == nil || err != nil { require.NoError(
t.Fatalf("construction must succeed with >= 1 workers") t, err, "construction must succeed with >= 1 workers",
} )
require.NotNil(t, m)
} }
} }
// Tests that empty graph results in empty centrality result. // Tests that empty graph results in empty centrality result.
func TestBetweennessCentralityEmptyGraph(t *testing.T) { func TestBetweennessCentralityEmptyGraph(t *testing.T) {
centralityMetric, err := NewBetweennessCentralityMetric(1) centralityMetric, err := NewBetweennessCentralityMetric(1)
if err != nil { require.NoError(
t.Fatalf("construction must succeed with positive number of workers") t, err,
} "construction must succeed with positive number of workers",
)
for _, chanGraph := range chanGraphs { for _, chanGraph := range chanGraphs {
graph, cleanup, err := chanGraph.genFunc() graph, cleanup, err := chanGraph.genFunc()
success := t.Run(chanGraph.name, func(t1 *testing.T) { success := t.Run(chanGraph.name, func(t1 *testing.T) {
if err != nil { require.NoError(t, err, "unable to create graph")
t1.Fatalf("unable to create graph: %v", err)
}
if cleanup != nil { if cleanup != nil {
defer cleanup() defer cleanup()
} }
if err := centralityMetric.Refresh(graph); err != nil { err := centralityMetric.Refresh(graph)
t.Fatalf("unexpected failure during metric refresh: %v", err) require.NoError(t, err)
}
centrality := centralityMetric.GetMetric(false) centrality := centralityMetric.GetMetric(false)
if len(centrality) > 0 { require.Equal(t, 0, len(centrality))
t.Fatalf("expected empty metric, got: %v", len(centrality))
}
centrality = centralityMetric.GetMetric(true) centrality = centralityMetric.GetMetric(true)
if len(centrality) > 0 { require.Equal(t, 0, len(centrality))
t.Fatalf("expected empty metric, got: %v", len(centrality))
}
}) })
if !success { if !success {
break break
@ -66,7 +64,7 @@ func TestBetweennessCentralityEmptyGraph(t *testing.T) {
func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) { func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
workers := []int{1, 3, 9, 100} workers := []int{1, 3, 9, 100}
results := []struct { tests := []struct {
normalize bool normalize bool
centrality []float64 centrality []float64
}{ }{
@ -84,49 +82,51 @@ func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
for _, chanGraph := range chanGraphs { for _, chanGraph := range chanGraphs {
numWorkers := numWorkers numWorkers := numWorkers
graph, cleanup, err := chanGraph.genFunc() graph, cleanup, err := chanGraph.genFunc()
if err != nil { require.NoError(t, err, "unable to create graph")
t.Fatalf("unable to create graph: %v", err)
}
if cleanup != nil { if cleanup != nil {
defer cleanup() defer cleanup()
} }
testName := fmt.Sprintf("%v %d workers", chanGraph.name, numWorkers) testName := fmt.Sprintf(
"%v %d workers", chanGraph.name, numWorkers,
)
success := t.Run(testName, func(t1 *testing.T) { success := t.Run(testName, func(t1 *testing.T) {
centralityMetric, err := NewBetweennessCentralityMetric( metric, err := NewBetweennessCentralityMetric(
numWorkers, numWorkers,
) )
if err != nil { require.NoError(
t.Fatalf("construction must succeed with " + t, err,
"positive number of workers") "construction must succeed with "+
} "positive number of workers",
)
graphNodes := buildTestGraph(t1, graph, centralityTestGraph) graphNodes := buildTestGraph(
if err := centralityMetric.Refresh(graph); err != nil { t1, graph, centralityTestGraph,
t1.Fatalf("error while calculating betweeness centrality") )
}
for _, expected := range results { err = metric.Refresh(graph)
require.NoError(t, err)
for _, expected := range tests {
expected := expected expected := expected
centrality := centralityMetric.GetMetric(expected.normalize) centrality := metric.GetMetric(
expected.normalize,
)
if len(centrality) != centralityTestGraph.nodes { require.Equal(t,
t.Fatalf("expected %v values, got: %v", centralityTestGraph.nodes,
centralityTestGraph.nodes, len(centrality)) len(centrality),
} )
for node, nodeCentrality := range expected.centrality { for i, c := range expected.centrality {
nodeID := NewNodeID(graphNodes[node]) nodeID := NewNodeID(
calculatedCentrality, ok := centrality[nodeID] graphNodes[i],
if !ok { )
t1.Fatalf("no result for node: %x (%v)", result, ok := centrality[nodeID]
nodeID, node) require.True(t, ok)
} require.Equal(t, c, result)
if nodeCentrality != calculatedCentrality {
t1.Errorf("centrality for node: %v "+
"should be %v, got: %v",
node, nodeCentrality, calculatedCentrality)
}
} }
} }
}) })