-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewMapRegistryTests.cs
More file actions
163 lines (133 loc) · 5.02 KB
/
ViewMapRegistryTests.cs
File metadata and controls
163 lines (133 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using FluentAssertions;
using Xunit;
namespace Birko.Data.Views.Tests;
#region Test Mappings
public class CustomerViewMapping : IViewMapping<CustomerView>
{
public void Configure(ViewDefinitionBuilder<CustomerView> builder)
{
builder
.From<Customer>()
.Select<Customer, Guid>(c => c.Guid!.Value, v => v.CustomerId)
.Select<Customer, string>(c => c.Name, v => v.CustomerName);
}
}
public class CustomerOrderSummaryMapping : IViewMapping<CustomerOrderSummary>
{
public void Configure(ViewDefinitionBuilder<CustomerOrderSummary> builder)
{
builder
.HasName("customer_order_summary")
.HasQueryMode(ViewQueryMode.Persistent)
.From<Customer>()
.LeftJoin<Customer, Order, Guid>(c => c.Guid!.Value, o => o.CustomerId)
.Select<Customer, Guid>(c => c.Guid!.Value, v => v.CustomerId)
.Select<Customer, string>(c => c.Name, v => v.CustomerName)
.GroupBy<Customer, Guid>(c => c.Guid!.Value)
.GroupBy<Customer, string>(c => c.Name)
.Count<Order>(v => v.OrderCount)
.Sum<Order, decimal>(o => o.Total, v => v.TotalSpent);
}
}
public class ProductCategorySummaryMapping : IViewMapping<ProductCategorySummary>
{
public void Configure(ViewDefinitionBuilder<ProductCategorySummary> builder)
{
builder
.HasName("product_category_summary")
.HasQueryMode(ViewQueryMode.Auto)
.From<Product>()
.Select<Product, string>(p => p.Category, v => v.Category)
.GroupBy<Product, string>(p => p.Category)
.Count<Product>(v => v.ProductCount)
.Avg<Product, decimal>(p => p.Price, v => v.AvgPrice);
}
}
#endregion
public class ViewMapRegistryTests
{
#region Register
[Fact]
public void Register_SingleMapping_StoresDefinition()
{
var registry = new ViewMapRegistry();
registry.Register(new CustomerViewMapping());
registry.HasDefinition<CustomerView>().Should().BeTrue();
var def = registry.GetDefinition<CustomerView>();
def.Should().NotBeNull();
def!.PrimarySource.Should().Be(typeof(Customer));
def.Fields.Should().HaveCount(2);
}
[Fact]
public void Register_MultipleMappings_StoresAll()
{
var registry = new ViewMapRegistry();
registry.Register(new CustomerViewMapping());
registry.Register(new CustomerOrderSummaryMapping());
registry.HasDefinition<CustomerView>().Should().BeTrue();
registry.HasDefinition<CustomerOrderSummary>().Should().BeTrue();
}
[Fact]
public void GetDefinition_Unregistered_ReturnsNull()
{
var registry = new ViewMapRegistry();
registry.HasDefinition<CustomerView>().Should().BeFalse();
registry.GetDefinition<CustomerView>().Should().BeNull();
}
#endregion
#region RegisterFromAssembly
[Fact]
public void RegisterFromAssembly_DiscoversAllMappings()
{
var registry = new ViewMapRegistry();
registry.RegisterFromAssembly(typeof(CustomerViewMapping).Assembly);
registry.HasDefinition<CustomerView>().Should().BeTrue();
registry.HasDefinition<CustomerOrderSummary>().Should().BeTrue();
registry.HasDefinition<ProductCategorySummary>().Should().BeTrue();
}
[Fact]
public void RegisterFromAssembly_ProducesValidDefinitions()
{
var registry = new ViewMapRegistry();
registry.RegisterFromAssembly(typeof(CustomerOrderSummaryMapping).Assembly);
var def = registry.GetDefinition<CustomerOrderSummary>();
def.Should().NotBeNull();
def!.Name.Should().Be("customer_order_summary");
def.QueryMode.Should().Be(ViewQueryMode.Persistent);
def.HasAggregates.Should().BeTrue();
def.HasJoins.Should().BeTrue();
}
#endregion
#region GetDefinition by Type
[Fact]
public void GetDefinition_ByType_Works()
{
var registry = new ViewMapRegistry();
registry.Register(new CustomerViewMapping());
var def = registry.GetDefinition(typeof(CustomerView));
def.Should().NotBeNull();
def!.PrimarySource.Should().Be(typeof(Customer));
}
[Fact]
public void HasDefinition_ByType_Works()
{
var registry = new ViewMapRegistry();
registry.Register(new CustomerViewMapping());
registry.HasDefinition(typeof(CustomerView)).Should().BeTrue();
registry.HasDefinition(typeof(ProductCategorySummary)).Should().BeFalse();
}
#endregion
#region GetAll
[Fact]
public void GetAll_ReturnsAllRegistered()
{
var registry = new ViewMapRegistry();
registry.Register(new CustomerViewMapping());
registry.Register(new CustomerOrderSummaryMapping());
var all = registry.GetAll().ToList();
all.Should().HaveCount(2);
all.Select(x => x.Key).Should().Contain(typeof(CustomerView));
all.Select(x => x.Key).Should().Contain(typeof(CustomerOrderSummary));
}
#endregion
}