Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest derived interfaces #470

Open
wants to merge 1 commit into
base: release-8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//-----------------------------------------------------------------------------
// <copyright file="DerivedInterfacesControllers.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;

namespace Microsoft.AspNetCore.OData.E2E.Tests.DerivedInterfaces
{
public class CustomersController : ODataController
{
static List<Customer> Customers { get; set; }

static CustomersController()
{
Customers = new List<Customer>
{
new Customer {
Id = 1,
Name = "Customer 1",
Order = new VipOrder()
{
Id = 10,
Product = "Test Product 1",
LoyaltyCardNo = "42"
}
},
new Customer {
Id = 2,
Name = "Customer 1",
Order = new VipOrder()
{
Id = 11,
Product = "Test Product 2",
LoyaltyCardNo = "43"
}
},
};
}

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet("odata/Customers/Microsoft.AspNetCore.OData.E2E.Tests.DerivedInterfaces.Customers")]
public IActionResult Get()
{
return Ok(Customers);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
// <copyright file="DerivedInterfacesDataModel.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.AspNetCore.OData.E2E.Tests.DerivedInterfaces
{
public class Customer : IIdentifiable
{
public int Id { get; set; }
public string Name { get; set; }

public IOrder Order { get; set; }
}

public class VipOrder : IOrder
{
public int Id { get; set; }
public string Product { get; set; }
public string LoyaltyCardNo { get; set; }
}

public interface IOrder : IIdentifiable
{
public string Product { get; set; }
}

public interface IIdentifiable
{
public int Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//-----------------------------------------------------------------------------
// <copyright file="DerivedInterfacesTests.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.TestCommon;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using Xunit;

namespace Microsoft.AspNetCore.OData.E2E.Tests.DerivedInterfaces
{
public class DerivedTypeTests : WebApiTestBase<DerivedTypeTests>
{
public DerivedTypeTests(WebApiTestFixture<DerivedTypeTests> fixture)
: base(fixture)
{
}

protected static void UpdateConfigureServices(IServiceCollection services)
{
services.ConfigureControllers(typeof(CustomersController));
services.AddControllers().AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel()).Count().Filter().OrderBy().Expand().SetMaxTop(null).Select());
}

private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
return builder.GetEdmModel();
}

[Fact]
public async Task QueryCustomersFiltered()
{
// Arrange
string requestUri = "/odata/Customers?$filter=Order/Id eq 11";

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implements IDisposable. Please wrap it in a using block

HttpClient client = CreateClient();

// Act
HttpResponseMessage response = await client.SendAsync(request);
string py = await response.Content.ReadAsStringAsync();

// Assert
Assert.DoesNotContain("error", py);
Assert.True(response.IsSuccessStatusCode);

string expectedContent = "\"Id\":2,\"Name\":\"Customer 1\"";
Assert.Contains(expectedContent, expectedContent);
}
}
}