This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
results_test.go
171 lines (140 loc) · 4.01 KB
/
results_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/******************************************************************************
Cloud Resource Counter
File: results.go
Summary: Collects results (in the form of column names and column values) and
writes to a CSV file
******************************************************************************/
package main
import (
"encoding/csv"
"io"
"strings"
"testing"
"github.com/expel-io/cloud-resource-counter/mock"
)
// Test the initialization of Results struct and invocation of NewRow
func TestResultsInitAndNewRow(t *testing.T) {
// Create our test cases
cases := []struct {
StoreHeaders bool
ExpectedRows int
}{
{
StoreHeaders: true,
ExpectedRows: 1,
},
{
ExpectedRows: 0,
},
}
// Loop through the test cases
for _, c := range cases {
// Create an instance of Results
results := Results{
StoreHeaders: c.StoreHeaders,
}
results.Init()
// How many rows are there?
actualRows := len(results.Rows)
// Does it match?
if actualRows != c.ExpectedRows {
t.Errorf("Error: TestResultsInit (before NewRow()) returned %d; expected %d", actualRows, c.ExpectedRows)
}
// Let's add another row and try again...
results.NewRow()
// How many rows are there?
actualRows = len(results.Rows)
// Does it match?
if actualRows != c.ExpectedRows+1 {
t.Errorf("Error: TestResultsInit (after NewRow()) returned %d; expected %d", actualRows, c.ExpectedRows+1)
}
}
}
func TestResultsAppend(t *testing.T) {
// Create a Builder to hold our generated results
builder := strings.Builder{}
// Create an instance of Results
results := Results{
StoreHeaders: true,
Writer: &builder,
}
results.Init()
results.NewRow()
// Add a bunch of column names and row values...
results.Append("col1", "row1_col1")
results.Append("col2", 123)
results.Append("col3", 456)
results.Append("col4", 789)
// Create our mock activity monitor
mon := mock.ActivityMonitorImpl{}
// Save to our mock Writer
results.Save(&mon)
// Verify that no errors were encountered
if mon.ErrorOccured {
t.Errorf("Encountered an error during Results.Save: %s", mon.ErrorMessage)
}
// Read the generate results back into our CSV module...
csvReader := csv.NewReader(strings.NewReader(builder.String()))
// Expected rows...
expected := [][]string{
{
"col1", "col2", "col3", "col4",
}, {
"row1_col1", "123", "456", "789",
},
}
row := 0
for {
// Read a Row of data...
record, err := csvReader.Read()
// Have we read all of the rows?
if err == io.EOF {
break
}
// Do we have an error?
if err != nil {
t.Errorf("Unexpected error while reading row of data: %v", err)
}
// Are we asking for a row which we don't expect?
if row == len(expected) {
t.Errorf("We have encountered more stored rows than expected")
}
// Are the length the same?
if len(record) != len(expected[row]) {
t.Errorf("Unexpected number of columns: expected %d, got %d", len(expected[row]), len(record))
}
// Does the record match?
for ix, actualVal := range record {
if expected[row][ix] != actualVal {
t.Errorf("Unexpected value for row %d, column %d: expected %v, got %v", row, ix, expected[row][ix], actualVal)
}
}
// Increment our row counter
row = row + 1
}
// Did we look at all of the expected rows?
if row != len(expected) {
t.Errorf("We have fewer stored rows than expected")
} else if mon.ProgramExited {
t.Errorf("Unexpected Exit: The program unexpected exited with status code=%d", mon.ExitCode)
}
}
func TestResultsNoSave(t *testing.T) {
// Create an instance of Results
results := Results{}
results.Init()
results.NewRow()
// Add a bunch of column names and row values...
results.Append("col1", "row1_col1")
results.Append("col2", 123)
results.Append("col3", 456)
results.Append("col4", 789)
// Create our mock activity monitor
mon := mock.ActivityMonitorImpl{}
// Save to our mock Writer
results.Save(&mon)
// Verify that no errors were encountered
if mon.ErrorOccured {
t.Errorf("Encountered an error during Results.Save: %s", mon.ErrorMessage)
}
}