Skip to content

Commit 7311bc6

Browse files
committed
added ReferenceBag object to be able to test on an unordered bag.
Created the PersistenceSpecification Extentions for the ReferenceBag objects
1 parent d79203a commit 7311bc6

File tree

6 files changed

+378
-0
lines changed

6 files changed

+378
-0
lines changed

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
<Compile Include="MappingModel\HibernateMappingTester.cs" />
402402
<Compile Include="MappingModel\MappingTestingExtensions.cs" />
403403
<Compile Include="Testing\Values\ListSpecs.cs" />
404+
<Compile Include="Testing\Values\ReferenceBagSpec.cs" />
404405
<Compile Include="Testing\Values\ReferenceListSpecs.cs" />
405406
<Compile Include="Testing\Values\ReferencePropertySpecs.cs" />
406407
<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/PersistenceSpecificationExtensions.cs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,167 @@ public static PersistenceSpecification<T> CheckList<T, TListElement>(this Persis
250250
return spec.RegisterCheckedProperty(list, elementComparer);
251251
}
252252

253+
254+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
255+
Expression<Func<T, IEnumerable<TListElement>>> expression,
256+
IEnumerable<TListElement> propertyValue)
257+
{
258+
return spec.CheckInverseBag(expression, propertyValue, (IEqualityComparer)null);
259+
}
260+
261+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
262+
Expression<Func<T, IEnumerable<TListElement>>> expression,
263+
IEnumerable<TListElement> propertyValue,
264+
IEqualityComparer elementComparer)
265+
{
266+
Accessor property = ReflectionHelper.GetAccessor(expression);
267+
268+
return spec.RegisterCheckedPropertyWithoutTransactionalSave(new ReferenceBag<T, TListElement>(property, propertyValue), elementComparer);
269+
}
270+
271+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
272+
Expression<Func<T, IEnumerable<TListElement>>> expression,
273+
IEnumerable<TListElement> propertyValue,
274+
params Func<TListElement, object>[] propertiesToCompare)
275+
{
276+
// Because of the params keyword, the compiler can select this overload
277+
// instead of the one above, even when no funcs are supplied in the method call.
278+
if (propertiesToCompare == null || propertiesToCompare.Length == 0)
279+
return spec.CheckInverseBag(expression, propertyValue, (IEqualityComparer)null);
280+
281+
return spec.CheckInverseBag(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
282+
}
283+
284+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
285+
Expression<Func<T, IEnumerable<TListElement>>> expression,
286+
IEnumerable<TListElement> propertyValue,
287+
Action<T, TListElement> listItemSetter)
288+
{
289+
return spec.CheckInverseBag(expression, propertyValue, null, listItemSetter);
290+
}
291+
292+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
293+
Expression<Func<T, IEnumerable<TListElement>>> expression,
294+
IEnumerable<TListElement> propertyValue,
295+
Action<T, IEnumerable<TListElement>> listSetter)
296+
{
297+
return spec.CheckInverseBag(expression, propertyValue, null, listSetter);
298+
}
299+
300+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
301+
Expression<Func<T, IEnumerable<TListElement>>> expression,
302+
IEnumerable<TListElement> propertyValue,
303+
IEqualityComparer elementComparer,
304+
Action<T, TListElement> listItemSetter)
305+
{
306+
Accessor property = ReflectionHelper.GetAccessor(expression);
307+
308+
var list = new ReferenceBag<T, TListElement>(property, propertyValue);
309+
list.ValueSetter = (target, propertyInfo, value) =>
310+
{
311+
foreach (var item in value)
312+
{
313+
listItemSetter(target, item);
314+
}
315+
};
316+
317+
return spec.RegisterCheckedPropertyWithoutTransactionalSave(list, elementComparer);
318+
}
319+
320+
public static PersistenceSpecification<T> CheckInverseBag<T, TListElement>(this PersistenceSpecification<T> spec,
321+
Expression<Func<T, IEnumerable<TListElement>>> expression,
322+
IEnumerable<TListElement> propertyValue,
323+
IEqualityComparer elementComparer,
324+
Action<T, IEnumerable<TListElement>> listSetter)
325+
{
326+
Accessor property = ReflectionHelper.GetAccessor(expression);
327+
328+
var list = new ReferenceBag<T, TListElement>(property, propertyValue);
329+
list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
330+
331+
return spec.RegisterCheckedPropertyWithoutTransactionalSave(list, elementComparer);
332+
}
333+
334+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
335+
Expression<Func<T, IEnumerable<TListElement>>> expression,
336+
IEnumerable<TListElement> propertyValue)
337+
{
338+
return spec.CheckBag(expression, propertyValue, (IEqualityComparer)null);
339+
}
340+
341+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
342+
Expression<Func<T, IEnumerable<TListElement>>> expression,
343+
IEnumerable<TListElement> propertyValue,
344+
IEqualityComparer elementComparer)
345+
{
346+
Accessor property = ReflectionHelper.GetAccessor(expression);
347+
348+
return spec.RegisterCheckedProperty(new ReferenceBag<T, TListElement>(property, propertyValue), elementComparer);
349+
}
350+
351+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
352+
Expression<Func<T, IEnumerable<TListElement>>> expression,
353+
IEnumerable<TListElement> propertyValue,
354+
params Func<TListElement, object>[] propertiesToCompare)
355+
{
356+
// Because of the params keyword, the compiler can select this overload
357+
// instead of the one above, even when no funcs are supplied in the method call.
358+
if (propertiesToCompare == null || propertiesToCompare.Length == 0)
359+
return spec.CheckBag(expression, propertyValue, (IEqualityComparer)null);
360+
361+
return spec.CheckBag(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
362+
}
363+
364+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
365+
Expression<Func<T, IEnumerable<TListElement>>> expression,
366+
IEnumerable<TListElement> propertyValue,
367+
Action<T, TListElement> listItemSetter)
368+
{
369+
return spec.CheckBag(expression, propertyValue, null, listItemSetter);
370+
}
371+
372+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
373+
Expression<Func<T, IEnumerable<TListElement>>> expression,
374+
IEnumerable<TListElement> propertyValue,
375+
Action<T, IEnumerable<TListElement>> listSetter)
376+
{
377+
return spec.CheckBag(expression, propertyValue, null, listSetter);
378+
}
379+
380+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
381+
Expression<Func<T, IEnumerable<TListElement>>> expression,
382+
IEnumerable<TListElement> propertyValue,
383+
IEqualityComparer elementComparer,
384+
Action<T, TListElement> listItemSetter)
385+
{
386+
Accessor property = ReflectionHelper.GetAccessor(expression);
387+
388+
var list = new ReferenceBag<T, TListElement>(property, propertyValue);
389+
list.ValueSetter = (target, propertyInfo, value) =>
390+
{
391+
foreach (var item in value)
392+
{
393+
listItemSetter(target, item);
394+
}
395+
};
396+
397+
return spec.RegisterCheckedProperty(list, elementComparer);
398+
}
399+
400+
public static PersistenceSpecification<T> CheckBag<T, TListElement>(this PersistenceSpecification<T> spec,
401+
Expression<Func<T,IEnumerable<TListElement>>> expression,
402+
IEnumerable<TListElement> propertyValue,
403+
IEqualityComparer elementComparer,
404+
Action<T, IEnumerable<TListElement>> listSetter)
405+
{
406+
Accessor property = ReflectionHelper.GetAccessor(expression);
407+
408+
var list = new ReferenceBag<T, TListElement>(property, propertyValue);
409+
list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
410+
411+
return spec.RegisterCheckedProperty(list, elementComparer);
412+
}
413+
253414
public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
254415
Expression<Func<T, object>> expression,
255416
IEnumerable<TListElement> propertyValue)

0 commit comments

Comments
 (0)