|
| 1 | +// Copyright (c) Cratis. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Cratis.Chronicle.Auditing; |
| 5 | +using Cratis.Chronicle.Events; |
| 6 | +using Cratis.Chronicle.EventSequences; |
| 7 | +using Cratis.Chronicle.InProcess.Integration.Projections.Scenarios.when_projecting_with_children_within_children_using_parent_key_from_context.Concepts; |
| 8 | +using Cratis.Chronicle.InProcess.Integration.Projections.Scenarios.when_projecting_with_children_within_children_using_parent_key_from_context.Events; |
| 9 | +using Cratis.Chronicle.InProcess.Integration.Projections.Scenarios.when_projecting_with_children_within_children_using_parent_key_from_context.ReadModels; |
| 10 | +using Cratis.Chronicle.Observation; |
| 11 | +using MongoDB.Driver; |
| 12 | +using context = Cratis.Chronicle.InProcess.Integration.Projections.Scenarios.when_projecting_with_children_within_children_using_parent_key_from_context.and_all_events_are_appended_in_one_transaction.context; |
| 13 | + |
| 14 | +namespace Cratis.Chronicle.InProcess.Integration.Projections.Scenarios.when_projecting_with_children_within_children_using_parent_key_from_context; |
| 15 | + |
| 16 | +[Collection(ChronicleCollection.Name)] |
| 17 | +public class and_all_events_are_appended_in_one_transaction(context context) : Given<context>(context) |
| 18 | +{ |
| 19 | + const string SimulationName = "Test Simulation"; |
| 20 | + const string ConfigurationName = "Test Configuration"; |
| 21 | + const string HubName = "Test Hub"; |
| 22 | + |
| 23 | + const double Distance = 100.0; |
| 24 | + const double Time = 2.5; |
| 25 | + const double Cost = 50.0; |
| 26 | + const double Waste = 10.0; |
| 27 | + |
| 28 | + public class context(ChronicleInProcessFixture fixture) : Specification(fixture) |
| 29 | + { |
| 30 | + public SimulationId SimulationId; |
| 31 | + public ConfigurationId ConfigurationId; |
| 32 | + public HubId HubId; |
| 33 | + public IEnumerable<FailedPartition> FailedPartitions = []; |
| 34 | + public SimulationDashboard Result; |
| 35 | + public EventSequenceNumber LastEventSequenceNumber = EventSequenceNumber.First; |
| 36 | + |
| 37 | + public override IEnumerable<Type> EventTypes => [typeof(SimulationAdded), typeof(SimulationConfigurationAdded), typeof(HubAddedToSimulationConfiguration), typeof(WeightsSetForSimulationConfiguration)]; |
| 38 | + public override IEnumerable<Type> Projections => [typeof(SimulationDashboardProjection)]; |
| 39 | + |
| 40 | + protected override void ConfigureServices(IServiceCollection services) |
| 41 | + { |
| 42 | + services.AddSingleton(new SimulationDashboardProjection()); |
| 43 | + } |
| 44 | + |
| 45 | + void Establish() |
| 46 | + { |
| 47 | + SimulationId = Guid.Parse("0089d57f-9095-4b56-b948-ab170de8e0ee"); |
| 48 | + ConfigurationId = Guid.Parse("754fd741-adae-4fbd-8c47-2d622cc0b274"); |
| 49 | + HubId = Guid.Parse("eff2cf7a-4121-438b-94fd-139775a09f57"); |
| 50 | + } |
| 51 | + |
| 52 | + async Task Because() |
| 53 | + { |
| 54 | + var projection = EventStore.Projections.GetHandlerFor<SimulationDashboardProjection>(); |
| 55 | + await projection.WaitTillActive(); |
| 56 | + |
| 57 | + var events = new EventForEventSourceId[] |
| 58 | + { |
| 59 | + new(ConfigurationId, new WeightsSetForSimulationConfiguration(Distance, Time, Cost, Waste), Causation.Unknown()), |
| 60 | + new(ConfigurationId, new HubAddedToSimulationConfiguration(HubId, HubName), Causation.Unknown()), |
| 61 | + new(SimulationId, new SimulationAdded(SimulationName), Causation.Unknown()), |
| 62 | + new(SimulationId, new SimulationConfigurationAdded(ConfigurationId, ConfigurationName), Causation.Unknown()) |
| 63 | + }; |
| 64 | + |
| 65 | + var appendResult = await EventStore.EventLog.AppendMany(events); |
| 66 | + LastEventSequenceNumber = appendResult.SequenceNumbers.Last(); |
| 67 | + |
| 68 | + await projection.WaitTillReachesEventSequenceNumber(LastEventSequenceNumber); |
| 69 | + |
| 70 | + FailedPartitions = await projection.GetFailedPartitions(); |
| 71 | + |
| 72 | + var collection = ChronicleFixture.ReadModels.Database.GetCollection<SimulationDashboard>(); |
| 73 | + var queryResult = await collection.FindAsync(_ => true); |
| 74 | + var allResults = await queryResult.ToListAsync(); |
| 75 | + Result = allResults.FirstOrDefault(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + void should_have_no_failed_partitions() |
| 81 | + { |
| 82 | + if (Context.FailedPartitions.Any()) |
| 83 | + { |
| 84 | + var failures = Context.FailedPartitions.ToList(); |
| 85 | + var messages = failures.SelectMany(f => f.Attempts.Select(a => a.Messages)).SelectMany(m => m).ToList(); |
| 86 | + var stackTraces = failures.SelectMany(f => f.Attempts.Select(a => a.StackTrace)).ToList(); |
| 87 | + var combined = string.Join("\n\n", messages.Zip(stackTraces, (msg, stack) => $"Message: {msg}\nStack: {stack}")); |
| 88 | + throw new Xunit.Sdk.XunitException($"Failed partitions:\n{combined}"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] void should_return_model() => Context.Result.ShouldNotBeNull(); |
| 93 | + [Fact] void should_have_simulation_name() => Context.Result.Name.ShouldEqual(SimulationName); |
| 94 | + [Fact] void should_have_one_configuration() => Context.Result.Configurations.Count.ShouldEqual(1); |
| 95 | + [Fact] void should_have_configuration_id_on_child() => Context.Result.Configurations[0].ConfigurationId.ShouldEqual(Context.ConfigurationId); |
| 96 | + [Fact] void should_have_configuration_name_on_child() => Context.Result.Configurations[0].Name.ShouldEqual(ConfigurationName); |
| 97 | + [Fact] void should_have_one_hub_on_configuration() => Context.Result.Configurations[0].Hubs.Count.ShouldEqual(1); |
| 98 | + [Fact] void should_have_hub_id_on_nested_child() => Context.Result.Configurations[0].Hubs[0].HubId.ShouldEqual(Context.HubId); |
| 99 | + [Fact] void should_have_hub_name_on_nested_child() => Context.Result.Configurations[0].Hubs[0].Name.ShouldEqual(HubName); |
| 100 | + [Fact] void should_set_the_event_sequence_number_to_last_event() => Context.Result.__lastHandledEventSequenceNumber.ShouldEqual(Context.LastEventSequenceNumber); |
| 101 | + [Fact] void should_set_distance_on_configuration() => Context.Result.Configurations[0].Distance.ShouldEqual(Distance); |
| 102 | + [Fact] void should_set_time_on_configuration() => Context.Result.Configurations[0].Time.ShouldEqual(Time); |
| 103 | + [Fact] void should_set_cost_on_configuration() => Context.Result.Configurations[0].Cost.ShouldEqual(Cost); |
| 104 | + [Fact] void should_set_waste_on_configuration() => Context.Result.Configurations[0].Waste.ShouldEqual(Waste); |
| 105 | +} |
0 commit comments