|
| 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