|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using NUnit.Framework; |
| 3 | +using OA.Data; |
| 4 | +using OA.Domain.Entities; |
| 5 | +using OA.Persistence.Repository; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace OA.Test.Unit.Persistence |
| 9 | +{ |
| 10 | + public class GenericRepositoryTest |
| 11 | + { |
| 12 | + private DbContextOptionsBuilder builder; |
| 13 | + |
| 14 | + Customer customer; |
| 15 | + |
| 16 | + [SetUp] |
| 17 | + public void Setup() |
| 18 | + { |
| 19 | + |
| 20 | + builder = new DbContextOptionsBuilder(); |
| 21 | + builder.UseInMemoryDatabase("InMemoryCustomerDB"); |
| 22 | + |
| 23 | + customer = new Customer |
| 24 | + { |
| 25 | + CustomerName = "Shweta Naik", |
| 26 | + Address = "Bangalore" |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void CheckGenenricRepositoryAddCustomer() |
| 32 | + { |
| 33 | + using (var context = new CustomerContext(builder.Options)) |
| 34 | + { |
| 35 | + var customerRepository = new GenericRepository<Customer>(context); |
| 36 | + customerRepository.Add(customer); |
| 37 | + var result = customerRepository.SaveChanges(); |
| 38 | + Assert.IsTrue(result); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void CheckGenenricRepositoryUpdateCustomer() |
| 44 | + { |
| 45 | + using (var context = new CustomerContext(builder.Options)) |
| 46 | + { |
| 47 | + var customerRepository = new GenericRepository<Customer>(context); |
| 48 | + customerRepository.Update(customer); |
| 49 | + var result = customerRepository.SaveChanges(); |
| 50 | + Assert.IsTrue(result); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void CheckGenenricRepositoryDeleteCustomer() |
| 56 | + { |
| 57 | + using (var context = new CustomerContext(builder.Options)) |
| 58 | + { |
| 59 | + var customerRepository = new GenericRepository<Customer>(context); |
| 60 | + |
| 61 | + customerRepository.Add(customer); |
| 62 | + customerRepository.SaveChanges(); |
| 63 | + |
| 64 | + customerRepository.Delete(customer.Id); |
| 65 | + var result = customerRepository.SaveChanges(); |
| 66 | + Assert.IsTrue(result); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void CheckGenenricRepositoryGetCustomer() |
| 72 | + { |
| 73 | + using (var context = new CustomerContext(builder.Options)) |
| 74 | + { |
| 75 | + |
| 76 | + var customerRepository = new GenericRepository<Customer>(context); |
| 77 | + customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" }); |
| 78 | + customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" }); |
| 79 | + customerRepository.SaveChanges(); |
| 80 | + |
| 81 | + var cust = customerRepository.GetAll(); |
| 82 | + |
| 83 | + Assert.LessOrEqual(2, cust.Count()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void CheckGenenricRepositoryGetByIdCustomer() |
| 89 | + { |
| 90 | + using (var context = new CustomerContext(builder.Options)) |
| 91 | + { |
| 92 | + var customerRepository = new GenericRepository<Customer>(context); |
| 93 | + customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" }); |
| 94 | + customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" }); |
| 95 | + customerRepository.SaveChanges(); |
| 96 | + |
| 97 | + var cust = customerRepository.GetById(1); |
| 98 | + |
| 99 | + Assert.AreEqual(1, cust.Id); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | +} |
0 commit comments