From 6dd108963e6ace4288a882ba2bca294c459ec722 Mon Sep 17 00:00:00 2001 From: manureini <1554111+manureini@users.noreply.github.com> Date: Thu, 3 Feb 2022 22:09:38 +0100 Subject: [PATCH] unittest derived interfaces --- .../DerivedInterfacesControllers.cs | 55 +++++++++++++++++ .../DerivedInterfacesDataModel.cs | 34 +++++++++++ .../DerivedInterfacesTests.cs | 59 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesControllers.cs create mode 100644 test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesDataModel.cs create mode 100644 test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesTests.cs diff --git a/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesControllers.cs b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesControllers.cs new file mode 100644 index 000000000..115e38f8b --- /dev/null +++ b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesControllers.cs @@ -0,0 +1,55 @@ +//----------------------------------------------------------------------------- +// +// Copyright (c) .NET Foundation and Contributors. All rights reserved. +// See License.txt in the project root for license information. +// +//------------------------------------------------------------------------------ + +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 Customers { get; set; } + + static CustomersController() + { + Customers = new List + { + 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); + } + } +} diff --git a/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesDataModel.cs b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesDataModel.cs new file mode 100644 index 000000000..0b85e266a --- /dev/null +++ b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesDataModel.cs @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------------- +// +// Copyright (c) .NET Foundation and Contributors. All rights reserved. +// See License.txt in the project root for license information. +// +//------------------------------------------------------------------------------ + +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; } + } +} diff --git a/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesTests.cs b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesTests.cs new file mode 100644 index 000000000..29fed4620 --- /dev/null +++ b/test/Microsoft.AspNetCore.OData.E2E.Tests/DerivedInterfaces/DerivedInterfacesTests.cs @@ -0,0 +1,59 @@ +//----------------------------------------------------------------------------- +// +// Copyright (c) .NET Foundation and Contributors. All rights reserved. +// See License.txt in the project root for license information. +// +//------------------------------------------------------------------------------ + +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 + { + public DerivedTypeTests(WebApiTestFixture 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("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); + 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); + } + } +}