Skip to content

Commit

Permalink
EfCore8 Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed Nov 19, 2023
1 parent 8f5aad9 commit b09092c
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 15 deletions.
149 changes: 149 additions & 0 deletions Tester.Integration.EFCore8/CustomersRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> _dictionary = null!;
//private IConfiguration _configuration;

[OneTimeSetUp]
public void OneTimeSetUp()
{
_dictionary = new Dictionary<string, string>
{
{ "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<EntityFramework_Reverse_POCO_Generator.Customer> 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);
}
}
}
22 changes: 11 additions & 11 deletions Tester.Integration.EFCore8/EfrpgTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> A { get; set; } // A
DbSet<Aaref> Aarefs { get; set; } // AAREF
Expand Down Expand Up @@ -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<V7EfrpgTestDbContext> options)
public V8EfrpgTestDbContext(DbContextOptions<V8EfrpgTestDbContext> options)
: base(options)
{
}
Expand Down Expand Up @@ -1803,19 +1803,19 @@ public decimal UdfNetSale(int? quantity = null, decimal? listPrice = null, decim

#region Database context factory

public class V7EfrpgTestDbContextFactory : IDesignTimeDbContextFactory<V7EfrpgTestDbContext>
public class V8EfrpgTestDbContextFactory : IDesignTimeDbContextFactory<V8EfrpgTestDbContext>
{
public V7EfrpgTestDbContext CreateDbContext(string[] args)
public V8EfrpgTestDbContext CreateDbContext(string[] args)
{
return new V7EfrpgTestDbContext();
return new V8EfrpgTestDbContext();
}
}

#endregion

#region Fake Database context

public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext
public class FakeV8EfrpgTestDbContext : IV8EfrpgTestDbContext
{
public DbSet<A> A { get; set; } // A
public DbSet<Aaref> Aarefs { get; set; } // AAREF
Expand Down Expand Up @@ -1922,9 +1922,9 @@ public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext
public DbSet<WVN_VArticle> 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<A>("AId");
Aarefs = new FakeDbSet<Aaref>("C1", "C2");
Expand Down
4 changes: 2 additions & 2 deletions Tester.Integration.EFCore8/EfrpgTest.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Tester.Integration.EFCore8/EfrpgTestAudit.txt
Original file line number Diff line number Diff line change
@@ -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.
Loading

0 comments on commit b09092c

Please sign in to comment.