Skip to content

Commit 98e8999

Browse files
authored
feat: Validate undefined column in TestResource (#144)
I found quite a few issue where we use resource.Set but we dont define the column and the destination pluginfails to write because the column doesn't exist
1 parent 093ee5e commit 98e8999

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

plugins/source_testing.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plugins
22

33
import (
44
"context"
5+
"encoding/json"
56
"testing"
67

78
"github.com/cloudquery/faker/v3"
@@ -75,9 +76,36 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso
7576

7677
func validateResource(t *testing.T, resource *schema.Resource) {
7778
t.Helper()
79+
// we want to marshal and unmarshal to mimic over-the-wire behavior
80+
b, err := json.Marshal(resource.Data)
81+
if err != nil {
82+
t.Fatalf("failed to marshal resource data: %v", err)
83+
}
84+
var data map[string]interface{}
85+
if err := json.Unmarshal(b, &data); err != nil {
86+
t.Fatalf("failed to unmarshal resource data: %v", err)
87+
}
7888
for _, columnName := range resource.Table.Columns.Names() {
79-
if resource.Get(columnName) == nil && !resource.Table.Columns.Get(columnName).IgnoreInTests {
89+
if data[columnName] == nil && !resource.Table.Columns.Get(columnName).IgnoreInTests {
8090
t.Errorf("table: %s with unset column %s", resource.Table.Name, columnName)
8191
}
92+
val := data[columnName]
93+
if val != nil {
94+
switch resource.Table.Columns.Get(columnName).Type {
95+
case schema.TypeJSON:
96+
if _, err := json.Marshal(val); err != nil {
97+
t.Errorf("table: %s with invalid json column %s", resource.Table.Name, columnName)
98+
}
99+
default:
100+
// todo
101+
}
102+
}
103+
}
104+
105+
// check that every key in the returned object exist as a column in the table
106+
for key := range data {
107+
if col := resource.Table.Columns.Get(key); col == nil {
108+
t.Errorf("table: %s with unknown column %s", resource.Table.Name, key)
109+
}
82110
}
83111
}

0 commit comments

Comments
 (0)