-
-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support to seed data when using RunBigQueryContainer #2523
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,8 @@ import ( | |||||||||
"errors" | ||||||||||
"fmt" | ||||||||||
"log" | ||||||||||
"path/filepath" | ||||||||||
"testing" | ||||||||||
|
||||||||||
"cloud.google.com/go/bigquery" | ||||||||||
"google.golang.org/api/iterator" | ||||||||||
|
@@ -23,7 +25,7 @@ func ExampleRunBigQueryContainer() { | |||||||||
|
||||||||||
bigQueryContainer, err := gcloud.RunBigQueryContainer( | ||||||||||
ctx, | ||||||||||
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.4.3"), | ||||||||||
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.6.1"), | ||||||||||
gcloud.WithProjectID("bigquery-project"), | ||||||||||
) | ||||||||||
if err != nil { | ||||||||||
|
@@ -78,8 +80,75 @@ func ExampleRunBigQueryContainer() { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
fmt.Println(val) | ||||||||||
fmt.Println(val[0]) | ||||||||||
// Output: | ||||||||||
// 30 | ||||||||||
} | ||||||||||
|
||||||||||
func TestBigQueryWithDataYamlFile(t *testing.T) { | ||||||||||
ctx := context.Background() | ||||||||||
|
||||||||||
absPath, err := filepath.Abs(filepath.Join(".", "testdata", "data.yaml")) | ||||||||||
if err != nil { | ||||||||||
log.Fatalf("failed to run container: %v", err) | ||||||||||
} | ||||||||||
Comment on lines
+92
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: use require.NoError to simplify, more below
Suggested change
|
||||||||||
|
||||||||||
bigQueryContainer, err := gcloud.RunBigQueryContainer( | ||||||||||
ctx, | ||||||||||
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.6.1"), | ||||||||||
gcloud.WithProjectID("test"), | ||||||||||
gcloud.WithDataYamlFile(absPath), | ||||||||||
) | ||||||||||
if err != nil { | ||||||||||
log.Fatalf("failed to run container: %v", err) | ||||||||||
} | ||||||||||
|
||||||||||
defer func() { | ||||||||||
if err := bigQueryContainer.Terminate(ctx); err != nil { | ||||||||||
log.Fatalf("failed to terminate container: %v", err) | ||||||||||
} | ||||||||||
}() | ||||||||||
Comment on lines
+106
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: use |
||||||||||
|
||||||||||
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...) | ||||||||||
if err != nil { | ||||||||||
log.Fatalf("failed to create bigquery client: %v", err) // nolint:gocritic | ||||||||||
} | ||||||||||
defer client.Close() | ||||||||||
|
||||||||||
selectQuery := client.Query("SELECT * FROM dataset1.table_a where name = @name") | ||||||||||
selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{ | ||||||||||
{Name: "name", Value: "bob"}, | ||||||||||
} | ||||||||||
it, err := selectQuery.Read(ctx) | ||||||||||
if err != nil { | ||||||||||
log.Fatalf("failed to read query: %v", err) | ||||||||||
} | ||||||||||
|
||||||||||
var val []bigquery.Value | ||||||||||
for { | ||||||||||
err := it.Next(&val) | ||||||||||
if errors.Is(err, iterator.Done) { | ||||||||||
break | ||||||||||
} | ||||||||||
if err != nil { | ||||||||||
log.Fatalf("failed to iterate: %v", err) | ||||||||||
} | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: left over output comments below. |
||||||||||
|
||||||||||
// Output: | ||||||||||
// [30] | ||||||||||
expectedValue := int64(30) | ||||||||||
actualValue := val[0] | ||||||||||
if expectedValue != actualValue { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: use require.Equal or EqualValue |
||||||||||
t.Errorf("BigQuery value didn't match. \nExpected %v, \nbut got: %v", expectedValue, actualValue) | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,12 +41,14 @@ func newGCloudContainer(ctx context.Context, port int, c testcontainers.Containe | |
} | ||
|
||
type options struct { | ||
ProjectID string | ||
ProjectID string | ||
DataYamlFile string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: this doesn't seem to be ever used. |
||
} | ||
|
||
func defaultOptions() options { | ||
return options{ | ||
ProjectID: defaultProjectID, | ||
ProjectID: defaultProjectID, | ||
DataYamlFile: "/data.yaml", | ||
} | ||
} | ||
|
||
|
@@ -69,6 +71,22 @@ func WithProjectID(projectID string) Option { | |
} | ||
} | ||
|
||
// WithDataYamlFile seeds the Bigquery project for the GCloud container. | ||
func WithDataYamlFile(dataYamlFile string) testcontainers.CustomizeRequestOption { | ||
Comment on lines
+74
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: remove the duplication between function name and paramater |
||
return func(req *testcontainers.GenericContainerRequest) error { | ||
mtellis2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dataFile := testcontainers.ContainerFile{ | ||
HostFilePath: dataYamlFile, | ||
ContainerFilePath: "/data.yaml", | ||
FileMode: 0o755, | ||
} | ||
|
||
req.Files = append(req.Files, dataFile) | ||
req.Cmd = append(req.Cmd, "--data-from-yaml", "/data.yaml") | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// applyOptions applies the options to the container request and returns the settings. | ||
func applyOptions(req *testcontainers.GenericContainerRequest, opts []testcontainers.ContainerCustomizer) (options, error) { | ||
settings := defaultOptions() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
projects: | ||
- id: test | ||
datasets: | ||
- id: dataset1 | ||
tables: | ||
- id: table_a | ||
columns: | ||
- name: id | ||
type: INTEGER | ||
- name: name | ||
type: STRING | ||
- name: createdAt | ||
type: TIMESTAMP | ||
data: | ||
- id: 1 | ||
name: alice | ||
createdAt: "2022-10-21T00:00:00" | ||
- id: 30 | ||
name: bob | ||
createdAt: "2022-10-21T00:00:00" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this needs a rebase as there has been some work to ensure this correctly returns the container even on error.