Skip to content

Commit

Permalink
chore: do not allow multiple calls to WithDataYAML
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Dec 11, 2024
1 parent cd8324c commit f447009
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/modules/gcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ An example of a `data.yaml` file that seeds the BigQuery instance with datasets
<!--/codeinclude-->

!!!warning
This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYAML` options, only the last one is used.
This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYAML` options, an error is returned.

### BigTable

Expand Down
44 changes: 1 addition & 43 deletions modules/gcloud/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,47 +160,5 @@ func TestBigQueryWithDataYamlFile_multiple(t *testing.T) {
gcloud.WithDataYAML(r2), // last file will be used
)
testcontainers.CleanupContainer(t, bigQueryContainer)
require.NoError(t, err)

projectID := bigQueryContainer.Settings.ProjectID

opts := []option.ClientOption{
option.WithEndpoint(bigQueryContainer.URI),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
internaloption.SkipDialSettingsValidation(),
}

client, err := bigquery.NewClient(ctx, projectID, opts...)
require.NoError(t, err)
defer client.Close()

t.Run("select/dataset1-not-found", func(t *testing.T) {
selectQuery := client.Query("SELECT * FROM dataset1.table_a where name = @name")
selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{
{Name: "name", Value: "bob"},
}
_, err := selectQuery.Read(ctx)
require.Error(t, err)
})

t.Run("select/dataset2", func(t *testing.T) {
selectQuery := client.Query("SELECT * FROM dataset2.table_b where name = @name")
selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{
{Name: "name", Value: "naomi"},
}
it, err := selectQuery.Read(ctx)
require.NoError(t, err)

var val []bigquery.Value
for {
err := it.Next(&val)
if errors.Is(err, iterator.Done) {
break
}
require.NoError(t, err)
}

require.Equal(t, int64(60), val[0])
})
require.Error(t, err)
}
17 changes: 13 additions & 4 deletions modules/gcloud/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcloud

import (
"context"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -59,7 +60,7 @@ func defaultOptions() options {
var _ testcontainers.ContainerCustomizer = (*Option)(nil)

// Option is an option for the GCloud container.
type Option func(*options)
type Option func(*options) error

// Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
func (o Option) Customize(*testcontainers.GenericContainerRequest) error {
Expand All @@ -69,8 +70,9 @@ func (o Option) Customize(*testcontainers.GenericContainerRequest) error {

// WithProjectID sets the project ID for the GCloud container.
func WithProjectID(projectID string) Option {
return func(o *options) {
return func(o *options) error {
o.ProjectID = projectID
return nil
}
}

Expand All @@ -81,8 +83,13 @@ func WithProjectID(projectID string) Option {
// Other GCloud containers will ignore this option.
// If this option is passed multiple times, the last added will be used.
func WithDataYAML(r io.Reader) Option {
return func(o *options) {
return func(o *options) error {
if o.bigQueryDataYaml != nil {
return errors.New("data yaml file already exists")
}

o.bigQueryDataYaml = r
return nil
}
}

Expand All @@ -91,7 +98,9 @@ func applyOptions(req *testcontainers.GenericContainerRequest, opts []testcontai
settings := defaultOptions()
for _, opt := range opts {
if apply, ok := opt.(Option); ok {
apply(&settings)
if err := apply(&settings); err != nil {
return options{}, err
}
}
if err := opt.Customize(req); err != nil {
return options{}, err
Expand Down

0 comments on commit f447009

Please sign in to comment.