Skip to content

Commit

Permalink
Finished PurchaseTests
Browse files Browse the repository at this point in the history
  • Loading branch information
garioncox committed Feb 28, 2024
1 parent 79ddd08 commit 399930f
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 6 deletions.
10 changes: 10 additions & 0 deletions KakeysBakery.ClassLib/Services/Implementations/AddonServiceMaui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public Task DeleteAddOnAsync(int addonId)
throw new NotImplementedException();
}

public Task<Addon?> GetAddonAsync(int id)
{
throw new NotImplementedException();
}

public Task<Addon?> GetAddonAsync(string addonName)
{
throw new NotImplementedException();
}

public Task<List<Addon>> GetAddonListAsync()
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public Task DeleteBaseGoodAsync(int basegoodId)
throw new NotImplementedException();
}

public Task<Basegood?> GetBaseGoodAsync(int id)
{
throw new NotImplementedException();
}

public Task<Basegood?> GetBaseGoodAsync(string name)
{
throw new NotImplementedException();
}

public Task<List<Basegood>> GetBaseGoodListAsync()
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace KakeysBakeryClassLib.Services.Interfaces;
public interface IPurchaseService
{
public Task<List<Purchase>> GetPurchaseListAsync();
public Task<Purchase?> GetPurchaseAsync(int purchaseId);
public Task CreatePurchaseAsync(Purchase purchase);
public Task DeletePurchaseAsync(int purchaseId);
public Task UpdatePurchaseAsync(Purchase purchase);
Expand Down
131 changes: 131 additions & 0 deletions KakeysBakery.Tests/PurchaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Json;

namespace KakeysBakery.Tests;

public class PurchaseTests : IClassFixture<BakeryFactory>
{
public HttpClient client { get; set; }
public BakeryFactory bakeryFactory { get; set; }
public PurchaseTests(BakeryFactory Factory)
{
client = Factory.CreateDefaultClient();
}

[Fact]
public void CanPassATest()
{
Assert.Equal(1, 1);
}

[Fact]
public async Task Get_PurchaseList()
{
// ARRANGE
Purchase testPurchase = new()
{
Id = 245,
Actualprice = (decimal)100.40
};

await client.PostAsJsonAsync("api/Purchase/add", testPurchase);

// ACT
List<Purchase>? result = await client.GetFromJsonAsync<List<Purchase>>("api/Purchase/getall");

// ASSERT
Assert.NotNull(result);
Assert.NotEmpty(result);
}

[Fact]
public async Task Get_Purchase_ById()
{
// ARRANGE
Purchase testPurchase = new()
{
Id = 246,
Actualprice = (decimal)100.40
};

await client.PostAsJsonAsync("api/Purchase/add", testPurchase);

// ACT
Purchase? result = await client.GetFromJsonAsync<Purchase>($"api/Purchase/get/{testPurchase.Id}");

// ASSERT
Assert.NotNull(result);

Assert.Equal(testPurchase.Actualprice, result.Actualprice);
Assert.Equal(testPurchase.Id, result.Id);
}

[Fact]
public async Task Create_Purchase()
{
// ARRANGE
Purchase testPurchase = new()
{
Id = 248,
Actualprice = (decimal)100.40
};

// ACT
await client.PostAsJsonAsync("api/Purchase/add", testPurchase);
Purchase? result = await client.GetFromJsonAsync<Purchase>($"api/Purchase/get/{testPurchase.Id}");

// Assert
Assert.NotNull(result);

Assert.Equal(testPurchase.Actualprice, result.Actualprice);
Assert.Equal(testPurchase.Id, result.Id);
}

[Fact]
public async Task Edit_Purchase()
{
// ARRANGE
Purchase testPurchase = new()
{
Id = 249,
Actualprice = (decimal)100.40
};

await client.PostAsJsonAsync("api/Purchase/add", testPurchase);

// ACT
testPurchase.Actualprice = (decimal)123.50;
await client.PatchAsJsonAsync("api/Purchase/update", testPurchase);

Purchase? result = await client.GetFromJsonAsync<Purchase>($"api/Purchase/get/{testPurchase.Id}");


// ASSERT
Assert.NotNull(result);

Assert.Equal(testPurchase.Actualprice, result.Actualprice);
Assert.Equal(testPurchase.Id, result.Id);
}

[Fact]
public async Task Delete_Purchase()
{
// ARRANGE
Purchase testPurchase = new()
{
Id = 250,
Actualprice = (decimal)100.40
};

await client.PostAsJsonAsync("api/Purchase/add", testPurchase);

// ACT
await client.DeleteAsync($"api/Purchase/delete/{testPurchase.Id}");

// ASSERT
await Assert.ThrowsAsync<HttpRequestException>(async () =>
{
await client.GetFromJsonAsync<Addon>($"api/Purchase/get/{testPurchase.Id}");
});
}
}
22 changes: 17 additions & 5 deletions KakeysBakery/Controllers/PurchaseController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KakeysBakeryClassLib.Data;
using KakeysBakery.Services;
using KakeysBakeryClassLib.Data;
using KakeysBakeryClassLib.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -14,25 +15,36 @@ public PurchaseController(IPurchaseService PurchaseService)
}

[HttpGet("getall")]
public async Task<List<Purchase>> GetAddonsAsync()
public async Task<List<Purchase>> GetPurchaseListAsync()
{
return await purchaseService.GetPurchaseListAsync();
}

[HttpGet("get/{id}")]
public async Task<IActionResult> GetPurchaseAsync(int id)
{
var addon = await purchaseService.GetPurchaseAsync(id);
if (addon == null)
{
return NotFound(); // Return 404 Not Found status
}
return Ok(addon); // Return the addon if found
}

[HttpPost("add")]
public async Task CreateAddOnAsync(Purchase addon)
public async Task CreatePurchaseAsync(Purchase addon)
{
await purchaseService.CreatePurchaseAsync(addon);
}

[HttpDelete("delete/{id}")]
public async Task DeleteAddOnAsync(int id)
public async Task DeletePurchaseAsync(int id)
{
await purchaseService.DeletePurchaseAsync(id);
}

[HttpPatch("update")]
public async Task UpdateAddonAsync(Purchase addon)
public async Task UpdatePurchaseAsync(Purchase addon)
{
await purchaseService.UpdatePurchaseAsync(addon);
}
Expand Down
9 changes: 8 additions & 1 deletion KakeysBakery/Services/PurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task DeletePurchaseAsync(int purchaseID)
{
try
{
Purchase purchase = await _context.Purchases.FirstOrDefaultAsync(a => a.Id == purchaseID);
Purchase? purchase = await _context.Purchases.FirstOrDefaultAsync(a => a.Id == purchaseID);
if (purchase != null)
{
_context.Purchases.Remove(purchase);
Expand All @@ -46,6 +46,13 @@ public async Task<List<Purchase>> GetPurchaseListAsync()
catch { return new List<Purchase>(); }
}

public async Task<Purchase?> GetPurchaseAsync(int id)
{
return await _context.Purchases
.Where(p => p.Id == id)
.FirstOrDefaultAsync();
}

public Task UpdatePurchaseAsync(Purchase purchase)
{
try
Expand Down

0 comments on commit 399930f

Please sign in to comment.