Skip to content

Commit 9d94532

Browse files
committed
Improve PartialIndexTest
- Prevent test of conditional expression fail on SQL Server because the storage has no support of CASE statement within WHERE part - Added extra cases for entity fields comparison to cover all supported cases - Added case of two entity references comparison
1 parent c2f1cd7 commit 9d94532

2 files changed

Lines changed: 247 additions & 11 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using NUnit.Framework;
5+
using NUnit.Framework.Interfaces;
6+
7+
namespace Xtensive.Orm.Tests
8+
{
9+
/// <summary>
10+
/// Base attribute for test storage requirement
11+
/// </summary>
12+
public abstract class RequireProvderAttribute : Attribute, ITestAction
13+
{
14+
private Version minVersion = null;
15+
private Version maxVersion = null;
16+
17+
/// <summary>
18+
/// Gets or sets Minimal version that required for test.
19+
/// If not set, version check is not applied
20+
/// </summary>
21+
public virtual string MinVersion
22+
{
23+
get { return (minVersion == null) ? null : minVersion.ToString(); }
24+
set {
25+
if (value == null)
26+
minVersion = null;
27+
else if (!Version.TryParse(value, out minVersion))
28+
throw new ArgumentException("Not a version string", nameof(value));
29+
}
30+
}
31+
32+
/// <summary>
33+
/// Gets or sets Maximal version that required for test.
34+
/// If not set, version check is not applied.
35+
/// </summary>
36+
public virtual string MaxVersion
37+
{
38+
get { return (maxVersion == null) ? null : maxVersion.ToString(); }
39+
set {
40+
if (value == null)
41+
maxVersion = null;
42+
else if (!Version.TryParse(value, out maxVersion))
43+
throw new ArgumentException("Not a version string", nameof(value));
44+
}
45+
}
46+
47+
protected abstract StorageProvider RequiredProviders { get; }
48+
49+
public ActionTargets Targets => ActionTargets.Test;
50+
51+
public void AfterTest(ITest test)
52+
{
53+
Require.ProviderIs(RequiredProviders);
54+
if (minVersion != null)
55+
Require.ProviderVersionAtLeast(minVersion);
56+
if (maxVersion != null)
57+
Require.ProviderVersionAtMost(maxVersion);
58+
}
59+
60+
public void BeforeTest(ITest test) { }
61+
}
62+
63+
[AttributeUsage(AttributeTargets.Method)]
64+
public class RequireSqlServerAttribute : RequireProvderAttribute
65+
{
66+
protected override StorageProvider RequiredProviders => StorageProvider.SqlServer;
67+
}
68+
69+
[AttributeUsage(AttributeTargets.Method)]
70+
public class RequirePostgreSqlAttribute : RequireProvderAttribute
71+
{
72+
protected override StorageProvider RequiredProviders => StorageProvider.PostgreSql;
73+
}
74+
75+
[AttributeUsage(AttributeTargets.Method)]
76+
public class RequireMySqlAttribute : RequireProvderAttribute
77+
{
78+
protected override StorageProvider RequiredProviders => StorageProvider.MySql;
79+
}
80+
81+
[AttributeUsage(AttributeTargets.Method)]
82+
public class RequireFirebirdAttribute : RequireProvderAttribute
83+
{
84+
protected override StorageProvider RequiredProviders => StorageProvider.Firebird;
85+
}
86+
87+
[AttributeUsage(AttributeTargets.Method)]
88+
public class RequireOracleSqlAttribute : RequireProvderAttribute
89+
{
90+
protected override StorageProvider RequiredProviders => StorageProvider.Oracle;
91+
}
92+
93+
[AttributeUsage(AttributeTargets.Method)]
94+
public class RequireSqliteAttribute : RequireProvderAttribute
95+
{
96+
protected override StorageProvider RequiredProviders => StorageProvider.Sqlite;
97+
}
98+
99+
[AttributeUsage(AttributeTargets.Method)]
100+
public class RequireSeveralProvidersAttribute : RequireProvderAttribute
101+
{
102+
private readonly StorageProvider providers;
103+
104+
protected override StorageProvider RequiredProviders => providers;
105+
106+
public override string MinVersion
107+
{
108+
get => throw new NotSupportedException();
109+
set => throw new NotSupportedException();
110+
}
111+
112+
public override string MaxVersion
113+
{
114+
get => throw new NotSupportedException();
115+
set => throw new NotSupportedException();
116+
}
117+
118+
public RequireSeveralProvidersAttribute(StorageProvider allowedProviders)
119+
{
120+
providers = allowedProviders;
121+
}
122+
}
123+
}

