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

2540110741_AxelValent Practice UnitTest API #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions CartTesting/BaseTestCart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerTesting
{
public class BaseTestCart
{
protected readonly WebApplicationFactory<Program> _factory;
public BaseTestCart()
{
_factory = new WebApplicationFactory<Program>();
}

public HttpClient GetClient => _factory.CreateClient();
}
}
24 changes: 24 additions & 0 deletions CartTesting/CartTesting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
94 changes: 94 additions & 0 deletions CartTesting/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Contracts.RequestModels.Cart;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using Newtonsoft.Json;
using System.Net;
using System.Text;

namespace CustomerTesting
{
[TestFixture]
public class Tests : BaseTestCart
{
private readonly HttpClient _client;
public Tests()
{
_client = GetClient;
}

[Test]
public async Task TestCreate()
{
// arrange
var client = _factory.CreateClient();
var fromBody = new CreateCartRequest()
{
Product = "ProductTest",
Price = 110000
};
// act
//var response = await client.GetAsync("api/v1/customer");
var temp = new StringContent(JsonConvert.SerializeObject(fromBody), Encoding.UTF8, "application/json");
var response = await client.PostAsync("api/v1/product", temp);

Assert.That(response.IsSuccessStatusCode, Is.True);
Assert.Pass();
}

[Test]
public async Task TestGet()
{
// arrange
var client = _factory.CreateClient();

var response = await client.GetAsync("api/v1/product");

//var content = await response.Content.ReadAsStringAsync();

Assert.That(response.IsSuccessStatusCode, Is.True);
}

[Test]
public async Task TestGetByID()
{
// arrange
var client = _factory.CreateClient();

var response = await client.GetAsync("api/v1/product");

Assert.That(response.IsSuccessStatusCode, Is.True);
}

[Test]
public async Task TestUpdate()
{
// arrange
var client = _factory.CreateClient();
var fromBody = new UpdateProductModel()
{
Name = "UpdatedName",
Price = 120000
};

// act
var temp = new StringContent(JsonConvert.SerializeObject(fromBody), Encoding.UTF8, "application/json");
var response = await _client.PutAsync($"api/v1/product/product id", temp);

// assert
Assert.That(response.IsSuccessStatusCode, Is.True);
}

[Test]
public async Task TestDelete()
{
// arrange
var client = _factory.CreateClient();

// act
var response = await _client.DeleteAsync($"api/v1/product/product id here");

// assert
Assert.That(response.IsSuccessStatusCode, Is.True);
}
}
}
15 changes: 15 additions & 0 deletions Contracts/RequestModels/Cart/CartDataListRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Contracts.ResponseModels.Cart;
using Contracts.ResponseModels.Customer;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Cart
{
public class CartDataListRequest : IRequest<CartDataListResponse>
{
}
}
18 changes: 18 additions & 0 deletions Contracts/RequestModels/Cart/CreateCartRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Contracts.ResponseModels.Cart;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Cart
{
public class CreateCartRequest : IRequest<CreateCartResponse>
{
public Guid ProductID { get; set; }
public Guid CustomerID { get; set; }

public int Quantity { get; set; }
}
}
16 changes: 16 additions & 0 deletions Contracts/RequestModels/Cart/DeleteCartRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Contracts.ResponseModels.Cart;
using Contracts.ResponseModels.Customer;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Cart
{
public class DeleteCartRequest : IRequest<DeleteCartResponse>
{
public Guid CartID { get; set; }
}
}
23 changes: 23 additions & 0 deletions Contracts/RequestModels/Cart/UpdateCartRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Contracts.ResponseModels.Cart;
using Contracts.ResponseModels.Customer;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Cart
{
public class UpdateCartRequest : UpdateCartModel, IRequest<UpdateCartResponse>
{
public Guid? CartID { get; set; }

}
public class UpdateCartModel
{
//public Guid ProductID { get; set; }
//public Guid CustomerID { get; set; }
public int Quantity { get; set; }
}
}
15 changes: 15 additions & 0 deletions Contracts/RequestModels/Customer/DeleteCustomerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Contracts.ResponseModels.Customer;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Customer
{
public class DeleteCustomerRequest : IRequest<DeleteCustomerResponse>
{
public Guid CustomerID { get; set; }
}
}
21 changes: 21 additions & 0 deletions Contracts/RequestModels/Customer/UpdateCustomerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Contracts.ResponseModels.Customer;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Customer
{
public class UpdateCustomerRequest : UpdateCustomerModel, IRequest<UpdateCustomerResponse>
{
public Guid? CustomerID { get; set; }

}
public class UpdateCustomerModel
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
}
17 changes: 17 additions & 0 deletions Contracts/RequestModels/Product/CreateProductRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Contracts.ResponseModels.Customer;
using Contracts.ResponseModels.Product;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Product
{
public class CreateProductRequest : IRequest<CreateProductResponse>
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
}
15 changes: 15 additions & 0 deletions Contracts/RequestModels/Product/DeleteProductRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Contracts.ResponseModels.Product;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Product
{
public class DeleteProductRequest : IRequest<DeleteProductResponse>
{
public Guid ProductID { get; set; }
}
}
14 changes: 14 additions & 0 deletions Contracts/RequestModels/Product/ProductDataListRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Contracts.ResponseModels.Product;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Product
{
public class ProductDataListRequest : IRequest<ProductDataListResponse>
{
}
}
21 changes: 21 additions & 0 deletions Contracts/RequestModels/Product/UpdateProductRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Contracts.ResponseModels.Customer;
using Contracts.ResponseModels.Product;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.RequestModels.Product
{
public class UpdateProductRequest : UpdateProductModel, IRequest<UpdateProductResponse>
{
public Guid? ProductID { get; set; }
}
public class UpdateProductModel
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
}
25 changes: 25 additions & 0 deletions Contracts/ResponseModels/Cart/CartDataListResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.ResponseModels.Cart
{
public class CartDataListResponse
{
public List<CartData> CartDatas { get; set; } = new List<CartData>();
}

public class CartData
{
public Guid CartID { get; set; }

public string ProductName { get; set; } = string.Empty;

public string CustomerName { get; set; } = string.Empty;
public decimal Price { get; set; }
public int Quantity { get; set; }
public decimal SubTotal => Price * Quantity;
}
}
13 changes: 13 additions & 0 deletions Contracts/ResponseModels/Cart/CreateCartResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.ResponseModels.Cart
{
public class CreateCartResponse
{
public Guid CartID { get; set; }
}
}
13 changes: 13 additions & 0 deletions Contracts/ResponseModels/Cart/DeleteCartResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.ResponseModels.Cart
{
public class DeleteCartResponse
{
public string Message { get; set; } = string.Empty;
}
}
14 changes: 14 additions & 0 deletions Contracts/ResponseModels/Cart/UpdateCartResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contracts.ResponseModels.Cart
{
public class UpdateCartResponse
{
public bool Success { get; set; }
public string Message { get; set; } = string.Empty;
}
}
Loading