forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinGenericDynamicComponentTests.cs
195 lines (167 loc) · 5.56 KB
/
JoinGenericDynamicComponentTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NUnit.Framework;
namespace NHibernate.Test.MappingByCode.ExplicitMappingTests
{
[TestFixture]
public class JoinGenericDynamicComponentTests
{
public class MyClass
{
public virtual int Code { get; set; }
public virtual string JoinedName { get; set; }
public virtual MyOther Relation { get; set; }
public virtual MyOther JoinedRelation { get; set; }
public virtual IDictionary<string, object> Attributes { get; set; }
public virtual IDictionary<string, object> JoinedAttributes { get; set; }
}
public class MyOther
{
public int Id { get; set; }
}
public class MyClassMap : ClassMapping<MyClass>
{
public MyClassMap()
{
// basic table related properties
ManyToOne(x => x.Relation);
Component(p => p.Attributes,
new
{
IsVisible = false,
Hash = default(Guid),
Reference = default(MyOther)
},
m =>
{
m.Property(p => p.IsVisible);
m.Property(p => p.Hash);
m.ManyToOne(p => p.Reference);
});
Property(x => x.Code);
// joined table stuff
Join("JoinedAttributes", x =>
{
x.Property(p => p.JoinedName);
x.Component(p => p.JoinedAttributes,
new
{
OtherReference = default(MyOther),
Description = string.Empty,
Age = 0,
},
m =>
{
m.ManyToOne(p => p.OtherReference);
m.Property(p => p.Description);
m.Property(p => p.Age);
});
x.ManyToOne(p => p.JoinedRelation);
});
}
}
private static HbmClass CompileClassMapping()
{
var mapper = new ModelMapper();
mapper.AddMapping(typeof(MyClassMap));
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
var hbmClass = hbmMapping.RootClasses[0];
return hbmClass;
}
[Test]
public void WhenPropertyIsMappedOnRootThenItBelongsToRootTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
var rootProperties = hbmClass.Properties;
// <property name="Code"
var hbmPropCode = rootProperties
.FirstOrDefault(p => p.Name == "Code");
Assert.That(hbmPropCode, Is.Not.Null);
Assert.That(hbmPropCode, Is.TypeOf<HbmProperty>());
}
[Test]
public void WhenDynamicComponentIsMappedOnRootThenItBelongsToRootTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
var rootProperties = hbmClass.Properties;
// <dynamic-component name="Attributes"
var hbmPropAttributes = rootProperties
.FirstOrDefault(p => p.Name == "Attributes")
;
Assert.That(hbmPropAttributes, Is.Not.Null);
Assert.That(hbmPropAttributes, Is.TypeOf<HbmDynamicComponent>());
}
[Test]
public void WhenRelationIsMappedOnRootThenItBelongsToRootTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
var rootProperties = hbmClass.Properties;
// <many-to-one name="Relation"
var hbmPropRelation = rootProperties
.FirstOrDefault(p => p.Name == "Relation");
Assert.That(hbmPropRelation, Is.Not.Null);
Assert.That(hbmPropRelation, Is.TypeOf<HbmManyToOne>());
}
[Test]
public void WhenJoinedPropertyIsMappedOnJoinThenItBelongsToJoinTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
// <join table="JoinedAttributes">
var hbmJoined = hbmClass.Joins.FirstOrDefault();
Assert.That(hbmJoined, Is.Not.Null);
var rootProperties = hbmJoined.Properties;
// <join table="JoinedAttributes">
// <dynamic-component name="Attributes"
var hbmPropJoinedName = rootProperties
.FirstOrDefault(p => p.Name == "JoinedName");
Assert.That(hbmPropJoinedName, Is.Not.Null);
Assert.That(hbmPropJoinedName, Is.TypeOf<HbmProperty>());
}
[Test]
public void WhenJoinedRelationIsMappedOnJoinThenItBelongsToJoinTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
// <join table="JoinedAttributes">
var hbmJoined = hbmClass.Joins.FirstOrDefault();
Assert.That(hbmJoined, Is.Not.Null);
var rootProperties = hbmJoined.Properties;
// <join table="JoinedAttributes">
// <many-to-one name="JoinedRelation"
var hbmPropJoinedRelation = rootProperties
.FirstOrDefault(p => p.Name == "JoinedRelation");
Assert.That(hbmPropJoinedRelation, Is.Not.Null);
Assert.That(hbmPropJoinedRelation, Is.TypeOf<HbmManyToOne>());
}
[Test]
public void WhenJoinedDynamicComponentIsMappedOnJoinThenItBelongsToJoinTable()
{
// <class name="MyClass"">
var hbmClass = CompileClassMapping();
Assert.That(hbmClass, Is.Not.Null);
// <join table="JoinedAttributes">
var hbmJoined = hbmClass.Joins.FirstOrDefault();
Assert.That(hbmJoined, Is.Not.Null);
var rootProperties = hbmJoined.Properties;
// <join table="JoinedAttributes">
// <dynamic-component name="JoinedAttributes">
var hbmPropJoinedAttributes = rootProperties
.FirstOrDefault(p => p.Name == "JoinedAttributes");
Assert.That(hbmPropJoinedAttributes, Is.Not.Null);
Assert.That(hbmPropJoinedAttributes, Is.TypeOf<HbmDynamicComponent>());
}
}
}