1
+ /* Copyright 2010-2012 10gen Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System ;
17
+ using System . Collections . Generic ;
18
+ using System . IO ;
19
+ using System . Linq ;
20
+ using System . Runtime . Serialization ;
21
+ using System . Text ;
22
+ using NUnit . Framework ;
23
+
24
+ using MongoDB . Driver ;
25
+ using MongoDB . Driver . Linq ;
26
+
27
+ namespace MongoDB . DriverUnitTests . Jira
28
+ {
29
+ [ TestFixture ]
30
+ public class CSharp471Tests
31
+ {
32
+ public class Base
33
+ {
34
+ public Guid Id { get ; set ; }
35
+
36
+ public string A { get ; set ; }
37
+ }
38
+
39
+ public class T1 : Base
40
+ {
41
+ public string B { get ; set ; }
42
+ }
43
+
44
+ public class T2 : Base
45
+ {
46
+ public string C { get ; set ; }
47
+ }
48
+
49
+ [ Test ]
50
+ public void CastTest ( )
51
+ {
52
+ var server = MongoServer . Create ( ) ;
53
+ var db = server . GetDatabase ( "test" ) ;
54
+ var collection = db . GetCollection < Base > ( "castTest" ) ;
55
+ collection . Drop ( ) ;
56
+
57
+ var t1 = new T1 { Id = Guid . NewGuid ( ) , A = "T1.A" , B = "T1.B" } ;
58
+ var t2 = new T2 { Id = Guid . NewGuid ( ) , A = "T2.A" } ;
59
+ collection . Insert ( t1 ) ;
60
+ collection . Insert ( t2 ) ;
61
+
62
+ var query = from t in collection . AsQueryable ( )
63
+ where t is T1 && ( ( T1 ) t ) . B == "T1.B"
64
+ select t ;
65
+
66
+ var translatedQuery = MongoQueryTranslator . Translate ( query ) ;
67
+ Assert . IsInstanceOf < SelectQuery > ( translatedQuery ) ;
68
+ Assert . AreSame ( collection , translatedQuery . Collection ) ;
69
+ Assert . AreSame ( typeof ( Base ) , translatedQuery . DocumentType ) ;
70
+
71
+ var selectQuery = ( SelectQuery ) translatedQuery ;
72
+ Assert . AreEqual ( "(Base t) => ((t is T1) && ((T1)t.B == \" T1.B\" ))" , ExpressionFormatter . ToString ( selectQuery . Where ) ) ;
73
+ Assert . IsNull ( selectQuery . OrderBy ) ;
74
+ Assert . IsNull ( selectQuery . Projection ) ;
75
+ Assert . IsNull ( selectQuery . Skip ) ;
76
+ Assert . IsNull ( selectQuery . Take ) ;
77
+
78
+ Assert . AreEqual ( "{ \" _t\" : \" T1\" , \" B\" : \" T1.B\" }" , selectQuery . BuildQuery ( ) . ToString ( ) ) ;
79
+
80
+ var results = query . ToList ( ) ;
81
+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
82
+ Assert . That ( results [ 0 ] , Is . InstanceOf ( typeof ( T1 ) ) ) ;
83
+ Assert . That ( results [ 0 ] . A , Is . EqualTo ( "T1.A" ) ) ;
84
+ }
85
+ }
86
+ }
0 commit comments