Orm/Xtensive.Orm.Tests/Storage/PartialIndexTest.cs

Lines changed: 124 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Xtensive.Orm.Model;
1515
using Xtensive.Orm.Providers;
1616
using Xtensive.Orm.Tests.Storage.PartialIndexTestModel;
17+
using NUnit.Framework.Interfaces;
1718

1819
namespace Xtensive.Orm.Tests.Storage.PartialIndexTestModel
1920
{
@@ -70,25 +71,97 @@ public class SimpleFilterWithProperty : TestBase
7071
}
7172

7273
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
73-
public class FilterOnReferenceField : TestBase
74+
public class FilterOnReferenceField1 : TestBase
7475
{
75-
public static Expression<Func<FilterOnReferenceField, bool>> Index() =>
76+
public static Expression<Func<FilterOnReferenceField1, bool>> Index() =>
7677
test => test.Target != null;
7778

7879
[Field]
7980
public TargetEntity Target { get; set; }
8081
}
8182

8283
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
83-
public class FilterOnComplexReferenceField : TestBase
84+
public class FilterOnReferenceField2 : TestBase
8485
{
85-
public static Expression<Func<FilterOnComplexReferenceField, bool>> Index() =>
86+
public static Expression<Func<FilterOnReferenceField2, bool>> Index() =>
87+
test => test.Target == null;
88+
89+
[Field]
90+
public TargetEntity Target { get; set; }
91+
}
92+
93+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
94+
public class FilterOnReferenceField3 : TestBase
95+
{
96+
public static Expression<Func<FilterOnReferenceField3, bool>> Index() =>
97+
test => test.Target == test.Alien;
98+
99+
[Field]
100+
public TargetEntity Target { get; set; }
101+
102+
[Field]
103+
public TargetEntity Alien { get; set; }
104+
}
105+
106+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
107+
public class FilterOnReferenceField4 : TestBase
108+
{
109+
public static Expression<Func<FilterOnReferenceField4, bool>> Index() =>
110+
test => test.Target != test.Alien;
111+
112+
[Field]
113+
public TargetEntity Target { get; set; }
114+
115+
[Field]
116+
public TargetEntity Alien { get; set; }
117+
}
118+
119+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
120+
public class FilterOnComplexReferenceField1 : TestBase
121+
{
122+
public static Expression<Func<FilterOnComplexReferenceField1, bool>> Index() =>
86123
test => test.Target != null;
87124

88125
[Field]
89126
public ComplexTargetEntity Target { get; set; }
90127
}
91128

