Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const (
// DefaultProjectID is the default project ID for the Datastore container.
DefaultProjectID = "test-project"
)
Variables ¶
View Source
var WithProjectID = shared.WithProjectID
WithProjectID re-exports the common GCloud WithProjectID option
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
// contains filtered or unexported fields
}
Container represents the Datastore container type used in the module
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
Run creates an instance of the Datastore GCloud container type. The URI uses the empty string as the protocol.
Example ¶
// runDatastoreContainer {
ctx := context.Background()
datastoreContainer, err := tcdatastore.Run(
ctx,
"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
tcdatastore.WithProjectID("datastore-project"),
)
defer func() {
if err := testcontainers.TerminateContainer(datastoreContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to run container: %v", err)
return
}
// }
// datastoreClient {
projectID := datastoreContainer.ProjectID()
options := []option.ClientOption{
option.WithEndpoint(datastoreContainer.URI()),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
}
dsClient, err := datastore.NewClient(ctx, projectID, options...)
if err != nil {
log.Printf("failed to create client: %v", err)
return
}
defer dsClient.Close()
// }
type Task struct {
Description string
}
k := datastore.NameKey("Task", "sample", nil)
data := Task{
Description: "my description",
}
_, err = dsClient.Put(ctx, k, &data)
if err != nil {
log.Printf("failed to put data: %v", err)
return
}
saved := Task{}
err = dsClient.Get(ctx, k, &saved)
if err != nil {
log.Printf("failed to get data: %v", err)
return
}
fmt.Println(saved.Description)
Output: my description
Click to show internal directories.
Click to hide internal directories.