/
HasManyConventionTests.cs
250 lines (189 loc) · 6.02 KB
/
HasManyConventionTests.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Linq;
using FluentNHibernate.Automapping.TestFixtures;
using FluentNHibernate.Conventions.Helpers.Builders;
using FluentNHibernate.Conventions.Instances;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel.Collections;
using FluentNHibernate.Testing.FluentInterfaceTests;
using NUnit.Framework;
namespace FluentNHibernate.Testing.ConventionsTests.ApplyingToModel;
[TestFixture]
public class HasManyConventionTests
{
PersistenceModel model;
[SetUp]
public void CreatePersistenceModel()
{
model = new PersistenceModel();
}
[Test]
public void ShouldSetAccessProperty()
{
Convention(x => x.Access.Property());
VerifyModel(x => x.Access.ShouldEqual("property"));
}
[Test]
public void ShouldSetBatchSizeProperty()
{
Convention(x => x.BatchSize(100));
VerifyModel(x => x.BatchSize.ShouldEqual(100));
}
[Test]
public void ShouldSetCacheProperty()
{
Convention(x => x.Cache.ReadWrite());
VerifyModel(x => x.Cache.Usage.ShouldEqual("read-write"));
}
[Test]
public void ShouldSetCascadeProperty()
{
Convention(x => x.Cascade.None());
VerifyModel(x => x.Cascade.ShouldEqual("none"));
}
[Test]
public void ShouldSetClassProperty()
{
Convention(x => x.Relationship.CustomClass(typeof(int)));
VerifyModel(x => x.Relationship.Class.GetUnderlyingSystemType().ShouldEqual(typeof(int)));
}
[Test]
public void ShouldSetCheckConstraintProperty()
{
Convention(x => x.Check("constraint = 0"));
VerifyModel(x => x.Check.ShouldEqual("constraint = 0"));
}
[Test]
public void ShouldSetCollectionTypeProperty()
{
Convention(x => x.CollectionType<string>());
VerifyModel(x => x.CollectionType.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
}
[Test]
public void ShouldSetFetchProperty()
{
Convention(x => x.Fetch.Select());
VerifyModel(x => x.Fetch.ShouldEqual("select"));
}
[Test]
public void ShouldSetGenericProperty()
{
Convention(x => x.Generic());
VerifyModel(x => x.Generic.ShouldBeTrue());
}
[Test]
public void ShouldSetInverseProperty()
{
Convention(x => x.Inverse());
VerifyModel(x => x.Inverse.ShouldBeTrue());
}
[Test]
public void ShouldSetKeyColumnNameProperty()
{
Convention(x => x.Key.Column("xxx"));
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("xxx"));
}
[Test]
public void ShouldSetElementColumnNameProperty()
{
Convention(x => x.Element.Column("xxx"));
VerifyModel(x => x.Element.Columns.First().Name.ShouldEqual("xxx"));
}
[Test]
public void ShouldSetElementTypePropertyUsingGeneric()
{
Convention(x => x.Element.Type<string>());
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
}
[Test]
public void ShouldSetElementTypePropertyUsingTypeOf()
{
Convention(x => x.Element.Type(typeof(string)));
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
}
[Test]
public void ShouldSetElementTypePropertyUsingString()
{
Convention(x => x.Element.Type(typeof(string).AssemblyQualifiedName));
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
}
[Test]
public void ShouldSetLazyProperty()
{
Convention(x => x.LazyLoad());
VerifyModel(x => x.Lazy.ShouldEqual(Lazy.True));
}
[Test]
public void ShouldSetOptimisticLockProperty()
{
Convention(x => x.OptimisticLock());
VerifyModel(x => x.OptimisticLock.ShouldEqual(true));
}
[Test]
public void ShouldSetPersisterProperty()
{
Convention(x => x.Persister<SecondCustomPersister>());
VerifyModel(x => x.Persister.GetUnderlyingSystemType().ShouldEqual(typeof(SecondCustomPersister)));
}
[Test]
public void ShouldSetSchemaProperty()
{
Convention(x => x.Schema("test"));
VerifyModel(x => x.Schema.ShouldEqual("test"));
}
[Test]
public void ShouldSetWhereProperty()
{
Convention(x => x.Where("y = 2"));
VerifyModel(x => x.Where.ShouldEqual("y = 2"));
}
[Test]
public void ShouldSetForeignKeyProperty()
{
Convention(x => x.Key.ForeignKey("xxx"));
VerifyModel(x => x.Key.ForeignKey.ShouldEqual("xxx"));
}
[Test]
public void ShouldSetPropertyRefProperty()
{
Convention(x => x.Key.PropertyRef("xxx"));
VerifyModel(x => x.Key.PropertyRef.ShouldEqual("xxx"));
}
[Test]
public void ShouldSetTableNameProperty()
{
Convention(x => x.Table("xxx"));
VerifyModel(x => x.TableName.ShouldEqual("xxx"));
}
[Test]
public void ShouldSetNotFoundProperty()
{
Convention(x => x.Relationship.NotFound.Ignore());
VerifyModel(x => x.Relationship.NotFound.ShouldEqual("ignore"));
}
[Test]
public void ShouldSetInversePropertyToFalseWhenUsingNot()
{
Convention(x => x.Not.Inverse());
VerifyModel(x => x.Inverse.ShouldBeFalse());
}
#region Helpers
void Convention(Action<IOneToManyCollectionInstance> convention)
{
model.Conventions.Add(new OneToManyCollectionConventionBuilder().Always(convention));
}
void VerifyModel(Action<CollectionMapping> modelVerification)
{
var classMap = new ClassMap<ExampleInheritedClass>();
classMap.Id(x => x.Id);
var map = classMap.HasMany(x => x.Children);
model.Add(classMap);
var generatedModels = model.BuildMappings();
var modelInstance = generatedModels
.First(x => x.Classes.FirstOrDefault(c => c.Type == typeof(ExampleInheritedClass)) is not null)
.Classes.First()
.Collections.First();
modelVerification(modelInstance);
}
#endregion
}