Untitled
unknown
plain_text
2 years ago
14 kB
10
Indexable
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Threading;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;
using Product_Stock_Maintenance.models;
using ProductStockMaintenance.Repo;
namespace UnitTest
{
[TestFixture]
public class RepoTest
{
private string ConnectionStrings = "Server=localhost;Database=Template;User=root;Password=Psnumber#2023;";
[Test]
[TestCase("pavan", "pavan@123")]
[TestCase("bharadwaj", "bharadwaj@123")]
[TestCase("invalidUser", "invalidPassword")]
public async Task ValidateUserTest(string username, string password)
{
//Arrange
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
// Act
var repo = new Repo(dbContext);
var result = await repo.ValidateUser(username, password);
// Assert
switch (result)
{
case "admin":
case "users":
case "Invalid login Credentials ":
Assert.Pass();
break;
default:
Assert.Fail();
break;
}
}
}
[Test]
[TestCase("pavan", "pavan@123")]
public async Task catchValidateUserTest(string username, string password)
{
//Arrange
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
// Act
var repo = new Repo(dbContext);
try
{
var result = await repo.ValidateUser(username, password);
throw new Exception(" ");
}
catch(Exception){
Assert.Pass();
}
}
}
[Test]
public async Task ProductsTest()
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.Products();
if (result != null)
{
Assert.Pass();
}
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
public async Task catchProductsTest()
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.Products();
throw new Exception(" ");
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
[TestCase(102, 46)]
[TestCase(110, 5)]
public async Task AddQuantityTest(int id, int quantity)
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.AddQuantity(id, quantity);
var output = result.value as Inventory;
Console.WriteLine(output);
/*switch(result){
case "product not existed in the inevntory":
case (result.Quantity >= 46):
Assert.Pass();
break;
default:
Assert.Fail();
break;
}*/
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
[TestCase(110, 5)]
public async Task catchAddQuantityTest(int id, int quantity)
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.AddQuantity(id, quantity);
throw new Exception(" ");
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
[TestCase(102)]
[TestCase(102)]
public async Task DeleteProductTest(int id)
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.DeleteProduct(id);
if (result != null)
{
Assert.Pass();
}
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
public async Task catchDeleteProductTest()
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.DeleteProduct(12);
throw new Exception(" ");
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
public async Task AddNewProductsTest()
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
var newproduct = new Inventory { ProductId = 01, ProductName = "iphone15", Price = 100000, Quantity = 2, CreatedBy = "pavan" };
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.AddNewProducts(newproduct);
if (result == newproduct)
{
Assert.Pass();
}
else
{
Assert.Fail();
}
}
catch (Exception)
{
Assert.Pass();
}
}
}
[Test]
public async Task catchAddNewProductsTest()
{
var options = new DbContextOptionsBuilder<TemplateContext>()
.UseMySql(ConnectionStrings, new MySqlServerVersion(new Version(8, 0, 23)))
.Options;
var newproduct = new Inventory { ProductId = 01, ProductName = "iphone15", Price = 100000, Quantity = 2, CreatedBy = "pavan" };
using (var dbContext = new TemplateContext(options))
{
var repo = new Repo(dbContext);
try
{
var result = await repo.AddNewProducts(newproduct);
throw new Exception(" ");
}
catch (Exception)
{
Assert.Pass();
}
}
}
}
}
repo
using Microsoft.EntityFrameworkCore;
using Product_Stock_Maintenance.models;
namespace ProductStockMaintenance.Repo
{
public class Repo:IRepo
{
private readonly TemplateContext _context;
public Repo(TemplateContext db)
{
_context = db;
}
public async Task<string> ValidateUser(string username,string password)
{
try
{
var users = await (from data in _context.Logins
where data.Username==username && data.Password==password
select data).FirstOrDefaultAsync();
if(users != null )
{
return (bool)users.AdminAccess ? "admin" : "users";
}
else
{
return "Invalid login Credentials ";
}
}
catch (Exception ex)
{
throw new Exception("error occured",ex);
}
}
public async Task<Object> Products()
{
try
{
var inventory =await(from i in _context.Inventories
from c in _context.Categorynames
where i.CategoryId==c.CategoryId && i.IsDelete==true
select new {i.ProductId,i.ProductName,i.Price,i.Quantity,c.Category,i.ProductImage} ).ToListAsync();
return inventory;
}
catch (Exception ex)
{
throw new Exception("No product Found ",ex);
}
}
public async Task<object> AddQuantity(int Product_Id, int quantity)
{
try
{
var res = _context.Inventories.Where(x => x.ProductId == Product_Id) ;
res.ExecuteUpdate(setters => setters.SetProperty(x => x.Quantity, x => x.Quantity + quantity));
bool var=res.Any();
if(var)
{
await _context.SaveChangesAsync();
return res;
}
return "product not existed in the inevntory";
}
catch(Exception ex)
{
throw new Exception(" " ,ex);
}
}
public async Task<object> DeleteProduct(int productId)
{
try
{
_context.Database.EnsureCreated();
var delete = await (from s in _context.Inventories
where s.ProductId == productId && s.IsDelete==true
select s).FirstOrDefaultAsync();
if (delete != null)
{
delete.IsDelete = false;
await _context.SaveChangesAsync();
return delete;
}
return "Invalid ProductId OR This product already deleted";
}
catch(Exception ex)
{
throw new Exception(" " ,ex);
}
}
public async Task<object> AddNewProducts(Inventory inventory)
{
try
{
_context.Database.EnsureCreated();
inventory.IsDelete=true;
var newobject= await(from s in _context.Inventories
select new{ s.ProductName,s.Quantity,s.Price,s.CreatedBy,s.CategoryId}).ToListAsync();
if (newobject.Any(obj =>obj.ProductName==inventory.ProductName))
{
return "Dupliate Entry";
}
_context.SaveChanges();
return inventory;
}
catch(Exception ex)
{
throw new Exception(" " ,ex);
}
}Editor is loading...
Leave a Comment