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

Add Products controller tests #10

Merged
merged 3 commits into from
Nov 14, 2024
Merged
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
9 changes: 1 addition & 8 deletions DotNetSampleApp.Tests/CustomersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ namespace DotNetSampleApp.Tests;
[TestOf(typeof(Customers))]
public class CustomersTest
{
[AllowNull]
private Client _fauna;

[AllowNull]
private Customers _cust;

[OneTimeSetUp]
public void Setup()
{
_fauna = new Client(new Configuration(TestSetup.Secret)
{
Endpoint = new Uri("http://localhost:8443"),
});
_cust = new Customers(_fauna);
_cust = new Customers(TestSetup.Client);
}

[Test]
Expand Down
118 changes: 118 additions & 0 deletions DotNetSampleApp.Tests/ProductsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Diagnostics.CodeAnalysis;
using DotNetSampleApp.Controllers;
using DotNetSampleApp.Models;
using Fauna;
using Fauna.Types;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;

namespace DotNetSampleApp.Tests;

[TestFixture]
[TestOf(typeof(Products))]
public class ProductsTest
{
[AllowNull]
private Products _products;

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

[Test]
public async Task PaginateProductsTest()
{
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 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 res = await _products.UpdateProductById(id, productRequest);

switch (res)
{
case OkObjectResult r:
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);
}
}
6 changes: 4 additions & 2 deletions DotNetSampleApp.Tests/TestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace DotNetSampleApp.Tests;
public class TestSetup
{
public static string Secret = "";
public static Client Client = null!;

[OneTimeSetUp]
public void SetupOnce()
Expand All @@ -28,11 +29,12 @@ public void SetupOnce()
Secret = Environment.GetEnvironmentVariable("FAUNA_SECRET")!;
}

var client = new Client(new Configuration(Secret)
Client = new Client(new Configuration(Secret)
{
Endpoint = new Uri("http://localhost:8443"),
});
SeedService.Init(client);

SeedService.Init(Client);
}

}
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,10 @@ Customer documents and related API responses:
"totalPurchaseAmt": 36000
}
```
## Development

### Local Testing
1. Install the latest Fauna CLI: `npm install -g fauna-shell`
2. Start Fauna in a container: `docker run --rm --name fauna -p 8443:8443 -p 8084:8084 fauna/faunadb`
3. Configure the schema: `./setup-local.sh`
4. Run tests: `dotnet test`
23 changes: 23 additions & 0 deletions setup-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

fauna eval --url=http://localhost:8443 --secret=secret "Database.create({ name: 'ECommerceDotnet' })"
FAUNA_SECRET=$(fauna eval --url=http://localhost:8443 --secret=secret --format=json "Key.create({ role: 'admin', database: 'ECommerceDotnet' }).secret" | jq -r)

# Check if .fauna-project does NOT exist in the current directory
if [ ! -f ".fauna-project" ]; then
# Perform your action here, e.g., print a message
echo ".fauna-project does not exist. Creating..."
cat << EOF > .fauna-project
schema_directory=schema
default=local

[environment.local]
endpoint=local
database=ECommerceDotnet
EOF
fi

fauna schema push -y --url=http://localhost:8443 --secret="$FAUNA_SECRET"
fauna schema commit -y --url=http://localhost:8443 --secret="$FAUNA_SECRET"

echo "Secret: $FAUNA_SECRET"
Loading