Skip to content

Commit ff5e6de

Browse files
committed
Test for TypeId replace
1 parent c1a8496 commit ff5e6de

File tree

1 file changed

+300
-0
lines changed

1 file changed

+300
-0
lines changed
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using NUnit.Framework;
8+
using Xtensive.Orm.Configuration;
9+
using Xtensive.Orm.Tests.Storage.TypeIdAsParameterTestModel;
10+
11+
namespace Xtensive.Orm.Tests.Storage
12+
{
13+
public class TypeIdAsParameterTest : AutoBuildTest
14+
{
15+
private ConcurrentDictionary<Type, int> typeIds = new();
16+
17+
protected override DomainConfiguration BuildConfiguration()
18+
{
19+
var configuration = base.BuildConfiguration();
20+
configuration.UpgradeMode = DomainUpgradeMode.Recreate;
21+
configuration.Types.Register(typeof(SimpleEntity).Assembly, typeof(SimpleEntity).Namespace);
22+
23+
return configuration;
24+
}
25+
26+
protected override void PopulateData()
27+
{
28+
foreach(var typeInfo in Domain.Model.Types.Entities) {
29+
_ = typeIds.TryAdd(typeInfo.UnderlyingType, typeInfo.TypeId);
30+
}
31+
}
32+
33+
[Test]
34+
public void SimpleQueryTest()
35+
{
36+
var typeId = typeIds[typeof(SimpleEntity)];
37+
38+
using (var session = Domain.OpenSession())
39+
using (var tx = session.OpenTransaction()) {
40+
41+
session.Events.DbCommandExecuting += OnDbCommandExecuting;
42+
43+
var result = session.Query.All<SimpleEntity>().ToList();
44+
var whereResult = session.Query.All<SimpleEntity>().Where(e => e.TypeId == typeId).ToList();
45+
46+
var orderByResult = session.Query.All<SimpleEntity>().OrderBy(e => e.TypeId).ToList();
47+
var groupByResult = session.Query.All<SimpleEntity>().GroupBy(e => e.TypeId).ToList();
48+
49+
session.Events.DbCommandExecuting -= OnDbCommandExecuting;
50+
}
51+
52+
void OnDbCommandExecuting(object sender, DbCommandEventArgs args)
53+
{
54+
if (args.Command.CommandText.Contains(typeId.ToString())) {
55+
throw new AssertionException("Not all TypeId mentions were replaced");
56+
}
57+
}
58+
}
59+
60+
[Test]
61+
public void UnionOfQuerues()
62+
{
63+
var typeId = typeIds[typeof(SimpleEntity)];
64+
65+
using (var session = Domain.OpenSession())
66+
using (var tx = session.OpenTransaction()) {
67+
session.Events.DbCommandExecuting += OnDbCommandExecuting;
68+
69+
var result = session.Query.All<SimpleEntity>().Where(e => e.Id > 10)
70+
.Union(session.Query.All<SimpleEntity>().Where(e => e.Id < 10))
71+
.ToList();
72+
73+
session.Events.DbCommandExecuting -= OnDbCommandExecuting;
74+
}
75+
76+
void OnDbCommandExecuting(object sender, DbCommandEventArgs args)
77+
{
78+
if (args.Command.CommandText.Contains(typeId.ToString())) {
79+
throw new AssertionException("Not all TypeId mentions were replaced");
80+
}
81+
}
82+
}
83+
84+
[Test]
85+
public void InterfaceTest()
86+
{
87+
var rootTypeId = typeIds[typeof(InterfaceHierarchyRoot)];
88+
var midTypeId = typeIds[typeof(InterfaceHierarchyMid)];
89+
var leafTypeId = typeIds[typeof(InterfaceHierarchyLeaf)];
90+
91+
using (var session = Domain.OpenSession())
92+
using (var tx = session.OpenTransaction()) {
93+
session.Events.DbCommandExecuting += OnDbCommandExecuting;
94+
95+
var result = session.Query.All<IBaseEnity>().ToList();
96+
var whereResult = session.Query.All<IBaseEnity>().ToList();
97+
98+
session.Events.DbCommandExecuting -= OnDbCommandExecuting;
99+
}
100+
101+
void OnDbCommandExecuting(object sender, DbCommandEventArgs args)
102+
{
103+
var commandText = (ReadOnlySpan<char>) args.Command.CommandText;
104+
if (commandText.Contains((ReadOnlySpan<char>) rootTypeId.ToString(), StringComparison.Ordinal)
105+
|| commandText.Contains((ReadOnlySpan<char>) midTypeId.ToString(), StringComparison.Ordinal)
106+
|| commandText.Contains((ReadOnlySpan<char>) leafTypeId.ToString(), StringComparison.Ordinal)) {
107+
throw new AssertionException("Not all TypeId mentions were replaced");
108+
}
109+
}
110+
}
111+
112+
[Test]
113+
public void ConcreteTableHierarchyTest()
114+
{
115+
var rootTypeId = typeIds[typeof(BasicConcreteHierarchyRoot)];
116+
var midTypeId = typeIds[typeof(BasicConcreteHierarchyMid)];
117+
var leafTypeId = typeIds[typeof(BasicConcreteHierarchyLeaf)];
118+
119+
using (var session = Domain.OpenSession())
120+
using (var tx = session.OpenTransaction()) {
121+
session.Events.DbCommandExecuting += OnDbCommandExecuting;
122+
123+
var result1 = session.Query.All<BasicConcreteHierarchyRoot>().ToList();
124+
var whereResult1 = session.Query.All<BasicConcreteHierarchyRoot>()
125+
.Where(e => e.TypeId == rootTypeId || e.TypeId == midTypeId || e.TypeId == leafTypeId)
126+
.ToList();
127+
var orderByResult1 = session.Query.All<BasicConcreteHierarchyRoot>()
128+
.OrderBy(e => e.TypeId)
129+
.ToList();
130+
var groupByResult1 = session.Query.All<BasicConcreteHierarchyRoot>()
131+
.GroupBy(e => e.TypeId)
132+
.ToList();
133+
134+
var result2 = session.Query.All<BasicConcreteHierarchyMid>().ToList();
135+
var whereResult2 = session.Query.All<BasicConcreteHierarchyMid>()
136+
.Where(e => e.TypeId == midTypeId || e.TypeId == leafTypeId)
137+
.ToList();
138+
var orderByResult2 = session.Query.All<BasicConcreteHierarchyMid>()
139+
.OrderBy(e => e.TypeId)
140+
.ToList();
141+
var groupByResult2 = session.Query.All<BasicConcreteHierarchyMid>()
142+
.GroupBy(e => e.TypeId)
143+
.ToList();
144+
145+
var result3 = session.Query.All<BasicConcreteHierarchyLeaf>().ToList();
146+
var whereResult3 = session.Query.All<BasicConcreteHierarchyLeaf>()
147+
.Where(e => e.TypeId == leafTypeId)
148+
.ToList();
149+
var orderByResult3 = session.Query.All<BasicConcreteHierarchyLeaf>()
150+
.OrderBy(e => e.TypeId)
151+
.ToList();
152+
var groupByResult3 = session.Query.All<BasicConcreteHierarchyLeaf>()
153+
.GroupBy(e => e.TypeId)
154+
.ToList();
155+
156+
session.Events.DbCommandExecuting -= OnDbCommandExecuting;
157+
}
158+
159+
void OnDbCommandExecuting(object sender, DbCommandEventArgs args)
160+
{
161+
var commandText = (ReadOnlySpan<char>) args.Command.CommandText;
162+
if (commandText.Contains((ReadOnlySpan<char>) rootTypeId.ToString(), StringComparison.Ordinal)
163+
|| commandText.Contains((ReadOnlySpan<char>) midTypeId.ToString(), StringComparison.Ordinal)
164+
|| commandText.Contains((ReadOnlySpan<char>) leafTypeId.ToString(), StringComparison.Ordinal)) {
165+
throw new AssertionException("Not all TypeId mentions were replaced");
166+
}
167+
}
168+
}
169+
}
170+
}
171+
172+
namespace Xtensive.Orm.Tests.Storage.TypeIdAsParameterTestModel
173+
{
174+
[HierarchyRoot]
175+
public sealed class SimpleEntity : Entity
176+
{
177+
[Field, Key]
178+
public int Id { get; private set; }
179+
180+
[Field]
181+
public string Name { get; set; }
182+
183+
public SimpleEntity(Session session)
184+
: base(session)
185+
{
186+
}
187+
}
188+
189+
[HierarchyRoot(IncludeTypeId = true)]
190+
public sealed class SimpleEntityWithIncludedId : Entity
191+
{
192+
[Field, Key]
193+
public int Id { get; private set; }
194+
195+
[Field]
196+
public string Name { get; set; }
197+
198+
public SimpleEntityWithIncludedId(Session session)
199+
: base(session)
200+
{
201+
202+
}
203+
}
204+
205+
[HierarchyRoot(InheritanceSchema = Orm.Model.InheritanceSchema.ConcreteTable)]
206+
public class BasicConcreteHierarchyRoot : Entity
207+
{
208+
[Field, Key]
209+
public int Id { get; private set; }
210+
211+
[Field]
212+
public string Name { get; set; }
213+
214+
public BasicConcreteHierarchyRoot(Session session)
215+
: base(session)
216+
{
217+
}
218+
}
219+
220+
public class BasicConcreteHierarchyMid : BasicConcreteHierarchyRoot
221+
{
222+
[Field]
223+
public DateTime CreationDate { get; set; }
224+
225+
public BasicConcreteHierarchyMid(Session session)
226+
: base(session)
227+
{
228+
}
229+
}
230+
231+
public class BasicConcreteHierarchyLeaf : BasicConcreteHierarchyMid
232+
{
233+
[Field]
234+
public int SomeValue { get; set; }
235+
236+
public BasicConcreteHierarchyLeaf(Session session)
237+
: base(session)
238+
{
239+
}
240+
}
241+
242+
243+
public interface IBaseEnity : IEntity
244+
{
245+
[Field]
246+
int Id { get; }
247+
248+
[Field]
249+
string Name { get; set; }
250+
}
251+
252+
public interface IHasCreationDate : IBaseEnity
253+
{
254+
[Field]
255+
DateTime CreationDate { get; set; }
256+
}
257+
258+
public interface IHasSomeValue : IHasCreationDate
259+
{
260+
[Field]
261+
int SomeValue { get; set; }
262+
}
263+
264+
[HierarchyRoot(InheritanceSchema = Orm.Model.InheritanceSchema.ConcreteTable)]
265+
public class InterfaceHierarchyRoot : Entity, IBaseEnity
266+
{
267+
[Field, Key]
268+
public int Id { get; private set; }
269+
270+
[Field]
271+
public string Name { get; set; }
272+
273+
public InterfaceHierarchyRoot(Session session)
274+
: base(session)
275+
{
276+
}
277+
}
278+
279+
public class InterfaceHierarchyMid : InterfaceHierarchyRoot, IHasCreationDate
280+
{
281+
[Field]
282+
public DateTime CreationDate { get; set; }
283+
284+
public InterfaceHierarchyMid(Session session)
285+
: base(session)
286+
{
287+
}
288+
}
289+
290+
public class InterfaceHierarchyLeaf : InterfaceHierarchyMid, IHasSomeValue
291+
{
292+
[Field]
293+
public int SomeValue { get; set; }
294+
295+
public InterfaceHierarchyLeaf(Session session)
296+
: base(session)
297+
{
298+
}
299+
}
300+
}

0 commit comments

Comments
 (0)