Skip to content

Commit 92323b8

Browse files
committed
XmlRepository now saves as 1.5 version.
Some tests recovered.
1 parent 8195bd5 commit 92323b8

8 files changed

Lines changed: 407 additions & 138 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,4 @@ _Pvt_Extensions
242242
/packages
243243
/nlog.txt
244244
/SharpRepository.Tests.Integration/Data/Db4o
245+
/SharpRepository.Tests.Integration/Data/Xml/Contact.xml

SharpRepository.Tests.Integration/Data/EfDataDirectoryFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public static string Build()
2121
_num++; // since it goes through and calls this for each test before running them, we need a different database for each test or else the auto increment goes to 2 on the second test with an add and it fails
2222
// surprisingly the timing isn;'t too bad for this, the first test takes about 7 secs for EF to model the DB and create it, then each other test is super quick in creating the DB file
2323

24-
if (File.Exists(file)) { File.Delete(file); }
24+
if (File.Exists(file)) {
25+
File.Delete(file);
26+
}
2527

2628
return file;
2729
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32

43
namespace SharpRepository.Tests.Integration.Helpers
54
{
@@ -9,9 +8,8 @@ public class CurrentDirectory : RelativeDirectory
98
/// Sets the relative directory location based on the environment's current directory.
109
/// Often this is the project's debug location when running locally.
1110
/// </summary>
12-
public CurrentDirectory() : base(System.AppContext.BaseDirectory)
11+
public CurrentDirectory() : base(AppDomain.CurrentDomain.BaseDirectory)
1312
{
14-
1513
}
1614
}
1715
}

SharpRepository.Tests.Integration/SharpRepository.Tests.Integration.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
11+
<PackageReference Include="EntityFramework.SqlServerCompact" Version="6.1.3" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
1213
<PackageReference Include="Moq" Version="4.7.99" />
1314
<PackageReference Include="NUnit" Version="3.8.1" />
@@ -44,4 +45,10 @@
4445
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
4546
</ItemGroup>
4647

48+
<ItemGroup>
49+
<None Update="appsettings.json">
50+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
51+
</None>
52+
</ItemGroup>
53+
4754
</Project>
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+

