Skip to content

Commit ffe9772

Browse files
owerkopchester89
authored andcommitted
Added failing test for pairing relation ends.
Pairing should check property types correspondence to avoid pair collection with reference property if their types does not match.
1 parent 2ae8014 commit ffe9772

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@
396396
<Compile Include="Visitors\ComponentReferenceResolutionVisitorSpecs.cs" />
397397
<Compile Include="Visitors\RelationshipPairingVisitorSpec.cs" />
398398
<Compile Include="Visitors\RelationshipPairingVisitorSpecSupportClasses.cs" />
399+
<Compile Include="Visitors\RelationshipPairingSpec.cs" />
399400
<Compile Include="Xml\MappingXmlTestHelper.cs" />
400401
</ItemGroup>
401402
<ItemGroup>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentNHibernate.Automapping;
4+
using FluentNHibernate.Diagnostics;
5+
using FluentNHibernate.MappingModel;
6+
using FluentNHibernate.MappingModel.Collections;
7+
using NUnit.Framework;
8+
using System.Linq;
9+
10+
namespace FluentNHibernate.Testing.Visitors
11+
{
12+
[TestFixture]
13+
public class when_there_is_any_unpaired_collection_property_alphabetically_before_collection_property_which_has_corresponding_back_reference_from_child : RelationshipPairingSpec
14+
{
15+
CollectionMapping collectionWithoutBackrefAndAlphabeticallyFirst;
16+
CollectionMapping collectionWithoutBackrefAndAlphabeticallyLast;
17+
CollectionMapping collectionWithCorrespondingBackref;
18+
ManyToOneMapping referenceFromChildSecondTypeToParent;
19+
20+
public override void establish_context()
21+
{
22+
var autoPersistenceModel = AutoMap.Source(new Types(), new AutomappingConfiguration());
23+
var hibernateMappings = autoPersistenceModel.BuildMappings();
24+
25+
var classMapping = hibernateMappings.SelectMany(h => h.Classes).Single(c => c.Type == typeof(Parent));
26+
collectionWithoutBackrefAndAlphabeticallyFirst = classMapping.Collections.Single(c => c.ChildType == typeof(ChildFirstType));
27+
collectionWithCorrespondingBackref = classMapping.Collections.Single(c => c.ChildType == typeof(ChildSecondType));
28+
collectionWithoutBackrefAndAlphabeticallyLast = classMapping.Collections.Single(c => c.ChildType == typeof(ChildThirdType));
29+
}
30+
31+
public override void because()
32+
{
33+
34+
}
35+
36+
[Test]
37+
public void should_pair_with_corresponding_property()
38+
{
39+
collectionWithoutBackrefAndAlphabeticallyFirst.OtherSide.ShouldBeNull();
40+
collectionWithCorrespondingBackref.OtherSide.ShouldNotBeNull();
41+
}
42+
}
43+
44+
[TestFixture]
45+
public class when_relation_has_two_ends : RelationshipPairingSpec
46+
{
47+
CollectionMapping collectionWithCorrespondingBackref;
48+
ManyToOneMapping referenceFromChildSecondTypeToParent;
49+
50+
public override void establish_context()
51+
{
52+
var autoPersistenceModel = AutoMap.Source(new Types(), new AutomappingConfiguration());
53+
var hibernateMappings = autoPersistenceModel.BuildMappings();
54+
55+
var classMapping = hibernateMappings.SelectMany(h => h.Classes).Single(c => c.Type == typeof(Parent));
56+
collectionWithCorrespondingBackref = classMapping.Collections.Single(c => c.ChildType == typeof(ChildSecondType));
57+
}
58+
59+
public override void because()
60+
{
61+
62+
}
63+
64+
[Test]
65+
public void should_use_single_foreign_key_column()
66+
{
67+
var columnMappingFromParentSide = collectionWithCorrespondingBackref.Key.Columns.Single();
68+
var columnMappingFromChildSide = ((ManyToOneMapping)collectionWithCorrespondingBackref.OtherSide).Columns.Single();
69+
70+
columnMappingFromParentSide.Name.ShouldEqual(columnMappingFromChildSide.Name);
71+
}
72+
}
73+
74+
public abstract class RelationshipPairingSpec : Specification
75+
{
76+
protected class Types : ITypeSource
77+
{
78+
public IEnumerable<Type> GetTypes()
79+
{
80+
return new[] { typeof(Parent), typeof(ChildFirstType), typeof(ChildSecondType), typeof(ChildThirdType) };
81+
}
82+
83+
public void LogSource(IDiagnosticLogger logger)
84+
{ }
85+
86+
public string GetIdentifier()
87+
{
88+
return "3C423683-08F3-4FD8-9B9C-A043B11C52B5";
89+
}
90+
}
91+
92+
protected class AutomappingConfiguration : DefaultAutomappingConfiguration
93+
{
94+
public override bool ShouldMap(Type type)
95+
{
96+
return true;
97+
}
98+
}
99+
100+
protected class Parent
101+
{
102+
public int Id { get; set; }
103+
public string Property { get; set; }
104+
public IList<ChildFirstType> AChildrenOfFirstType { get; set; }
105+
public IList<ChildSecondType> BChildrenOfSecondType { get; set; }
106+
public IList<ChildThirdType> CChildrenOfThirdType { get; set; }
107+
}
108+
109+
protected class ChildFirstType
110+
{
111+
public int Id { get; set; }
112+
public string Property { get; set; }
113+
}
114+
115+
protected class ChildSecondType
116+
{
117+
public int Id { get; set; }
118+
public string Property { get; set; }
119+
public Parent MyParent { get; set; }
120+
}
121+
122+
protected class ChildThirdType
123+
{
124+
public int Id { get; set; }
125+
public string Property { get; set; }
126+
}
127+
128+
}
129+
}

0 commit comments

Comments
 (0)