|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using EntityFramework_Reverse_POCO_Generator; |
| 4 | +using Generator.Tests.Common; |
| 5 | +using NUnit.Framework; |
| 6 | +using Tester.BusinessLogic; |
| 7 | + |
| 8 | +namespace Tester.Integration.EFCore8 |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + [Category(Constants.Integration)] |
| 12 | + [Category(Constants.DbType.SqlServer)] |
| 13 | + public class CustomersRepositoryTests |
| 14 | + { |
| 15 | + private MyDbContext _db = null!; |
| 16 | + private Dictionary<string, string> _dictionary = null!; |
| 17 | + //private IConfiguration _configuration; |
| 18 | + |
| 19 | + [OneTimeSetUp] |
| 20 | + public void OneTimeSetUp() |
| 21 | + { |
| 22 | + _dictionary = new Dictionary<string, string> |
| 23 | + { |
| 24 | + { "ALFKI", "Alfreds Futterkiste" }, |
| 25 | + { "ANATR", "Ana Trujillo Emparedados y helados" }, |
| 26 | + { "ANTON", "Antonio Moreno Taquería" }, |
| 27 | + { "AROUT", "Around the Horn" }, |
| 28 | + { "BERGS", "Berglunds snabbköp" }, |
| 29 | + { "BLAUS", "Blauer See Delikatessen" }, |
| 30 | + { "BLONP", "Blondesddsl père et fils" }, |
| 31 | + { "BOLID", "Bólido Comidas preparadas" }, |
| 32 | + { "BONAP", "Bon app'" }, |
| 33 | + { "BOTTM", "Bottom-Dollar Markets"} |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + [OneTimeTearDown] |
| 38 | + public void OneTimeTearDown() |
| 39 | + { |
| 40 | + var customer = _db.Customers.FirstOrDefault(x => x.CustomerId == "TEST."); |
| 41 | + if (customer == null) |
| 42 | + return; |
| 43 | + _db.Customers.Remove(customer); |
| 44 | + _db.SaveChanges(); |
| 45 | + } |
| 46 | + |
| 47 | + [SetUp] |
| 48 | + public void SetUp() |
| 49 | + { |
| 50 | + //_configuration = new ConfigurationBuilder() |
| 51 | + // .AddJsonFile("appsettings.json", false, false) |
| 52 | + // .Build(); |
| 53 | + |
| 54 | + //_db = new MyDbContext(_configuration); |
| 55 | + _db = new MyDbContext(); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void UseEfDirectly() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + |
| 63 | + // Act |
| 64 | + var data = _db.Customers.Take(10).OrderBy(x => x.CustomerId).ToList(); |
| 65 | + |
| 66 | + // Assert |
| 67 | + AssertCustomerData(data); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void UseEfViaRepository() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var customersRepository = new CustomersRepository(_db); |
| 75 | + |
| 76 | + // Act |
| 77 | + var data = customersRepository.GetTop10().ToList(); |
| 78 | + |
| 79 | + // Assert |
| 80 | + AssertCustomerData(data); |
| 81 | + } |
| 82 | + |
| 83 | + private void AssertCustomerData(List<EntityFramework_Reverse_POCO_Generator.Customer> data) |
| 84 | + { |
| 85 | + Assert.AreEqual(_dictionary.Count, data.Count); |
| 86 | + foreach (var customer in data) |
| 87 | + { |
| 88 | + Assert.IsTrue(_dictionary.ContainsKey(customer.CustomerId)); |
| 89 | + Assert.AreEqual(_dictionary[customer.CustomerId], customer.CompanyName); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void InsertAndDeleteTestRecordSuccessfullyViaFindById() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + var db2 = new MyDbContext(); |
| 98 | + var db3 = new MyDbContext(); |
| 99 | + var customersRepository1 = new CustomersRepository(_db); |
| 100 | + var customersRepository2 = new CustomersRepository(db2); |
| 101 | + var customersRepository3 = new CustomersRepository(db3); |
| 102 | + var customer = new EntityFramework_Reverse_POCO_Generator.Customer |
| 103 | + { |
| 104 | + CustomerId = "TEST.", |
| 105 | + CompanyName = "Integration testing" |
| 106 | + }; |
| 107 | + |
| 108 | + // Act |
| 109 | + customersRepository1.AddCustomer(customer); |
| 110 | + var customer2 = customersRepository2.FindById(customer.CustomerId); |
| 111 | + customersRepository2.DeleteCustomer(customer2); |
| 112 | + var customer3 = customersRepository3.FindById(customer.CustomerId); // Should not be found |
| 113 | + |
| 114 | + // Assert |
| 115 | + Assert.IsNotNull(customer2); |
| 116 | + Assert.AreEqual(customer.CustomerId, customer2.CustomerId); |
| 117 | + Assert.AreEqual(customer.CompanyName, customer2.CompanyName); |
| 118 | + Assert.IsNull(customer3); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public void InsertAndDeleteTestRecordSuccessfullyViaFind() |
| 123 | + { |
| 124 | + // Arrange |
| 125 | + var db2 = new MyDbContext(); |
| 126 | + var db3 = new MyDbContext(); |
| 127 | + var customersRepository1 = new CustomersRepository(_db); |
| 128 | + var customersRepository2 = new CustomersRepository(db2); |
| 129 | + var customersRepository3 = new CustomersRepository(db3); |
| 130 | + var customer = new EntityFramework_Reverse_POCO_Generator.Customer |
| 131 | + { |
| 132 | + CustomerId = "TEST.", |
| 133 | + CompanyName = "Integration testing" |
| 134 | + }; |
| 135 | + |
| 136 | + // Act |
| 137 | + customersRepository1.AddCustomer(customer); |
| 138 | + var customer2 = customersRepository2.Find(customer.CustomerId); |
| 139 | + customersRepository2.DeleteCustomer(customer2); |
| 140 | + var customer3 = customersRepository3.Find(customer.CustomerId); // Should not be found |
| 141 | + |
| 142 | + // Assert |
| 143 | + Assert.IsNotNull(customer2); |
| 144 | + Assert.AreEqual(customer.CustomerId, customer2.CustomerId); |
| 145 | + Assert.AreEqual(customer.CompanyName, customer2.CompanyName); |
| 146 | + Assert.IsNull(customer3); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments