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

Port QueryCount and CancellationToken tests #3167

Open
wants to merge 3 commits into
base: main
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,143 @@
//-----------------------------------------------------------------------------
// <copyright file="CancellationTokenTestsController.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.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.OData.Client.E2E.Tests.Common.Server.EndToEnd;

namespace Microsoft.OData.Client.E2E.Tests.DeltaTests.Server;
Copy link
Preview

Copilot AI Jan 20, 2025

Choose a reason for hiding this comment

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

The semicolon should be replaced with a curly brace to properly define the namespace.

Suggested change
namespace Microsoft.OData.Client.E2E.Tests.DeltaTests.Server;
namespace Microsoft.OData.Client.E2E.Tests.DeltaTests.Server {

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

public class CancellationTokenTestsController : ODataController
{
private static CommonEndToEndDataSource _dataSource;

[EnableQuery(PageSize = 5)]
[HttpGet("odata/Customers")]
public IActionResult GetCustomers(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Ok(_dataSource.Customers);
}

[EnableQuery]
[HttpGet("odata/Customers({key})")]
public IActionResult GetCustomer([FromODataUri] int key, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var customer = _dataSource.Customers?.FirstOrDefault(c => c.CustomerId == key);
if (customer == null)
{
return NotFound();
}

return Ok(customer);
}

[HttpPost("odata/Customers")]
public IActionResult AddCustomer([FromBody] Customer customer, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_dataSource.Customers?.Add(customer);
return Created(customer);
}

[HttpPost("odata/Customers({key})/Default.ChangeCustomerAuditInfo")]
public IActionResult ChangeCustomerAuditInfo([FromODataUri] int key, [FromBody] AuditInfo auditInfo, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var customer = _dataSource.Customers?.FirstOrDefault(c => c.CustomerId == key);
if (customer == null)
{
return NotFound();
}

customer.Auditing = auditInfo;
return Created(auditInfo);
}

[HttpPost("odata/Customers({key})/Orders/$ref")]
public IActionResult AddOrderRefToCustomer([FromODataUri] int key, [FromBody] Uri orderUri, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

if (orderUri == null)
{
return BadRequest();
}

// Extract the order ID from the URI
var lastSegment = orderUri.Segments.Last();
var orderId = int.Parse(Regex.Match(lastSegment, @"\d+").Value);

// Find the order by ID
var order = _dataSource.Orders?.SingleOrDefault(d => d.OrderId == orderId);
if (order == null)
{
return NotFound();
}

// Add the order reference to the customer
var customer = _dataSource.Customers?.SingleOrDefault(c => c.CustomerId == key);
if (customer == null)
{
return NotFound();
}

customer.Orders ??= [];
Copy link
Preview

Copilot AI Jan 20, 2025

Choose a reason for hiding this comment

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

The empty array initialization is incorrect.

Suggested change
customer.Orders ??= [];
customer.Orders ??= new List<Order>();

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
customer.Orders.Add(order);

return Ok(customer);
}

[HttpPost("odata/Orders")]
public IActionResult AddOrder([FromBody] Order order, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_dataSource.Orders?.Add(order);
return Created(order);
}

[HttpPost("odata/Cars")]
public async Task<IActionResult> AddCar([FromBody] Car car, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_dataSource.Cars?.Add(car);
return Created(car);
}

[HttpPut("odata/Cars({key})/Photo")]
public IActionResult UpdateCarPhoto([FromODataUri] int key, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var car = _dataSource.Cars?.FirstOrDefault(c => c.VIN == key);
if (car == null)
{
return NotFound();
}

var photo = Request.Body;
car.Photo = photo;
return Ok();
}

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

return Ok();
}
}
Loading