2+
using System.Linq;
3+
using NUnit.Framework;
4+
using SharpRepository.EfCoreRepository;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Caching;
7+
using SharpRepository.Tests.Integration.Data;
8+
using SharpRepository.Tests.Integration.TestObjects;
9+
using Shouldly;
10+
using System.Collections.Generic;
11+
using System.Diagnostics;
12+
using SharpRepository.Repository.FetchStrategies;
13+
using SharpRepository.Repository.Queries;
14+
using SharpRepository.Repository.Specifications;
15+
using Microsoft.EntityFrameworkCore;
16+
using Microsoft.Data.Sqlite;
17+
18+
namespace SharpRepository.Tests.Integration.Spikes
19+
{
20+
public class MyEfCoreRepository : EfCoreRepository<Contact, string>
21+
{
22+
public MyEfCoreRepository(DbContext dbContext, ICachingStrategy<Contact, string> cachingStrategy = null) : base(dbContext, cachingStrategy)
23+
{
24+
}
25+
26+
public bool LazyLoadValue
27+
{
28+
get { return false; } // TODO: return Context.Configuration.LazyLoadingEnabled; }
29+
}
30+
}
31+
32+
[TestFixture]
33+
public class EfCoreLazyLoadSpike
34+
{
35+
private TestObjectContextCore dbContext;
36+
private List<string> queries;
37+
38+
[SetUp]
39+
public void SetupRepository()
40+
{
41+
queries = new List<string>();
42+
var dbPath = EfDataDirectoryFactory.Build();
43+
44+
var connection = new SqliteConnection("DataSource=:memory:");
45+
connection.Open();
46+
47+
var options = new DbContextOptionsBuilder<TestObjectContextCore>()
48+
.UseSqlite(connection)
49+
.Options;
50+
51+
// Create the schema in the database
52+
var context = new TestObjectContextCore(options);
53+
54+
dbContext = new TestObjectContextCore(options);
55+
dbContext.Database.EnsureCreated();
56+
const int totalItems = 5;
57+
58+
for (int i = 1; i <= totalItems; i++)
59+
{
60+
dbContext.Contacts.Add(
61+
new Contact
62+
{
63+
ContactId = i.ToString(),
64+
Name = "Test User " + i,
65+
EmailAddresses = new List<EmailAddress> {
66+
new EmailAddress {
67+
ContactId = i.ToString(),
68+
EmailAddressId = i,
69+
Email = "omar.piani." + i.ToString() + "@email.com",
70+
Label = "omar.piani." + i.ToString()
71+
}
72+
}
73+
});
74+
}
75+
76+
dbContext.SaveChanges();
77+
78+
// TODO: reistantiate in order to lose caches
79+
//dbContext = new TestObjectContext(options);
80+
//dbContext.Database.EnsureCreated();
81+
}
82+
83+
[Test]
84+
public void EfCore_LazyLoad_Set_To_False()
85+
{
86+
//dbContext.Configuration.LazyLoadingEnabled = false;
87+
var repository = new MyEfCoreRepository(dbContext);
88+
repository.LazyLoadValue.ShouldBeFalse();
89+
}
90+
91+
[Test]
92+
public void EfCore_GetAll_Without_Includes_LazyLoads_Email()
93+
{
94+
//dbContext.Configuration.LazyLoadingEnabled = true;
95+
//dbContext.Database.Log = sql =>
96+
//{
97+
// if (sql.Contains("SELECT"))
98+
// {
99+
// queries.Add(sql);
100+
// }
101+
//};
102+
var repository = new MyEfCoreRepository(dbContext);
103+
104+
var contact = repository.GetAll().First();
105+
contact.Name.ShouldBe("Test User 1");
106+
dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(3);
107+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
108+
dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4);
109+
}
110+
111+
[Test]
112+
public void EfCore_GetAll_With_Includes_In_Strategy_LazyLoads_Email()
113+
{
114+
//dbContext.Configuration.LazyLoadingEnabled = true;
115+
//dbContext.Database.Log = sql =>
116+
//{
117+
// if (sql.Contains("SELECT"))
118+
// {
119+
// queries.Add(sql);
120+
// }
121+
//};
122+
var repository = new MyEfCoreRepository(dbContext);
123+
124+
125+
var strategy = new GenericFetchStrategy<Contact>();
126+
strategy.Include(x => x.EmailAddresses);
127+
128+
var contact = repository.GetAll(strategy).First();
129+
contact.Name.ShouldBe("Test User 1");
130+
dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4);
131+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
132+
dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4);
133+
}
134+
135+
[Test]
136+
public void EfCore_GetAll_With_Includes_In_Strategy_String_LazyLoads_Email()
137+
{
138+
//dbContext.Configuration.LazyLoadingEnabled = true;
139+
//dbContext.Database.Log = sql =>
140+
//{
141+
// if (sql.Contains("SELECT"))
142+
// {
143+
// queries.Add(sql);
144+
// }
145+
//};
146+
var repository = new MyEfCoreRepository(dbContext);
147+
148+
149+
var strategy = new GenericFetchStrategy<Contact>();
150+
strategy.Include(x => x.EmailAddresses);
151+
152+
var contact = repository.GetAll(strategy).First();
153+
contact.Name.ShouldBe("Test User 1");
154+
queries.Count().ShouldBe(1);
155+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
156+
queries.Count().ShouldBe(1);
157+
}
158+
159+
[Test]
160+
public void EfCore_GetAll_With_Text_Include_LazyLoads_Email()
161+
{
162+
//dbContext.Configuration.LazyLoadingEnabled = true;
163+
//dbContext.Database.Log = sql =>
164+
//{
165+
// if (sql.Contains("SELECT"))
166+
// {
167+
// queries.Add(sql);
168+
// }
169+
//};
170+
var repository = new MyEfCoreRepository(dbContext);
171+
172+
var contact = repository.GetAll("EmailAddresses").First();
173+
contact.Name.ShouldBe("Test User 1");
174+
queries.Count().ShouldBe(1);
175+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
176+
queries.Count().ShouldBe(1);
177+
}
178+
179+
[Test]
180+
public void EfCore_GetAll_With_Text_Include_And_Pagination_LazyLoads_Email()
181+
{
182+
//dbContext.Configuration.LazyLoadingEnabled = true;
183+
//dbContext.Database.Log = sql =>
184+
//{
185+
// if (sql.Contains("SELECT"))
186+
// {
187+
// queries.Add(sql);
188+
// }
189+
//};
190+
var repository = new MyEfCoreRepository(dbContext);
191+
192+
var pagination = new PagingOptions<Contact>(1, 4, "ContactId");
193+
194+
var contact = repository.GetAll(pagination, "EmailAddresses").First();
195+
contact.Name.ShouldBe("Test User 1");
196+
queries.Count().ShouldBe(2); // first query is count for total records
197+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
198+
queries.Count().ShouldBe(2);
199+
}
200+
201+
[Test]
202+
public void EfCore_FindAll_With_Include_And_Predicate_In_Specs_LazyLoads_Email()
203+
{
204+
//dbContext.Configuration.LazyLoadingEnabled = true;
205+
//dbContext.Database.Log = sql =>
206+
//{
207+
// if (sql.Contains("SELECT"))
208+
// {
209+
// queries.Add(sql);
210+
// }
211+
//};
212+
213+
;
214+
215+
var repository = new MyEfCoreRepository(dbContext);
216+
217+
var findAllBySpec = new Specification<Contact>(obj => obj.ContactId == "1")
218+
.And(obj => obj.EmailAddresses.Any(m => m.Email == "[email protected]"));
219+
220+
var specification = new Specification<Contact>(obj => obj.Name == "Test User 1");
221+
222+
findAllBySpec.FetchStrategy = new GenericFetchStrategy<Contact>();
223+
findAllBySpec.FetchStrategy
224+
.Include(obj => obj.EmailAddresses);
225+
226+
// NOTE: This line will erase my FetchStrategy from above
227+
if (null != specification)
228+
{
229+
findAllBySpec = findAllBySpec.And(specification);
230+
}
231+
232+
var contact = repository.FindAll(findAllBySpec).First();
233+
contact.Name.ShouldBe("Test User 1");
234+
dbContext.QueryLog.Count(s => s.Contains("QUERY")).ShouldBe(1); // first query is count for total records
235+
contact.EmailAddresses.First().Email.ShouldBe("[email protected]");
236+
dbContext.QueryLog.Count(s => s.Contains("QUERY")).ShouldBe(1);
237+
238+
repository.FindAll(findAllBySpec).Count().ShouldBe(1);
239+
}
240+
}
241+
}

0 commit comments

Comments
 (0)