diff --git a/Tester.Integration.EFCore8/CustomersRepositoryTests.cs b/Tester.Integration.EFCore8/CustomersRepositoryTests.cs new file mode 100644 index 00000000..fcf25132 --- /dev/null +++ b/Tester.Integration.EFCore8/CustomersRepositoryTests.cs @@ -0,0 +1,149 @@ +using System.Collections.Generic; +using System.Linq; +using EntityFramework_Reverse_POCO_Generator; +using Generator.Tests.Common; +using NUnit.Framework; +using Tester.BusinessLogic; + +namespace Tester.Integration.EFCore8 +{ + [TestFixture] + [Category(Constants.Integration)] + [Category(Constants.DbType.SqlServer)] + public class CustomersRepositoryTests + { + private MyDbContext _db = null!; + private Dictionary _dictionary = null!; + //private IConfiguration _configuration; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _dictionary = new Dictionary + { + { "ALFKI", "Alfreds Futterkiste" }, + { "ANATR", "Ana Trujillo Emparedados y helados" }, + { "ANTON", "Antonio Moreno Taquería" }, + { "AROUT", "Around the Horn" }, + { "BERGS", "Berglunds snabbköp" }, + { "BLAUS", "Blauer See Delikatessen" }, + { "BLONP", "Blondesddsl père et fils" }, + { "BOLID", "Bólido Comidas preparadas" }, + { "BONAP", "Bon app'" }, + { "BOTTM", "Bottom-Dollar Markets"} + }; + } + + [OneTimeTearDown] + public void OneTimeTearDown() + { + var customer = _db.Customers.FirstOrDefault(x => x.CustomerId == "TEST."); + if (customer == null) + return; + _db.Customers.Remove(customer); + _db.SaveChanges(); + } + + [SetUp] + public void SetUp() + { + //_configuration = new ConfigurationBuilder() + // .AddJsonFile("appsettings.json", false, false) + // .Build(); + + //_db = new MyDbContext(_configuration); + _db = new MyDbContext(); + } + + [Test] + public void UseEfDirectly() + { + // Arrange + + // Act + var data = _db.Customers.Take(10).OrderBy(x => x.CustomerId).ToList(); + + // Assert + AssertCustomerData(data); + } + + [Test] + public void UseEfViaRepository() + { + // Arrange + var customersRepository = new CustomersRepository(_db); + + // Act + var data = customersRepository.GetTop10().ToList(); + + // Assert + AssertCustomerData(data); + } + + private void AssertCustomerData(List data) + { + Assert.AreEqual(_dictionary.Count, data.Count); + foreach (var customer in data) + { + Assert.IsTrue(_dictionary.ContainsKey(customer.CustomerId)); + Assert.AreEqual(_dictionary[customer.CustomerId], customer.CompanyName); + } + } + + [Test] + public void InsertAndDeleteTestRecordSuccessfullyViaFindById() + { + // Arrange + var db2 = new MyDbContext(); + var db3 = new MyDbContext(); + var customersRepository1 = new CustomersRepository(_db); + var customersRepository2 = new CustomersRepository(db2); + var customersRepository3 = new CustomersRepository(db3); + var customer = new EntityFramework_Reverse_POCO_Generator.Customer + { + CustomerId = "TEST.", + CompanyName = "Integration testing" + }; + + // Act + customersRepository1.AddCustomer(customer); + var customer2 = customersRepository2.FindById(customer.CustomerId); + customersRepository2.DeleteCustomer(customer2); + var customer3 = customersRepository3.FindById(customer.CustomerId); // Should not be found + + // Assert + Assert.IsNotNull(customer2); + Assert.AreEqual(customer.CustomerId, customer2.CustomerId); + Assert.AreEqual(customer.CompanyName, customer2.CompanyName); + Assert.IsNull(customer3); + } + + [Test] + public void InsertAndDeleteTestRecordSuccessfullyViaFind() + { + // Arrange + var db2 = new MyDbContext(); + var db3 = new MyDbContext(); + var customersRepository1 = new CustomersRepository(_db); + var customersRepository2 = new CustomersRepository(db2); + var customersRepository3 = new CustomersRepository(db3); + var customer = new EntityFramework_Reverse_POCO_Generator.Customer + { + CustomerId = "TEST.", + CompanyName = "Integration testing" + }; + + // Act + customersRepository1.AddCustomer(customer); + var customer2 = customersRepository2.Find(customer.CustomerId); + customersRepository2.DeleteCustomer(customer2); + var customer3 = customersRepository3.Find(customer.CustomerId); // Should not be found + + // Assert + Assert.IsNotNull(customer2); + Assert.AreEqual(customer.CustomerId, customer2.CustomerId); + Assert.AreEqual(customer.CompanyName, customer2.CompanyName); + Assert.IsNull(customer3); + } + } +} diff --git a/Tester.Integration.EFCore8/EfrpgTest.cs b/Tester.Integration.EFCore8/EfrpgTest.cs index 84a18bf7..b982ba8f 100644 --- a/Tester.Integration.EFCore8/EfrpgTest.cs +++ b/Tester.Integration.EFCore8/EfrpgTest.cs @@ -24,11 +24,11 @@ using System.Threading; using System.Threading.Tasks; -namespace V7EfrpgTest +namespace V8EfrpgTest { #region Database context interface - public interface IV7EfrpgTestDbContext : IDisposable + public interface IV8EfrpgTestDbContext : IDisposable { DbSet A { get; set; } // A DbSet Aarefs { get; set; } // AAREF @@ -358,13 +358,13 @@ public interface IV7EfrpgTestDbContext : IDisposable #region Database context - public class V7EfrpgTestDbContext : DbContext, IV7EfrpgTestDbContext + public class V8EfrpgTestDbContext : DbContext, IV8EfrpgTestDbContext { - public V7EfrpgTestDbContext() + public V8EfrpgTestDbContext() { } - public V7EfrpgTestDbContext(DbContextOptions options) + public V8EfrpgTestDbContext(DbContextOptions options) : base(options) { } @@ -1803,11 +1803,11 @@ public decimal UdfNetSale(int? quantity = null, decimal? listPrice = null, decim #region Database context factory - public class V7EfrpgTestDbContextFactory : IDesignTimeDbContextFactory + public class V8EfrpgTestDbContextFactory : IDesignTimeDbContextFactory { - public V7EfrpgTestDbContext CreateDbContext(string[] args) + public V8EfrpgTestDbContext CreateDbContext(string[] args) { - return new V7EfrpgTestDbContext(); + return new V8EfrpgTestDbContext(); } } @@ -1815,7 +1815,7 @@ public V7EfrpgTestDbContext CreateDbContext(string[] args) #region Fake Database context - public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext + public class FakeV8EfrpgTestDbContext : IV8EfrpgTestDbContext { public DbSet A { get; set; } // A public DbSet Aarefs { get; set; } // AAREF @@ -1922,9 +1922,9 @@ public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext public DbSet WVN_VArticles { get; set; } // v_Articles public DbSet<Брендытовара> Брендытовара { get; set; } // Бренды товара - public FakeV7EfrpgTestDbContext() + public FakeV8EfrpgTestDbContext() { - _database = new FakeDatabaseFacade(new V7EfrpgTestDbContext()); + _database = new FakeDatabaseFacade(new V8EfrpgTestDbContext()); A = new FakeDbSet("AId"); Aarefs = new FakeDbSet("C1", "C2"); diff --git a/Tester.Integration.EFCore8/EfrpgTest.tt b/Tester.Integration.EFCore8/EfrpgTest.tt index 63f5dec2..92f3a3ba 100644 --- a/Tester.Integration.EFCore8/EfrpgTest.tt +++ b/Tester.Integration.EFCore8/EfrpgTest.tt @@ -17,10 +17,10 @@ Settings.FileManagerType = FileManagerType.EfCore; // .NET project = VisualStudio; .NET Core project = EfCore; No output (testing only) = Null Settings.ConnectionString = "Data Source=(local);Initial Catalog=EfrpgTest;Integrated Security=True;MultipleActiveResultSets=True;Encrypt=false;TrustServerCertificate=true"; Settings.ConnectionStringName = "MyDbContext"; // ConnectionString key as specified in your app.config/web.config/appsettings.json - Settings.DbContextName = "V7EfrpgTestDbContext"; // Class name for the DbContext to be generated. Note: If generating separate files, please give the db context a different name from this tt filename. + Settings.DbContextName = "V8EfrpgTestDbContext"; // Class name for the DbContext to be generated. Note: If generating separate files, please give the db context a different name from this tt filename. Settings.OnConfiguration = OnConfiguration.ConnectionString; // EFCore only. Determines the code generated within DbContext.OnConfiguration(). Please read https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/OnConfiguration Settings.GenerateSeparateFiles = false; Settings.GenerateSeparateFiles = false; - Settings.Namespace = "V7EfrpgTest"; // Override the default namespace here + Settings.Namespace = "V8EfrpgTest"; // Override the default namespace here Settings.AddUnitTestingDbContext = true; // Will add a FakeDbContext and FakeDbSet for easy unit testing. Read https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/FakeDbContext Settings.TrimCharFields = true; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database. Settings.DisableGeographyTypes = false; diff --git a/Tester.Integration.EFCore8/EfrpgTestAudit.txt b/Tester.Integration.EFCore8/EfrpgTestAudit.txt index 7536a249..1de26737 100644 --- a/Tester.Integration.EFCore8/EfrpgTestAudit.txt +++ b/Tester.Integration.EFCore8/EfrpgTestAudit.txt @@ -1,4 +1,4 @@ # This file contains a list of the files generated by the EfrpgTest.tt file. # Please do not edit this file. It is used to delete files that may get filtered out during the next run. -# Time start = 19/11/2023 15:11:46 -# Time end = 19/11/2023 15:11:47, duration = 1.29 seconds. +# Time start = 19/11/2023 21:21:32 +# Time end = 19/11/2023 21:21:33, duration = 1.26 seconds. diff --git a/Tester.Integration.EFCore8/FakeDbSetTests.cs b/Tester.Integration.EFCore8/FakeDbSetTests.cs new file mode 100644 index 00000000..ab065abb --- /dev/null +++ b/Tester.Integration.EFCore8/FakeDbSetTests.cs @@ -0,0 +1,221 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Generator.Tests.Common; +using NUnit.Framework; +using V8EfrpgTest; + +namespace Tester.Integration.EFCore8 +{ + [TestFixture] + [Category(Constants.CI)] + public class FakeDbSetTests + { + private V8EfrpgTest.FakeDbSet _dbSet = null!; + private List _list = null!; + + [SetUp] + public void SetUp() + { + _dbSet = new V8EfrpgTest.FakeDbSet("AId"); + + _list = new List + { + new() { AId = 1, C1 = 2, C2 = 3 }, + new() { AId = 4, C1 = 5, C2 = 6 } + }; + } + + + [Test] + public void AsEnumerable() + { + _dbSet.AddRange(_list); + var result = _dbSet.AsEnumerable(); + Assert.AreEqual(2, result.Count()); + } + + [Test] + public async Task AsAsyncEnumerable() + { + var count = 0; + await _dbSet.AddRangeAsync(_list); + await foreach (var y in _dbSet.AsAsyncEnumerable()) + ++count; + + Assert.AreEqual(2, count); + } + + [Test] + [TestCase(1, 2, 3)] + [TestCase(4, 5, 6)] + public void Find(int id, int c1, int c2) + { + _dbSet.AddRange(_list); + var result = _dbSet.Find(id); + + Assert.IsNotNull(result); + Assert.AreEqual(id, result.AId); + Assert.AreEqual(c1, result.C1); + Assert.AreEqual(c2, result.C2); + } + + [Test] + [TestCase(1, 2, 3)] + [TestCase(4, 5, 6)] + public async Task FindAsync_CancellationToken(int id, int c1, int c2) + { + await _dbSet.AddRangeAsync(_list); + var cancellationToken = new CancellationToken(); + object[] keyValues = { id }; + var result = await _dbSet.FindAsync(keyValues, cancellationToken); + + Assert.IsNotNull(result); + Assert.AreEqual(id, result.AId); + Assert.AreEqual(c1, result.C1); + Assert.AreEqual(c2, result.C2); + } + + [Test] + [TestCase(1, 2, 3)] + [TestCase(4, 5, 6)] + public async Task FindAsync(int id, int c1, int c2) + { + await _dbSet.AddRangeAsync(_list); + var result = await _dbSet.FindAsync(id); + + Assert.IsNotNull(result); + Assert.AreEqual(id, result.AId); + Assert.AreEqual(c1, result.C1); + Assert.AreEqual(c2, result.C2); + } + + [Test] + public void Add() + { + _dbSet.Add(_list[0]); + _dbSet.Add(_list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public async Task AddAsync() + { + await _dbSet.AddAsync(_list[0]); + await _dbSet.AddAsync(_list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void AddRange_params() + { + _dbSet.AddRange(_list[0], _list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void AddRange_IEnumerable() + { + _dbSet.AddRange(_list); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public async Task AddRangeAsync_params() + { + await _dbSet.AddRangeAsync(_list[0], _list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public async Task AddRangeAsync_IEnumerable() + { + await _dbSet.AddRangeAsync(_list); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void Attach() + { + _dbSet.Attach(_list[0]); + _dbSet.Attach(_list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void AttachRange_params() + { + _dbSet.AttachRange(_list[0], _list[1]); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void AttachRange_IEnumerable() + { + _dbSet.AttachRange(_list); + Assert.AreEqual(2, _dbSet.Count()); + } + + [Test] + public void RemoveRange_params() + { + _dbSet.RemoveRange(_list[0], _list[1]); + Assert.AreEqual(0, _dbSet.Count()); + } + + [Test] + public void RemoveRange_IEnumerable() + { + _dbSet.RemoveRange(_list); + Assert.AreEqual(0, _dbSet.Count()); + } + + [Test] + public void Update() + { + _dbSet.AddRange(_list); + + _list[0].C1 = 987; + _dbSet.Update(_list[0]); + Assert.AreEqual(2, _dbSet.Count()); + + var result = _dbSet.Find(_list[0].AId); + Assert.AreEqual(987, result.C1); + } + + [Test] + public void UpdateRange() + { + _dbSet.AddRange(_list); + + _list[0].C1 = 987; + _dbSet.UpdateRange(_list); + Assert.AreEqual(2, _dbSet.Count()); + + var result = _dbSet.Find(_list[0].AId); + Assert.AreEqual(987, result.C1); + + result = _dbSet.Find(_list[1].AId); + Assert.AreEqual(_list[1].C1, result.C1); + } + + [Test] + public void GetList() + { + _dbSet.AddRange(_list); + var result = _dbSet.GetList(); + Assert.AreEqual(_list, result); + } + + [Test] + public void GetEnumerator() + { + var n = 0; + foreach (var entity in _dbSet) + { + Assert.AreEqual(_list[n++], entity); + } + } + } +} \ No newline at end of file diff --git a/Tester.Integration.EFCore8/StoredProcedureTests.cs b/Tester.Integration.EFCore8/StoredProcedureTests.cs new file mode 100644 index 00000000..dd164c50 --- /dev/null +++ b/Tester.Integration.EFCore8/StoredProcedureTests.cs @@ -0,0 +1,44 @@ +using EntityFramework_Reverse_POCO_Generator; +using Generator.Tests.Common; +using NUnit.Framework; + +namespace Tester.Integration.EFCore8 +{ + [TestFixture] + [Category(Constants.Integration)] + [Category(Constants.DbType.SqlServer)] + public class StoredProcedureTests + { + [SetUp] + public void SetUp() + { + _northwind = new MyDbContext(); + } + + private MyDbContext _northwind = null!; + + [Test] + [TestCase("ALFKI")] + [TestCase("FAMIA")] + [TestCase("WOLZA")] + public void CustOrderHist(string customerId) + { + Assert.IsNotNull(_northwind); + var data = _northwind.CustOrderHist(customerId); + var asyncData = _northwind.CustOrderHistAsync(customerId).Result; + + Assert.IsNotNull(data); + Assert.IsNotNull(asyncData); + Assert.AreEqual(data.Count, asyncData.Count); + + for (var n = 0; n < data.Count; ++n) + { + var dataItem = data[n]; + var asyncItem = asyncData[n]; + + Assert.AreEqual(dataItem.ProductName, asyncItem.ProductName); + Assert.AreEqual(dataItem.Total, asyncItem.Total); + } + } + } +} \ No newline at end of file