Skip to content

Commit d59eee5

Browse files
authored
Merge pull request #2324 from Cratis:fix/recursive
Fix/recursive
2 parents 28c6ded + ba00854 commit d59eee5

File tree

7 files changed

+674
-11
lines changed

7 files changed

+674
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
#pragma warning disable SA1402 // File may only contain a single type
5+
6+
using Cratis.Chronicle.Contracts.Projections;
7+
using Cratis.Chronicle.Events;
8+
9+
namespace Cratis.Chronicle.Projections.ModelBound.for_ModelBoundProjectionBuilder.when_building_model.with_children_having;
10+
11+
public class child_with_arithmetic_attributes : given.a_model_bound_projection_builder
12+
{
13+
ProjectionDefinition _result;
14+
15+
void Establish()
16+
{
17+
event_types = new EventTypesForSpecifications([
18+
typeof(AccountOpened),
19+
typeof(DepositMade),
20+
typeof(WithdrawalMade),
21+
typeof(TransactionRecorded),
22+
typeof(InterestAdded)]);
23+
builder = new ModelBoundProjectionBuilder(naming_policy, event_types);
24+
}
25+
26+
void Because() => _result = builder.Build(typeof(Bank));
27+
28+
[Fact] void should_return_definition() => _result.ShouldNotBeNull();
29+
30+
[Fact] void should_have_children_for_accounts()
31+
{
32+
_result.Children.Keys.ShouldContain(nameof(Bank.Accounts));
33+
}
34+
35+
[Fact] void should_have_from_definition_for_deposit_made()
36+
{
37+
var eventType = event_types.GetEventTypeFor(typeof(DepositMade)).ToContract();
38+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
39+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
40+
}
41+
42+
[Fact] void should_have_from_definition_for_withdrawal_made()
43+
{
44+
var eventType = event_types.GetEventTypeFor(typeof(WithdrawalMade)).ToContract();
45+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
46+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
47+
}
48+
49+
[Fact] void should_have_from_definition_for_transaction_recorded()
50+
{
51+
var eventType = event_types.GetEventTypeFor(typeof(TransactionRecorded)).ToContract();
52+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
53+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
54+
}
55+
56+
[Fact] void should_have_from_definition_for_interest_added()
57+
{
58+
var eventType = event_types.GetEventTypeFor(typeof(InterestAdded)).ToContract();
59+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
60+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
61+
}
62+
63+
[Fact] void should_add_balance_from_deposit_made()
64+
{
65+
var eventType = event_types.GetEventTypeFor(typeof(DepositMade)).ToContract();
66+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
67+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
68+
var expression = fromDef.Properties[nameof(Account.Balance)];
69+
expression.ShouldContain(WellKnownExpressions.Add);
70+
expression.ShouldContain(nameof(DepositMade.Amount));
71+
}
72+
73+
[Fact] void should_subtract_balance_from_withdrawal_made()
74+
{
75+
var eventType = event_types.GetEventTypeFor(typeof(WithdrawalMade)).ToContract();
76+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
77+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
78+
var expression = fromDef.Properties[nameof(Account.Balance)];
79+
expression.ShouldContain(WellKnownExpressions.Subtract);
80+
expression.ShouldContain(nameof(WithdrawalMade.Amount));
81+
}
82+
83+
[Fact] void should_increment_transaction_count_from_transaction_recorded()
84+
{
85+
var eventType = event_types.GetEventTypeFor(typeof(TransactionRecorded)).ToContract();
86+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
87+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
88+
var expression = fromDef.Properties[nameof(Account.TransactionCount)];
89+
expression.ShouldEqual($"{WellKnownExpressions.Increment}()");
90+
}
91+
92+
[Fact] void should_count_total_deposits()
93+
{
94+
var eventType = event_types.GetEventTypeFor(typeof(DepositMade)).ToContract();
95+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
96+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
97+
var expression = fromDef.Properties[nameof(Account.TotalDeposits)];
98+
expression.ShouldEqual($"{WellKnownExpressions.Count}()");
99+
}
100+
101+
[Fact] void should_add_interest_total_from_interest_added()
102+
{
103+
var eventType = event_types.GetEventTypeFor(typeof(InterestAdded)).ToContract();
104+
var childrenDef = _result.Children[nameof(Bank.Accounts)];
105+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
106+
var expression = fromDef.Properties[nameof(Account.TotalInterest)];
107+
expression.ShouldContain(WellKnownExpressions.Add);
108+
expression.ShouldContain(nameof(InterestAdded.InterestAmount));
109+
}
110+
}
111+
112+
[EventType]
113+
public record AccountOpened(AccountId Id, AccountName Name);
114+
115+
[EventType]
116+
public record DepositMade(decimal Amount);
117+
118+
[EventType]
119+
public record WithdrawalMade(decimal Amount);
120+
121+
[EventType]
122+
public record TransactionRecorded();
123+
124+
[EventType]
125+
public record InterestAdded(decimal InterestAmount);
126+
127+
public record BankId(Guid Value);
128+
public record AccountId(Guid Value);
129+
public record AccountName(string Value);
130+
131+
public record Account(
132+
AccountId Id,
133+
AccountName Name,
134+
135+
[AddFrom<DepositMade>(nameof(DepositMade.Amount))]
136+
[SubtractFrom<WithdrawalMade>(nameof(WithdrawalMade.Amount))]
137+
decimal Balance,
138+
139+
[Increment<TransactionRecorded>]
140+
int TransactionCount,
141+
142+
[Count<DepositMade>]
143+
int TotalDeposits,
144+
145+
[AddFrom<InterestAdded>(nameof(InterestAdded.InterestAmount))]
146+
decimal TotalInterest);
147+
148+
[FromEvent<BankCreated>]
149+
public record Bank(
150+
BankId Id,
151+
152+
[ChildrenFrom<AccountOpened>(identifiedBy: nameof(Account.Id))]
153+
IEnumerable<Account> Accounts);
154+
155+
[EventType]
156+
public record BankCreated(BankId Id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
#pragma warning disable SA1402 // File may only contain a single type
5+
6+
using Cratis.Chronicle.Contracts.Projections;
7+
using Cratis.Chronicle.Events;
8+
9+
namespace Cratis.Chronicle.Projections.ModelBound.for_ModelBoundProjectionBuilder.when_building_model.with_children_having;
10+
11+
public class child_with_from_event_and_set_from : given.a_model_bound_projection_builder
12+
{
13+
ProjectionDefinition _result;
14+
15+
void Establish()
16+
{
17+
event_types = new EventTypesForSpecifications([
18+
typeof(ConfigAdded),
19+
typeof(WeightsSetForConfig),
20+
typeof(RunEnded)]);
21+
builder = new ModelBoundProjectionBuilder(naming_policy, event_types);
22+
}
23+
24+
void Because() => _result = builder.Build(typeof(Dashboard));
25+
26+
[Fact] void should_return_definition() => _result.ShouldNotBeNull();
27+
28+
[Fact] void should_have_children_for_configurations()
29+
{
30+
_result.Children.Keys.ShouldContain(nameof(Dashboard.Configurations));
31+
}
32+
33+
[Fact] void should_have_from_definition_for_simulation_configuration_added()
34+
{
35+
var eventType = event_types.GetEventTypeFor(typeof(ConfigAdded)).ToContract();
36+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
37+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
38+
}
39+
40+
[Fact] void should_have_from_definition_for_weights_set()
41+
{
42+
var eventType = event_types.GetEventTypeFor(typeof(WeightsSetForConfig)).ToContract();
43+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
44+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
45+
}
46+
47+
[Fact] void should_have_from_definition_for_simulation_run_ended()
48+
{
49+
var eventType = event_types.GetEventTypeFor(typeof(RunEnded)).ToContract();
50+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
51+
childrenDef.From.Keys.ShouldContain(et => et.IsEqual(eventType));
52+
}
53+
54+
[Fact] void should_map_name_from_weights_set_event()
55+
{
56+
var eventType = event_types.GetEventTypeFor(typeof(WeightsSetForConfig)).ToContract();
57+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
58+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
59+
fromDef.Properties.Keys.ShouldContain(nameof(Config.Name));
60+
}
61+
62+
[Fact] void should_map_description_from_weights_set_event()
63+
{
64+
var eventType = event_types.GetEventTypeFor(typeof(WeightsSetForConfig)).ToContract();
65+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
66+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
67+
fromDef.Properties.Keys.ShouldContain(nameof(Config.Description));
68+
}
69+
70+
[Fact] void should_map_last_simulation_total_distance_from_simulation_run_ended()
71+
{
72+
var eventType = event_types.GetEventTypeFor(typeof(RunEnded)).ToContract();
73+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
74+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
75+
fromDef.Properties.Keys.ShouldContain(nameof(Config.LastSimulationTotalDistance));
76+
fromDef.Properties[nameof(Config.LastSimulationTotalDistance)].ShouldEqual(nameof(RunEnded.TotalDistance));
77+
}
78+
79+
[Fact] void should_map_last_simulation_total_time_from_simulation_run_ended()
80+
{
81+
var eventType = event_types.GetEventTypeFor(typeof(RunEnded)).ToContract();
82+
var childrenDef = _result.Children[nameof(Dashboard.Configurations)];
83+
var fromDef = childrenDef.From.Single(kvp => kvp.Key.IsEqual(eventType)).Value;
84+
fromDef.Properties.Keys.ShouldContain(nameof(Config.LastSimulationTotalTime));
85+
fromDef.Properties[nameof(Config.LastSimulationTotalTime)].ShouldEqual(nameof(RunEnded.TotalTime));
86+
}
87+
}
88+
89+
[EventType]
90+
public record ConfigAdded(
91+
ConfigId Id,
92+
ConfigName Name,
93+
ConfigDescription Description);
94+
95+
[EventType]
96+
public record WeightsSetForConfig(
97+
ConfigName Name,
98+
ConfigDescription Description);
99+
100+
[EventType]
101+
public record RunEnded(
102+
Distance TotalDistance,
103+
TimeSpan TotalTime,
104+
Co2FootPrint TotalWaste,
105+
Cost TotalCost);
106+
107+
public record ConfigId(Guid Value);
108+
public record ConfigName(string Value);
109+
public record ConfigDescription(string Value);
110+
public record DashboardId(Guid Value);
111+
public record Distance(double Value);
112+
public record Cost(decimal Value);
113+
114+
[FromEvent<WeightsSetForConfig>]
115+
public record Config(
116+
ConfigId Id,
117+
ConfigName Name,
118+
ConfigDescription Description,
119+
120+
[SetFrom<RunEnded>(nameof(RunEnded.TotalDistance))]
121+
Distance LastSimulationTotalDistance,
122+
123+
[SetFrom<RunEnded>(nameof(RunEnded.TotalTime))]
124+
TimeSpan LastSimulationTotalTime,
125+
126+
[SetFrom<RunEnded>(nameof(RunEnded.TotalWaste))]
127+
Co2FootPrint LastSimulationTotalWaste,
128+
129+
[SetFrom<RunEnded>(nameof(RunEnded.TotalCost))]
130+
Cost LastSimulationTotalCost);
131+
132+
[FromEvent<DashboardAdded>]
133+
public record Dashboard(
134+
DashboardId Id,
135+
[ChildrenFrom<ConfigAdded>(identifiedBy: nameof(Config.Id))]
136+
IEnumerable<Config> Configurations);
137+
138+
[EventType]
139+
public record DashboardAdded(DashboardId Id);

0 commit comments

Comments
 (0)