github.com/jfrog/jfrog-cli-core/v2@v2.52.0/pipelines/commands/status_test.go (about) 1 package commands 2 3 import ( 4 "github.com/gookit/color" 5 "github.com/jfrog/jfrog-client-go/pipelines/services" 6 "github.com/stretchr/testify/assert" 7 "testing" 8 "time" 9 ) 10 11 func TestMonitorStatusChange(t *testing.T) { 12 type args struct { 13 pipeStatus string 14 reStatus string 15 } 16 testCases := []struct { 17 name string 18 args args 19 want bool 20 }{ 21 {"test when result status is different from previous state", args{"waiting", "processing"}, true}, 22 {"test when result status is same as previous", args{"processing", "processing"}, false}, 23 } 24 for _, testCase := range testCases { 25 t.Run(testCase.name, func(t *testing.T) { 26 assert.Equal(t, pipelineStatusChanged(testCase.args.pipeStatus, testCase.args.reStatus), testCase.want) 27 }) 28 } 29 } 30 31 func TestHasPipelineRunEnded(t *testing.T) { 32 type args struct { 33 pipeStatus string 34 } 35 testCases := []struct { 36 name string 37 args args 38 want bool 39 }{ 40 {"when status is not one of ending status", args{"processing"}, false}, 41 {"when status is ending status", args{"cancelled"}, true}, 42 {"when status is ending status", args{"failure"}, true}, 43 {"when status is ending status", args{"error"}, true}, 44 {"when status is ending status", args{"timeout"}, true}, 45 } 46 for _, tt := range testCases { 47 t.Run(tt.name, func(t *testing.T) { 48 if got := pipelineRunEnded(tt.args.pipeStatus); got != tt.want { 49 t.Errorf("hasPipelineRunEnded() = %v, want %v", got, tt.want) 50 } 51 }) 52 } 53 } 54 55 func TestGetPipelineStatusAndColorCode(t *testing.T) { 56 type args struct { 57 pipeline *services.Pipelines 58 } 59 r := services.Run{ 60 StatusCode: 4002, 61 DurationSeconds: 432, 62 CreatedAt: time.Now(), 63 } 64 r1 := services.Run{ 65 StatusCode: 4003, 66 DurationSeconds: 432, 67 CreatedAt: time.Now(), 68 } 69 r2 := services.Run{ 70 StatusCode: 4001, 71 DurationSeconds: 432, 72 CreatedAt: time.Now(), 73 } 74 p := &services.Pipelines{ 75 Run: r, 76 } 77 p1 := &services.Pipelines{ 78 Run: r1, 79 } 80 p2 := &services.Pipelines{ 81 Run: r2, 82 } 83 testCases := []struct { 84 name string 85 args args 86 want string 87 want1 color.Color 88 want2 string 89 }{ 90 {"should return color code green", args{p}, "success", color.Green, "0D 0H 7M 12S"}, 91 {"should return color code red", args{p1}, "failure", color.Red, "0D 0H 7M 12S"}, 92 {"should return color code blue", args{p2}, "processing", color.Blue, "0D 0H 7M 12S"}, 93 } 94 for _, tt := range testCases { 95 t.Run(tt.name, func(t *testing.T) { 96 got, got1, got2 := getPipelineStatusAndColorCode(tt.args.pipeline) 97 if string(got) != tt.want { 98 t.Errorf("getPipelineStatusAndColorCode() got = %v, want %v", got, tt.want) 99 } 100 if got1 != tt.want1 { 101 t.Errorf("getPipelineStatusAndColorCode() got1 = %v, want %v", got1, tt.want1) 102 } 103 if got2 != tt.want2 { 104 t.Errorf("getPipelineStatusAndColorCode() got2 = %v, want %v", got2, tt.want2) 105 } 106 }) 107 } 108 } 109 110 func TestConvertSecToDay(t *testing.T) { 111 type args struct { 112 sec int 113 } 114 testCases := []struct { 115 name string 116 args args 117 want string 118 }{ 119 {"Test when given seconds", args{60}, "0D 0H 1M 0S"}, 120 } 121 for _, tt := range testCases { 122 t.Run(tt.name, func(t *testing.T) { 123 if got := convertSecToDay(tt.args.sec); got != tt.want { 124 t.Errorf("convertSecToDay() = %v, want %v", got, tt.want) 125 } 126 }) 127 } 128 }