55// Created: 2009.11.13
66
77using System ;
8+ using System . Linq ;
89using System . Linq . Expressions ;
910using NUnit . Framework ;
1011using Xtensive . Core ;
1112using Xtensive . Linq ;
1213using Xtensive . Orm . Configuration ;
1314using Xtensive . Orm . Tests . Linq . CustomExpressionCompilersModel ;
14- using System . Linq ;
1515
1616namespace Xtensive . Orm . Tests . Linq . CustomExpressionCompilersModel
1717{
@@ -80,24 +80,21 @@ public int InstanceMethod(int value)
8080 return value * Id ;
8181 }
8282 }
83- }
8483
85- namespace Xtensive . Orm . Tests . Linq
86- {
87- [ CompilerContainer ( typeof ( Expression ) ) ]
84+ [ CompilerContainer ( typeof ( Expression ) ) ]
8885 internal static class CustomLinqCompilerContainer
8986 {
90- [ Compiler ( typeof ( Person ) , "Fullname" , TargetKind . PropertyGet ) ]
87+ [ Compiler ( typeof ( Person ) , "Fullname" , TargetKind . PropertyGet ) ]
9188 public static Expression FullName ( Expression personExpression )
9289 {
9390 Expression < Func < Person , string > > ex = person => person . FirstName + " " + person . LastName ;
9491 return ex . BindParameters ( personExpression ) ;
9592 }
9693
97- [ Compiler ( typeof ( Person ) , "AddPrefix" , TargetKind . Method ) ]
94+ [ Compiler ( typeof ( Person ) , "AddPrefix" , TargetKind . Method ) ]
9895 public static Expression AddPrefix ( Expression personExpression , Expression prefixExpression )
9996 {
100- Expression < Func < Person , string , string > > ex = ( person , prefix ) => prefix + person . LastName ;
97+ Expression < Func < Person , string , string > > ex = ( person , prefix ) => prefix + person . LastName ;
10198 return ex . BindParameters ( personExpression , prefixExpression ) ;
10299 }
103100
@@ -108,22 +105,24 @@ public static Expression Current(Expression assignmentExpression)
108105 return ex . BindParameters ( assignmentExpression ) ;
109106 }
110107 }
108+ }
111109
110+ namespace Xtensive . Orm . Tests . Linq
111+ {
112112 public class CustomExpressionCompilers : AutoBuildTest
113113 {
114114 protected override DomainConfiguration BuildConfiguration ( )
115115 {
116116 var config = base . BuildConfiguration ( ) ;
117- config . Types . Register ( typeof ( Person ) . Assembly , typeof ( Person ) . Namespace ) ;
118- config . Types . Register ( typeof ( CustomLinqCompilerContainer ) ) ;
117+ config . Types . Register ( typeof ( Person ) . Assembly , typeof ( Person ) . Namespace ) ;
119118 RegisterLinqExtensions ( config ) ;
120119 return config ;
121120 }
122121
123122 private static void RegisterLinqExtensions ( DomainConfiguration config )
124123 {
125124 var extensions = config . LinqExtensions ;
126- var type = typeof ( RegistrarTestEntity ) ;
125+ var type = typeof ( RegistrarTestEntity ) ;
127126 Expression < Func < int > > staticProperty = ( ) => 0 ;
128127 extensions . Register ( type . GetProperty ( "StaticProperty" ) , staticProperty ) ;
129128 Expression < Func < RegistrarTestEntity , int > > instanceProperty = e => e . Id % 10 ;
@@ -137,35 +136,41 @@ private static void RegisterLinqExtensions(DomainConfiguration config)
137136 [ Test ]
138137 public void MainTest ( )
139138 {
140- using ( var session = Domain . OpenSession ( ) ) {
141- using ( var t = session . OpenTransaction ( ) ) {
142- Fill ( ) ;
143- var expected1 = session . Query . All < Person > ( ) . AsEnumerable ( ) . OrderBy ( p => p . Id ) . Select ( p => p . Fullname ) . ToList ( ) ;
144- Assert . Greater ( expected1 . Count , 0 ) ;
145- var fullNames1 = session . Query . All < Person > ( ) . OrderBy ( p => p . Id ) . Select ( p => p . Fullname ) . ToList ( ) ;
146- Assert . IsTrue ( expected1 . SequenceEqual ( fullNames1 ) ) ;
147-
148- var expected2 = session . Query . All < Person > ( ) . AsEnumerable ( ) . OrderBy ( p => p . Id ) . Select ( p => p . AddPrefix ( "Mr. " ) ) . ToList ( ) ;
149- var fullNames2 = session . Query . All < Person > ( ) . OrderBy ( p => p . Id ) . Select ( p => p . AddPrefix ( "Mr. " ) ) . ToList ( ) ;
150- Assert . IsTrue ( expected2 . SequenceEqual ( fullNames2 ) ) ;
151- // Rollback
152- }
139+ using ( var session = Domain . OpenSession ( ) )
140+ using ( var t = session . OpenTransaction ( ) ) {
141+ Fill ( ) ;
142+ var expected1 = session . Query . All < Person > ( ) . AsEnumerable ( ) . OrderBy ( p => p . Id ) . Select ( p => p . Fullname ) . ToList ( ) ;
143+ Assert . Greater ( expected1 . Count , 0 ) ;
144+ var fullNames1 = session . Query . All < Person > ( ) . OrderBy ( p => p . Id ) . Select ( p => p . Fullname ) . ToList ( ) ;
145+ Assert . IsTrue ( expected1 . SequenceEqual ( fullNames1 ) ) ;
146+
147+ var expected2 = session . Query . All < Person > ( ) . AsEnumerable ( ) . OrderBy ( p => p . Id ) . Select ( p => p . AddPrefix ( "Mr. " ) ) . ToList ( ) ;
148+ var fullNames2 = session . Query . All < Person > ( ) . OrderBy ( p => p . Id ) . Select ( p => p . AddPrefix ( "Mr. " ) ) . ToList ( ) ;
149+ Assert . IsTrue ( expected2 . SequenceEqual ( fullNames2 ) ) ;
150+ // Rollback
153151 }
154152 }
155153
156154 [ Test ]
157155 public void AssignmentCurrentTest ( )
158156 {
157+ var baseDate = DateTime . Today ;
158+ ( bool active , DateTime startDate , DateTime ? endDate ) [ ] dataset = new [ ] {
159+ ( true , baseDate . AddYears ( - 20 ) , ( DateTime ? ) null ) ,
160+ ( false , baseDate . AddYears ( - 20 ) , ( DateTime ? ) null ) ,
161+ ( false , baseDate . AddYears ( - 5 ) , baseDate . AddYears ( 20 ) ) ,
162+ ( true , baseDate . AddYears ( 1 ) , baseDate . AddYears ( 20 ) ) ,
163+ ( true , baseDate . AddYears ( - 15 ) , baseDate . AddYears ( 20 ) ) ,
164+ } ;
165+
159166 using ( var session = Domain . OpenSession ( ) )
160167 using ( var t = session . OpenTransaction ( ) ) {
161- new Assignment ( ) { Active = true , Start = new DateTime ( 2009 , 11 , 23 ) , End = null } ;
162- new Assignment ( ) { Active = false , Start = new DateTime ( 2009 , 10 , 3 ) , End = null } ;
163- new Assignment ( ) { Active = false , Start = new DateTime ( 2020 , 01 , 10 ) , End = new DateTime ( 2044 , 12 , 3 ) } ;
164- new Assignment ( ) { Active = true , Start = new DateTime ( 2026 , 01 , 10 ) , End = new DateTime ( 2045 , 11 , 3 ) } ;
165- new Assignment ( ) { Active = true , Start = new DateTime ( 2010 , 01 , 10 ) , End = new DateTime ( 2035 , 11 , 3 ) } ;
168+ foreach ( var data in dataset ) {
169+ _ = new Assignment ( ) { Active = data . active , Start = data . startDate , End = data . endDate } ;
170+ }
166171
167172 var currentCount = session . Query . All < Assignment > ( ) . Count ( a => a . Current ) ;
168- Assert . AreEqual ( 2 , currentCount ) ;
173+ Assert . That ( currentCount , Is . EqualTo ( 2 ) ) ;
169174 // Rollback
170175 }
171176 }
@@ -197,9 +202,9 @@ public void RegistrarTest()
197202
198203 private void Fill ( )
199204 {
200- new Person { FirstName = "Ivan" , LastName = "Semenov" } ;
201- new Person { FirstName = "John" , LastName = "Smith" } ;
202- new Person { FirstName = "Andrew" , LastName = "Politkovsky" } ;
205+ _ = new Person { FirstName = "Ivan" , LastName = "Semenov" } ;
206+ _ = new Person { FirstName = "John" , LastName = "Smith" } ;
207+ _ = new Person { FirstName = "Andrew" , LastName = "Politkovsky" } ;
203208 }
204209 }
205210}
0 commit comments