Skip to content

Commit ddfb977

Browse files
committed
Test for the issue
1 parent da68a4f commit ddfb977

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// Copyright (C) 2023 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.Core;
10+
using Xtensive.Orm.Configuration;
11+
using Xtensive.Orm.Tests.Issues.IssueGithub312_DefaultExpressionProblemModel;
12+
13+
namespace Xtensive.Orm.Tests.Issues
14+
{
15+
namespace IssueGithub312_DefaultExpressionProblemModel
16+
{
17+
[HierarchyRoot]
18+
public sealed class TestEntity : Entity
19+
{
20+
[Field, Key]
21+
public int Id { get; private set; }
22+
23+
[Field]
24+
public string Name { get; set; }
25+
26+
[Field]
27+
public int Value { get; set; }
28+
29+
public TestEntity(Session session)
30+
: base(session)
31+
{
32+
}
33+
}
34+
}
35+
36+
public class IssueGithub312_DefaultExpressionProblem : AutoBuildTest
37+
{
38+
protected override DomainConfiguration BuildConfiguration()
39+
{
40+
var configuration = base.BuildConfiguration();
41+
configuration.Types.Register(typeof(TestEntity));
42+
configuration.UpgradeMode = DomainUpgradeMode.Recreate;
43+
return configuration;
44+
}
45+
46+
protected override void PopulateData()
47+
{
48+
using (var session = Domain.OpenSession())
49+
using (var tx = session.OpenTransaction()) {
50+
_ = new TestEntity(session) { Name = null, Value = 0 };
51+
_ = new TestEntity(session) { Name = string.Empty, Value = 0 };
52+
_ = new TestEntity(session) { Name = "-2", Value = -2 };
53+
_ = new TestEntity(session) { Name = "-1", Value = -1 };
54+
_ = new TestEntity(session) { Name = "1", Value = 1 };
55+
_ = new TestEntity(session) { Name = "2", Value = 2 };
56+
tx.Complete();
57+
}
58+
}
59+
60+
[Test]
61+
public void DefaultOfObject()
62+
{
63+
using (var session = Domain.OpenSession())
64+
using (var tx = session.OpenTransaction()) {
65+
var param = Expression.Parameter(typeof(TestEntity), "o");
66+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
67+
Expression.Equal(
68+
Expression.Property(param, nameof(TestEntity.Name)),
69+
Expression.Default(typeof(object))),
70+
new[] { param });
71+
72+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(1));
73+
74+
param = Expression.Parameter(typeof(TestEntity), "o");
75+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
76+
Expression.NotEqual(
77+
Expression.Property(param, nameof(TestEntity.Name)),
78+
Expression.Default(typeof(object))),
79+
new[] { param });
80+
81+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(5));
82+
}
83+
}
84+
85+
[Test]
86+
public void DefaultOfValueType()
87+
{
88+
using (var session = Domain.OpenSession())
89+
using (var tx = session.OpenTransaction()) {
90+
var param = Expression.Parameter(typeof(TestEntity), "o");
91+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
92+
Expression.Equal(
93+
Expression.Property(param, nameof(TestEntity.Value)),
94+
Expression.Default(typeof(int))),
95+
new[] { param });
96+
97+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(2));
98+
99+
param = Expression.Parameter(typeof(TestEntity), "o");
100+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
101+
Expression.NotEqual(
102+
Expression.Property(param, nameof(TestEntity.Value)),
103+
Expression.Default(typeof(int))),
104+
new[] { param });
105+
106+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(4));
107+
}
108+
}
109+
110+
[Test]
111+
public void DefaultOfReferenceType()
112+
{
113+
using (var session = Domain.OpenSession())
114+
using (var tx = session.OpenTransaction()) {
115+
var param = Expression.Parameter(typeof(TestEntity), "o");
116+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
117+
Expression.Equal(
118+
Expression.Property(param, nameof(TestEntity.Name)),
119+
Expression.Default(typeof(string))),
120+
new[] { param });
121+
122+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(1));
123+
124+
param = Expression.Parameter(typeof(TestEntity), "o");
125+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
126+
Expression.NotEqual(
127+
Expression.Property(param, nameof(TestEntity.Name)),
128+
Expression.Default(typeof(string))),
129+
new[] { param });
130+
131+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(5));
132+
}
133+
}
134+
135+
[Test]
136+
public void ConstantNullObjectValue()
137+
{
138+
using (var session = Domain.OpenSession())
139+
using (var tx = session.OpenTransaction()) {
140+
var param = Expression.Parameter(typeof(TestEntity), "o");
141+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
142+
Expression.Equal(
143+
Expression.Property(param, nameof(TestEntity.Name)),
144+
Expression.Constant((object) null)),
145+
new[] { param });
146+
147+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(1));
148+
149+
param = Expression.Parameter(typeof(TestEntity), "o");
150+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
151+
Expression.NotEqual(
152+
Expression.Property(param, nameof(TestEntity.Name)),
153+
Expression.Constant((object) null)),
154+
new[] { param });
155+
156+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(5));
157+
}
158+
}
159+
160+
[Test]
161+
public void ConstantValueTypeDefalut()
162+
{
163+
using (var session = Domain.OpenSession())
164+
using (var tx = session.OpenTransaction()) {
165+
var param = Expression.Parameter(typeof(TestEntity), "o");
166+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
167+
Expression.Equal(
168+
Expression.Property(param, nameof(TestEntity.Value)),
169+
Expression.Constant(0)),
170+
new[] { param });
171+
172+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(2));
173+
174+
param = Expression.Parameter(typeof(TestEntity), "o");
175+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
176+
Expression.NotEqual(
177+
Expression.Property(param, nameof(TestEntity.Value)),
178+
Expression.Constant(0)),
179+
new[] { param });
180+
181+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(4));
182+
}
183+
}
184+
185+
[Test]
186+
public void ConstantNullStringValue()
187+
{
188+
using (var session = Domain.OpenSession())
189+
using (var tx = session.OpenTransaction()) {
190+
var param = Expression.Parameter(typeof(TestEntity), "o");
191+
var lambda = Expression.Lambda<Func<TestEntity, bool>>(
192+
Expression.Equal(
193+
Expression.Property(param, nameof(TestEntity.Name)),
194+
Expression.Constant((string) null)),
195+
new[] { param });
196+
197+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(1));
198+
199+
param = Expression.Parameter(typeof(TestEntity), "o");
200+
lambda = Expression.Lambda<Func<TestEntity, bool>>(
201+
Expression.NotEqual(
202+
Expression.Property(param, nameof(TestEntity.Name)),
203+
Expression.Constant((string) null)),
204+
new[] { param });
205+
206+
Assert.That(session.Query.All<TestEntity>().Where(lambda).Count(), Is.EqualTo(5));
207+
}
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)