-
Notifications
You must be signed in to change notification settings - Fork 270
feat(firestore): Added Firestore Multi Database Support #733
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
base: dev
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 |
|---|---|---|
|
|
@@ -20,9 +20,27 @@ import ( | |
| "reflect" | ||
| "testing" | ||
|
|
||
| "time" | ||
|
|
||
| "cloud.google.com/go/firestore" | ||
| "firebase.google.com/go/v4/integration/internal" | ||
| ) | ||
|
|
||
| var ( | ||
| cityData = map[string]interface{}{ | ||
| "name": "Mountain View", | ||
| "country": "USA", | ||
| "population": int64(77846), | ||
| "capital": false, | ||
| } | ||
| movieData = map[string]interface{}{ | ||
| "Name": "Interstellar", | ||
| "Year": int64(2014), | ||
| "Runtime": "2h 49m", | ||
| "Academy Award Winner": true, | ||
| } | ||
| ) | ||
|
|
||
| func TestFirestore(t *testing.T) { | ||
| if testing.Short() { | ||
| log.Println("skipping Firestore integration tests in short mode.") | ||
|
|
@@ -40,11 +58,130 @@ func TestFirestore(t *testing.T) { | |
| } | ||
|
|
||
| doc := client.Collection("cities").Doc("Mountain View") | ||
| if _, err := doc.Set(ctx, cityData); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| snap, err := doc.Get(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(snap.Data(), cityData) { | ||
| t.Errorf("Get() = %v; want %v", snap.Data(), cityData) | ||
| } | ||
| if _, err := doc.Delete(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
Comment on lines
+61
to
+73
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. To ensure test resources are cleaned up even if assertions fail, it's better to use if _, err := doc.Set(ctx, cityData); err != nil {
t.Fatal(err)
}
defer doc.Delete(ctx)
snap, err := doc.Get(ctx)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(snap.Data(), cityData) {
t.Errorf("Get() = %v; want %v", snap.Data(), cityData)
} |
||
| } | ||
|
|
||
| func TestFirestoreWithDatabase(t *testing.T) { | ||
| if testing.Short() { | ||
| log.Println("skipping Firestore integration tests in short mode.") | ||
| return | ||
| } | ||
| ctx := context.Background() | ||
| app, err := internal.NewTestApp(ctx, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // This test requires a non-default database to exist in the project. | ||
| // If it doesn't exist, this test will fail. | ||
| client, err := app.FirestoreWithDatabase(ctx, "testing-database") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| doc := client.Collection("cities").NewDoc() | ||
| if _, err := doc.Set(ctx, cityData); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| snap, err := doc.Get(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(snap.Data(), cityData) { | ||
| t.Errorf("Get() = %v; want %v", snap.Data(), cityData) | ||
| } | ||
| if _, err := doc.Delete(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
Comment on lines
+95
to
+107
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. To ensure test resources are cleaned up even if assertions fail, it's better to use if _, err := doc.Set(ctx, cityData); err != nil {
t.Fatal(err)
}
defer doc.Delete(ctx)
snap, err := doc.Get(ctx)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(snap.Data(), cityData) {
t.Errorf("Get() = %v; want %v", snap.Data(), cityData)
} |
||
| } | ||
|
|
||
| func TestFirestoreMultiDB(t *testing.T) { | ||
| if testing.Short() { | ||
| log.Println("skipping Firestore integration tests in short mode.") | ||
| return | ||
| } | ||
| ctx := context.Background() | ||
| app, err := internal.NewTestApp(ctx, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| cityClient, err := app.Firestore(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| // This test requires a non-default database to exist in the project. | ||
| movieClient, err := app.FirestoreWithDatabase(ctx, "testing-database") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| cityDoc := cityClient.Collection("cities").NewDoc() | ||
| movieDoc := movieClient.Collection("movies").NewDoc() | ||
|
|
||
| if _, err := cityDoc.Set(ctx, cityData); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := movieDoc.Set(ctx, movieData); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| citySnap, err := cityDoc.Get(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| movieSnap, err := movieDoc.Get(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(citySnap.Data(), cityData) { | ||
| t.Errorf("City Get() = %v; want %v", citySnap.Data(), cityData) | ||
| } | ||
| if !reflect.DeepEqual(movieSnap.Data(), movieData) { | ||
| t.Errorf("Movie Get() = %v; want %v", movieSnap.Data(), movieData) | ||
| } | ||
|
|
||
| if _, err := cityDoc.Delete(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := movieDoc.Delete(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
Comment on lines
+134
to
+162
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. To ensure test resources are cleaned up even if assertions fail, it's better to use if _, err := cityDoc.Set(ctx, cityData); err != nil {
t.Fatal(err)
}
defer cityDoc.Delete(ctx)
if _, err := movieDoc.Set(ctx, movieData); err != nil {
t.Fatal(err)
}
defer movieDoc.Delete(ctx)
citySnap, err := cityDoc.Get(ctx)
if err != nil {
t.Fatal(err)
}
movieSnap, err := movieDoc.Get(ctx)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(citySnap.Data(), cityData) {
t.Errorf("City Get() = %v; want %v", citySnap.Data(), cityData)
}
if !reflect.DeepEqual(movieSnap.Data(), movieData) {
t.Errorf("Movie Get() = %v; want %v", movieSnap.Data(), movieData)
} |
||
| } | ||
|
|
||
| func TestServerTimestamp(t *testing.T) { | ||
| if testing.Short() { | ||
| log.Println("skipping Firestore integration tests in short mode.") | ||
| return | ||
| } | ||
| ctx := context.Background() | ||
| app, err := internal.NewTestApp(ctx, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| client, err := app.Firestore(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| doc := client.Collection("cities").NewDoc() | ||
| data := map[string]interface{}{ | ||
| "name": "Mountain View", | ||
| "country": "USA", | ||
| "population": int64(77846), | ||
| "capital": false, | ||
| "name": "Mountain View", | ||
| "timestamp": firestore.ServerTimestamp, | ||
| } | ||
| if _, err := doc.Set(ctx, data); err != nil { | ||
| t.Fatal(err) | ||
|
|
@@ -53,8 +190,12 @@ func TestFirestore(t *testing.T) { | |
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(snap.Data(), data) { | ||
| t.Errorf("Get() = %v; want %v", snap.Data(), data) | ||
| got := snap.Data() | ||
| if got["name"] != "Mountain View" { | ||
| t.Errorf("Name = %v; want Mountain View", got["name"]) | ||
| } | ||
| if _, ok := got["timestamp"].(time.Time); !ok { | ||
| t.Errorf("Timestamp is not a time.Time: %v", got["timestamp"]) | ||
| } | ||
| if _, err := doc.Delete(ctx); err != nil { | ||
| t.Fatal(err) | ||
|
|
||
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.
This project ID check is duplicated from the
Firestore()function. To improve maintainability and reduce code duplication, consider refactoringFirestore()to callFirestoreWithDatabase()with the default database ID. This would centralize the client creation logic and the project ID validation.