Skip to content

Commit 3fc9111

Browse files
committed
Merge pull request #139 from CedricYao/master
Edits to the Persistence Specification class.
2 parents b326c46 + 7311bc6 commit 3fc9111

File tree

7 files changed

+436
-0
lines changed

7 files changed

+436
-0
lines changed

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@
402402
<Compile Include="MappingModel\HibernateMappingTester.cs" />
403403
<Compile Include="MappingModel\MappingTestingExtensions.cs" />
404404
<Compile Include="Testing\Values\ListSpecs.cs" />
405+
<Compile Include="Testing\Values\ReferenceBagSpec.cs" />
405406
<Compile Include="Testing\Values\ReferenceListSpecs.cs" />
406407
<Compile Include="Testing\Values\ReferencePropertySpecs.cs" />
407408
<Compile Include="Testing\Values\PropertySpecs.cs" />

src/FluentNHibernate.Testing/Testing/PersistenceSpecificationExtensionsSpecs.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.ComponentModel;
45
using System.Linq;
56
using FluentNHibernate.Testing.Testing.Values;
67
using FluentNHibernate.Testing.Values;
@@ -247,6 +248,36 @@ public void should_invoke_the_custom_setter()
247248
}
248249
}
249250

251+
[TestFixture]
252+
public class when_a_unordered_bag_is_added :With_persistence_specification<ReferenceEntity>
253+
{
254+
OtherEntity entity1 = new OtherEntity();
255+
OtherEntity entity2 = new OtherEntity();
256+
257+
public override void because()
258+
{
259+
sut.CheckBag(x => x.ReferenceList, new[] {entity1, entity2});
260+
}
261+
262+
[Test]
263+
public void should_add_a_reference_bag_check()
264+
{
265+
sut.AllProperties.FirstOrDefault().ShouldBeOfType(typeof(ReferenceBag<ReferenceEntity, OtherEntity>));
266+
}
267+
268+
[Test]
269+
public void should_add_only_one_check_to_the_specification()
270+
{
271+
sut.AllProperties.ShouldHaveCount(1);
272+
}
273+
274+
[Test]
275+
public void should_set_the_custom_equality_comparer()
276+
{
277+
sut.AllProperties.FirstOrDefault().EntityEqualityComparer.ShouldEqual(comparer);
278+
}
279+
}
280+
250281
[TestFixture]
251282
public class When_a_checked_list_is_added : With_persistence_specification<ReferenceEntity>
252283
{
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using FluentNHibernate.Testing.Values;
5+
using FluentNHibernate.Utils;
6+
using FluentNHibernate.Utils.Reflection;
7+
using NUnit.Framework;
8+
9+
namespace FluentNHibernate.Testing.Testing.Values
10+
{
11+
public class ReferenceBagSpec
12+
{
13+
public abstract class With_initialized_bag : Specification
14+
{
15+
protected Accessor property;
16+
protected ListEntity target;
17+
protected ReferenceBag<ListEntity, string> sut;
18+
19+
public override void establish_context()
20+
{
21+
property = ReflectionHelper.GetAccessor((Expression<Func<ListEntity, IEnumerable<string>>>)(x => x.GetterAndSetter));
22+
target = new ListEntity();
23+
24+
sut = new ReferenceBag<ListEntity, string>(property, new[] { "foo", "bar", "baz" });
25+
}
26+
}
27+
28+
[TestFixture]
29+
public class When_the_checked_bag_has_multiple_items : With_initialized_bag
30+
{
31+
public override void establish_context()
32+
{
33+
var baz = "baz";
34+
property = ReflectionHelper.GetAccessor((Expression<Func<ListEntity, IEnumerable<string>>>)(x => x.GetterAndSetter));
35+
target = new ListEntity();
36+
sut = new ReferenceBag<ListEntity, string>(property, new[] {"foo", baz, baz, "bar"});
37+
38+
target.GetterAndSetter = new[] {baz, baz, "bar", "foo"};
39+
}
40+
41+
public override void because()
42+
{
43+
sut.CheckValue(target);
44+
}
45+
46+
[Test]
47+
public void should_succeed()
48+
{
49+
thrown_exception.ShouldBeNull();
50+
}
51+
}
52+
53+
[TestFixture]
54+
public class when_checked_bag_has_less_items : With_initialized_bag
55+
{
56+
public override void establish_context()
57+
{
58+
base.establish_context();
59+
target.GetterAndSetter = new[] {"foo", "bar"};
60+
}
61+
62+
public override void because()
63+
{
64+
sut.CheckValue(target);
65+
}
66+
67+
[Test]
68+
public void should_throw_exception()
69+
{
70+
thrown_exception.ShouldBeOfType(typeof(ApplicationException));
71+
}
72+
73+
[Test]
74+
public void should_state_that_bag_doesnt_match()
75+
{
76+
var exception = (ApplicationException)thrown_exception;
77+
exception.Message.ShouldEqual("Actual count (2) does not equal expected count (3)");
78+
}
79+
}
80+
81+
[TestFixture]
82+
public class when_the_checked_list_has_different_elements : With_initialized_bag
83+
{
84+
public override void establish_context()
85+
{
86+
base.establish_context();
87+
target.GetterAndSetter = new[] {"bad", "bar", "foo"};
88+
}
89+
90+
public override void because()
91+
{
92+
sut.CheckValue(target);
93+
}
94+
95+
[Test]
96+
public void should_throw_exception()
97+
{
98+
thrown_exception.ShouldBeOfType(typeof(ApplicationException));
99+
}
100+
101+
[Test]
102+
public void should_state_that_bag_doesnt_match()
103+
{
104+
var exception = (ApplicationException)thrown_exception;
105+
exception.Message.ShouldEqual("Actual count of item bad (1) does not equal expected item count (0)");
106+
}
107+
}
108+
109+
[TestFixture]
110+
public class When_the_checked_list_has_transposed_items_of_the_expected_list : With_initialized_bag
111+
{
112+
public override void establish_context()
113+
{
114+
base.establish_context();
115+
target.GetterAndSetter = new[] { "baz", "bar", "foo" };
116+
}
117+
118+
public override void because()
119+
{
120+
sut.CheckValue(target);
121+
}
122+
123+
[Test]
124+
public void should_succeed()
125+
{
126+
thrown_exception.ShouldBeNull();
127+
}
128+
}
129+
}
130+
}

src/FluentNHibernate/FluentNHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@
450450
<Compile Include="Mapping\Providers\IPropertyMappingProvider.cs" />
451451
<Compile Include="Mapping\Providers\IReferenceComponentMappingProvider.cs" />
452452
<Compile Include="Mapping\TuplizerPart.cs" />
453+
<Compile Include="Testing\Values\ReferenceBag.cs" />
453454
<Compile Include="Utils\StringLikeness.cs" />
454455
<Compile Include="Visitors\AmbiguousComponentReferenceException.cs" />
455456
<Compile Include="Visitors\RelationshipKeyPairingVisitor.cs" />

src/FluentNHibernate/Testing/PersistenceSpecification.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property)
8787
return RegisterCheckedProperty(property, null);
8888
}
8989

90+
public PersistenceSpecification<T> RegisterCheckedPropertyWithoutTransactionalSave(Property<T> property, IEqualityComparer equalityComparer)
91+
{
92+
property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer;
93+
allProperties.Add(property);
94+
95+
return this;
96+
}
9097
public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property, IEqualityComparer equalityComparer)
9198
{
9299
property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer;

0 commit comments

Comments
 (0)