Skip to content

Commit dffdda7

Browse files
committed
Create test for the issue
1 parent 7193d7a commit dffdda7

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Copyright (C) 2024 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.Linq;
7+
using System.Linq.Expressions;
8+
using NUnit.Framework;
9+
using Xtensive.Orm.Configuration;
10+
using Xtensive.Orm.Tests.Issues.IssueJira0754_FirstOrDefultOnEntitySetCauseIncorrectResultsModel;
11+
12+
13+
namespace Xtensive.Orm.Tests.Issues.IssueJira0754_FirstOrDefultOnEntitySetCauseIncorrectResultsModel
14+
{
15+
[HierarchyRoot]
16+
public class TestEntity : Entity
17+
{
18+
[Key]
19+
[Field(Nullable = false)]
20+
public Guid Id { get; private set; }
21+
22+
[Field]
23+
public string String { get; set; }
24+
25+
[Field]
26+
public ListEntity LinkOnList { get; set; }
27+
28+
[Field]
29+
[Association(PairTo = "Owner")]
30+
public EntitySet<ListEntity> List { get; set; }
31+
32+
[Field]
33+
public TestEntity2 Link { get; set; }
34+
35+
public TestEntity2 VirtualList { get; set; }
36+
37+
public TestEntity2 VirtualLink { get; set; }
38+
39+
public TestEntity(Session session)
40+
: base(session)
41+
{
42+
}
43+
}
44+
45+
[HierarchyRoot]
46+
public class ListEntity : Entity
47+
{
48+
[Key]
49+
[Field(Nullable = false)]
50+
public Guid Id { get; private set; }
51+
52+
[Field]
53+
public string String { get; set; }
54+
55+
[Field(Nullable = false)]
56+
public TestEntity Owner { get; set; }
57+
58+
[Field(Nullable = false)]
59+
public TestEntity2 Link { get; set; }
60+
61+
public ListEntity(Session session)
62+
: base(session)
63+
{
64+
}
65+
}
66+
67+
[HierarchyRoot]
68+
public class TestEntity2 : Entity
69+
{
70+
public TestEntity2(Session session)
71+
: base(session)
72+
{
73+
}
74+
75+
[Key]
76+
[Field(Nullable = false)]
77+
public Guid Id { get; private set; }
78+
79+
[Field(Nullable = false)]
80+
public string String { get; set; }
81+
}
82+
}
83+
84+
85+
namespace Xtensive.Orm.Tests.Issues
86+
{
87+
public class IssueJira0754_FirstOrDefultOnEntitySetCauseIncorrectResults : AutoBuildTest
88+
{
89+
protected override DomainConfiguration BuildConfiguration()
90+
{
91+
var config = base.BuildConfiguration();
92+
config.UpgradeMode = DomainUpgradeMode.Recreate;
93+
94+
config.Types.Register(typeof(TestEntity));
95+
config.Types.Register(typeof(TestEntity2));
96+
config.Types.Register(typeof(ListEntity));
97+
98+
Expression<Func<TestEntity, TestEntity2>> exp = e => e.List.FirstOrDefault().Link;
99+
Expression<Func<TestEntity, TestEntity2>> exp2 = e => e.LinkOnList.Link;
100+
config.LinqExtensions.Register(typeof(TestEntity).GetProperty("VirtualList"), exp);
101+
config.LinqExtensions.Register(typeof(TestEntity).GetProperty("VirtualLink"), exp2);
102+
103+
return config;
104+
}
105+
106+
protected override void PopulateData()
107+
{
108+
using (var session = Domain.OpenSession())
109+
using (var tx = session.OpenTransaction()) {
110+
var item = new TestEntity(session) { String = "test" };
111+
var item2 = new TestEntity2(session) { String = "test" };
112+
var list = new ListEntity(session) { String = "test", Owner = item, Link = item2 };
113+
item.LinkOnList = list;
114+
115+
_ = new TestEntity(session) { String = "test2" };
116+
117+
tx.Complete();
118+
}
119+
}
120+
121+
[Test]
122+
public void Case1()
123+
{
124+
using (var session = Domain.OpenSession())
125+
using (var tx = session.OpenTransaction()) {
126+
var count = session.Query.All<TestEntity>()
127+
.Select(e => new { e.VirtualList.String, e.VirtualList.Id })
128+
.Count();
129+
Assert.That(count, Is.EqualTo(2));
130+
131+
var entities = session.Query.All<TestEntity>()
132+
.Select(e => new { e.VirtualList.String, e.VirtualList.Id })
133+
.ToArray().OrderBy(x => x.Id).ToArray();
134+
135+
Assert.That(entities[0].String, Is.Null);
136+
Assert.That(entities[0].Id, Is.EqualTo(Guid.Empty));
137+
138+
Assert.That(entities[1].String, Is.EqualTo("test"));
139+
Assert.That(entities[1].Id, Is.Not.EqualTo(Guid.Empty));
140+
}
141+
}
142+
143+
[Test]
144+
public void Case2()
145+
{
146+
using (var s = Domain.OpenSession())
147+
using (s.Activate())
148+
using (var t = s.OpenTransaction()) {
149+
var count = s.Query.All<TestEntity>()
150+
.Select(e => new { e.String, e.Id })
151+
.Count();
152+
Assert.That(count, Is.EqualTo(2));
153+
154+
var entities = s.Query.All<TestEntity>()
155+
.Select(e => new { e.String, e.Id })
156+
.ToArray().OrderBy(x => x.String).ToArray();
157+
158+
Assert.That(entities[0].String, Is.EqualTo("test"));
159+
Assert.That(entities[0].Id, Is.Not.EqualTo(Guid.Empty));
160+
161+
Assert.That(entities[1].String, Is.EqualTo("test2"));
162+
Assert.That(entities[1].Id, Is.Not.EqualTo(Guid.Empty));
163+
}
164+
}
165+
166+
[Test]
167+
public void Case3()
168+
{
169+
using (var s = Domain.OpenSession())
170+
using (s.Activate())
171+
using (var t = s.OpenTransaction()) {
172+
var count = s.Query.All<TestEntity>()
173+
.Select(e => new { e.LinkOnList.String, e.LinkOnList.Id })
174+
.Count();
175+
Assert.That(count, Is.EqualTo(2));
176+
177+
var entities = s.Query.All<TestEntity>()
178+
.Select(e => new { e.LinkOnList.String, e.LinkOnList.Id })
179+
.ToArray()
180+
.OrderBy(x => x.String)
181+
.ToArray();
182+
183+
Assert.That(entities[0].String, Is.Null);
184+
Assert.That(entities[0].Id, Is.EqualTo(Guid.Empty));
185+
186+
Assert.That(entities[1].String, Is.EqualTo("test"));
187+
Assert.That(entities[1].Id, Is.Not.EqualTo(Guid.Empty));
188+
}
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)