-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcsv_test.go
143 lines (112 loc) · 3.85 KB
/
csv_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
/*
Copyright © 2022 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main_test
// Generated documentation is available at:
// https://pkg.go.dev/github.com/RedHatInsights/insights-results-aggregator-exporter
//
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/insights-results-aggregator-exporter/packages/csv_test.html
import (
"bytes"
"testing"
main "github.com/RedHatInsights/insights-results-aggregator-exporter"
"github.com/stretchr/testify/assert"
)
// TestDisabledRulesToCSVNilBuffer check how nil buffer is handled by
// DisabledRulesToCSV function
func TestDisabledRulesToCSVNilBuffer(t *testing.T) {
// empty list
disabledRules := []main.DisabledRuleInfo{}
err := main.DisabledRulesToCSV(nil, disabledRules)
assert.Error(t, err, "Buffer is nil")
}
// TestDisabledRulesToCSVEmptyListOfRules check exporting empty list of
// disabled rules into CSV
func TestDisabledRulesToCSVEmptyListOfRules(t *testing.T) {
// buffer
buffer := new(bytes.Buffer)
// empty list
disabledRules := []main.DisabledRuleInfo{}
err := main.DisabledRulesToCSV(buffer, disabledRules)
assert.Nil(t, err, "Error is not expected")
content := buffer.String()
expected := "Rule,Count\n"
assert.Equal(t, expected, content)
}
// TestDisabledRulesToCSVE check exporting non-empty list of disabled rules
// into CSV
func TestDisabledRulesToCSV(t *testing.T) {
// buffer
buffer := new(bytes.Buffer)
// empty list
disabledRules := []main.DisabledRuleInfo{
{"first", 1},
{"second", 2},
{"third", 3},
}
err := main.DisabledRulesToCSV(buffer, disabledRules)
assert.Nil(t, err, "Error is not expected")
content := buffer.String()
expected := "Rule,Count\nfirst,1\nsecond,2\nthird,3\n"
assert.Equal(t, expected, content)
}
// mustCreateStorage helper function creates dummy storage
func mustCreateStorage(t *testing.T) *main.DBStorage {
storage, err := main.NewStorage(&main.StorageConfiguration{
Driver: "sqlite3",
LogSQLQueries: true,
})
assert.NoError(t, err, "Storage constructor")
return storage
}
// TestTableMetadataToCSVNilBuffer check how nil buffer is handled by
// TableMetadataToCSV function
func TestTableMetadataToCSVNilBuffer(t *testing.T) {
// dummy storage
storage := mustCreateStorage(t)
// empty list
tableNames := []main.TableName{}
err := main.TableMetadataToCSV(nil, tableNames, *storage)
assert.Error(t, err, "Buffer is nil")
}
// TestTableMetadataToCSVEmptyListOfRules check exporting empty list of
// disabled rules into CSV
func TestTableMetadataToCSVEmptyListOfRules(t *testing.T) {
// dummy storage
storage := mustCreateStorage(t)
// buffer
buffer := new(bytes.Buffer)
// empty list
tableNames := []main.TableName{}
err := main.TableMetadataToCSV(buffer, tableNames, *storage)
assert.NoError(t, err, "Error not expected")
content := buffer.String()
expected := "Table name,Records\n"
assert.Equal(t, expected, content)
}
// TestTableMetadataToCSVE check exporting non-empty list of disabled rules
// into CSV
func TestTableMetadataToCSV(t *testing.T) {
// dummy storage
storage := mustCreateStorage(t)
// buffer
buffer := new(bytes.Buffer)
// non-empty list
tableNames := []main.TableName{
main.TableName("first"),
main.TableName("second"),
main.TableName("third"),
}
err := main.TableMetadataToCSV(buffer, tableNames, *storage)
assert.Error(t, err, "Storage error is not expected")
}