Skip to content

Commit

Permalink
Finish products tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Nov 14, 2024
1 parent b487e19 commit 514d1a2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
82 changes: 73 additions & 9 deletions DotNetSampleApp.Tests/ProductsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,95 @@ public void Setup()
[Test]
public async Task PaginateProductsTest()
{
var res = await _products.PaginateProducts(null, null, 5);
string? after = null;
do
{
var res = await _products.PaginateProducts(null, after, 5);
switch (res)
{
case OkObjectResult r:
var page = (r.Value! as Page<Product>)!;
Assert.That(page.Data.Count, Is.LessThanOrEqualTo(5));
after = page.After;
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
} while (after != null);
}

[Test]
public async Task CreateProductTest()
{
var productRequest = new ProductRequest
{
Name = "Shiny",
Description = "A shiny new thing",
Category = "electronics",
Price = 1999,
Stock = 100,
};
var res = await _products.CreateProduct(productRequest);
switch (res)
{
case OkObjectResult r:
var page = (r.Value! as Page<Product>)!;
Assert.That(page.Data.Count, Is.EqualTo(5));
Assert.That(page.After, Is.Not.Null);
var product = (r.Value! as Product)!;
Assert.That(product.Name, Is.EqualTo("Shiny"));
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
}

[Test]
public async Task UpdateProductByIdTest()
{
var id = (await TestSetup.Client.QueryAsync<string>(
Query.FQL($"let p = Product.all().first(); p.id")
)).Data;
var productRequest = new ProductRequest
{
Name = "Updated name",
Description = "Updated description.",
Price = 100,
Stock = 100,
Category = "electronics",
};

var res2 = await _products.PaginateProducts(null, null);
switch (res2)
var res = await _products.UpdateProductById(id, productRequest);

switch (res)
{
case OkObjectResult r:
var page = (r.Value! as Page<Product>)!;
Assert.That(page.Data.Count, Is.EqualTo(5));
Assert.That(page.After, Is.Not.Null);
var product = (r.Value! as Product)!;
Assert.That(product.Name, Is.EqualTo(productRequest.Name));
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
}

[Test]
public async Task SearchProductsTest()
{
string? after = null;
do
{
var res = await _products.SearchProducts(after, 5, 0, 10000);
switch (res)
{
case OkObjectResult r:
var products = (r.Value! as Page<Product>)!;
Assert.That(products.Data.Count, Is.LessThanOrEqualTo(5));
after = products.After;
break;
default:
Assert.Fail($"Unexpected result: {res}");
break;
}
} while (after != null);
}
}
2 changes: 1 addition & 1 deletion DotNetSampleApp/Controllers/Products.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task<IActionResult> UpdateProductById(
/// <param name="minPrice">Minimum price for filtering products (default is 0).</param>
/// <param name="maxPrice">Maximum price for filtering products (default is 1000).</param>
/// <returns>List of filtered and sorted products.</returns>
[ProducesResponseType(typeof(List<Product>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Page<Product>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpGet("search")]
Expand Down

0 comments on commit 514d1a2

Please sign in to comment.