Skip to content

Commit

Permalink
Port QueryOption Tests (#3156)
Browse files Browse the repository at this point in the history
* Update AspNetCoreOData version to 9.1.1

* Port QueryOption Tests
  • Loading branch information
WanjohiSammy authored Dec 24, 2024
1 parent 70eef8a commit 843ac15
Show file tree
Hide file tree
Showing 14 changed files with 1,625 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//---------------------------------------------------------------------
// <copyright file="ODataValueAssertEqualHelper.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

using Xunit;

namespace Microsoft.OData.Client.E2E.TestCommon.Helpers;

public static class ODataValueAssertEqualHelper
{
#region Util methods to AssertEqual ODataValues

public static void AssertODataValueEqual(ODataValue expected, ODataValue actual)
{
ODataPrimitiveValue expectedPrimitiveValue = expected as ODataPrimitiveValue;
ODataPrimitiveValue actualPrimitiveValue = actual as ODataPrimitiveValue;
if (expectedPrimitiveValue != null && actualPrimitiveValue != null)
{
AssertODataPrimitiveValueEqual(expectedPrimitiveValue, actualPrimitiveValue);
}
else
{
ODataEnumValue expectedEnumValue = expected as ODataEnumValue;
ODataEnumValue actualEnumValue = actual as ODataEnumValue;
if (expectedEnumValue != null && actualEnumValue != null)
{
AssertODataEnumValueEqual(expectedEnumValue, actualEnumValue);
}
else
{
ODataCollectionValue expectedCollectionValue = (ODataCollectionValue)expected;
ODataCollectionValue actualCollectionValue = (ODataCollectionValue)actual;
AssertODataCollectionValueEqual(expectedCollectionValue, actualCollectionValue);
}
}
}

private static void AssertODataCollectionValueEqual(ODataCollectionValue expectedCollectionValue, ODataCollectionValue actualCollectionValue)
{
Assert.NotNull(expectedCollectionValue);
Assert.NotNull(actualCollectionValue);
Assert.Equal(expectedCollectionValue.TypeName, actualCollectionValue.TypeName);
var expectedItemsArray = expectedCollectionValue.Items.OfType<object>().ToArray();
var actualItemsArray = actualCollectionValue.Items.OfType<object>().ToArray();

Assert.Equal(expectedItemsArray.Length, actualItemsArray.Length);
for (int i = 0; i < expectedItemsArray.Length; i++)
{
var expectedOdataValue = expectedItemsArray[i] as ODataValue;
var actualOdataValue = actualItemsArray[i] as ODataValue;
if (expectedOdataValue != null && actualOdataValue != null)
{
AssertODataValueEqual(expectedOdataValue, actualOdataValue);
}
else
{
Assert.Equal(expectedItemsArray[i], actualItemsArray[i]);
}
}
}

public static void AssertODataPropertiesEqual(IEnumerable<ODataProperty> expectedProperties, IEnumerable<ODataProperty> actualProperties)
{
if (expectedProperties == null && actualProperties == null)
{
return;
}

Assert.NotNull(expectedProperties);
Assert.NotNull(actualProperties);
var expectedPropertyArray = expectedProperties.ToArray();
var actualPropertyArray = actualProperties.ToArray();
Assert.Equal(expectedPropertyArray.Length, actualPropertyArray.Length);
for (int i = 0; i < expectedPropertyArray.Length; i++)
{
AssertODataPropertyEqual(expectedPropertyArray[i], actualPropertyArray[i]);
}
}

public static void AssertODataPropertyEqual(ODataProperty expectedOdataProperty, ODataProperty actualOdataProperty)
{
Assert.NotNull(expectedOdataProperty);
Assert.NotNull(actualOdataProperty);
Assert.Equal(expectedOdataProperty.Name, actualOdataProperty.Name);
AssertODataValueEqual(ToODataValue(expectedOdataProperty.Value), ToODataValue(actualOdataProperty.Value));
}

private static ODataValue ToODataValue(object value)
{
if (value == null)
{
return new ODataNullValue();
}

var odataValue = value as ODataValue;
if (odataValue != null)
{
return odataValue;
}

return new ODataPrimitiveValue(value);
}

private static void AssertODataPrimitiveValueEqual(ODataPrimitiveValue expectedPrimitiveValue, ODataPrimitiveValue actualPrimitiveValue)
{
Assert.NotNull(expectedPrimitiveValue);
Assert.NotNull(actualPrimitiveValue);
Assert.Equal(expectedPrimitiveValue.Value, actualPrimitiveValue.Value);
}

private static void AssertODataEnumValueEqual(ODataEnumValue expectedEnumValue, ODataEnumValue actualEnumValue)
{
Assert.NotNull(expectedEnumValue);
Assert.NotNull(actualEnumValue);
Assert.Equal(expectedEnumValue.Value, actualEnumValue.Value);
Assert.Equal(expectedEnumValue.TypeName, actualEnumValue.TypeName);
}

#endregion Util methods to AssertEqual ODataValues

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//-----------------------------------------------------------------------------
// <copyright file="LogAssertTraceListener.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.Diagnostics;

namespace Microsoft.OData.Client.E2E.TestCommon.Logs;

public class LogAssertTraceListener : TraceListener
{
public LogAssertTraceListener()
{
// Clear existing listeners and add this listener
Trace.Listeners.Clear();
Trace.Listeners.Add(this);
}

public override void Write(string? message) { }

public override void WriteLine(string? message) { }

public override void Fail(string? message, string? detailMessage)
{
// Log the assertion failure
Console.WriteLine($"DEBUG ASSERTION FAILED: {message} {detailMessage}");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.0.0-rc.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
ConfigureBeforeRouting(app, env);

app.UseODataRouteDebug();

app.UseODataBatching();

app.UseRouting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,20 @@ public void Initialize()
},
];

this.ProductDetails[0].Reviews = [this.ProductReviews[0]];
this.ProductDetails[0].Reviews = [this.ProductReviews[1]];
this.ProductDetails[0].Reviews = [this.ProductReviews[2]];
this.ProductDetails[0].Reviews = [this.ProductReviews[3]];
this.ProductDetails[1].Reviews = [this.ProductReviews[1]];
this.ProductDetails[1].Reviews = [this.ProductReviews[2]];
this.ProductDetails[1].Reviews = [this.ProductReviews[3]];
this.ProductDetails[0].Reviews = new List<ProductReview>
{
this.ProductReviews[0],
this.ProductReviews[1],
this.ProductReviews[2],
this.ProductReviews[3]
};

this.ProductDetails[1].Reviews = new List<ProductReview>
{
this.ProductReviews[1],
this.ProductReviews[2],
this.ProductReviews[3]
};

this.Calendars = new List<Calendar>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.0.0-rc.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//---------------------------------------------------------------------
// <copyright file="QueryOptionTestsController.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.OData.Client.E2E.Tests.Common.Server.Default;

namespace Microsoft.OData.Client.E2E.Tests.QueryOptionTests.Server
{
public class QueryOptionTestsController : ODataController
{
private static DefaultDataSource _dataSource;

[EnableQuery]
[HttpGet("odata/Products")]
public IActionResult GetProducts()
{
var products = _dataSource.Products;

return Ok(products);
}

[EnableQuery]
[HttpGet("odata/Products({key})")]
public IActionResult GetProduct([FromRoute] int key)
{
var product = _dataSource.Products?.SingleOrDefault(a => a.ProductID == key);

if (product == null)
{
return NotFound();
}

return Ok(product);
}

[EnableQuery]
[HttpGet("odata/People")]
public IActionResult GetPeople()
{
var people = _dataSource.People;

return Ok(people);
}

[EnableQuery]
[HttpGet("odata/People({key})")]
public IActionResult GetPerson([FromRoute] int key)
{
var person = _dataSource.People?.SingleOrDefault(a => a.PersonID == key);

if (person == null)
{
return NotFound();
}

return Ok(person);
}

[EnableQuery]
[HttpGet("odata/Customers")]
public IActionResult GetCustomers()
{
var customers = _dataSource.Customers;

return Ok(customers);
}

[EnableQuery]
[HttpGet("odata/Customers({key})")]
public IActionResult GetCustomer([FromRoute] int key)
{
var customer = _dataSource.Customers?.SingleOrDefault(a => a.PersonID == key);

if (customer == null)
{
return NotFound();
}

return Ok(customer);
}

[EnableQuery]
[HttpGet("odata/Customers({key})/Numbers")]
public IActionResult GetNumbersFromCustomer([FromRoute] int key)
{
var customer = _dataSource.Customers?.SingleOrDefault(a => a.PersonID == key);

if (customer == null)
{
return NotFound();
}

return Ok(customer.Numbers);
}

[EnableQuery]
[HttpGet("odata/ProductDetails")]
public IActionResult GetProductDetails()
{
var productDetails = _dataSource.ProductDetails;

return Ok(productDetails);
}

[HttpPost("odata/queryoption/Default.ResetDefaultDataSource")]
public IActionResult ResetDefaultDataSource()
{
_dataSource = DefaultDataSource.CreateInstance();

return Ok();
}
}
}
Loading

0 comments on commit 843ac15

Please sign in to comment.