Skip to content

Commit

Permalink
[WIP] Add tests for graph.go (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahalrs committed Jun 23, 2021
1 parent 49631d9 commit f33137a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pkg/workflow/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package workflow

import (
"errors"
)

var (
ErrNoNodesFound = errors.New("no nodes found in the graph")
ErrEntryNodeNotFound = errors.New("\"entry\" node not found")
)
7 changes: 3 additions & 4 deletions pkg/workflow/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package workflow
import (
"bytes"
"encoding/base64"
"fmt"

v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
Expand All @@ -22,8 +21,8 @@ type Node struct {
}

func Build(entry string, nodes map[string]v1alpha13.NodeStatus) (*Graph, error) {
if nodes == nil || len(nodes) == 0 {
return nil, fmt.Errorf("no nodes found in the graph")
if len(nodes) == 0 {
return nil, ErrNoNodesFound
}

g := &Graph{
Expand All @@ -33,7 +32,7 @@ func Build(entry string, nodes map[string]v1alpha13.NodeStatus) (*Graph, error)

e, ok := nodes[entry]
if !ok {
return nil, fmt.Errorf("\"entry\" node not found")
return nil, ErrEntryNodeNotFound
}

err := g.bft(e)
Expand Down
56 changes: 56 additions & 0 deletions pkg/workflow/graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package workflow

import (
"testing"

v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)

func TestBuild(t *testing.T) {
type args struct {
entry string
nodes map[string]v1alpha13.NodeStatus
}
testmap := make(map[string]v1alpha13.NodeStatus)
testmap["dummy"] = v1alpha13.NodeStatus{}

tests := []struct {
name string
args args
want *Graph
err error
}{
//
// TODO: Add more test cases.
//
{
name: "testing nil nodes",
args: args{
entry: "",
nodes: nil,
},
want: nil,
err: ErrNoNodesFound,
},
{
name: "testing unknown entry",
args: args{
entry: "unknown",
nodes: testmap,
},
want: nil,
err: ErrEntryNodeNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Build(tt.args.entry, tt.args.nodes)
if err != tt.err {
t.Errorf("Build() error = %v, want %v", err, tt.err)
}
if got != tt.want {
t.Errorf("Build() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f33137a

Please sign in to comment.