Skip to content

Commit 93a3afd

Browse files
author
Jeff Treuting
committed
Couple TransactionScope tests for EF
1 parent c098355 commit 93a3afd

3 files changed

Lines changed: 89 additions & 1 deletion

File tree

SharpRepository.Tests.Integration/RepositoryAddTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
3+
using System.Transactions;
44
using NUnit.Framework;
55
using SharpRepository.Repository;
66
using SharpRepository.Tests.Integration.TestAttributes;
@@ -66,5 +66,33 @@ public void Add_Should_Save_And_Assigned_New_Ids_To_Multiple(IRepository<Contact
6666
var added = repository.GetAll();
6767
added.Count().ShouldEqual(3);
6868
}
69+
70+
[ExecuteForRepositories(RepositoryTypes.Ef5)]
71+
public void Using_TransactionScope_Without_Complete_Should_Not_Add(IRepository<Contact, string> repository)
72+
{
73+
repository.Get("test"); // used to create the SqlCe database before being inside the transaction scope since that throws an error
74+
75+
using (var trans = new TransactionScope())
76+
{
77+
repository.Add(new Contact {Name = "Contact 1"});
78+
}
79+
80+
repository.GetAll().Count().ShouldEqual(0);
81+
}
82+
83+
[ExecuteForRepositories(RepositoryTypes.Ef5)]
84+
public void Using_TransactionScope_With_Complete_Should_Add(IRepository<Contact, string> repository)
85+
{
86+
repository.Get("test"); // used to create the SqlCe database before being inside the transaction scope since that throws an error
87+
88+
using (var trans = new TransactionScope())
89+
{
90+
repository.Add(new Contact { Name = "Contact 1" });
91+
92+
trans.Complete();
93+
}
94+
95+
repository.GetAll().Count().ShouldEqual(1);
96+
}
6997
}
7098
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
<Reference Include="System.configuration" />
113113
<Reference Include="System.Core" />
114114
<Reference Include="System.Data.Entity" />
115+
<Reference Include="System.Transactions" />
115116
<Reference Include="System.Xml.Linq" />
116117
<Reference Include="System.Data.DataSetExtensions" />
117118
<Reference Include="Microsoft.CSharp" />
@@ -134,6 +135,7 @@
134135
<Compile Include="Spikes\RavenDbEmbeddedTests.cs" />
135136
<Compile Include="Spikes\StandardCachingSpikes.cs" />
136137
<Compile Include="TestAttributes\ExecuteForAllRepositoriesAttribute.cs" />
138+
<Compile Include="TestAttributes\ExecuteForAllRepositoriesExcept.cs" />
137139
<Compile Include="TestAttributes\ExecuteForRepositoriesAttribute.cs" />
138140
<Compile Include="RepositoryAddTests.cs" />
139141
<Compile Include="RepositoryDeleteTests.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using SharpRepository.Tests.Integration.Data;
5+
6+
namespace SharpRepository.Tests.Integration.TestAttributes
7+
{
8+
public class ExecuteForAllRepositoriesExceptAttribute : TestCaseSourceAttribute
9+
{
10+
private static IEnumerable<TestCaseData> ForAllRepositoriesExceptTestCaseData
11+
{
12+
get
13+
{
14+
var repositoryTypes = new[]
15+
{
16+
RepositoryTypes.Dbo4,
17+
RepositoryTypes.RavenDb,
18+
RepositoryTypes.Xml,
19+
RepositoryTypes.MongoDb,
20+
RepositoryTypes.InMemory,
21+
RepositoryTypes.Ef5,
22+
RepositoryTypes.Cache,
23+
};
24+
25+
return RepositoryTestCaseDataFactory.Build(RemoveExceptions(repositoryTypes));
26+
}
27+
}
28+
29+
private static RepositoryTypes[] RemoveExceptions(RepositoryTypes[] repositoryTypes)
30+
{
31+
if (_exceptions == null || _exceptions.Length == 0)
32+
return repositoryTypes;
33+
34+
var list = new List<RepositoryTypes>();
35+
list.AddRange(repositoryTypes);
36+
foreach (var exception in _exceptions.Where(list.Contains))
37+
{
38+
list.Remove(exception);
39+
}
40+
41+
return list.ToArray();
42+
}
43+
44+
private static RepositoryTypes[] _exceptions;
45+
private static string _reason;
46+
47+
public ExecuteForAllRepositoriesExceptAttribute(string reason, params RepositoryTypes[] exceptions) : this()
48+
{
49+
_reason = reason; // TODO: it would be nice to find a way to display this in the Unit test results, but helpful in the unit test for human readable in the code
50+
_exceptions = exceptions;
51+
}
52+
53+
public ExecuteForAllRepositoriesExceptAttribute()
54+
: base(typeof(ExecuteForAllRepositoriesExceptAttribute), "ForAllRepositoriesExceptTestCaseData")
55+
{
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)