Skip to content

Commit

Permalink
test: Runs test
Browse files Browse the repository at this point in the history
  • Loading branch information
fchimpan committed Mar 27, 2024
1 parent c1e1031 commit d6af9d3
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/fchimpan/gh-workflow-stats/internal/github"
Expand Down Expand Up @@ -42,6 +43,7 @@ type options struct {

func workflowStats(cfg config, opt options, isJobs bool) error {
ctx := context.Background()
w := io.Writer(os.Stdout)
a := &github.GitHubAuthenticator{}
client, err := github.NewClient(cfg.host, a)
if err != nil {
Expand Down Expand Up @@ -127,7 +129,7 @@ func workflowStats(cfg config, opt options, isJobs bool) error {
if isRateLimit {
printer.RateLimitWarning(os.Stdout)
}
printer.Runs(wrs)
printer.Runs(w, wrs)
if isJobs {
printer.FailureJobs(jobs, opt.jobNum)
printer.LongestDurationJobs(jobs, opt.jobNum)
Expand Down
8 changes: 4 additions & 4 deletions internal/parser/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import (

const eps = 1e-9

type executionDurationStats struct {
type ExecutionDurationStats struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Avg float64 `json:"avg"`
Med float64 `json:"med"`
Std float64 `json:"std"`
}

func calcStats(d []float64) executionDurationStats {
func calcStats(d []float64) ExecutionDurationStats {
if len(d) == 0 {
return executionDurationStats{}
return ExecutionDurationStats{}
}
min := slices.Min(d)
max := slices.Max(d)
avg, _ := stats.Mean(d)
med, _ := stats.Median(d)
std, _ := stats.StandardDeviation(d)

return executionDurationStats{
return ExecutionDurationStats{
Min: min,
Max: max,
Avg: avg,
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ func TestCalcStats(t *testing.T) {
tests := []struct {
name string
input []float64
expected executionDurationStats
expected ExecutionDurationStats
}{
{
name: "Empty input",
input: []float64{},
expected: executionDurationStats{},
expected: ExecutionDurationStats{},
},
{
name: "Non-empty input",
input: []float64{1.5, 2.5, 3.5, 4.5, 5.5},
expected: executionDurationStats{Min: 1.5, Max: 5.5, Avg: 3.5, Med: 3.5, Std: 1.4142135623730951},
expected: ExecutionDurationStats{Min: 1.5, Max: 5.5, Avg: 3.5, Med: 3.5, Std: 1.4142135623730951},
},
}

Expand Down
4 changes: 2 additions & 2 deletions internal/parser/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type WorkflowJobsStatsSummary struct {
TotalRunsCount int `json:"total_runs_count"`
Rate Rate `json:"rate"`
Conclusions map[string]int `json:"conclusions"`
ExecutionDurationStats executionDurationStats `json:"execution_duration_stats"`
ExecutionDurationStats ExecutionDurationStats `json:"execution_duration_stats"`
StepSummary []*StepSummary `json:"steps_summary"`
}

Expand All @@ -26,7 +26,7 @@ type StepSummary struct {
RunsCount int `json:"runs_count"`
Conclusions map[string]int `json:"conclusion"`
Rate Rate `json:"rate"`
ExecutionDurationStats executionDurationStats `json:"execution_duration_stats"`
ExecutionDurationStats ExecutionDurationStats `json:"execution_duration_stats"`
FailureHTMLURL []string `json:"failure_html_url"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parser/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type WorkflowRunsStatsSummary struct {
TotalRunsCount int `json:"total_runs_count"`
Name string `json:"name"`
Rate Rate `json:"rate"`
ExecutionDurationStats executionDurationStats `json:"execution_duration_stats"`
ExecutionDurationStats ExecutionDurationStats `json:"execution_duration_stats"`
Conclusions map[string]*WorkflowRunsConclusion `json:"conclusions"`
}

Expand Down
6 changes: 3 additions & 3 deletions internal/parser/runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestWorkflowRunsParse(t *testing.T) {
FailureRate: 0,
OthersRate: 0,
},
ExecutionDurationStats: executionDurationStats{
ExecutionDurationStats: ExecutionDurationStats{
Min: 20.0,
Max: 40.0,
Avg: 30.0,
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestWorkflowRunsParse(t *testing.T) {
FailureRate: 0.5,
OthersRate: 0.5,
},
ExecutionDurationStats: executionDurationStats{
ExecutionDurationStats: ExecutionDurationStats{
Min: 0,
Max: 0,
Avg: 0,
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestWorkflowRunsParse(t *testing.T) {
FailureRate: 0.3333333333333333,
OthersRate: 0.3333333333333334,
},
ExecutionDurationStats: executionDurationStats{
ExecutionDurationStats: ExecutionDurationStats{
Min: 20.0,
Max: 20.0,
Avg: 20.0,
Expand Down
148 changes: 148 additions & 0 deletions internal/printer/runs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package printer

import (
"bytes"
"testing"

"github.com/fchimpan/gh-workflow-stats/internal/parser"
"github.com/stretchr/testify/assert"
)

func TestRuns(t *testing.T) {
type args struct {
wrs *parser.WorkflowRunsStatsSummary
}
tests := []struct {
name string
args args
wantW string
}{
{
name: "Empty",
args: args{
wrs: &parser.WorkflowRunsStatsSummary{
TotalRunsCount: 0,
Rate: parser.Rate{},
Conclusions: map[string]*parser.WorkflowRunsConclusion{
parser.ConclusionSuccess: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
parser.ConclusionFailure: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
parser.ConclusionOthers: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
},
},
},
wantW: "🏃 Total runs: 0\n ✔ Success: 0 (0.0%)\n ✖ Failure: 0 (0.0%)\n 🤔 Others: 0 (0.0%)\n\n⏰ Workflow run execution time stats\n Min: 0.0s\n Max: 0.0s\n Avg: 0.0s\n Med: 0.0s\n Std: 0.0s\n",
},
{
name: "Success",
args: args{
wrs: &parser.WorkflowRunsStatsSummary{
TotalRunsCount: 2,
Name: "CI",
Rate: parser.Rate{
SuccesRate: 1,
FailureRate: 0.00001,
OthersRate: 0,
},
ExecutionDurationStats: parser.ExecutionDurationStats{
Min: 20.0,
Max: 40.0,
Avg: 30.0,
Std: 10,
Med: 30.0001,
},
Conclusions: map[string]*parser.WorkflowRunsConclusion{
parser.ConclusionSuccess: {
RunsCount: 2,
WorkflowRuns: []*parser.WorkflowRun{
{
ID: 1,
Status: "completed",
},
{
ID: 2,
Status: "completed",
},
},
},
parser.ConclusionFailure: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
parser.ConclusionOthers: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
},
},
},
wantW: "🏃 Total runs: 2\n ✔ Success: 2 (100.0%)\n ✖ Failure: 0 (0.0%)\n 🤔 Others: 0 (0.0%)\n\n⏰ Workflow run execution time stats\n Min: 20.0s\n Max: 40.0s\n Avg: 30.0s\n Med: 30.0s\n Std: 10.0s\n",
},
{
name: "Mixed",
args: args{
wrs: &parser.WorkflowRunsStatsSummary{
TotalRunsCount: 3,
Name: "CI",
Rate: parser.Rate{
SuccesRate: 0.6666666666666666,
FailureRate: 0.3333333333333333,
OthersRate: 0,
},
ExecutionDurationStats: parser.ExecutionDurationStats{
Min: 20.0,
Max: 40.0,
Avg: 30.0,
Std: 10,
Med: 30.0001,
},
Conclusions: map[string]*parser.WorkflowRunsConclusion{
parser.ConclusionSuccess: {
RunsCount: 2,
WorkflowRuns: []*parser.WorkflowRun{
{
ID: 1,
Status: "completed",
},
{
ID: 2,
Status: "completed",
},
},
},
parser.ConclusionFailure: {
RunsCount: 1,
WorkflowRuns: []*parser.WorkflowRun{
{
ID: 3,
Status: "completed",
},
},
},
parser.ConclusionOthers: {
RunsCount: 0,
WorkflowRuns: []*parser.WorkflowRun{},
},
},
},
},
wantW: "🏃 Total runs: 3\n ✔ Success: 2 (66.7%)\n ✖ Failure: 1 (33.3%)\n 🤔 Others: 0 (0.0%)\n\n⏰ Workflow run execution time stats\n Min: 20.0s\n Max: 40.0s\n Avg: 30.0s\n Med: 30.0s\n Std: 10.0s\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
Runs(w, tt.args.wrs)
assert.Equal(t, tt.wantW, w.String())
})
}
}

0 comments on commit d6af9d3

Please sign in to comment.