Skip to content

Commit 20bcd30

Browse files
committed
test AsNoTracking
1 parent 653e335 commit 20bcd30

7 files changed

Lines changed: 275 additions & 7 deletions

File tree

SharpRepository.EfCoreRepository/EfCoreCompoundKeyRepositoryBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
7070
{
7171
var query = DbSet.AsQueryable();
7272

73-
if (fetchStrategy.NoTracking)
73+
if (fetchStrategy != null && fetchStrategy.NoTracking)
7474
{
7575
query = query.AsNoTracking();
7676
}
@@ -164,7 +164,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
164164
{
165165
var query = DbSet.AsQueryable();
166166

167-
if (fetchStrategy.NoTracking)
167+
if (fetchStrategy != null && fetchStrategy.NoTracking)
168168
{
169169
query = query.AsNoTracking();
170170
}

SharpRepository.EfCoreRepository/EfCoreRepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
8888
{
8989
var query = DbSet.AsQueryable();
9090

91-
if (fetchStrategy.NoTracking)
91+
if (fetchStrategy != null && fetchStrategy.NoTracking)
9292
{
9393
query = query.AsNoTracking();
9494
}

SharpRepository.EfRepository/EfCompoundKeyRepositoryBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
7171
{
7272
var query = DbSet.AsQueryable();
7373

74-
if (fetchStrategy.NoTracking)
74+
if (fetchStrategy != null && fetchStrategy.NoTracking)
7575
{
7676
query = query.AsNoTracking();
7777
}
@@ -165,7 +165,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
165165
{
166166
var query = DbSet.AsQueryable();
167167

168-
if (fetchStrategy.NoTracking)
168+
if (fetchStrategy != null && fetchStrategy.NoTracking)
169169
{
170170
query = query.AsNoTracking();
171171
}
@@ -260,7 +260,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
260260
{
261261
var query = DbSet.AsQueryable();
262262

263-
if (fetchStrategy.NoTracking)
263+
if (fetchStrategy != null && fetchStrategy.NoTracking)
264264
{
265265
query = query.AsNoTracking();
266266
}

SharpRepository.EfRepository/EfRepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
102102
{
103103
var query = DbSet.AsQueryable();
104104

105-
if (fetchStrategy.NoTracking)
105+
if (fetchStrategy != null && fetchStrategy.NoTracking)
106106
{
107107
query = query.AsNoTracking();
108108
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System.Data.Entity;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using SharpRepository.EfRepository;
5+
using SharpRepository.Tests.Integration.Data;
6+
using SharpRepository.Tests.Integration.TestObjects;
7+
using System.Collections.Generic;
8+
using Shouldly;
9+
using SharpRepository.Repository.FetchStrategies;
10+
11+
namespace SharpRepository.Tests.Integration.Spikes
12+
{
13+
[TestFixture]
14+
public class EfAsNoTracking
15+
{
16+
private TestObjectContext dbContext;
17+
18+
[SetUp]
19+
public void SetupRepository()
20+
{
21+
var dbPath = EfDataDirectoryFactory.Build();
22+
dbContext = new TestObjectContext("Data Source=" + dbPath);
23+
24+
const int totalItems = 5;
25+
26+
for (int i = 1; i <= totalItems; i++)
27+
{
28+
dbContext.Contacts.Add(
29+
new Contact
30+
{
31+
ContactId = i.ToString(),
32+
Name = "Test User " + i,
33+
EmailAddresses = new List<EmailAddress> {
34+
new EmailAddress {
35+
ContactId = i.ToString(),
36+
EmailAddressId = i,
37+
Email = "omar.piani." + i.ToString() + "@email.com",
38+
Label = "omar.piani." + i.ToString()
39+
}
40+
}
41+
});
42+
}
43+
44+
dbContext.SaveChanges();
45+
46+
// reistantiate in order to lose caches
47+
dbContext = new TestObjectContext("Data Source=" + dbPath);
48+
}
49+
50+
[Test]
51+
public void GetAllSateShouldBeUnchanged()
52+
{
53+
var repository = new EfRepository<Contact, string>(dbContext);
54+
55+
var firstContact = repository.GetAll().First();
56+
57+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
58+
}
59+
60+
[Test]
61+
public void GetAllWithStrategySateShouldBeUnchanged()
62+
{
63+
var repository = new EfRepository<Contact, string>(dbContext);
64+
65+
var strat = new GenericFetchStrategy<Contact>();
66+
67+
var firstContact = repository.GetAll(strat).First();
68+
69+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
70+
}
71+
72+
[Test]
73+
public void GetAllWithStrategyAsNoTrackingSateShouldBeUnchanged()
74+
{
75+
var repository = new EfRepository<Contact, string>(dbContext);
76+
77+
var strat = new GenericFetchStrategy<Contact>()
78+
.AsNoTracking();
79+
80+
var firstContact = repository.GetAll(strat).First();
81+
82+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Detached);
83+
}
84+
85+
[Test]
86+
public void GetSateShouldBeUnchanged()
87+
{
88+
var repository = new EfRepository<Contact, string>(dbContext);
89+
90+
var firstContact = repository.Get("1");
91+
92+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
93+
}
94+
95+
[Test]
96+
public void GetWithStrategySateShouldBeUnchanged()
97+
{
98+
var repository = new EfRepository<Contact, string>(dbContext);
99+
100+
var strat = new GenericFetchStrategy<Contact>();
101+
102+
var firstContact = repository.Get("1", strat);
103+
104+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
105+
}
106+
107+
[Test]
108+
public void GetWithStrategyAsNoTrackingSateShouldBeUnchanged()
109+
{
110+
var repository = new EfRepository<Contact, string>(dbContext);
111+
112+
var strat = new GenericFetchStrategy<Contact>()
113+
.AsNoTracking();
114+
115+
var firstContact = repository.Get("1", strat);
116+
117+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Detached);
118+
}
119+
}
120+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
using SharpRepository.EfCoreRepository;
4+
using SharpRepository.Tests.Integration.Data;
5+
using SharpRepository.Tests.Integration.TestObjects;
6+
using System.Collections.Generic;
7+
using Shouldly;
8+
using SharpRepository.Repository.FetchStrategies;
9+
using Microsoft.Data.Sqlite;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
namespace SharpRepository.Tests.Integration.Spikes
13+
{
14+
[TestFixture]
15+
public class EfCoreAsNoTracking
16+
{
17+
private TestObjectContextCore dbContext;
18+
19+
[SetUp]
20+
public void SetupRepository()
21+
{
22+
var dbPath = EfDataDirectoryFactory.Build();
23+
24+
var connection = new SqliteConnection("DataSource=:memory:");
25+
connection.Open();
26+
27+
var options = new DbContextOptionsBuilder<TestObjectContextCore>()
28+
.UseSqlite(connection)
29+
.Options;
30+
31+
using (dbContext = new TestObjectContextCore(options))
32+
{
33+
dbContext.Database.EnsureCreated();
34+
35+
const int totalItems = 5;
36+
37+
for (int i = 1; i <= totalItems; i++)
38+
{
39+
dbContext.Contacts.Add(
40+
new Contact
41+
{
42+
ContactId = i.ToString(),
43+
Name = "Test User " + i,
44+
EmailAddresses = new List<EmailAddress> {
45+
new EmailAddress {
46+
ContactId = i.ToString(),
47+
EmailAddressId = i,
48+
Email = "omar.piani." + i.ToString() + "@email.com",
49+
Label = "omar.piani." + i.ToString()
50+
}
51+
}
52+
});
53+
}
54+
55+
dbContext.SaveChanges();
56+
}
57+
58+
// reistantiate in order to lose caches
59+
dbContext = new TestObjectContextCore(options);
60+
}
61+
62+
[Test]
63+
public void EfCoreGetAllSateShouldBeUnchanged()
64+
{
65+
var repository = new EfCoreRepository<Contact, string>(dbContext);
66+
67+
var firstContact = repository.GetAll().First();
68+
69+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
70+
}
71+
72+
[Test]
73+
public void EfCoreGetAllWithStrategySateShouldBeUnchanged()
74+
{
75+
var repository = new EfCoreRepository<Contact, string>(dbContext);
76+
77+
var strat = new GenericFetchStrategy<Contact>();
78+
79+
var firstContact = repository.GetAll(strat).First();
80+
81+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
82+
}
83+
84+
[Test]
85+
public void EfCoreGetAllWithStrategyAsNoTrackingSateShouldBeUnchanged()
86+
{
87+
var repository = new EfCoreRepository<Contact, string>(dbContext);
88+
89+
var strat = new GenericFetchStrategy<Contact>()
90+
.AsNoTracking();
91+
92+
var firstContact = repository.GetAll(strat).First();
93+
94+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Detached);
95+
}
96+
97+
[Test]
98+
public void EfCoreGetSateShouldBeUnchanged()
99+
{
100+
var repository = new EfCoreRepository<Contact, string>(dbContext);
101+
102+
var firstContact = repository.Get("1");
103+
104+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
105+
}
106+
107+
[Test]
108+
public void EfCoreGetWithStrategySateShouldBeUnchanged()
109+
{
110+
var repository = new EfCoreRepository<Contact, string>(dbContext);
111+
112+
var strat = new GenericFetchStrategy<Contact>();
113+
114+
var firstContact = repository.Get("1", strat);
115+
116+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
117+
}
118+
119+
[Test]
120+
public void EfCoreGetWithStrategyAsNoTrackingSateShouldBeUnchanged()
121+
{
122+
var repository = new EfCoreRepository<Contact, string>(dbContext);
123+
124+
var strat = new GenericFetchStrategy<Contact>()
125+
.AsNoTracking();
126+
127+
var firstContact = repository.Get("1", strat);
128+
129+
dbContext.Entry(firstContact).State.ShouldBe(EntityState.Detached);
130+
}
131+
}
132+
}

SharpRepository.Tests/FetchStrategies/FetchStrategyTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,21 @@ public void FetchStrategy_May_Include_Multiple_Levels_First_Syntax()
5353

5454
strategy.IncludePaths.ShouldContain("EmailAddresses.Email");
5555
}
56+
57+
[Test]
58+
public void FetchStrategy_AsNoTracking_Default_Is_False()
59+
{
60+
new GenericFetchStrategy<Contact>()
61+
.NoTracking.ShouldBeFalse();
62+
}
63+
64+
[Test]
65+
public void FetchStrategy_AsNoTracking_Set_AsNoTracking()
66+
{
67+
var strategy = new GenericFetchStrategy<Contact>().AsNoTracking();
68+
69+
strategy.NoTracking.ShouldBeTrue();
70+
}
71+
5672
}
5773
}

0 commit comments

Comments
 (0)