Skip to content

Commit

Permalink
Add tests for Orders controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Nov 14, 2024
1 parent af50c5a commit d1c5808
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
66 changes: 66 additions & 0 deletions DotNetSampleApp.Tests/OrdersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Diagnostics.CodeAnalysis;
using DotNetSampleApp.Controllers;
using DotNetSampleApp.Models;
using Fauna;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;

namespace DotNetSampleApp.Tests;

[TestFixture]
[TestOf(typeof(Orders))]
public class OrdersTest
{
[AllowNull]
private Orders _orders;

[OneTimeSetUp]
public void Setup()
{
_orders = new Orders(TestSetup.Client);
}

[Test]
public async Task GetOrderTest()
{
var id = (await TestSetup.Client.QueryAsync<string>(
Query.FQL($"let o = Order.all().first(); o.id")
)).Data;
var res = await _orders.GetOrder(id);
switch (res)
{
case OkObjectResult r:
var order = (r.Value! as Order)!;
Assert.That(order.Id, Is.EqualTo(id));
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
}

[Test]
public async Task UpdateOrderTest()
{
var id = (await TestSetup.Client.QueryAsync<string>(
Query.FQL($"let o = Order.all().first(); o.id")
)).Data;

var req = new OrderRequest
{
Status = "processing"
};

var res = await _orders.UpdateOrder(id, req);
switch (res)
{
case OkObjectResult r:
var order = (r.Value! as Order)!;
Assert.That(order.Status, Is.EqualTo("processing"));
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
}
}
2 changes: 1 addition & 1 deletion DotNetSampleApp/Controllers/Orders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<IActionResult> GetOrder([FromRoute] string id)
// Connect to fauna using the client. The query method accepts an FQL query
// as a parameter and a generic type parameter representing the return type.
var res = await client.QueryAsync<Order>(query);
return StatusCode(StatusCodes.Status201Created, res.Data);
return Ok(res.Data);
}


Expand Down
4 changes: 3 additions & 1 deletion DotNetSampleApp/Services/SeedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static void Init(Client client)
{
// Clear Customer History
client.QueryAsync(Query.FQL($"Customer.all().forEach(c => c.delete())")).Wait();
client.QueryAsync(Query.FQL($"Product.all().forEach(c => c.delete())")).Wait();
client.QueryAsync(Query.FQL($"Order.all().forEach(c => c.delete())")).Wait();

// Ensure categories exist
client.QueryAsync(Query.FQL($$"""
Expand All @@ -34,7 +36,7 @@ public static void Init(Client client)
client.QueryAsync(Query.FQL($$"""
[
{name: 'iPhone', price: 10000, description: 'Apple flagship phone', stock: 100, category: 'electronics'},
{name: 'Drone', price: 9000, description: 'Fly and let people wonder if you are filming them!', stock: 0, category: 'electronics'},
{name: 'Drone', price: 9000, description: 'Fly and let people wonder if you are filming them!', stock: 1, category: 'electronics'},
{name: 'Signature Box III', price: 300000, description: 'Latest box by Hooli!', stock: 1000, category: 'electronics'},
{name: 'Raspberry Pi', price: 3000, description: 'A tiny computer', stock: 5, category: 'electronics'},
{name: 'For Whom the Bell Tolls', price: 899, description: 'A book by Ernest Hemingway', stock: 10, category: 'books'},
Expand Down

0 comments on commit d1c5808

Please sign in to comment.