Skip to content

Commit b09092c

Browse files
committed
EfCore8 Unit tests
1 parent 8f5aad9 commit b09092c

File tree

6 files changed

+429
-15
lines changed

6 files changed

+429
-15
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}

Tester.Integration.EFCore8/EfrpgTest.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
using System.Threading;
2525
using System.Threading.Tasks;
2626

27-
namespace V7EfrpgTest
27+
namespace V8EfrpgTest
2828
{
2929
#region Database context interface
3030

31-
public interface IV7EfrpgTestDbContext : IDisposable
31+
public interface IV8EfrpgTestDbContext : IDisposable
3232
{
3333
DbSet<A> A { get; set; } // A
3434
DbSet<Aaref> Aarefs { get; set; } // AAREF
@@ -358,13 +358,13 @@ public interface IV7EfrpgTestDbContext : IDisposable
358358

359359
#region Database context
360360

361-
public class V7EfrpgTestDbContext : DbContext, IV7EfrpgTestDbContext
361+
public class V8EfrpgTestDbContext : DbContext, IV8EfrpgTestDbContext
362362
{
363-
public V7EfrpgTestDbContext()
363+
public V8EfrpgTestDbContext()
364364
{
365365
}
366366

367-
public V7EfrpgTestDbContext(DbContextOptions<V7EfrpgTestDbContext> options)
367+
public V8EfrpgTestDbContext(DbContextOptions<V8EfrpgTestDbContext> options)
368368
: base(options)
369369
{
370370
}
@@ -1803,19 +1803,19 @@ public decimal UdfNetSale(int? quantity = null, decimal? listPrice = null, decim
18031803

18041804
#region Database context factory
18051805

1806-
public class V7EfrpgTestDbContextFactory : IDesignTimeDbContextFactory<V7EfrpgTestDbContext>
1806+
public class V8EfrpgTestDbContextFactory : IDesignTimeDbContextFactory<V8EfrpgTestDbContext>
18071807
{
1808-
public V7EfrpgTestDbContext CreateDbContext(string[] args)
1808+
public V8EfrpgTestDbContext CreateDbContext(string[] args)
18091809
{
1810-
return new V7EfrpgTestDbContext();
1810+
return new V8EfrpgTestDbContext();
18111811
}
18121812
}
18131813

18141814
#endregion
18151815

18161816
#region Fake Database context
18171817

1818-
public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext
1818+
public class FakeV8EfrpgTestDbContext : IV8EfrpgTestDbContext
18191819
{
18201820
public DbSet<A> A { get; set; } // A
18211821
public DbSet<Aaref> Aarefs { get; set; } // AAREF
@@ -1922,9 +1922,9 @@ public class FakeV7EfrpgTestDbContext : IV7EfrpgTestDbContext
19221922
public DbSet<WVN_VArticle> WVN_VArticles { get; set; } // v_Articles
19231923
public DbSet<Брендытовара> Брендытовара { get; set; } // Бренды товара
19241924

1925-
public FakeV7EfrpgTestDbContext()
1925+
public FakeV8EfrpgTestDbContext()
19261926
{
1927-
_database = new FakeDatabaseFacade(new V7EfrpgTestDbContext());
1927+
_database = new FakeDatabaseFacade(new V8EfrpgTestDbContext());
19281928

19291929
A = new FakeDbSet<A>("AId");
19301930
Aarefs = new FakeDbSet<Aaref>("C1", "C2");

Tester.Integration.EFCore8/EfrpgTest.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
Settings.FileManagerType = FileManagerType.EfCore; // .NET project = VisualStudio; .NET Core project = EfCore; No output (testing only) = Null
1818
Settings.ConnectionString = "Data Source=(local);Initial Catalog=EfrpgTest;Integrated Security=True;MultipleActiveResultSets=True;Encrypt=false;TrustServerCertificate=true";
1919
Settings.ConnectionStringName = "MyDbContext"; // ConnectionString key as specified in your app.config/web.config/appsettings.json
20-
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.
20+
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.
2121
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;
2222
Settings.GenerateSeparateFiles = false;
23-
Settings.Namespace = "V7EfrpgTest"; // Override the default namespace here
23+
Settings.Namespace = "V8EfrpgTest"; // Override the default namespace here
2424
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
2525
Settings.TrimCharFields = true; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
2626
Settings.DisableGeographyTypes = false;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# This file contains a list of the files generated by the EfrpgTest.tt file.
22
# Please do not edit this file. It is used to delete files that may get filtered out during the next run.
3-
# Time start = 19/11/2023 15:11:46
4-
# Time end = 19/11/2023 15:11:47, duration = 1.29 seconds.
3+
# Time start = 19/11/2023 21:21:32
4+
# Time end = 19/11/2023 21:21:33, duration = 1.26 seconds.

0 commit comments

Comments
 (0)