129+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
130+
public class FilterOnComplexReferenceField2 : TestBase
131+
{
132+
public static Expression<Func<FilterOnComplexReferenceField2, bool>> Index() =>
133+
test => test.Target == null;
134+
135+
[Field]
136+
public ComplexTargetEntity Target { get; set; }
137+
}
138+
139+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
140+
public class FilterOnComplexReferenceField3 : TestBase
141+
{
142+
public static Expression<Func<FilterOnComplexReferenceField3, bool>> Index() =>
143+
test => test.Target == test.Alien;
144+
145+
[Field]
146+
public ComplexTargetEntity Target { get; set; }
147+
148+
[Field]
149+
public ComplexTargetEntity Alien { get; set; }
150+
}
151+
152+
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
153+
public class FilterOnComplexReferenceField4 : TestBase
154+
{
155+
public static Expression<Func<FilterOnComplexReferenceField4, bool>> Index() =>
156+
test => test.Target != test.Alien;
157+
158+
[Field]
159+
public ComplexTargetEntity Target { get; set; }
160+
161+
[Field]
162+
public ComplexTargetEntity Alien { get; set; }
163+
}
164+
92165
[HierarchyRoot, Index(nameof(Target), Filter = nameof(Index))]
93166
public class FilterOnReferenceIdField : TestBase
94167
{
@@ -168,7 +241,7 @@ public class ContainsOperatorSupport : TestBase
168241
public class ConditionalExpressionSuport : TestBase
169242
{
170243
public static Expression<Func<ConditionalExpressionSuport, bool>> Index =>
171-
test => test.Id == ((test.TestField == null) ? 100 : 200) ? test.TestField.Contains("hello") : false;
244+
test => test.Id == 100 ? test.TestField.Contains("hello") : false;
172245

173246
[Field]
174247
public string TestField { get; set; }
@@ -530,10 +603,28 @@ private void AssertBuildFailure(params Type[] entities)
530603
public void SimpleFilterWithPropertyTest() => AssertBuildSuccess(typeof(SimpleFilterWithProperty));
531604

532605
[Test]
533-
public void FilterOnReferenceFieldTest() => AssertBuildSuccess(typeof(FilterOnReferenceField));
606+
public void FilterOnReferenceFieldTest1() => AssertBuildSuccess(typeof(FilterOnReferenceField1));
607+
608+
[Test]
609+
public void FilterOnReferenceFieldTest2() => AssertBuildSuccess(typeof(FilterOnReferenceField2));
610+
611+
[Test]
612+
public void FilterOnReferenceFieldTest3() => AssertBuildFailure(typeof(FilterOnReferenceField3));
534613

535614
[Test]
536-
public void FilterOnComplexReferenceFieldTest() => AssertBuildSuccess(typeof(FilterOnComplexReferenceField));
615+
public void FilterOnReferenceFieldTest4() => AssertBuildFailure(typeof(FilterOnReferenceField4));
616+
617+
[Test]
618+
public void FilterOnComplexReferenceFieldTest1() => AssertBuildSuccess(typeof(FilterOnComplexReferenceField1));
619+
620+
[Test]
621+
public void FilterOnComplexReferenceFieldTest2() => AssertBuildSuccess(typeof(FilterOnComplexReferenceField2));
622+
623+
[Test]
624+
public void FilterOnComplexReferenceFieldTest3() => AssertBuildFailure(typeof(FilterOnComplexReferenceField3));
625+
626+
[Test]
627+
public void FilterOnComplexReferenceFieldTest4() => AssertBuildFailure(typeof(FilterOnComplexReferenceField4));
537628

538629
[Test]
539630
public void FilterOnReferenceFieldIdTest() => AssertBuildSuccess(typeof(FilterOnReferenceIdField));
@@ -553,7 +644,7 @@ private void AssertBuildFailure(params Type[] entities)
553644
[Test]
554645
public void ContainsOperatorSupportTest() => AssertBuildSuccess(typeof(ContainsOperatorSupport));
555646

556-
[Test]
647+
[Test, RequirePostgreSql]
557648
public void ConditionalOperationSupportTest() => AssertBuildSuccess(typeof(ConditionalExpressionSuport));
558649

559650
[Test]
@@ -577,12 +668,34 @@ private void AssertBuildFailure(params Type[] entities)
577668
[Test]
578669
public void EnumFieldFilterTest() => AssertBuildSuccess(typeof(EnumFieldFilter));
579670

580-
[Test]
581-
public void ValidateTest()
671+
[Test, RequirePostgreSql]
672+
public void ValidateTestForPgSql()
673+
{
674+
var types = typeof(TestBase).Assembly
675+
.GetTypes()
676+
.Where(type => type.Namespace == typeof(TestBase).Namespace
677+
&& type != typeof(InheritanceClassTable)
678+
&& type != typeof(FilterOnReferenceField3)
679+
&& type != typeof(FilterOnReferenceField4)
680+
&& type != typeof(FilterOnComplexReferenceField3)
681+
&& type != typeof(FilterOnComplexReferenceField4))
682+
.ToList();
683+
BuildDomain(types, DomainUpgradeMode.Recreate);
684+
BuildDomain(types, DomainUpgradeMode.Validate);
685+
}
686+
687+
[Test, RequireSqlServer]
688+
public void ValidateTestForSqlServer()
582689
{
583690
var types = typeof(TestBase).Assembly
584691
.GetTypes()
585-
.Where(type => type.Namespace == typeof(TestBase).Namespace && type != typeof(InheritanceClassTable))
692+
.Where(type => type.Namespace == typeof(TestBase).Namespace
693+
&& type != typeof(InheritanceClassTable)
694+
&& type != typeof(ConditionalExpressionSuport)
695+
&& type != typeof(FilterOnReferenceField3)
696+
&& type != typeof(FilterOnReferenceField4)
697+
&& type != typeof(FilterOnComplexReferenceField3)
698+
&& type != typeof(FilterOnComplexReferenceField4))
586699
.ToList();
587700
BuildDomain(types, DomainUpgradeMode.Recreate);
588701
BuildDomain(types, DomainUpgradeMode.Validate);

0 commit comments

Comments
 (0)