diff --git a/DotNetSampleApp.Tests/OrdersTest.cs b/DotNetSampleApp.Tests/OrdersTest.cs new file mode 100644 index 0000000..18bf72a --- /dev/null +++ b/DotNetSampleApp.Tests/OrdersTest.cs @@ -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( + 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( + 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; + } + } +} \ No newline at end of file diff --git a/DotNetSampleApp/Controllers/Orders.cs b/DotNetSampleApp/Controllers/Orders.cs index 574335e..9148fa4 100644 --- a/DotNetSampleApp/Controllers/Orders.cs +++ b/DotNetSampleApp/Controllers/Orders.cs @@ -33,7 +33,7 @@ public async Task 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(query); - return StatusCode(StatusCodes.Status201Created, res.Data); + return Ok(res.Data); } diff --git a/DotNetSampleApp/Services/SeedService.cs b/DotNetSampleApp/Services/SeedService.cs index b1a4f40..eb55cda 100644 --- a/DotNetSampleApp/Services/SeedService.cs +++ b/DotNetSampleApp/Services/SeedService.cs @@ -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($$""" @@ -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'},