Skip to content

Commit

Permalink
update aggregation tests (#1013)
Browse files Browse the repository at this point in the history
* update aggregation tests

* update changes

* update changes
  • Loading branch information
ElizabethOkerio authored Aug 8, 2023
1 parent fa81478 commit ddc59f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ public class CustomersController : ODataController

public CustomersController(EntitySetAggregationContext context)
{
context.Database.EnsureCreated();
EntitySetAggregationContext.EnsureDatabaseCreated(context);
_context = context;

if (!_context.Customers.Any())
{
Generate();
}
}

[EnableQuery]
Expand All @@ -39,40 +34,6 @@ public SingleResult<Customer> Get(int key)
{
return SingleResult.Create(_context.Customers.Where(c => c.Id == key));
}

public void Generate()
{
for (int i = 1; i <= 3; i++)
{
var customer = new Customer
{
// Id = i,
Name = "Customer" + (i+1) % 2,
Orders =
new List<Order> {
new Order {
Name = "Order" + 2*i,
Price = i * 25,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
},
new Order {
Name = "Order" + 2*i+1,
Price = i * 75,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 75 }
}
},
Address = new Address
{
Name = "City" + i % 2,
Street = "Street" + i % 2,
}
};

_context.Customers.Add(customer);
}

_context.SaveChanges();
}
}

public class EmployeesController : ODataController
Expand Down Expand Up @@ -109,42 +70,14 @@ public class OrdersController : ODataController

public OrdersController(EntitySetAggregationContext context)
{
context.Database.EnsureCreated();
EntitySetAggregationContext.EnsureDatabaseCreated(context);
_context = context;

if (!_context.Orders.Any())
{
Generate();
}
}

[EnableQuery]
public IQueryable<Order> Get()
{
return _context.Orders;
}

[EnableQuery]
public SingleResult<Order> Get(int key)
{
return SingleResult.Create(_context.Orders.Where(c => c.Id == key));
}

public void Generate()
{
for (int i = 1; i <= 3; i++)
{
var order = new Order
{
Name = "Order" + 2 * i,
Price = i * 25,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
};

_context.Orders.Add(order);
}

_context.SaveChanges();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Microsoft.AspNetCore.OData.E2E.Tests.EntitySetAggregation
Expand All @@ -29,6 +30,45 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Customer>().OwnsOne(c => c.Address).WithOwner();
modelBuilder.Entity<Order>().OwnsOne(c => c.SaleInfo).WithOwner();
}

public static void EnsureDatabaseCreated(EntitySetAggregationContext context)
{
context.Database.EnsureCreated();

if (!context.Customers.Any())
{
for (int i = 1; i <= 3; i++)
{
var customer = new Customer
{
// Id = i,
Name = "Customer" + (i + 1) % 2,
Orders =
new List<Order> {
new Order {
Name = "Order" + 2*i,
Price = i * 25,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
},
new Order {
Name = "Order" + 2*i+1,
Price = i * 75,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 75 }
}
},
Address = new Address
{
Name = "City" + i % 2,
Street = "Street" + i % 2,
}
};

context.Customers.Add(customer);
}

context.SaveChanges();
}
}
}

public class Customer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task GroupByWithAggregationAndOrderByWorks()
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));

// Act
HttpResponseMessage response = Client.SendAsync(request).Result;
HttpResponseMessage response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand All @@ -136,12 +136,12 @@ public async Task GroupByWithAggregationAndOrderByDynamicPropsWorks()
{
// Arrange
string queryUrl = "aggregation/Orders?$apply=groupby((Name),aggregate(Price with sum as TotalPrice))&$orderby=TotalPrice desc";
string expectedResult = "{\"value\":[{\"Name\":\"Order6\",\"TotalPrice\":75},{\"Name\":\"Order4\",\"TotalPrice\":50},{\"Name\":\"Order2\",\"TotalPrice\":25}]}";
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}]}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));

// Act
HttpResponseMessage response = Client.SendAsync(request).Result;
HttpResponseMessage response = await Client.SendAsync(request);

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

// Act
HttpResponseMessage response = Client.SendAsync(request).Result;
HttpResponseMessage response = await Client.SendAsync(request);

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

// Act
HttpResponseMessage response = Client.SendAsync(request).Result;
HttpResponseMessage response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand Down

0 comments on commit ddc59f7

Please sign in to comment.