Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multiple databases
- Loading branch information
Showing
5 changed files
with
197 additions
and
40 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...Google.Cloud.Firestore/Google.Cloud.Firestore.IntegrationTests/FirestoreMultipleDbTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2022, Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Cloud.ClientTesting; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Firestore.IntegrationTests; | ||
|
||
[Collection(nameof(MultipleFirestoreDbFixture))] | ||
public class FirestoreMultipleDbTest | ||
{ | ||
private readonly MultipleFirestoreDbFixture _fixture; | ||
public FirestoreMultipleDbTest(MultipleFirestoreDbFixture multipleFirestoreDbFixture) => | ||
_fixture = multipleFirestoreDbFixture; | ||
|
||
[Fact] | ||
public async Task MultipleDbReadWriteTest() | ||
{ | ||
string dbIdPrefix = IdGenerator.FromGuid(prefix: "db", separator: "-"); | ||
string correctDbId = $"{dbIdPrefix}-correct"; | ||
string incorrectDbId = $"{dbIdPrefix}-incorrect"; | ||
string databaseLocationId = "us-east1"; | ||
string databaseType = "FIRESTORE_NATIVE"; | ||
|
||
// Create two databases one for reading and writing, one for reading to validate that correct database should be referenced. | ||
await _fixture.CreateDatabaseAsync(correctDbId, databaseLocationId, databaseType); | ||
await _fixture.CreateDatabaseAsync(incorrectDbId, databaseLocationId, databaseType); | ||
|
||
// Write data to the first database; referenced as correct database. | ||
FirestoreDb correctDbWriter = new FirestoreDbBuilder | ||
{ | ||
DatabaseId = correctDbId, | ||
ProjectId = _fixture.ProjectId | ||
}.Build(); | ||
await correctDbWriter.Collection("cities").Document("SF").SetAsync(new Dictionary<string, object> | ||
{ | ||
{ "Name", "San Francisco" }, | ||
{ "State", "CA" }, | ||
{ "Country", "USA" }, | ||
{ "Capital", false }, | ||
{ "Population", 860000 } | ||
}); | ||
|
||
// Read data from database that was referenced while writing. | ||
FirestoreDb correctDbReader = new FirestoreDbBuilder | ||
{ | ||
DatabaseId = correctDbId, | ||
ProjectId = _fixture.ProjectId | ||
}.Build(); | ||
DocumentReference readerDocRef = correctDbReader.Collection("cities").Document("SF"); | ||
DocumentSnapshot correctDbSnapshot = await readerDocRef.GetSnapshotAsync(); | ||
Dictionary<string, object> items = correctDbSnapshot.ToDictionary(); | ||
Assert.True(correctDbSnapshot.Exists); | ||
Assert.Equal(5, items.Count); | ||
|
||
// Try to read data from random incorrect database, to validate that the snapshot and data do not exist. | ||
FirestoreDb incorrectDbReader = new FirestoreDbBuilder { DatabaseId = incorrectDbId, ProjectId = _fixture.ProjectId }.Build(); | ||
DocumentReference incorrectDbDocRef = incorrectDbReader.Collection("cities").Document("SF"); | ||
DocumentSnapshot incorrectDbSnapshot = await incorrectDbDocRef.GetSnapshotAsync(); | ||
Assert.False(incorrectDbSnapshot.Exists); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...gle.Cloud.Firestore/Google.Cloud.Firestore.IntegrationTests/MultipleFirestoreDbFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2022, Google Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Apis.Auth.OAuth2; | ||
using Google.Apis.Http; | ||
using Google.Cloud.ClientTesting; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Firestore.IntegrationTests; | ||
|
||
[CollectionDefinition(nameof(MultipleFirestoreDbFixture))] | ||
public class MultipleFirestoreDbFixture : CloudProjectFixtureBase, ICollectionFixture<MultipleFirestoreDbFixture> | ||
{ | ||
private const string RestApiUrlBase = "https://firestore.googleapis.com/v1"; | ||
private const string ProjectEnvironmentVariable = "FIRESTORE_TEST_PROJECT"; | ||
private readonly HttpClient _httpClient; | ||
|
||
public MultipleFirestoreDbFixture() : base(ProjectEnvironmentVariable) | ||
{ | ||
// Scope used for the REST API to create databases. | ||
string scope = "https://www.googleapis.com/auth/datastore"; | ||
string credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); | ||
|
||
// Initalize gcloud credentials to be used with REST API calls. | ||
GoogleCredential googleCredential = GoogleCredential.FromFile(credentialsPath).CreateScoped(scope); | ||
_httpClient = new HttpClientFactory() | ||
.CreateHttpClient(new CreateHttpClientArgs { Initializers = { googleCredential } }); | ||
} | ||
|
||
// TODO: Use Google.Cloud.Firestore.Admin.V1 when that supports this operation. | ||
|
||
/// <summary> | ||
/// Creates a new Firestore database using the REST API. | ||
/// </summary> | ||
public async Task CreateDatabaseAsync(string databaseId, string locationId, string databaseType) | ||
{ | ||
var createDatabaseEndpoint = new Uri($"{RestApiUrlBase}/projects/{ProjectId}/databases?database_id={databaseId}"); | ||
var data = new { locationId = locationId, type = databaseType }; | ||
var httpData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); | ||
var response = await _httpClient.PostAsync(createDatabaseEndpoint, httpData); | ||
response.EnsureSuccessStatusCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters