Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const (
// DefaultProjectID is the default project ID for the Firestore 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 Firestore 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 Firestore GCloud container type. The URI uses the empty string as the protocol.
Example ¶
// runFirestoreContainer {
ctx := context.Background()
firestoreContainer, err := tcfirestore.Run(
ctx,
"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
tcfirestore.WithProjectID("firestore-project"),
)
defer func() {
if err := testcontainers.TerminateContainer(firestoreContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to run container: %v", err)
return
}
// }
// firestoreClient {
projectID := firestoreContainer.ProjectID()
conn, err := grpc.NewClient(firestoreContainer.URI(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
if err != nil {
log.Printf("failed to dial: %v", err)
return
}
options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := firestore.NewClient(ctx, projectID, options...)
if err != nil {
log.Printf("failed to create client: %v", err)
return
}
defer client.Close()
// }
users := client.Collection("users")
docRef := users.Doc("alovelace")
type Person struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
data := Person{
Firstname: "Ada",
Lastname: "Lovelace",
}
_, err = docRef.Create(ctx, data)
if err != nil {
log.Printf("failed to create document: %v", err)
return
}
docsnap, err := docRef.Get(ctx)
if err != nil {
log.Printf("failed to get document: %v", err)
return
}
var saved Person
if err := docsnap.DataTo(&saved); err != nil {
log.Printf("failed to convert data: %v", err)
return
}
fmt.Println(saved.Firstname, saved.Lastname)
Output: Ada Lovelace
Example (DatastoreMode) ¶
ctx := context.Background()
firestoreContainer, err := tcfirestore.Run(
ctx,
"gcr.io/google.com/cloudsdktool/cloud-sdk:513.0.0-emulators",
tcfirestore.WithProjectID("firestore-project"),
tcfirestore.WithDatastoreMode(),
)
defer func() {
if err := testcontainers.TerminateContainer(firestoreContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to run container: %v", err)
return
}
projectID := firestoreContainer.ProjectID()
conn, err := grpc.NewClient(firestoreContainer.URI(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
if err != nil {
log.Printf("failed to dial: %v", err)
return
}
options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := datastore.NewClient(ctx, projectID, options...)
if err != nil {
log.Printf("failed to create client: %v", err)
return
}
defer client.Close()
userKey := datastore.NameKey("users", "alovelace", nil)
type Person struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
data := Person{
Firstname: "Ada",
Lastname: "Lovelace",
}
_, err = client.Put(ctx, userKey, &data)
if err != nil {
log.Printf("failed to create entity: %v", err)
return
}
saved := Person{}
err = client.Get(ctx, userKey, &saved)
if err != nil {
log.Printf("failed to get entity: %v", err)
return
}
fmt.Println(saved.Firstname, saved.Lastname)
Output: Ada Lovelace
type Option ¶
type Option func(o *options) error
func WithDatastoreMode ¶
func WithDatastoreMode() Option
WithDatastoreMode sets the firestore emulator to run in datastore mode. Requires a cloud-sdk image with version 465.0.0 or higher
func (Option) Customize ¶
func (o Option) Customize(*testcontainers.GenericContainerRequest) error
Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
Click to show internal directories.
Click to hide internal directories.