Skip to content

Commit 27a7d41

Browse files
committed
added VB tests and added VB expression tree conversion to ExpressionNormalizer.
1 parent 61e262c commit 27a7d41

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed

Driver/Linq/Expressions/ExpressionNormalizer.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ protected override Expression VisitBinary(BinaryExpression node)
8282
}
8383
}
8484

85+
//VB creates coalescing operations when dealing with nullable value comparisons, so we try and make this look like C#\
86+
if (node.NodeType == ExpressionType.Coalesce)
87+
{
88+
var right = node.Right as ConstantExpression;
89+
if (node.Left.NodeType == ExpressionType.Equal
90+
&& node.Left.Type.IsGenericType
91+
&& node.Left.Type.GetGenericTypeDefinition() == typeof(Nullable<>)
92+
&& right != null
93+
&& (bool)right.Value == false)
94+
{
95+
node = (BinaryExpression)node.Left;
96+
return Expression.MakeBinary(
97+
ExpressionType.Equal,
98+
Visit(node.Left),
99+
Visit(node.Right),
100+
false,
101+
null);
102+
}
103+
}
104+
85105
if (result != null)
86106
{
87107
return result;

DriverUnitTestsVB/DriverUnitTestsVB.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<ItemGroup>
7070
<Compile Include="Linq\MongoQueryableTests.vb" />
7171
<Compile Include="Linq\MongoQueryProviderTests.vb" />
72+
<Compile Include="Linq\SelectNullableTests.vb" />
7273
<Compile Include="Linq\SelectOfTypeHierarchicalTests.vb" />
7374
<Compile Include="Linq\SelectOfTypeTests.vb" />
7475
<Compile Include="Linq\SelectQueryTests.vb" />
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
17+
Imports System.Collections.Generic
18+
Imports System.Linq
19+
Imports System.Text
20+
Imports NUnit.Framework
21+
22+
Imports MongoDB.Bson
23+
Imports MongoDB.Bson.Serialization.Attributes
24+
Imports MongoDB.Driver
25+
Imports MongoDB.Driver.Linq
26+
27+
Namespace MongoDB.DriverUnitTests.Linq
28+
<TestFixture()> _
29+
Public Class SelectNullableTests
30+
Private Enum E
31+
None
32+
A
33+
B
34+
End Enum
35+
36+
Private Class C
37+
Public Property Id() As ObjectId
38+
Get
39+
Return m_Id
40+
End Get
41+
Set(ByVal value As ObjectId)
42+
m_Id = Value
43+
End Set
44+
End Property
45+
Private m_Id As ObjectId
46+
<BsonElement("e")> _
47+
<BsonRepresentation(BsonType.[String])> _
48+
Public Property E() As System.Nullable(Of E)
49+
Get
50+
Return m_E
51+
End Get
52+
Set(ByVal value As System.Nullable(Of E))
53+
m_E = Value
54+
End Set
55+
End Property
56+
Private m_E As System.Nullable(Of E)
57+
<BsonElement("x")> _
58+
Public Property X() As System.Nullable(Of Integer)
59+
Get
60+
Return m_X
61+
End Get
62+
Set(ByVal value As System.Nullable(Of Integer))
63+
m_X = Value
64+
End Set
65+
End Property
66+
Private m_X As System.Nullable(Of Integer)
67+
End Class
68+
69+
Private _server As MongoServer
70+
Private _database As MongoDatabase
71+
Private _collection As MongoCollection(Of C)
72+
73+
<TestFixtureSetUp()> _
74+
Public Sub Setup()
75+
_server = Configuration.TestServer
76+
_database = Configuration.TestDatabase
77+
_collection = Configuration.GetTestCollection(Of C)()
78+
79+
_collection.Drop()
80+
_collection.Insert(New C() With { _
81+
.E = Nothing _
82+
})
83+
_collection.Insert(New C() With { _
84+
.E = E.A _
85+
})
86+
_collection.Insert(New C() With { _
87+
.E = E.B _
88+
})
89+
_collection.Insert(New C() With { _
90+
.X = Nothing _
91+
})
92+
_collection.Insert(New C() With { _
93+
.X = 1 _
94+
})
95+
_collection.Insert(New C() With { _
96+
.X = 2 _
97+
})
98+
End Sub
99+
100+
<Test()> _
101+
Public Sub TestWhereEEqualsA()
102+
Dim query = From c In _collection.AsQueryable(Of C)()
103+
Where c.E = E.A
104+
Select c
105+
106+
Dim translatedQuery = MongoQueryTranslator.Translate(query)
107+
Assert.IsInstanceOf(Of SelectQuery)(translatedQuery)
108+
Assert.AreSame(_collection, translatedQuery.Collection)
109+
Assert.AreSame(GetType(C), translatedQuery.DocumentType)
110+
111+
Dim selectQuery = DirectCast(translatedQuery, SelectQuery)
112+
Assert.IsNull(selectQuery.OrderBy)
113+
Assert.IsNull(selectQuery.Projection)
114+
Assert.IsNull(selectQuery.Skip)
115+
Assert.IsNull(selectQuery.Take)
116+
117+
Assert.AreEqual("{ ""e"" : ""A"" }", selectQuery.BuildQuery().ToJson())
118+
Assert.AreEqual(1, Consume(query))
119+
End Sub
120+
121+
<Test()> _
122+
Public Sub TestWhereEEqualsNull()
123+
Dim query = From c In _collection.AsQueryable(Of C)()
124+
Where c.E Is Nothing
125+
Select c
126+
127+
Dim translatedQuery = MongoQueryTranslator.Translate(query)
128+
Assert.IsInstanceOf(Of SelectQuery)(translatedQuery)
129+
Assert.AreSame(_collection, translatedQuery.Collection)
130+
Assert.AreSame(GetType(C), translatedQuery.DocumentType)
131+
132+
Dim selectQuery = DirectCast(translatedQuery, SelectQuery)
133+
Assert.IsNull(selectQuery.OrderBy)
134+
Assert.IsNull(selectQuery.Projection)
135+
Assert.IsNull(selectQuery.Skip)
136+
Assert.IsNull(selectQuery.Take)
137+
138+
Assert.AreEqual("{ ""e"" : null }", selectQuery.BuildQuery().ToJson())
139+
Assert.AreEqual(4, Consume(query))
140+
End Sub
141+
142+
<Test()> _
143+
Public Sub TestWhereXEquals1()
144+
Dim query = From c In _collection.AsQueryable(Of C)()
145+
Where c.X = 1
146+
Select c
147+
148+
Dim translatedQuery = MongoQueryTranslator.Translate(query)
149+
Assert.IsInstanceOf(Of SelectQuery)(translatedQuery)
150+
Assert.AreSame(_collection, translatedQuery.Collection)
151+
Assert.AreSame(GetType(C), translatedQuery.DocumentType)
152+
153+
Dim selectQuery = DirectCast(translatedQuery, SelectQuery)
154+
Assert.IsNull(selectQuery.OrderBy)
155+
Assert.IsNull(selectQuery.Projection)
156+
Assert.IsNull(selectQuery.Skip)
157+
Assert.IsNull(selectQuery.Take)
158+
159+
Assert.AreEqual("{ ""x"" : 1 }", selectQuery.BuildQuery().ToJson())
160+
Assert.AreEqual(1, Consume(query))
161+
End Sub
162+
163+
<Test()> _
164+
Public Sub TestWhereXEqualsNull()
165+
Dim query = From c In _collection.AsQueryable(Of C)()
166+
Where c.X Is Nothing
167+
Select c
168+
169+
Dim translatedQuery = MongoQueryTranslator.Translate(query)
170+
Assert.IsInstanceOf(Of SelectQuery)(translatedQuery)
171+
Assert.AreSame(_collection, translatedQuery.Collection)
172+
Assert.AreSame(GetType(C), translatedQuery.DocumentType)
173+
174+
Dim selectQuery = DirectCast(translatedQuery, SelectQuery)
175+
Assert.IsNull(selectQuery.OrderBy)
176+
Assert.IsNull(selectQuery.Projection)
177+
Assert.IsNull(selectQuery.Skip)
178+
Assert.IsNull(selectQuery.Take)
179+
180+
Assert.AreEqual("{ ""x"" : null }", selectQuery.BuildQuery().ToJson())
181+
Assert.AreEqual(4, Consume(query))
182+
End Sub
183+
184+
Private Function Consume(Of T)(ByVal query As IQueryable(Of T)) As Integer
185+
Dim count = 0
186+
For Each c In query
187+
count += 1
188+
Next
189+
Return count
190+
End Function
191+
End Class
192+
End Namespace

0 commit comments

Comments
 (0)