forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumTests.cs
260 lines (224 loc) · 6.83 KB
/
EnumTests.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
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.SqlTypes;
using NHibernate.Type;
using NUnit.Framework;
namespace NHibernate.Test.Linq
{
[TestFixture(typeof(EnumType<TestEnum>), "0")]
[TestFixture(typeof(EnumStringType<TestEnum>), "'Unspecified'")]
[TestFixture(typeof(EnumAnsiStringType<TestEnum>), "'Unspecified'")]
public class EnumTests : TestCaseMappingByCode
{
private IType _enumType;
private string _unspecifiedValue;
public EnumTests(System.Type enumType, string unspecifiedValue)
{
_enumType = (IType) Activator.CreateInstance(enumType);
_unspecifiedValue = unspecifiedValue;
}
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EnumEntity>(
rc =>
{
rc.Table("EnumEntity");
rc.Id(x => x.Id, m => m.Generator(Generators.Guid));
rc.Property(x => x.Name);
rc.Property(x => x.Enum1, m => m.Type(_enumType));
rc.Property(x => x.NullableEnum1, m =>
{
m.Type(_enumType);
m.Formula($"(case when Enum1 = {_unspecifiedValue} then null else Enum1 end)");
});
rc.Bag(x => x.Children, m =>
{
m.Cascade(Mapping.ByCode.Cascade.All);
m.Inverse(true);
},
a => a.OneToMany()
);
rc.ManyToOne(x => x.Other, m => m.Cascade(Mapping.ByCode.Cascade.All));
});
mapper.Class<EnumEntityChild>(
rc =>
{
rc.Table("EnumEntityChild");
rc.Id(x => x.Id, m => m.Generator(Generators.Guid));
rc.Property(x => x.Name);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
protected override void OnSetUp()
{
base.OnSetUp();
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
session.Save(new EnumEntity { Enum1 = TestEnum.Unspecified });
session.Save(new EnumEntity { Enum1 = TestEnum.Small });
session.Save(new EnumEntity { Enum1 = TestEnum.Small });
session.Save(new EnumEntity { Enum1 = TestEnum.Medium });
session.Save(new EnumEntity { Enum1 = TestEnum.Medium });
session.Save(new EnumEntity { Enum1 = TestEnum.Medium });
session.Save(new EnumEntity { Enum1 = TestEnum.Large });
session.Save(new EnumEntity { Enum1 = TestEnum.Large });
session.Save(new EnumEntity { Enum1 = TestEnum.Large });
session.Save(new EnumEntity { Enum1 = TestEnum.Large });
trans.Commit();
}
}
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");
session.Flush();
transaction.Commit();
}
}
[Test]
public void CanQueryOnEnum_Large_4()
{
CanQueryOnEnum(TestEnum.Large, 4);
}
[Test]
public void CanQueryOnEnum_Medium_3()
{
CanQueryOnEnum(TestEnum.Medium, 3);
}
[Test]
public void CanQueryOnEnum_Small_2()
{
CanQueryOnEnum(TestEnum.Small, 2);
}
[Test]
public void CanQueryOnEnum_Unspecified_1()
{
CanQueryOnEnum(TestEnum.Unspecified, 1);
}
private void CanQueryOnEnum(TestEnum type, int expectedCount)
{
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var query = session.Query<EnumEntity>().Where(x => x.Enum1 == type).ToList();
Assert.AreEqual(expectedCount, query.Count);
}
}
[Test]
public void CanQueryWithContainsOnTestEnum_Small_1()
{
var values = new[] { TestEnum.Small, TestEnum.Medium };
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var query = session.Query<EnumEntity>().Where(x => values.Contains(x.Enum1)).ToList();
Assert.AreEqual(5, query.Count);
}
}
[Test]
public void ConditionalNavigationProperty()
{
TestEnum? type = null;
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var entities = session.Query<EnumEntity>();
entities.Where(o => o.Enum1 == TestEnum.Large).ToList();
entities.Where(o => TestEnum.Large != o.Enum1).ToList();
entities.Where(o => (o.NullableEnum1 ?? TestEnum.Large) == TestEnum.Medium).ToList();
entities.Where(o => ((o.NullableEnum1 ?? type) ?? o.Enum1) == TestEnum.Medium).ToList();
entities.Where(o => (o.NullableEnum1.HasValue ? o.Enum1 : TestEnum.Unspecified) == TestEnum.Medium).ToList();
entities.Where(o => (o.Enum1 != TestEnum.Large
? (o.NullableEnum1.HasValue ? o.Enum1 : TestEnum.Unspecified)
: TestEnum.Small) == TestEnum.Medium).ToList();
entities.Where(o => (o.Enum1 == TestEnum.Large ? o.Other : o.Other).Name == "test").ToList();
}
}
[Test]
public void CanQueryComplexExpressionOnTestEnum()
{
//TODO: Fix issue on SQLite with type set to TestEnum.Unspecified
TestEnum? type = null;
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var entities = session.Query<EnumEntity>();
var query = (from user in entities
where (user.NullableEnum1 == TestEnum.Large
? TestEnum.Medium
: user.NullableEnum1 ?? user.Enum1
) == type
select new
{
user,
simple = user.Enum1,
condition = user.Enum1 == TestEnum.Large ? TestEnum.Medium : user.Enum1,
coalesce = user.NullableEnum1 ?? TestEnum.Medium
}).ToList();
Assert.That(query.Count, Is.EqualTo(0));
}
}
[Test]
public void CanProjectWithListTransformation()
{
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var entities = session.Query<EnumEntity>();
var query = entities.Select(user => new
{
user.Name,
simple = user.Enum1,
children = user.Children,
nullableEnum1IsLarge = user.NullableEnum1 == TestEnum.Large
}).ToList();
Assert.That(query.Count, Is.EqualTo(10));
}
}
}
public class EnumEntity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual TestEnum Enum1 { get; set; }
public virtual TestEnum? NullableEnum1 { get; set; }
public virtual int? NullableInt { get; set; }
public virtual EnumEntity Other { get; set; }
public virtual IList<EnumEntityChild> Children { get; set; } = new List<EnumEntityChild>();
}
public class EnumEntityChild
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public enum TestEnum
{
Unspecified,
Small,
Medium,
Large
}
[Serializable]
public class EnumAnsiStringType<T> : EnumStringType
{
private readonly string typeName;
public EnumAnsiStringType()
: base(typeof(T))
{
System.Type type = GetType();
typeName = type.FullName + ", " + type.Assembly.GetName().Name;
}
public override string Name
{
get { return typeName; }
}
public override SqlType SqlType => SqlTypeFactory.GetAnsiString(255);
}
}