/MongoDB.DriverUnitTestsVB/Linq/SelectQueryTests.vb
Visual Basic | 6675 lines | 5290 code | 1325 blank | 60 comment | 13 complexity | 93c4bfa458b3f004a7003cdda0b87696 MD5 | raw file
Possible License(s): Apache-2.0
Large files files are truncated, but you can click here to view the full file
- ' Copyright 2010-2013 10gen Inc.
- '*
- '* Licensed under the Apache License, Version 2.0 (the "License");
- '* you may not use this file except in compliance with the License.
- '* You may obtain a copy of the License at
- '*
- '* http://www.apache.org/licenses/LICENSE-2.0
- '*
- '* Unless required by applicable law or agreed to in writing, software
- '* distributed under the License is distributed on an "AS IS" BASIS,
- '* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- '* See the License for the specific language governing permissions and
- '* limitations under the License.
- '
- Imports System
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Text
- Imports System.Text.RegularExpressions
- Imports NUnit.Framework
- Imports MongoDB.Bson
- Imports MongoDB.Bson.Serialization.Attributes
- Imports MongoDB.Driver
- Imports MongoDB.Driver.Builders
- Imports MongoDB.Driver.Linq
- Imports System.Linq.Expressions
- Namespace MongoDB.DriverUnitTests.Linq
- <TestFixture()> _
- Public Class SelectQueryTests
- Public Enum E
- None
- A
- B
- C
- End Enum
- Public Class C
- Public Property Id() As ObjectId
- <BsonElement("x")> _
- Public Property X() As Integer
- <BsonElement("lx")> _
- Public Property LX() As Long
- <BsonElement("y")> _
- Public Property Y() As Integer
- <BsonElement("d")> _
- Public Property D() As D
- <BsonElement("da")> _
- Public Property DA() As List(Of D)
- <BsonElement("s")> _
- <BsonIgnoreIfNull()> _
- Public Property S() As String
- <BsonElement("a")> _
- <BsonIgnoreIfNull()> _
- Public Property A() As Integer()
- <BsonElement("b")> _
- Public Property B() As Boolean
- <BsonElement("l")> _
- <BsonIgnoreIfNull()> _
- Public Property L() As List(Of Integer)
- <BsonElement("dbref")> _
- <BsonIgnoreIfNull()> _
- Public Property DBRef() As MongoDBRef
- <BsonElement("e")> _
- <BsonIgnoreIfDefault()> _
- <BsonRepresentation(BsonType.[String])> _
- Public Property E() As E
- <BsonElement("ea")> _
- <BsonIgnoreIfNull()> _
- Public Property EA() As E()
- <BsonElement("sa")> _
- <BsonIgnoreIfNull()> _
- Public Property SA() As String()
- <BsonElement("ba")> _
- <BsonIgnoreIfNull()> _
- Public Property BA() As Boolean()
- End Class
- Public Class D
- <BsonElement("z")> _
- Public Z As Integer
- ' use field instead of property to test fields also
- Public Overrides Function Equals(ByVal obj As Object) As Boolean
- If obj Is Nothing OrElse Not obj.GetType().Equals(GetType(D)) Then
- Return False
- End If
- Return Z = DirectCast(obj, D).Z
- End Function
- Public Overrides Function GetHashCode() As Integer
- Return Z.GetHashCode()
- End Function
- Public Overrides Function ToString() As String
- Return String.Format("new D {{ Z = {0} }}", Z)
- End Function
- End Class
- ' used to test some query operators that have an IEqualityComparer parameter
- Private Class CEqualityComparer
- Implements IEqualityComparer(Of C)
- Public Function Equals1(ByVal x As C, ByVal y As C) As Boolean Implements IEqualityComparer(Of C).Equals
- Return x.Id.Equals(y.Id) AndAlso x.X.Equals(y.X) AndAlso x.Y.Equals(y.Y)
- End Function
- Public Function GetHashCode1(ByVal obj As C) As Integer Implements IEqualityComparer(Of C).GetHashCode
- Return obj.GetHashCode()
- End Function
- End Class
- ' used to test some query operators that have an IEqualityComparer parameter
- Private Class Int32EqualityComparer
- Implements IEqualityComparer(Of Integer)
- Public Function Equals1(ByVal x As Integer, ByVal y As Integer) As Boolean Implements IEqualityComparer(Of Integer).Equals
- Return x = y
- End Function
- Public Function GetHashCode1(ByVal obj As Integer) As Integer Implements IEqualityComparer(Of Integer).GetHashCode
- Return obj.GetHashCode()
- End Function
- End Class
- Private _server As MongoServer
- Private _database As MongoDatabase
- Private _collection As MongoCollection(Of C)
- Private _systemProfileCollection As MongoCollection(Of SystemProfileInfo)
- Private _id1 As ObjectId = ObjectId.GenerateNewId()
- Private _id2 As ObjectId = ObjectId.GenerateNewId()
- Private _id3 As ObjectId = ObjectId.GenerateNewId()
- Private _id4 As ObjectId = ObjectId.GenerateNewId()
- Private _id5 As ObjectId = ObjectId.GenerateNewId()
- <TestFixtureSetUp()> _
- Public Sub Setup()
- _server = Configuration.TestServer
- _server.Connect()
- _database = Configuration.TestDatabase
- _collection = Configuration.GetTestCollection(Of C)()
- _systemProfileCollection = _database.GetCollection(Of SystemProfileInfo)("system.profile")
- ' documents inserted deliberately out of order to test sorting
- _collection.Drop()
- _collection.Insert(New C() With
- {
- .Id = _id2,
- .X = 2,
- .LX = 2,
- .Y = 11,
- .D = New D() With {.Z = 22},
- .A = {2, 3, 4},
- .L = New List(Of Integer)({2, 3, 4})
- })
- _collection.Insert(New C() With
- {
- .Id = _id1,
- .X = 1,
- .LX = 1,
- .Y = 11,
- .D = New D() With {.Z = 11},
- .S = "abc",
- .SA = {"Tom", "Dick", "Harry"}
- })
- _collection.Insert(New C() With
- {
- .Id = _id3,
- .X = 3,
- .LX = 3,
- .Y = 33,
- .D = New D() With {.Z = 33},
- .B = True,
- .BA = {True},
- .E = E.A,
- .EA = New E() {E.A, E.B}
- })
- _collection.Insert(New C() With
- {
- .Id = _id5,
- .X = 5,
- .LX = 5,
- .Y = 44,
- .D = New D() With {.Z = 55},
- .DBRef = New MongoDBRef("db", "c", 1)
- })
- _collection.Insert(New C() With
- {
- .Id = _id4,
- .X = 4,
- .LX = 4,
- .Y = 44,
- .D = New D() With {.Z = 44},
- .DA = {New D() With {.Z = 333}}.ToList,
- .S = " xyz "
- })
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Aggregate query operator is not supported.")> _
- Public Sub TestAggregate()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Aggregate(Function(a, b) Nothing)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Aggregate query operator is not supported.")> _
- Public Sub TestAggregateWithAccumulator()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Aggregate(0, Function(a, c) 0)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Aggregate query operator is not supported.")> _
- Public Sub TestAggregateWithAccumulatorAndSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Aggregate(0, Function(a, c) 0, Function(a) a)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The All query operator is not supported.")> _
- Public Sub TestAll()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).All(Function(c) True)
- End Sub
- <Test()> _
- Public Sub TestAny()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Any()
- Assert.IsTrue(result)
- End Sub
- <Test()> _
- Public Sub TestAnyWhereXEquals1()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 1
- Select c).Any()
- Assert.IsTrue(result)
- End Sub
- <Test()> _
- Public Sub TestAnyWhereXEquals9()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).Any()
- Assert.IsFalse(result)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="Any with predicate after a projection is not supported.")> _
- Public Sub TestAnyWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().[Select](Function(c) c.Y).Any(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestAnyWithPredicateAfterWhere()
- Dim result = _collection.AsQueryable(Of C)().Where(Function(c) c.X = 1).Any(Function(c) c.Y = 11)
- Assert.IsTrue(result)
- End Sub
- <Test()> _
- Public Sub TestAnyWithPredicateFalse()
- Dim result = _collection.AsQueryable(Of C)().Any(Function(c) c.X = 9)
- Assert.IsFalse(result)
- End Sub
- <Test()> _
- Public Sub TestAnyWithPredicateTrue()
- Dim result = _collection.AsQueryable(Of C)().Any(Function(c) c.X = 1)
- Assert.IsTrue(result)
- End Sub
- <Test()> _
- Public Sub TestAsQueryableWithNothingElse()
- Dim query = _collection.AsQueryable(Of C)()
- Dim result = query.ToList()
- Assert.AreEqual(5, result.Count)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Average query operator is not supported.")> _
- Public Sub TestAverage()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select 1.0).Average()
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Average query operator is not supported.")> _
- Public Sub TestAverageNullable()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select New Nullable(Of Decimal)(1.0)).Average()
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Average query operator is not supported.")> _
- Public Sub TestAverageWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Average(Function(c) 1.0)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Average query operator is not supported.")> _
- Public Sub TestAverageWithSelectorNullable()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Average(Function(c) New Nullable(Of Decimal)(1.0))
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Cast query operator is not supported.")> _
- Public Sub TestCast()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select C).Cast(Of C)()
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Concat query operator is not supported.")> _
- Public Sub TestConcat()
- Dim source2 = New C(-1) {}
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).Concat(source2)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Contains query operator is not supported.")> _
- Public Sub TestContains()
- Dim item = New C()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Contains(item)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Contains query operator is not supported.")> _
- Public Sub TestContainsWithEqualityComparer()
- Dim item = New C()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Contains(item, New CEqualityComparer())
- End Sub
- <Test()> _
- Public Sub TestCountEquals2()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).Count()
- Assert.AreEqual(2, result)
- End Sub
- <Test()> _
- Public Sub TestCountEquals5()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Count()
- Assert.AreEqual(5, result)
- End Sub
- <Test()> _
- Public Sub TestCountWithPredicate()
- Dim result = _collection.AsQueryable(Of C)().Count(Function(c) c.Y = 11)
- Assert.AreEqual(2, result)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="Count with predicate after a projection is not supported.")> _
- Public Sub TestCountWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().[Select](Function(c) c.Y).Count(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestCountWithPredicateAfterWhere()
- Dim result = _collection.AsQueryable(Of C)().Where(Function(c) c.X = 1).Count(Function(c) c.Y = 11)
- Assert.AreEqual(1, result)
- End Sub
- <Test()> _
- Public Sub TestCountWithSkipAndTake()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Skip(2).Take(2).Count()
- Assert.AreEqual(2, result)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The DefaultIfEmpty query operator is not supported.")> _
- Public Sub TestDefaultIfEmpty()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).DefaultIfEmpty()
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The DefaultIfEmpty query operator is not supported.")> _
- Public Sub TestDefaultIfEmptyWithDefaultValue()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).DefaultIfEmpty(Nothing)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- Public Sub TestDistinctASub0()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.A(0)).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(2))
- End Sub
- <Test()> _
- Public Sub TestDistinctB()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.B).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(2, results.Count)
- Assert.IsTrue(results.Contains(False))
- Assert.IsTrue(results.Contains(True))
- End Sub
- <Test()> _
- Public Sub TestDistinctBASub0()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.BA(0)).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(True))
- End Sub
- <Test()> _
- Public Sub TestDistinctD()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.D).Distinct()
- Dim results = query.ToList()
- ' execute query
- Assert.AreEqual(5, results.Count)
- Assert.IsTrue(results.Contains(New D() With { _
- .Z = 11 _
- }))
- Assert.IsTrue(results.Contains(New D() With { _
- .Z = 22 _
- }))
- Assert.IsTrue(results.Contains(New D() With { _
- .Z = 33 _
- }))
- Assert.IsTrue(results.Contains(New D() With { _
- .Z = 44 _
- }))
- Assert.IsTrue(results.Contains(New D() With { _
- .Z = 55 _
- }))
- End Sub
- <Test()> _
- Public Sub TestDistinctDBRef()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.DBRef).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(New MongoDBRef("db", "c", 1)))
- End Sub
- <Test()> _
- Public Sub TestDistinctDBRefDatabase()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.DBRef.DatabaseName).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains("db"))
- End Sub
- <Test()> _
- Public Sub TestDistinctDZ()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.D.Z).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(5, results.Count)
- Assert.IsTrue(results.Contains(11))
- Assert.IsTrue(results.Contains(22))
- Assert.IsTrue(results.Contains(33))
- Assert.IsTrue(results.Contains(44))
- Assert.IsTrue(results.Contains(55))
- End Sub
- <Test()> _
- Public Sub TestDistinctE()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.E).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(E.A))
- End Sub
- <Test()> _
- Public Sub TestDistinctEASub0()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.EA(0)).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(E.A))
- End Sub
- <Test()> _
- Public Sub TestDistinctId()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.Id).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(5, results.Count)
- Assert.IsTrue(results.Contains(_id1))
- Assert.IsTrue(results.Contains(_id2))
- Assert.IsTrue(results.Contains(_id3))
- Assert.IsTrue(results.Contains(_id4))
- Assert.IsTrue(results.Contains(_id5))
- End Sub
- <Test()> _
- Public Sub TestDistinctLSub0()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.L(0)).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains(2))
- End Sub
- <Test()> _
- Public Sub TestDistinctS()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.S).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(2, results.Count)
- Assert.IsTrue(results.Contains("abc"))
- Assert.IsTrue(results.Contains(" xyz "))
- End Sub
- <Test()> _
- Public Sub TestDistinctSASub0()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.SA(0)).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(1, results.Count)
- Assert.IsTrue(results.Contains("Tom"))
- End Sub
- <Test()> _
- Public Sub TestDistinctX()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.X).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(5, results.Count)
- Assert.IsTrue(results.Contains(1))
- Assert.IsTrue(results.Contains(2))
- Assert.IsTrue(results.Contains(3))
- Assert.IsTrue(results.Contains(4))
- Assert.IsTrue(results.Contains(5))
- End Sub
- <Test()> _
- Public Sub TestDistinctXWithQuery()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Where c.X > 3
- Select c.X).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(2, results.Count)
- Assert.IsTrue(results.Contains(4))
- Assert.IsTrue(results.Contains(5))
- End Sub
- <Test()> _
- Public Sub TestDistinctY()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c.Y).Distinct()
- Dim results = query.ToList()
- Assert.AreEqual(3, results.Count)
- Assert.IsTrue(results.Contains(11))
- Assert.IsTrue(results.Contains(33))
- Assert.IsTrue(results.Contains(44))
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The version of the Distinct query operator with an equality comparer is not supported.")> _
- Public Sub TestDistinctWithEqualityComparer()
- Dim query = _collection.AsQueryable(Of C)().Distinct(New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- Public Sub TestElementAtOrDefaultWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).ElementAtOrDefault(2)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestElementAtOrDefaultWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).ElementAtOrDefault(0)
- Assert.IsNull(result)
- End Sub
- <Test()> _
- Public Sub TestElementAtOrDefaultWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).ElementAtOrDefault(0)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestElementAtOrDefaultWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).ElementAtOrDefault(1)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestElementAtWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).ElementAt(2)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(InvalidOperationException))> _
- Public Sub TestElementAtWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).ElementAt(0)
- End Sub
- <Test()> _
- Public Sub TestElementAtWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).ElementAt(0)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestElementAtWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).ElementAt(1)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Except query operator is not supported.")> _
- Public Sub TestExcept()
- Dim source2 = New C(-1) {}
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).Except(source2)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Except query operator is not supported.")> _
- Public Sub TestExceptWithEqualityComparer()
- Dim source2 = New C(-1) {}
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).Except(source2, New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).FirstOrDefault()
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).FirstOrDefault()
- Assert.IsNull(result)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).FirstOrDefault()
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="FirstOrDefault with predicate after a projection is not supported.")> _
- Public Sub TestFirstOrDefaultWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().[Select](Function(c) c.Y).FirstOrDefault(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithPredicateAfterWhere()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 1
- Select c).FirstOrDefault(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithPredicateNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).FirstOrDefault(Function(c) c.X = 9)
- Assert.IsNull(result)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithPredicateOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).FirstOrDefault(Function(c) c.X = 3)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithPredicateTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).FirstOrDefault(Function(c) c.Y = 11)
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstOrDefaultWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).FirstOrDefault()
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).First()
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(InvalidOperationException))> _
- Public Sub TestFirstWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).First()
- End Sub
- <Test()> _
- Public Sub TestFirstWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).First()
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="First with predicate after a projection is not supported.")> _
- Public Sub TestFirstWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().[Select](Function(c) c.Y).First(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestFirstWithPredicateAfterWhere()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 1
- Select c).First(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstWithPredicateNoMatch()
- Dim ex = Assert.Throws(Of InvalidOperationException)(Sub()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).First(Function(c) c.X = 9)
- End Sub)
- Assert.AreEqual(ExpectedErrorMessage.FirstEmptySequence, ex.Message)
- End Sub
- <Test()> _
- Public Sub TestFirstWithPredicateOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).First(Function(c) c.X = 3)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstWithPredicateTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).First(Function(c) c.Y = 11)
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestFirstWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).First()
- Assert.AreEqual(2, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelector()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndElementSelector()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(c) c)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndElementSelectorAndEqualityComparer()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(c) c, New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndElementSelectorAndResultSelector()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(c) c, Function(c, e) 1.0)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndElementSelectorAndResultSelectorAndEqualityComparer()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(c) c, Function(c, e) e.First(), New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndEqualityComparer()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndResultSelector()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(k, e) 1.0)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupBy query operator is not supported.")> _
- Public Sub TestGroupByWithKeySelectorAndResultSelectorAndEqualityComparer()
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).GroupBy(Function(c) c, Function(k, e) e.First(), New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupJoin query operator is not supported.")> _
- Public Sub TestGroupJoin()
- Dim inner = New C(-1) {}
- Dim query = _collection.AsQueryable(Of C)().GroupJoin(inner, Function(c) c, Function(c) c, Function(c, e) c)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The GroupJoin query operator is not supported.")> _
- Public Sub TestGroupJoinWithEqualityComparer()
- Dim inner = New C(-1) {}
- Dim query = _collection.AsQueryable(Of C)().GroupJoin(inner, Function(c) c, Function(c) c, Function(c, e) c, New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Intersect query operator is not supported.")> _
- Public Sub TestIntersect()
- Dim source2 = New C(-1) {}
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).Intersect(source2)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Intersect query operator is not supported.")> _
- Public Sub TestIntersectWithEqualityComparer()
- Dim source2 = New C(-1) {}
- Dim query = (From c In _collection.AsQueryable(Of C)()
- Select c).Intersect(source2, New CEqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Join query operator is not supported.")> _
- Public Sub TestJoin()
- Dim query = _collection.AsQueryable(Of C)().Join(_collection.AsQueryable(Of C)(), Function(c) c.X, Function(c) c.X, Function(x, y) x)
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="The Join query operator is not supported.")> _
- Public Sub TestJoinWithEqualityComparer()
- Dim query = _collection.AsQueryable(Of C)().Join(_collection.AsQueryable(Of C)(), Function(c) c.X, Function(c) c.X, Function(x, y) x, New Int32EqualityComparer())
- query.ToList()
- ' execute query
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).LastOrDefault()
- Assert.AreEqual(4, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).LastOrDefault()
- Assert.IsNull(result)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).LastOrDefault()
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithOrderBy()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Order By c.X
- Select c).LastOrDefault()
- Assert.AreEqual(5, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="LastOrDefault with predicate after a projection is not supported.")> _
- Public Sub TestLastOrDefaultWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().[Select](Function(c) c.Y).LastOrDefault(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithPredicateAfterWhere()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 1
- Select c).LastOrDefault(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithPredicateNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).LastOrDefault(Function(c) c.X = 9)
- Assert.IsNull(result)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithPredicateOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).LastOrDefault(Function(c) c.X = 3)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithPredicateTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).LastOrDefault(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastOrDefaultWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).LastOrDefault()
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastWithManyMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Last()
- Assert.AreEqual(4, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(InvalidOperationException))> _
- Public Sub TestLastWithNoMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 9
- Select c).Last()
- End Sub
- <Test()> _
- Public Sub TestLastWithOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 3
- Select c).Last()
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- <ExpectedException(GetType(NotSupportedException), ExpectedMessage:="Last with predicate after a projection is not supported.")> _
- Public Sub TestLastWithPredicateAfterProjection()
- Dim result = _collection.AsQueryable(Of C)().Select(Function(c) c.Y).Last(Function(y) y = 11)
- End Sub
- <Test()> _
- Public Sub TestLastWithPredicateAfterWhere()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.X = 1
- Select c).Last(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastWithPredicateNoMatch()
- Dim ex = Assert.Throws(Of InvalidOperationException)(Sub()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Last(Function(c) c.X = 9)
- End Sub)
- Assert.AreEqual(ExpectedErrorMessage.LastEmptySequence, ex.Message)
- End Sub
- <Test()> _
- Public Sub TestLastWithPredicateOneMatch()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Last(Function(c) c.X = 3)
- Assert.AreEqual(3, result.X)
- Assert.AreEqual(33, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastWithPredicateTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Last(Function(c) c.Y = 11)
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastWithOrderBy()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Order By c.X
- Select c).Last()
- Assert.AreEqual(5, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLastWithTwoMatches()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).Last()
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestLongCountEquals2()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Where c.Y = 11
- Select c).LongCount()
- Assert.AreEqual(2L, result)
- End Sub
- <Test()> _
- Public Sub TestLongCountEquals5()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).LongCount()
- Assert.AreEqual(5L, result)
- End Sub
- <Test()> _
- Public Sub TestLongCountWithSkipAndTake()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Skip(2).Take(2).LongCount()
- Assert.AreEqual(2L, result)
- End Sub
- <Test()> _
- Public Sub TestMaxDZWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c.D.Z).Max()
- Assert.AreEqual(55, result)
- End Sub
- <Test()> _
- Public Sub TestMaxDZWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Max(Function(c) c.D.Z)
- Assert.AreEqual(55, result)
- End Sub
- <Test()> _
- Public Sub TestMaxXWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c.X).Max()
- Assert.AreEqual(5, result)
- End Sub
- <Test()> _
- Public Sub TestMaxXWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Max(Function(c) c.X)
- Assert.AreEqual(5, result)
- End Sub
- <Test()> _
- Public Sub TestMaxXYWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select New With {c.X, c.Y}).Max()
- Assert.AreEqual(5, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- Public Sub TestMaxXYWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Max(Function(c) New With
- {
- c.X,
- c.Y
- })
- Assert.AreEqual(5, result.X)
- Assert.AreEqual(44, result.Y)
- End Sub
- <Test()> _
- Public Sub TestMinDZWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c.D.Z).Min()
- Assert.AreEqual(11, result)
- End Sub
- <Test()> _
- Public Sub TestMinDZWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Min(Function(c) c.D.Z)
- Assert.AreEqual(11, result)
- End Sub
- <Test()> _
- Public Sub TestMinXWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c.X).Min()
- Assert.AreEqual(1, result)
- End Sub
- <Test()> _
- Public Sub TestMinXWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Min(Function(c) c.X)
- Assert.AreEqual(1, result)
- End Sub
- <Test()> _
- Public Sub TestMinXYWithProjection()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select New With {c.X, c.Y}).Min()
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()> _
- Public Sub TestMinXYWithSelector()
- Dim result = (From c In _collection.AsQueryable(Of C)()
- Select c).Min(Function(c) New With
- {
- c.X,
- c.Y
- })
- Assert.AreEqual(1, result.X)
- Assert.AreEqual(11, result.Y)
- End Sub
- <Test()>
- Public Sub TestOrderByValueTypeWithObjectReturnType()
- Dim orderByClause As Expression(Of Func(Of C, Object)) = Function(c) c.LX
- Dim query = _collection.AsQueryable(Of C)().OrderBy(orderByClause)
- RunTestOrderByValueTypeWithMismatchingType(query, "(C c) => (Object)c.LX")
- End Sub
- <Test()>
- Public Sub TestOrderByValueTypeWithIComparableReturnType()
- Dim orderByClause As Expression(Of Func(Of C, IComparable)) = Function(c) c.LX
- Dim query = _collection.AsQueryable(Of C)().OrderBy(orderByClause)
- RunTestOrderByValueTypeWithMismatchingType(query, "(C c) => (IComparable)c.LX")
- End Sub
- Sub RunTestOrderByValueTypeWithMismatchingType(ByVal query As IOrderedQueryable, ByVal orderByString As String)
- Dim mongoQuery = MongoQueryTranslator.Translate(query)
- Assert.IsInstanceOf(Of SelectQuery)(mongoQuery)
- Dim selectQuery As SelectQuery = mongoQuery
- Assert.AreEqual(orderByString, ExpressionFormatter.ToString(selectQuery.OrderBy(0).Key))
- End Sub
- <Test()> _
- Public Sub TestOrderByAscending(…
Large files files are truncated, but you can click here to view the full file