Skip to content

Commit fa98af5

Browse files
committed
Add test of filtering by enum
1 parent 9f5c2cd commit fa98af5

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using NUnit.Framework;
10+
using Xtensive.Core;
11+
using Xtensive.Orm.Configuration;
12+
using Xtensive.Orm.Tests.Linq.WhereByEnumTestModel;
13+
14+
namespace Xtensive.Orm.Tests.Linq.WhereByEnumTestModel
15+
{
16+
public enum ByteEnum : byte
17+
{
18+
Zero = 0,
19+
One = 1,
20+
Two = 2,
21+
Three = 3,
22+
Four = 4,
23+
Max = byte.MaxValue
24+
}
25+
26+
public enum SByteEnum : sbyte
27+
{
28+
Zero = 0,
29+
One = 1,
30+
Two = 2,
31+
Three = 3,
32+
Four = 4,
33+
Max = sbyte.MaxValue
34+
}
35+
36+
public enum ShortEnum : short
37+
{
38+
Zero = 0,
39+
One = 1,
40+
Two = 2,
41+
Three = 3,
42+
Four = 4,
43+
Max = short.MaxValue
44+
}
45+
46+
public enum UShortEnum : ushort
47+
{
48+
Zero = 0,
49+
One = 1,
50+
Two = 2,
51+
Three = 3,
52+
Four = 4,
53+
Max = ushort.MaxValue
54+
}
55+
56+
public enum IntEnum : int
57+
{
58+
Zero = 0,
59+
One = 1,
60+
Two = 2,
61+
Three = 3,
62+
Four = 4,
63+
Max = int.MaxValue
64+
}
65+
66+
public enum UIntEnum : uint
67+
{
68+
Zero = 0,
69+
One = 1,
70+
Two = 2,
71+
Three = 3,
72+
Four = 4,
73+
Max = uint.MaxValue
74+
}
75+
76+
public enum LongEnum : long
77+
{
78+
Zero = 0,
79+
One = 1,
80+
Two = 2,
81+
Three = 3,
82+
Four = 4,
83+
Max = long.MaxValue
84+
}
85+
86+
public enum ULongEnum : ulong
87+
{
88+
Zero = 0,
89+
One = 1,
90+
Two = 2,
91+
Three = 3,
92+
Four = 4,
93+
Max = ulong.MaxValue
94+
}
95+
96+
[HierarchyRoot]
97+
[Index(nameof(EnumContainer.ByteEnumField))]
98+
[Index(nameof(EnumContainer.SByteEnumField))]
99+
[Index(nameof(EnumContainer.ShortEnumField))]
100+
[Index(nameof(EnumContainer.UShortEnumField))]
101+
[Index(nameof(EnumContainer.IntEnumField))]
102+
[Index(nameof(EnumContainer.UIntEnumField))]
103+
[Index(nameof(EnumContainer.LongEnumField))]
104+
[Index(nameof(EnumContainer.ULongEnumField))]
105+
public class EnumContainer : Entity
106+
{
107+
public const string Zero = "Zero";
108+
public const string One = "One";
109+
public const string Two = "Two";
110+
public const string Three = "Three";
111+
public const string Four = "Four";
112+
public const string Max = "Max";
113+
114+
[Field, Key]
115+
public int Id { get; private set; }
116+
117+
[Field]
118+
public ByteEnum ByteEnumField { get; set; }
119+
120+
[Field]
121+
public SByteEnum SByteEnumField { get; set; }
122+
123+
[Field]
124+
public ShortEnum ShortEnumField { get; set; }
125+
126+
[Field]
127+
public UShortEnum UShortEnumField { get; set; }
128+
129+
[Field]
130+
public IntEnum IntEnumField { get; set; }
131+
132+
[Field]
133+
public UIntEnum UIntEnumField { get; set; }
134+
135+
[Field]
136+
public LongEnum LongEnumField { get; set; }
137+
138+
[Field]
139+
public ULongEnum ULongEnumField { get; set; }
140+
141+
public EnumContainer(Session session, string enumValue)
142+
: base(session)
143+
{
144+
ByteEnumField = (ByteEnum) Enum.Parse(typeof(ByteEnum), enumValue);
145+
SByteEnumField = (SByteEnum) Enum.Parse(typeof(SByteEnum), enumValue);
146+
ShortEnumField = (ShortEnum) Enum.Parse(typeof(ShortEnum), enumValue);
147+
UShortEnumField = (UShortEnum) Enum.Parse(typeof(UShortEnum), enumValue);
148+
IntEnumField = (IntEnum) Enum.Parse(typeof(IntEnum), enumValue);
149+
UIntEnumField = (UIntEnum) Enum.Parse(typeof(UIntEnum), enumValue);
150+
LongEnumField = (LongEnum) Enum.Parse(typeof(LongEnum), enumValue);
151+
ULongEnumField = (ULongEnum) Enum.Parse(typeof(ULongEnum), enumValue);
152+
}
153+
}
154+
}
155+
156+
namespace Xtensive.Orm.Tests.Linq
157+
{
158+
public class WhereByEnumTest : AutoBuildTest
159+
{
160+
private Session SharedSession;
161+
162+
public override void TestFixtureSetUp()
163+
{
164+
base.TestFixtureSetUp();
165+
CreateSessionAndTransaction();
166+
SharedSession = Session.Current;
167+
}
168+
169+
public override void TestFixtureTearDown()
170+
{
171+
SharedSession = null;
172+
base.TestFixtureTearDown();
173+
}
174+
175+
protected override DomainConfiguration BuildConfiguration()
176+
{
177+
var config = base.BuildConfiguration();
178+
config.Types.Register(typeof(EnumContainer));
179+
config.UpgradeMode = DomainUpgradeMode.Recreate;
180+
return config;
181+
}
182+
183+
protected override void PopulateData()
184+
{
185+
using (var session = Domain.OpenSession())
186+
using (var tx = session.OpenTransaction()) {
187+
_ = new EnumContainer(session, EnumContainer.Zero);
188+
_ = new EnumContainer(session, EnumContainer.One);
189+
_ = new EnumContainer(session, EnumContainer.Two);
190+
_ = new EnumContainer(session, EnumContainer.Three);
191+
_ = new EnumContainer(session, EnumContainer.Four);
192+
_ = new EnumContainer(session, EnumContainer.Max);
193+
tx.Complete();
194+
}
195+
}
196+
197+
[Test]
198+
public void ByteTest()
199+
{
200+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
201+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.Zero).ToArray(1);
202+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.One).ToArray(1);
203+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.Two).ToArray(1);
204+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.Three).ToArray(1);
205+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.Four).ToArray(1);
206+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ByteEnumField == ByteEnum.Max).ToArray(1);
207+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
208+
}
209+
210+
[Test]
211+
public void SByteTest()
212+
{
213+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
214+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.Zero).ToArray(1);
215+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.One).ToArray(1);
216+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.Two).ToArray(1);
217+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.Three).ToArray(1);
218+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.Four).ToArray(1);
219+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.SByteEnumField == SByteEnum.Max).ToArray(1);
220+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
221+
}
222+
223+
[Test]
224+
public void ShortTest()
225+
{
226+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
227+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.Zero).ToArray(1);
228+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.One).ToArray(1);
229+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.Two).ToArray(1);
230+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.Three).ToArray(1);
231+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.Four).ToArray(1);
232+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ShortEnumField == ShortEnum.Max).ToArray(1);
233+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
234+
}
235+
236+
[Test]
237+
public void UShortTest()
238+
{
239+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
240+
241+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.Zero).ToArray(1);
242+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.One).ToArray(1);
243+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.Two).ToArray(1);
244+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.Three).ToArray(1);
245+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.Four).ToArray(1);
246+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UShortEnumField == UShortEnum.Max).ToArray(1);
247+
248+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
249+
}
250+
251+
[Test]
252+
public void IntTest()
253+
{
254+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
255+
256+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.Zero).ToArray(1);
257+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.One).ToArray(1);
258+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.Two).ToArray(1);
259+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.Three).ToArray(1);
260+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.Four).ToArray(1);
261+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.IntEnumField == IntEnum.Max).ToArray(1);
262+
263+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
264+
}
265+
266+
[Test]
267+
public void UIntTest()
268+
{
269+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
270+
271+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.Zero).ToArray(1);
272+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.One).ToArray(1);
273+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.Two).ToArray(1);
274+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.Three).ToArray(1);
275+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.Four).ToArray(1);
276+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.UIntEnumField == UIntEnum.Max).ToArray(1);
277+
278+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
279+
}
280+
281+
[Test]
282+
public void LongTest()
283+
{
284+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
285+
286+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.Zero).ToArray(1);
287+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.One).ToArray(1);
288+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.Two).ToArray(1);
289+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.Three).ToArray(1);
290+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.Four).ToArray(1);
291+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.LongEnumField == LongEnum.Max).ToArray(1);
292+
293+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
294+
}
295+
296+
[Test]
297+
public void ULontTest()
298+
{
299+
SharedSession.Events.DbCommandExecuting += PrintCommandToConsole;
300+
301+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.Zero).ToArray(1);
302+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.One).ToArray(1);
303+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.Two).ToArray(1);
304+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.Three).ToArray(1);
305+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.Four).ToArray(1);
306+
_ = SharedSession.Query.All<EnumContainer>().Where(e => e.ULongEnumField == ULongEnum.Max).ToArray(1);
307+
308+
SharedSession.Events.DbCommandExecuting -= PrintCommandToConsole;
309+
}
310+
311+
private static void PrintCommandToConsole(object sender, DbCommandEventArgs args) => Console.WriteLine(args.Command.CommandText);
312+
}
313+
}

0 commit comments

Comments
 (0)