Skip to content

Commit ddc59f7

Browse files
update aggregation tests (#1013)
* update aggregation tests * update changes * update changes
1 parent fa81478 commit ddc59f7

File tree

3 files changed

+47
-74
lines changed

3 files changed

+47
-74
lines changed

test/Microsoft.AspNetCore.OData.E2E.Tests/EntitySetAggregation/EntitySetAggregationController.cs

+2-69
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ public class CustomersController : ODataController
1919

2020
public CustomersController(EntitySetAggregationContext context)
2121
{
22-
context.Database.EnsureCreated();
22+
EntitySetAggregationContext.EnsureDatabaseCreated(context);
2323
_context = context;
24-
25-
if (!_context.Customers.Any())
26-
{
27-
Generate();
28-
}
2924
}
3025

3126
[EnableQuery]
@@ -39,40 +34,6 @@ public SingleResult<Customer> Get(int key)
3934
{
4035
return SingleResult.Create(_context.Customers.Where(c => c.Id == key));
4136
}
42-
43-
public void Generate()
44-
{
45-
for (int i = 1; i <= 3; i++)
46-
{
47-
var customer = new Customer
48-
{
49-
// Id = i,
50-
Name = "Customer" + (i+1) % 2,
51-
Orders =
52-
new List<Order> {
53-
new Order {
54-
Name = "Order" + 2*i,
55-
Price = i * 25,
56-
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
57-
},
58-
new Order {
59-
Name = "Order" + 2*i+1,
60-
Price = i * 75,
61-
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 75 }
62-
}
63-
},
64-
Address = new Address
65-
{
66-
Name = "City" + i % 2,
67-
Street = "Street" + i % 2,
68-
}
69-
};
70-
71-
_context.Customers.Add(customer);
72-
}
73-
74-
_context.SaveChanges();
75-
}
7637
}
7738

7839
public class EmployeesController : ODataController
@@ -109,42 +70,14 @@ public class OrdersController : ODataController
10970

11071
public OrdersController(EntitySetAggregationContext context)
11172
{
112-
context.Database.EnsureCreated();
73+
EntitySetAggregationContext.EnsureDatabaseCreated(context);
11374
_context = context;
114-
115-
if (!_context.Orders.Any())
116-
{
117-
Generate();
118-
}
11975
}
12076

12177
[EnableQuery]
12278
public IQueryable<Order> Get()
12379
{
12480
return _context.Orders;
12581
}
126-
127-
[EnableQuery]
128-
public SingleResult<Order> Get(int key)
129-
{
130-
return SingleResult.Create(_context.Orders.Where(c => c.Id == key));
131-
}
132-
133-
public void Generate()
134-
{
135-
for (int i = 1; i <= 3; i++)
136-
{
137-
var order = new Order
138-
{
139-
Name = "Order" + 2 * i,
140-
Price = i * 25,
141-
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
142-
};
143-
144-
_context.Orders.Add(order);
145-
}
146-
147-
_context.SaveChanges();
148-
}
14982
}
15083
}

test/Microsoft.AspNetCore.OData.E2E.Tests/EntitySetAggregation/EntitySetAggregationDataModel.cs

+40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//------------------------------------------------------------------------------
77

88
using System.Collections.Generic;
9+
using System.Linq;
910
using Microsoft.EntityFrameworkCore;
1011

1112
namespace Microsoft.AspNetCore.OData.E2E.Tests.EntitySetAggregation
@@ -29,6 +30,45 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2930
modelBuilder.Entity<Customer>().OwnsOne(c => c.Address).WithOwner();
3031
modelBuilder.Entity<Order>().OwnsOne(c => c.SaleInfo).WithOwner();
3132
}
33+
34+
public static void EnsureDatabaseCreated(EntitySetAggregationContext context)
35+
{
36+
context.Database.EnsureCreated();
37+
38+
if (!context.Customers.Any())
39+
{
40+
for (int i = 1; i <= 3; i++)
41+
{
42+
var customer = new Customer
43+
{
44+
// Id = i,
45+
Name = "Customer" + (i + 1) % 2,
46+
Orders =
47+
new List<Order> {
48+
new Order {
49+
Name = "Order" + 2*i,
50+
Price = i * 25,
51+
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
52+
},
53+
new Order {
54+
Name = "Order" + 2*i+1,
55+
Price = i * 75,
56+
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 75 }
57+
}
58+
},
59+
Address = new Address
60+
{
61+
Name = "City" + i % 2,
62+
Street = "Street" + i % 2,
63+
}
64+
};
65+
66+
context.Customers.Add(customer);
67+
}
68+
69+
context.SaveChanges();
70+
}
71+
}
3272
}
3373

3474
public class Customer

test/Microsoft.AspNetCore.OData.E2E.Tests/EntitySetAggregation/EntitySetAggregationTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public async Task GroupByWithAggregationAndOrderByWorks()
115115
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
116116

117117
// Act
118-
HttpResponseMessage response = Client.SendAsync(request).Result;
118+
HttpResponseMessage response = await Client.SendAsync(request);
119119

120120
// Assert
121121
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -136,12 +136,12 @@ public async Task GroupByWithAggregationAndOrderByDynamicPropsWorks()
136136
{
137137
// Arrange
138138
string queryUrl = "aggregation/Orders?$apply=groupby((Name),aggregate(Price with sum as TotalPrice))&$orderby=TotalPrice desc";
139-
string expectedResult = "{\"value\":[{\"Name\":\"Order6\",\"TotalPrice\":75},{\"Name\":\"Order4\",\"TotalPrice\":50},{\"Name\":\"Order2\",\"TotalPrice\":25}]}";
139+
string expectedResult = "{\"value\":[{\"Name\":\"Order61\",\"TotalPrice\":225},{\"Name\":\"Order41\",\"TotalPrice\":150},{\"Name\":\"Order21\",\"TotalPrice\":75},{\"Name\":\"Order6\",\"TotalPrice\":75},{\"Name\":\"Order4\",\"TotalPrice\":50},{\"Name\":\"Order2\",\"TotalPrice\":25}]}";
140140
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
141141
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
142142

143143
// Act
144-
HttpResponseMessage response = Client.SendAsync(request).Result;
144+
HttpResponseMessage response = await Client.SendAsync(request);
145145

146146
// Assert
147147
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -161,7 +161,7 @@ public async Task AggregationWithFilterWorks()
161161
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
162162

163163
// Act
164-
HttpResponseMessage response = Client.SendAsync(request).Result;
164+
HttpResponseMessage response = await Client.SendAsync(request);
165165

166166
// Assert
167167
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -180,7 +180,7 @@ public async Task GroupByWithAggregationAndFilterByWorks()
180180
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
181181

182182
// Act
183-
HttpResponseMessage response = Client.SendAsync(request).Result;
183+
HttpResponseMessage response = await Client.SendAsync(request);
184184

185185
// Assert
186186
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

0 commit comments

Comments
 (0)