From e1bb149120c01da1ccab490d5cbc643a558f9167 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 15:47:31 +0200 Subject: [PATCH 1/6] ignore in tests should be accounted for values chack --- plugins/source_testing.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/source_testing.go b/plugins/source_testing.go index e44a00b486..3636b618dd 100644 --- a/plugins/source_testing.go +++ b/plugins/source_testing.go @@ -107,7 +107,13 @@ func validateResources(t *testing.T, resources []*schema.Resource) { // Make sure every column has at least one value. for i, hasValue := range columnsWithValues { - if !hasValue && !(table.Columns[i].Name == "_cq_parent_id" && table.Parent == nil) { + col := table.Columns[i] + switch { + case col.IgnoreInTests, + hasValue, + col.Name == "_cq_parent_id" && table.Parent != nil: + // nop + default: t.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) } } From 786296bac8335c9861e06e0ec5c158f0db72f2bf Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 17:21:18 +0200 Subject: [PATCH 2/6] add test --- plugins/source_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugins/source_test.go b/plugins/source_test.go index 63b60d87e9..51dc73fb39 100644 --- a/plugins/source_test.go +++ b/plugins/source_test.go @@ -296,3 +296,22 @@ func testSyncTable(t *testing.T, tc syncTestCase) { t.Fatal(err) } } + +func TestIgnoredColumns(t *testing.T) { + validateResources(t, schema.Resources{ + { + Item: struct { + A *string + }{}, + Table: &schema.Table{ + Columns: schema.ColumnList{ + { + Name: "a", + Type: schema.TypeString, + IgnoreInTests: true, + }, + }, + }, + }, + }) +} From 2966eff5d4654f61d3ff57a8f8f21d43f4e9c6f1 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 17:30:47 +0200 Subject: [PATCH 3/6] test positive & negative --- plugins/source_test.go | 20 ++++++++++++++++++-- plugins/source_testing.go | 11 ++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/plugins/source_test.go b/plugins/source_test.go index 51dc73fb39..7caef23a34 100644 --- a/plugins/source_test.go +++ b/plugins/source_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" "github.com/rs/zerolog" + "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" ) @@ -298,7 +299,7 @@ func testSyncTable(t *testing.T, tc syncTestCase) { } func TestIgnoredColumns(t *testing.T) { - validateResources(t, schema.Resources{ + require.NoError(t, validateResources(schema.Resources{ { Item: struct { A *string @@ -313,5 +314,20 @@ func TestIgnoredColumns(t *testing.T) { }, }, }, - }) + })) + require.Error(t, validateResources(schema.Resources{ + { + Item: struct { + A *string + }{}, + Table: &schema.Table{ + Columns: schema.ColumnList{ + { + Name: "a", + Type: schema.TypeString, + }, + }, + }, + }, + })) } diff --git a/plugins/source_testing.go b/plugins/source_testing.go index 3636b618dd..d83e6dc085 100644 --- a/plugins/source_testing.go +++ b/plugins/source_testing.go @@ -2,10 +2,12 @@ package plugins import ( "context" + "fmt" "testing" "github.com/cloudquery/plugin-sdk/schema" "github.com/cloudquery/plugin-sdk/specs" + "github.com/stretchr/testify/require" ) func TestSourcePluginSync(t *testing.T, plugin *SourcePlugin, spec specs.Source, opts ...TestSourcePluginOption) { @@ -73,7 +75,7 @@ func validateTable(t *testing.T, table *schema.Table, resources []*schema.Resour t.Errorf("Expected table %s to be synced but it was not found", table.Name) return } - validateResources(t, tableResources) + require.NoError(t, validateResources(tableResources)) } func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Resource) { @@ -86,9 +88,7 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso // Validates that every column has at least one non-nil value. // Also does some additional validations. -func validateResources(t *testing.T, resources []*schema.Resource) { - t.Helper() - +func validateResources(resources []*schema.Resource) error { table := resources[0].Table // A set of column-names that have values in at least one of the resources. @@ -114,7 +114,8 @@ func validateResources(t *testing.T, resources []*schema.Resource) { col.Name == "_cq_parent_id" && table.Parent != nil: // nop default: - t.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) + return fmt.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) } } + return nil } From 9ddf19e5a6337fae4d52d592ed44cd1924203b68 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 18:11:21 +0200 Subject: [PATCH 4/6] allow passing tests --- plugins/source_test.go | 8 +++++--- plugins/source_testing.go | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/plugins/source_test.go b/plugins/source_test.go index 7caef23a34..5a3e8e9e4f 100644 --- a/plugins/source_test.go +++ b/plugins/source_test.go @@ -299,7 +299,7 @@ func testSyncTable(t *testing.T, tc syncTestCase) { } func TestIgnoredColumns(t *testing.T) { - require.NoError(t, validateResources(schema.Resources{ + require.Empty(t, validateResourcesHelper(schema.Resources{ { Item: struct { A *string @@ -315,7 +315,7 @@ func TestIgnoredColumns(t *testing.T) { }, }, })) - require.Error(t, validateResources(schema.Resources{ + errs := validateResourcesHelper(schema.Resources{ { Item: struct { A *string @@ -329,5 +329,7 @@ func TestIgnoredColumns(t *testing.T) { }, }, }, - })) + }) + require.NotEmpty(t, errs) + require.Error(t, errs[0]) } diff --git a/plugins/source_testing.go b/plugins/source_testing.go index d83e6dc085..47c77ff16e 100644 --- a/plugins/source_testing.go +++ b/plugins/source_testing.go @@ -7,7 +7,6 @@ import ( "github.com/cloudquery/plugin-sdk/schema" "github.com/cloudquery/plugin-sdk/specs" - "github.com/stretchr/testify/require" ) func TestSourcePluginSync(t *testing.T, plugin *SourcePlugin, spec specs.Source, opts ...TestSourcePluginOption) { @@ -75,7 +74,7 @@ func validateTable(t *testing.T, table *schema.Table, resources []*schema.Resour t.Errorf("Expected table %s to be synced but it was not found", table.Name) return } - require.NoError(t, validateResources(tableResources)) + validateResources(t, tableResources) } func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Resource) { @@ -88,7 +87,17 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso // Validates that every column has at least one non-nil value. // Also does some additional validations. -func validateResources(resources []*schema.Resource) error { +func validateResources(t *testing.T, resources []*schema.Resource) { + t.Helper() + for _, err := range validateResourcesHelper(resources) { + if err != nil { + t.Error(err) + } + } +} + +func validateResourcesHelper(resources []*schema.Resource) []error { + var errs []error table := resources[0].Table // A set of column-names that have values in at least one of the resources. @@ -114,8 +123,8 @@ func validateResources(resources []*schema.Resource) error { col.Name == "_cq_parent_id" && table.Parent != nil: // nop default: - return fmt.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) + errs = append(errs, fmt.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name)) } } - return nil + return errs } From 782e004aad3487d61cf53447c4b4a0d0514afff0 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 19:17:02 +0200 Subject: [PATCH 5/6] ret old signature --- plugins/source_test.go | 40 +++++++++------------------------------ plugins/source_testing.go | 13 +------------ 2 files changed, 10 insertions(+), 43 deletions(-) diff --git a/plugins/source_test.go b/plugins/source_test.go index 5a3e8e9e4f..df03e5e6ba 100644 --- a/plugins/source_test.go +++ b/plugins/source_test.go @@ -9,7 +9,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" "github.com/rs/zerolog" - "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" ) @@ -299,37 +298,16 @@ func testSyncTable(t *testing.T, tc syncTestCase) { } func TestIgnoredColumns(t *testing.T) { - require.Empty(t, validateResourcesHelper(schema.Resources{ - { - Item: struct { - A *string - }{}, - Table: &schema.Table{ - Columns: schema.ColumnList{ - { - Name: "a", - Type: schema.TypeString, - IgnoreInTests: true, - }, - }, - }, - }, - })) - errs := validateResourcesHelper(schema.Resources{ - { - Item: struct { - A *string - }{}, - Table: &schema.Table{ - Columns: schema.ColumnList{ - { - Name: "a", - Type: schema.TypeString, - }, + validateResources(t, schema.Resources{{ + Item: struct{ A *string }{}, + Table: &schema.Table{ + Columns: schema.ColumnList{ + { + Name: "a", + Type: schema.TypeString, + IgnoreInTests: true, }, }, }, - }) - require.NotEmpty(t, errs) - require.Error(t, errs[0]) + }}) } diff --git a/plugins/source_testing.go b/plugins/source_testing.go index 47c77ff16e..c981963929 100644 --- a/plugins/source_testing.go +++ b/plugins/source_testing.go @@ -2,7 +2,6 @@ package plugins import ( "context" - "fmt" "testing" "github.com/cloudquery/plugin-sdk/schema" @@ -89,15 +88,6 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso // Also does some additional validations. func validateResources(t *testing.T, resources []*schema.Resource) { t.Helper() - for _, err := range validateResourcesHelper(resources) { - if err != nil { - t.Error(err) - } - } -} - -func validateResourcesHelper(resources []*schema.Resource) []error { - var errs []error table := resources[0].Table // A set of column-names that have values in at least one of the resources. @@ -123,8 +113,7 @@ func validateResourcesHelper(resources []*schema.Resource) []error { col.Name == "_cq_parent_id" && table.Parent != nil: // nop default: - errs = append(errs, fmt.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name)) + t.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) } } - return errs } From 5ef5e7a2cd1433feb0dc34b5ad3d0240a9556062 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 24 Nov 2022 19:19:26 +0200 Subject: [PATCH 6/6] ease if --- plugins/source_testing.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/source_testing.go b/plugins/source_testing.go index c981963929..5cd054e66c 100644 --- a/plugins/source_testing.go +++ b/plugins/source_testing.go @@ -88,6 +88,7 @@ func validateTables(t *testing.T, tables schema.Tables, resources []*schema.Reso // Also does some additional validations. func validateResources(t *testing.T, resources []*schema.Resource) { t.Helper() + table := resources[0].Table // A set of column-names that have values in at least one of the resources. @@ -107,12 +108,8 @@ func validateResources(t *testing.T, resources []*schema.Resource) { // Make sure every column has at least one value. for i, hasValue := range columnsWithValues { col := table.Columns[i] - switch { - case col.IgnoreInTests, - hasValue, - col.Name == "_cq_parent_id" && table.Parent != nil: - // nop - default: + emptyExpected := col.Name == "_cq_parent_id" && table.Parent == nil + if !hasValue && !emptyExpected && !col.IgnoreInTests { t.Errorf("table: %s column %s has no values", table.Name, table.Columns[i].Name) } }