Skip to content

Commit d979863

Browse files
authored
fix(testing): Validate all tables and relations (#85)
I was expecting my Azure mock tests that have relations to fail as I don't handle mocking the related APIs. I was surprised to see them pass 🙃 The reason is that we need to validate that we got the related resources too. ---
1 parent 9613860 commit d979863

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

plugins/source_testing.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cloudquery/faker/v3"
88
"github.com/cloudquery/plugin-sdk/schema"
99
"github.com/cloudquery/plugin-sdk/specs"
10+
"github.com/stretchr/testify/require"
1011
)
1112

1213
type ResourceTestCase struct {
@@ -23,34 +24,52 @@ func init() {
2324
_ = faker.SetRandomMapAndSliceMaxSize(1)
2425
}
2526

26-
// type
27-
2827
func TestSourcePluginSync(t *testing.T, plugin *SourcePlugin, spec specs.Source) {
29-
// t.Parallel()
3028
t.Helper()
31-
// No need for configuration or db connection, get it out of the way first
32-
// testTableIdentifiersForProvider(t, resource.Provider)
3329

34-
// l := testlog.New(t)
35-
// l.SetLevel(hclog.Info)
36-
// resource.Plugin.Logger = l
37-
resources := make(chan *schema.Resource)
30+
resourcesChannel := make(chan *schema.Resource)
3831
var fetchErr error
3932

4033
go func() {
41-
defer close(resources)
42-
fetchErr = plugin.Sync(context.Background(), spec, resources)
34+
defer close(resourcesChannel)
35+
fetchErr = plugin.Sync(context.Background(), spec, resourcesChannel)
4336
}()
44-
totalResources := 0
45-
for resource := range resources {
46-
totalResources++
47-
validateResource(t, resource)
37+
38+
syncedResources := make([]*schema.Resource, 0)
39+
for resource := range resourcesChannel {
40+
syncedResources = append(syncedResources, resource)
4841
}
49-
if fetchErr != nil {
50-
t.Fatal(fetchErr)
42+
require.NoError(t, fetchErr)
43+
44+
validateTables(t, plugin.Tables(), syncedResources)
45+
}
46+
47+
func getTableResource(t *testing.T, table *schema.Table, resources []*schema.Resource) *schema.Resource {
48+
t.Helper()
49+
for _, resource := range resources {
50+
if resource.Table.Name == table.Name {
51+
return resource
52+
}
5153
}
52-
if totalResources == 0 {
53-
t.Fatal("no resources fetched")
54+
55+
return nil
56+
}
57+
58+
func validateTable(t *testing.T, table *schema.Table, resources []*schema.Resource) {
59+
t.Helper()
60+
resource := getTableResource(t, table, resources)
61+
if resource == nil {
62+
t.Errorf("Expected table %s to be synced but it was not found", table.Name)
63+
return
64+
}
65+
validateResource(t, resource)
66+
}
67+
68+
func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Resource) {
69+
t.Helper()
70+
for _, table := range tables {
71+
validateTable(t, table, resources)
72+
validateTables(t, table.Relations, resources)
5473
}
5574
}
5675

0 commit comments

Comments
 (0)