github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/group_counter_graph_test.go (about)

     1  package controldisplay
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  type counterGraphTest struct {
    10  	failedControls   int
    11  	totalControls    int
    12  	maxTotalControls int
    13  
    14  	expectedString string
    15  }
    16  
    17  // when calculating chart elements, round UP success to next segment, round DOWN space count
    18  func testCasesCounterGraph() map[string]counterGraphTest {
    19  	return map[string]counterGraphTest{
    20  
    21  		"0/10 max 20  (zero pass)": {
    22  			maxTotalControls: 20,
    23  			failedControls:   0,
    24  			totalControls:    10,
    25  			// each segment is 12 -> 5 success, 5 blank
    26  			//[xxxxx     ]
    27  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    28  				ControlColors.CountGraphBracket("["),
    29  				ControlColors.CountGraphFail(strings.Repeat("=", 0)),
    30  				ControlColors.CountGraphPass(strings.Repeat("=", 5)),
    31  				strings.Repeat(" ", 5),
    32  				ControlColors.CountGraphBracket("]")),
    33  		},
    34  
    35  		"1/10 max 20 (less than 1 segment failed)": {
    36  			maxTotalControls: 20,
    37  			totalControls:    10,
    38  			failedControls:   1,
    39  			// each segment is 2 -> 1 fail, 4 success, 5 blank
    40  			//	[Xxxxx     ]
    41  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    42  				ControlColors.CountGraphBracket("["),
    43  				ControlColors.CountGraphFail(strings.Repeat("=", 1)),
    44  				ControlColors.CountGraphPass(strings.Repeat("=", 4)),
    45  				strings.Repeat(" ", 5),
    46  				ControlColors.CountGraphBracket("]")),
    47  		},
    48  		"2/10 max 20 (exactly 1 segment failed)": {
    49  			maxTotalControls: 20,
    50  			totalControls:    10,
    51  			failedControls:   2,
    52  			// each segment is 2 -> 1 fail, 4 success, 5 blank
    53  			//	[Xxxxx     ]
    54  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    55  				ControlColors.CountGraphBracket("["),
    56  				ControlColors.CountGraphFail(strings.Repeat("=", 1)),
    57  				ControlColors.CountGraphPass(strings.Repeat("=", 4)),
    58  				strings.Repeat(" ", 5),
    59  				ControlColors.CountGraphBracket("]")),
    60  		},
    61  		"3/10 max 20 (more than 1 segment failed)": {
    62  			maxTotalControls: 20,
    63  			totalControls:    10,
    64  			failedControls:   3,
    65  			// each segment is 3 -> 2 fail, 3 success, 5 blank
    66  			//	[Xxxxx     ]
    67  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    68  				ControlColors.CountGraphBracket("["),
    69  				ControlColors.CountGraphFail(strings.Repeat("=", 2)),
    70  				ControlColors.CountGraphPass(strings.Repeat("=", 3)),
    71  				strings.Repeat(" ", 5),
    72  				ControlColors.CountGraphBracket("]")),
    73  		},
    74  
    75  		"0/12 max 28 (zero pass)": {
    76  			maxTotalControls: 28,
    77  			totalControls:    12,
    78  			failedControls:   0,
    79  			// segment=2.8 -> 0 fail, 5 success, 5 blank
    80  			// [Xxxxx     ]
    81  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    82  				ControlColors.CountGraphBracket("["),
    83  				ControlColors.CountGraphFail(strings.Repeat("=", 0)),
    84  				ControlColors.CountGraphPass(strings.Repeat("=", 5)),
    85  				strings.Repeat(" ", 5),
    86  				ControlColors.CountGraphBracket("]")),
    87  		},
    88  
    89  		"1/12 max 28 (less than 1 segment failed)": {
    90  			maxTotalControls: 28,
    91  			totalControls:    12,
    92  			failedControls:   1,
    93  			// segment=2.8 -> 1 fail, 4 success, 5 blank
    94  			// [Xxxxx     ]
    95  			expectedString: fmt.Sprintf("%s%s%s%s%s",
    96  				ControlColors.CountGraphBracket("["),
    97  				ControlColors.CountGraphFail(strings.Repeat("=", 1)),
    98  				ControlColors.CountGraphPass(strings.Repeat("=", 4)),
    99  				strings.Repeat(" ", 5),
   100  				ControlColors.CountGraphBracket("]")),
   101  		},
   102  
   103  		"3/12 max 28 (more than 1 segment failed)": {
   104  			maxTotalControls: 28,
   105  			totalControls:    12,
   106  			failedControls:   3,
   107  			// segment=2.8 -> 2 fail, 3 success, 5 blank
   108  			// [Xxxxx     ]
   109  			expectedString: fmt.Sprintf("%s%s%s%s%s",
   110  				ControlColors.CountGraphBracket("["),
   111  				ControlColors.CountGraphFail(strings.Repeat("=", 2)),
   112  				ControlColors.CountGraphPass(strings.Repeat("=", 3)),
   113  				strings.Repeat(" ", 5),
   114  				ControlColors.CountGraphBracket("]")),
   115  		},
   116  
   117  		" 0/17 max 51 (zero pass)": {
   118  			maxTotalControls: 51,
   119  			totalControls:    17,
   120  			failedControls:   0,
   121  			// segment=5.1 -> 0 fail, 4 success, 6 blank
   122  			// [xxxx      ]
   123  			expectedString: fmt.Sprintf("%s%s%s%s%s",
   124  				ControlColors.CountGraphBracket("["),
   125  				ControlColors.CountGraphFail(strings.Repeat("=", 0)),
   126  				ControlColors.CountGraphPass(strings.Repeat("=", 4)),
   127  				strings.Repeat(" ", 6),
   128  				ControlColors.CountGraphBracket("]")),
   129  		},
   130  		"4/17 max 51 (less than 1 segment failed)": {
   131  			maxTotalControls: 51,
   132  			totalControls:    17,
   133  			failedControls:   4,
   134  			// segment=5.1 -> 1 fail, 3 success, 6 blank
   135  			// [xxxx      ]
   136  			expectedString: fmt.Sprintf("%s%s%s%s%s",
   137  				ControlColors.CountGraphBracket("["),
   138  				ControlColors.CountGraphFail(strings.Repeat("=", 1)),
   139  				ControlColors.CountGraphPass(strings.Repeat("=", 3)),
   140  				strings.Repeat(" ", 6),
   141  				ControlColors.CountGraphBracket("]")),
   142  		},
   143  		"6/17 max 51 (more than 1 segment failed)": {
   144  			maxTotalControls: 51,
   145  			totalControls:    17,
   146  			failedControls:   6,
   147  			// segment=5.1 -> 2 fail, 2 success, 6 blank
   148  			// [xxxx      ]
   149  			expectedString: fmt.Sprintf("%s%s%s%s%s",
   150  				ControlColors.CountGraphBracket("["),
   151  				ControlColors.CountGraphFail(strings.Repeat("=", 2)),
   152  				ControlColors.CountGraphPass(strings.Repeat("=", 2)),
   153  				strings.Repeat(" ", 6),
   154  				ControlColors.CountGraphBracket("]")),
   155  		},
   156  		"71/80 max 560 rounding error": {
   157  			maxTotalControls: 560,
   158  			totalControls:    80,
   159  			failedControls:   71,
   160  			// segment=5.1 -> 2 fail, 2 success, 6 blank
   161  			// [xxxx      ]
   162  			expectedString: fmt.Sprintf("%s%s%s%s%s",
   163  				ControlColors.CountGraphBracket("["),
   164  				ControlColors.CountGraphFail(strings.Repeat("=", 2)),
   165  				ControlColors.CountGraphPass(strings.Repeat("=", 1)),
   166  				strings.Repeat(" ", 7),
   167  				ControlColors.CountGraphBracket("]")),
   168  		},
   169  	}
   170  }
   171  
   172  func TestCounterGraph(t *testing.T) {
   173  	themeDef := ColorSchemes["dark"]
   174  	scheme, _ := NewControlColorScheme(themeDef)
   175  	ControlColors = scheme
   176  
   177  	for name, test := range testCasesCounterGraph() {
   178  		counterGraph := NewCounterGraphRenderer(test.failedControls, test.totalControls, test.maxTotalControls, CounterGraphRendererOptions{FailedColorFunc: ControlColors.CountGraphFail})
   179  		output := counterGraph.Render()
   180  		if output != test.expectedString {
   181  			t.Errorf("Test: '%s'' FAILED : \nexpected:\n %s, \ngot:\n %s\n", name, test.expectedString, output)
   182  		}
   183  	}
   184  }