Skip to content

Commit 983e57b

Browse files
authored
fix: Tests (#281)
<!-- Explain what problem this PR addresses --> ---
1 parent 757cb9f commit 983e57b

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

plugins/source_testing.go

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,28 @@ func TestSourcePluginSync(t *testing.T, plugin *SourcePlugin, logger zerolog.Log
4646
validateTables(t, plugin.Tables(), syncedResources)
4747
}
4848

49-
func getTableResource(t *testing.T, table *schema.Table, resources []*schema.Resource) *schema.Resource {
49+
func getTableResources(t *testing.T, table *schema.Table, resources []*schema.Resource) []*schema.Resource {
5050
t.Helper()
51+
52+
tableResources := make([]*schema.Resource, 0)
53+
5154
for _, resource := range resources {
5255
if resource.Table.Name == table.Name {
53-
return resource
56+
tableResources = append(tableResources, resource)
5457
}
5558
}
5659

57-
return nil
60+
return tableResources
5861
}
5962

6063
func validateTable(t *testing.T, table *schema.Table, resources []*schema.Resource) {
6164
t.Helper()
62-
resource := getTableResource(t, table, resources)
63-
if resource == nil {
65+
tableResources := getTableResources(t, table, resources)
66+
if len(tableResources) == 0 {
6467
t.Errorf("Expected table %s to be synced but it was not found", table.Name)
6568
return
6669
}
67-
validateResource(t, resource)
70+
validateResources(t, tableResources)
6871
}
6972

7073
func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Resource) {
@@ -75,43 +78,61 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso
7578
}
7679
}
7780

78-
func validateResource(t *testing.T, resource *schema.Resource) {
81+
// Validates that every column has at least one non-nil value.
82+
// Also does some additional validations.
83+
func validateResources(t *testing.T, resources []*schema.Resource) {
7984
t.Helper()
80-
// we want to marshal and unmarshal to mimic over-the-wire behavior
81-
b, err := json.Marshal(resource.Data)
82-
if err != nil {
83-
t.Fatalf("failed to marshal resource data: %v", err)
84-
}
85-
var data map[string]interface{}
86-
if err := json.Unmarshal(b, &data); err != nil {
87-
t.Fatalf("failed to unmarshal resource data: %v", err)
88-
}
89-
for _, columnName := range resource.Table.Columns.Names() {
90-
if data[columnName] == nil && !resource.Table.Columns.Get(columnName).IgnoreInTests {
91-
t.Errorf("table: %s with unset column %s", resource.Table.Name, columnName)
85+
86+
table := resources[0].Table
87+
88+
// A set of column-names that have values in at least one of the resources.
89+
columnsWithValues := make(map[string]bool)
90+
91+
for _, resource := range resources {
92+
// we want to marshal and unmarshal to mimic over-the-wire behavior
93+
b, err := json.Marshal(resource.Data)
94+
if err != nil {
95+
t.Fatalf("failed to marshal resource data: %v", err)
9296
}
93-
val := data[columnName]
94-
if val != nil {
97+
var data map[string]interface{}
98+
if err := json.Unmarshal(b, &data); err != nil {
99+
t.Fatalf("failed to unmarshal resource data: %v", err)
100+
}
101+
102+
for columnName, value := range data {
103+
if value == nil {
104+
continue
105+
}
106+
107+
columnsWithValues[columnName] = true
108+
95109
switch resource.Table.Columns.Get(columnName).Type {
96110
case schema.TypeJSON:
97-
switch val.(type) {
111+
switch value.(type) {
98112
case string, []byte:
99113
t.Errorf("table: %s JSON column %s is being set with a string or byte slice. Either the unmarhsalled object should be passed in, or the column type should be changed to string", resource.Table.Name, columnName)
100114
continue
101115
}
102-
if _, err := json.Marshal(val); err != nil {
103-
t.Errorf("table: %s with invalid json column %s", resource.Table.Name, columnName)
116+
if _, err := json.Marshal(value); err != nil {
117+
t.Errorf("table: %s with invalid json column %s", table.Name, columnName)
104118
}
105119
default:
106120
// todo
107121
}
108122
}
123+
124+
// check that every key in the returned object exist as a column in the table
125+
for key := range data {
126+
if col := resource.Table.Columns.Get(key); col == nil {
127+
t.Errorf("table: %s with unknown column %s", table.Name, key)
128+
}
129+
}
109130
}
110131

111-
// check that every key in the returned object exist as a column in the table
112-
for key := range data {
113-
if col := resource.Table.Columns.Get(key); col == nil {
114-
t.Errorf("table: %s with unknown column %s", resource.Table.Name, key)
132+
// Make sure every column has at least one value.
133+
for _, columnName := range table.Columns.Names() {
134+
if _, ok := columnsWithValues[columnName]; !ok && !table.Columns.Get(columnName).IgnoreInTests {
135+
t.Errorf("Expected column %s to have at least one non-nil value but it was not found", columnName)
115136
}
116137
}
117138
}

0 commit comments

Comments
 (0)