Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
snapshot testing sdk (#137)
Browse files Browse the repository at this point in the history
Change `TestResource` both for mock testing and integration tests
  • Loading branch information
yevgenypats authored Dec 29, 2021
1 parent 258b514 commit 5ccec20
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 622 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.6.0-beta] - 2021-12-20
### :gear: Changed
* **Breaking Change**: changed `TestResource` API [#137](https://github.com/cloudquery/cq-provider-sdk/pull/137)

## [v0.5.7]- 2021-12-20

### :gear: Changed
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang-migrate/migrate/v4 v4.15.0
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.3.0
github.com/hashicorp/go-hclog v1.0.0
github.com/hashicorp/go-plugin v1.4.3
Expand All @@ -25,6 +26,7 @@ require (
github.com/jackc/pgx/v4 v4.13.0
github.com/mitchellh/hashstructure v1.1.0
github.com/modern-go/reflect2 v1.0.2
github.com/sergi/go-diff v1.2.0
github.com/spf13/afero v1.6.0
github.com/spf13/cast v1.4.1
github.com/stretchr/testify v1.7.0
Expand All @@ -46,7 +48,6 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
Expand Down
6 changes: 3 additions & 3 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func (p *Provider) GetProviderConfig(_ context.Context, _ *cqproto.GetProviderCo

func (p *Provider) ConfigureProvider(_ context.Context, request *cqproto.ConfigureProviderRequest) (*cqproto.ConfigureProviderResponse, error) {
if p.meta != nil {
return &cqproto.ConfigureProviderResponse{Error: fmt.Sprintf("provider %s was already configured", p.Name)}, nil
return &cqproto.ConfigureProviderResponse{Error: fmt.Sprintf("provider %s was already configured", p.Name)}, fmt.Errorf("provider %s was already configured", p.Name)
}
if p.Logger == nil {
return &cqproto.ConfigureProviderResponse{Error: fmt.Sprintf("provider %s logger not defined, make sure to run it with serve", p.Name)}, nil
return &cqproto.ConfigureProviderResponse{Error: fmt.Sprintf("provider %s logger not defined, make sure to run it with serve", p.Name)}, fmt.Errorf("provider %s logger not defined, make sure to run it with serve", p.Name)
}
// set database creator
if p.databaseCreator == nil {
Expand All @@ -116,7 +116,7 @@ func (p *Provider) ConfigureProvider(_ context.Context, request *cqproto.Configu
// if we received an empty config we notify in log and only use defaults.
if len(request.Config) == 0 {
p.Logger.Info("Received empty configuration, using only defaults")
} else if err := hclsimple.Decode("config.json", request.Config, nil, providerConfig); err != nil {
} else if err := hclsimple.Decode("config.hcl", request.Config, nil, providerConfig); err != nil {
p.Logger.Error("Failed to load configuration.", "error", err)
return &cqproto.ConfigureProviderResponse{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestProvider_ConfigureProvider(t *testing.T) {
ExtraFields: nil,
})
assert.Equal(t, "provider unitest logger not defined, make sure to run it with serve", resp.Error)
assert.Nil(t, err)
assert.NotNil(t, err)
// set logger this time
tp.Logger = hclog.Default()
resp, err = tp.ConfigureProvider(context.Background(), &cqproto.ConfigureProviderRequest{
Expand Down
3 changes: 3 additions & 0 deletions provider/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type Column struct {
IgnoreError IgnoreErrorFunc
// Creation options allow modifying how column is defined when table is created
CreationOptions ColumnCreationOptions
// IgnoreInIntTests if true tests integration tests wont compare this column
// in snapshot testing. Usually used for columns like: meta, last_updated
IgnoreInIntTests bool

// meta holds serializable information about the column's resolvers and functions
meta *ColumnMeta
Expand Down
10 changes: 10 additions & 0 deletions provider/schema/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schema
import (
"crypto"
"fmt"
"strings"

"github.com/mitchellh/hashstructure"
"github.com/thoas/go-funk"
Expand Down Expand Up @@ -105,6 +106,15 @@ func (r *Resource) GenerateCQId() error {
return nil
}

func (r Resource) getColumnByName(column string) *Column {
for _, c := range r.table.Columns {
if strings.Compare(column, c.Name) == 0 {
return &c
}
}
return nil
}

func hashUUID(objs interface{}) (uuid.UUID, error) {
// Use SHA1 because it's fast and is reasonably enough protected against accidental collisions.
// There is no scenario here where intentional created collisions could do harm.
Expand Down
Loading

0 comments on commit 5ccec20

Please sign in to comment.