| | | 1 | | using Google.Cloud.Firestore; |
| | | 2 | | using Grpc.Core; |
| | | 3 | | using Testcontainers.Firestore; |
| | | 4 | | using TUnit.Core.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace TestUtilities; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Shared Testcontainers-based Firestore emulator fixture for integration tests. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// This fixture is shared across test projects so repository tests and higher-level |
| | | 13 | | /// integration tests use the same emulator lifecycle and collection-clearing behavior. |
| | | 14 | | /// </remarks> |
| | | 15 | | public sealed class FirestoreFixture : IAsyncInitializer, IAsyncDisposable |
| | | 16 | | { |
| | | 17 | | public const string SharedKey = "FirestoreEmulator"; |
| | | 18 | | public const string ContextDocumentsParallelKey = "Firestore:ContextDocuments"; |
| | | 19 | | public const string PredictionsParallelKey = "Firestore:Predictions"; |
| | | 20 | | public const string KpiParallelKey = "Firestore:Kpi"; |
| | | 21 | | public const string OrchestratorIntegrationParallelKey = "Firestore:OrchestratorIntegration"; |
| | | 22 | | |
| | | 23 | | private const string ContextDocumentsCollection = "context-documents"; |
| | | 24 | | private const string MatchPredictionsCollection = "match-predictions"; |
| | | 25 | | private const string MatchesCollection = "matches"; |
| | | 26 | | private const string BonusPredictionsCollection = "bonus-predictions"; |
| | | 27 | | private const string KpiDocumentsCollection = "kpi-documents"; |
| | | 28 | | private const string EmulatorImageTag = "google/cloud-sdk:550.0.0-emulators"; |
| | | 29 | | |
| | | 30 | | private readonly FirestoreContainer _container; |
| | | 31 | | |
| | 1 | 32 | | public FirestoreDb Db { get; private set; } = null!; |
| | 1 | 33 | | public string ProjectId { get; } = $"test-project-{Guid.NewGuid():N}"; |
| | | 34 | | |
| | 1 | 35 | | public FirestoreFixture() |
| | | 36 | | { |
| | 1 | 37 | | _container = new FirestoreBuilder() |
| | 1 | 38 | | .WithImage(EmulatorImageTag) |
| | 1 | 39 | | .WithEnvironment("CLOUDSDK_CORE_CHECK_GCE_METADATA", "false") |
| | 1 | 40 | | .Build(); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task InitializeAsync() |
| | | 44 | | { |
| | 1 | 45 | | await _container.StartAsync(); |
| | | 46 | | |
| | 1 | 47 | | var firestoreDbBuilder = new FirestoreDbBuilder |
| | 1 | 48 | | { |
| | 1 | 49 | | ProjectId = ProjectId, |
| | 1 | 50 | | Endpoint = _container.GetEmulatorEndpoint(), |
| | 1 | 51 | | ChannelCredentials = ChannelCredentials.Insecure |
| | 1 | 52 | | }; |
| | | 53 | | |
| | 1 | 54 | | Db = await firestoreDbBuilder.BuildAsync(); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task ClearContextDocumentsAsync() |
| | | 58 | | { |
| | 1 | 59 | | await ClearCollectionAsync(ContextDocumentsCollection); |
| | 1 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task ClearPredictionsAsync() |
| | | 63 | | { |
| | 1 | 64 | | await Task.WhenAll( |
| | 1 | 65 | | ClearCollectionAsync(MatchPredictionsCollection), |
| | 1 | 66 | | ClearCollectionAsync(MatchesCollection), |
| | 1 | 67 | | ClearCollectionAsync(BonusPredictionsCollection)); |
| | 1 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task ClearKpiDocumentsAsync() |
| | | 71 | | { |
| | 1 | 72 | | await ClearCollectionAsync(KpiDocumentsCollection); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task ClearOrchestratorIntegrationAsync() |
| | | 76 | | { |
| | 1 | 77 | | await Task.WhenAll( |
| | 1 | 78 | | ClearPredictionsAsync(), |
| | 1 | 79 | | ClearContextDocumentsAsync()); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | private async Task ClearCollectionAsync(string collectionName) |
| | | 83 | | { |
| | 1 | 84 | | using var httpClient = new HttpClient(); |
| | 1 | 85 | | var endpoint = _container.GetEmulatorEndpoint().TrimEnd('/'); |
| | 1 | 86 | | var deleteUrl = $"{endpoint}/emulator/v1/projects/{ProjectId}/databases/(default)/documents/{collectionName}"; |
| | | 87 | | |
| | 1 | 88 | | var response = await httpClient.DeleteAsync(deleteUrl); |
| | 1 | 89 | | if (!response.IsSuccessStatusCode && response.StatusCode != System.Net.HttpStatusCode.NotFound) |
| | | 90 | | { |
| | 0 | 91 | | response.EnsureSuccessStatusCode(); |
| | | 92 | | } |
| | 1 | 93 | | } |
| | | 94 | | |
| | | 95 | | public async ValueTask DisposeAsync() |
| | | 96 | | { |
| | 1 | 97 | | await _container.DisposeAsync(); |
| | 1 | 98 | | } |
| | | 99 | | } |