/MongoDB.DriverUnitTests/Linq/SelectQueryTests.cs
C# | 7426 lines | 6215 code | 1191 blank | 20 comment | 280 complexity | 3d7df08119c880689b739bef954eecb7 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.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text.RegularExpressions;
- using MongoDB.Bson;
- using MongoDB.Bson.Serialization.Attributes;
- using MongoDB.Driver;
- using MongoDB.Driver.Builders;
- using MongoDB.Driver.Linq;
- using NUnit.Framework;
- namespace MongoDB.DriverUnitTests.Linq
- {
- [TestFixture]
- public class SelectQueryTests
- {
- public enum E
- {
- None,
- A,
- B,
- C
- }
- public class C
- {
- public ObjectId Id { get; set; }
- [BsonElement("x")]
- public int X { get; set; }
- [BsonElement("lx")]
- public long LX { get; set; }
- [BsonElement("y")]
- public int Y { get; set; }
- [BsonElement("d")]
- public D D { get; set; }
- [BsonElement("da")]
- public List<D> DA { get; set; }
- [BsonElement("s")]
- [BsonIgnoreIfNull]
- public string S { get; set; }
- [BsonElement("a")]
- [BsonIgnoreIfNull]
- public int[] A { get; set; }
- [BsonElement("b")]
- public bool B { get; set; }
- [BsonElement("l")]
- [BsonIgnoreIfNull]
- public List<int> L { get; set; }
- [BsonElement("dbref")]
- [BsonIgnoreIfNull]
- public MongoDBRef DBRef { get; set; }
- [BsonElement("e")]
- [BsonIgnoreIfDefault]
- [BsonRepresentation(BsonType.String)]
- public E E { get; set; }
- [BsonElement("ea")]
- [BsonIgnoreIfNull]
- public E[] EA { get; set; }
- [BsonElement("sa")]
- [BsonIgnoreIfNull]
- public string[] SA { get; set; }
- [BsonElement("ba")]
- [BsonIgnoreIfNull]
- public bool[] BA { get; set; }
- }
- public class D
- {
- [BsonElement("z")]
- public int Z; // use field instead of property to test fields also
- public override bool Equals(object obj)
- {
- if (obj == null || GetType() != obj.GetType()) { return false; }
- return Z == ((D)obj).Z;
- }
- public override int GetHashCode()
- {
- return Z.GetHashCode();
- }
- public override string ToString()
- {
- return string.Format("new D {{ Z = {0} }}", Z);
- }
- }
- // used to test some query operators that have an IEqualityComparer parameter
- private class CEqualityComparer : IEqualityComparer<C>
- {
- public bool Equals(C x, C y)
- {
- return x.Id.Equals(y.Id) && x.X.Equals(y.X) && x.Y.Equals(y.Y);
- }
- public int GetHashCode(C obj)
- {
- return obj.GetHashCode();
- }
- }
- // used to test some query operators that have an IEqualityComparer parameter
- private class Int32EqualityComparer : IEqualityComparer<int>
- {
- public bool Equals(int x, int y)
- {
- return x == y;
- }
- public int GetHashCode(int obj)
- {
- return obj.GetHashCode();
- }
- }
- private MongoServer _server;
- private MongoDatabase _database;
- private MongoCollection<C> _collection;
- private MongoCollection<SystemProfileInfo> _systemProfileCollection;
- private ObjectId _id1 = ObjectId.GenerateNewId();
- private ObjectId _id2 = ObjectId.GenerateNewId();
- private ObjectId _id3 = ObjectId.GenerateNewId();
- private ObjectId _id4 = ObjectId.GenerateNewId();
- private ObjectId _id5 = ObjectId.GenerateNewId();
- [TestFixtureSetUp]
- public void Setup()
- {
- _server = Configuration.TestServer;
- _server.Connect();
- _database = Configuration.TestDatabase;
- _collection = Configuration.GetTestCollection<C>();
- _systemProfileCollection = _database.GetCollection<SystemProfileInfo>("system.profile");
- // documents inserted deliberately out of order to test sorting
- _collection.Drop();
- _collection.Insert(new C { Id = _id2, X = 2, LX = 2, Y = 11, D = new D { Z = 22 }, A = new[] { 2, 3, 4 }, DA = new List<D> { new D { Z = 111 }, new D { Z = 222 } }, L = new List<int> { 2, 3, 4 } });
- _collection.Insert(new C { Id = _id1, X = 1, LX = 1, Y = 11, D = new D { Z = 11 }, S = "abc", SA = new string[] { "Tom", "Dick", "Harry" } });
- _collection.Insert(new C { Id = _id3, X = 3, LX = 3, Y = 33, D = new D { Z = 33 }, B = true, BA = new bool[] { true }, E = E.A, EA = new E[] { E.A, E.B } });
- _collection.Insert(new C { Id = _id5, X = 5, LX = 5, Y = 44, D = new D { Z = 55 }, DBRef = new MongoDBRef("db", "c", 1) });
- _collection.Insert(new C { Id = _id4, X = 4, LX = 4, Y = 44, D = new D { Z = 44 }, S = " xyz ", DA = new List<D> { new D { Z = 333 }, new D { Z = 444 } } });
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Aggregate query operator is not supported.")]
- public void TestAggregate()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Aggregate((a, b) => null);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Aggregate query operator is not supported.")]
- public void TestAggregateWithAccumulator()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Aggregate<C, int>(0, (a, c) => 0);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Aggregate query operator is not supported.")]
- public void TestAggregateWithAccumulatorAndSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Aggregate<C, int, int>(0, (a, c) => 0, a => a);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The All query operator is not supported.")]
- public void TestAll()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).All(c => true);
- }
- [Test]
- public void TestAny()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Any();
- Assert.IsTrue(result);
- }
- [Test]
- public void TestAnyWhereXEquals1()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 1
- select c).Any();
- Assert.IsTrue(result);
- }
- [Test]
- public void TestAnyWhereXEquals9()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).Any();
- Assert.IsFalse(result);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Any with predicate after a projection is not supported.")]
- public void TestAnyWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).Any(y => y == 11);
- }
- [Test]
- public void TestAnyWithPredicateAfterWhere()
- {
- var result = _collection.AsQueryable<C>().Where(c => c.X == 1).Any(c => c.Y == 11);
- Assert.IsTrue(result);
- }
- [Test]
- public void TestAnyWithPredicateFalse()
- {
- var result = _collection.AsQueryable<C>().Any(c => c.X == 9);
- Assert.IsFalse(result);
- }
- [Test]
- public void TestAnyWithPredicateTrue()
- {
- var result = _collection.AsQueryable<C>().Any(c => c.X == 1);
- Assert.IsTrue(result);
- }
- [Test]
- public void TestAsQueryableWithNothingElse()
- {
- var query = _collection.AsQueryable<C>();
- var result = query.ToList();
- Assert.AreEqual(5, result.Count);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Average query operator is not supported.")]
- public void TestAverage()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select 1.0).Average();
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Average query operator is not supported.")]
- public void TestAverageNullable()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select (double?)1.0).Average();
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Average query operator is not supported.")]
- public void TestAverageWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Average(c => 1.0);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Average query operator is not supported.")]
- public void TestAverageWithSelectorNullable()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Average(c => (double?)1.0);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Cast query operator is not supported.")]
- public void TestCast()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).Cast<C>();
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Concat query operator is not supported.")]
- public void TestConcat()
- {
- var source2 = new C[0];
- var query = (from c in _collection.AsQueryable<C>()
- select c).Concat(source2);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Contains query operator is not supported.")]
- public void TestContains()
- {
- var item = new C();
- var result = (from c in _collection.AsQueryable<C>()
- select c).Contains(item);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Contains query operator is not supported.")]
- public void TestContainsWithEqualityComparer()
- {
- var item = new C();
- var result = (from c in _collection.AsQueryable<C>()
- select c).Contains(item, new CEqualityComparer());
- }
- [Test]
- public void TestCountEquals2()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).Count();
- Assert.AreEqual(2, result);
- }
- [Test]
- public void TestCountEquals5()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Count();
- Assert.AreEqual(5, result);
- }
- [Test]
- public void TestCountWithPredicate()
- {
- var result = _collection.AsQueryable<C>().Count(c => c.Y == 11);
- Assert.AreEqual(2, result);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Count with predicate after a projection is not supported.")]
- public void TestCountWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).Count(y => y == 11);
- }
- [Test]
- public void TestCountWithPredicateAfterWhere()
- {
- var result = _collection.AsQueryable<C>().Where(c => c.X == 1).Count(c => c.Y == 11);
- Assert.AreEqual(1, result);
- }
- [Test]
- public void TestCountWithSkipAndTake()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Skip(2).Take(2).Count();
- Assert.AreEqual(2, result);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The DefaultIfEmpty query operator is not supported.")]
- public void TestDefaultIfEmpty()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).DefaultIfEmpty();
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The DefaultIfEmpty query operator is not supported.")]
- public void TestDefaultIfEmptyWithDefaultValue()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).DefaultIfEmpty(null);
- query.ToList(); // execute query
- }
- [Test]
- public void TestDistinctASub0()
- {
- if (_server.BuildInfo.Version >= new Version(1, 8, 0))
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.A[0]).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(2));
- }
- }
- [Test]
- public void TestDistinctB()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.B).Distinct();
- var results = query.ToList();
- Assert.AreEqual(2, results.Count);
- Assert.IsTrue(results.Contains(false));
- Assert.IsTrue(results.Contains(true));
- }
- [Test]
- public void TestDistinctBASub0()
- {
- if (_server.BuildInfo.Version >= new Version(1, 8, 0))
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.BA[0]).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(true));
- }
- }
- [Test]
- public void TestDistinctD()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.D).Distinct();
- var results = query.ToList(); // execute query
- Assert.AreEqual(5, results.Count);
- Assert.IsTrue(results.Contains(new D { Z = 11 }));
- Assert.IsTrue(results.Contains(new D { Z = 22 }));
- Assert.IsTrue(results.Contains(new D { Z = 33 }));
- Assert.IsTrue(results.Contains(new D { Z = 44 }));
- Assert.IsTrue(results.Contains(new D { Z = 55 }));
- }
- [Test]
- public void TestDistinctDBRef()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.DBRef).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(new MongoDBRef("db", "c", 1)));
- }
- [Test]
- public void TestDistinctDBRefDatabase()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.DBRef.DatabaseName).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains("db"));
- }
- [Test]
- public void TestDistinctDZ()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.D.Z).Distinct();
- var 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));
- }
- [Test]
- public void TestDistinctE()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.E).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(E.A));
- }
- [Test]
- public void TestDistinctEASub0()
- {
- if (_server.BuildInfo.Version >= new Version(1, 8, 0))
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.EA[0]).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(E.A));
- }
- }
- [Test]
- public void TestDistinctId()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.Id).Distinct();
- var 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));
- }
- [Test]
- public void TestDistinctLSub0()
- {
- if (_server.BuildInfo.Version >= new Version(1, 8, 0))
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.L[0]).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains(2));
- }
- }
- [Test]
- public void TestDistinctS()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.S).Distinct();
- var results = query.ToList();
- Assert.AreEqual(2, results.Count);
- Assert.IsTrue(results.Contains("abc"));
- Assert.IsTrue(results.Contains(" xyz "));
- }
- [Test]
- public void TestDistinctSASub0()
- {
- if (_server.BuildInfo.Version >= new Version(1, 8, 0))
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.SA[0]).Distinct();
- var results = query.ToList();
- Assert.AreEqual(1, results.Count);
- Assert.IsTrue(results.Contains("Tom"));
- }
- }
- [Test]
- public void TestDistinctX()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.X).Distinct();
- var 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));
- }
- [Test]
- public void TestDistinctXWithQuery()
- {
- var query = (from c in _collection.AsQueryable<C>()
- where c.X >3
- select c.X).Distinct();
- var results = query.ToList();
- Assert.AreEqual(2, results.Count);
- Assert.IsTrue(results.Contains(4));
- Assert.IsTrue(results.Contains(5));
- }
- [Test]
- public void TestDistinctY()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c.Y).Distinct();
- var results = query.ToList();
- Assert.AreEqual(3, results.Count);
- Assert.IsTrue(results.Contains(11));
- Assert.IsTrue(results.Contains(33));
- Assert.IsTrue(results.Contains(44));
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The version of the Distinct query operator with an equality comparer is not supported.")]
- public void TestDistinctWithEqualityComparer()
- {
- var query = _collection.AsQueryable<C>().Distinct(new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- public void TestElementAtOrDefaultWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).ElementAtOrDefault(2);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestElementAtOrDefaultWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).ElementAtOrDefault(0);
- Assert.IsNull(result);
- }
- [Test]
- public void TestElementAtOrDefaultWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).ElementAtOrDefault(0);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestElementAtOrDefaultWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).ElementAtOrDefault(1);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestElementAtWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).ElementAt(2);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- [ExpectedException(typeof(InvalidOperationException))]
- public void TestElementAtWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).ElementAt(0);
- }
- [Test]
- public void TestElementAtWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).ElementAt(0);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestElementAtWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).ElementAt(1);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Except query operator is not supported.")]
- public void TestExcept()
- {
- var source2 = new C[0];
- var query = (from c in _collection.AsQueryable<C>()
- select c).Except(source2);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Except query operator is not supported.")]
- public void TestExceptWithEqualityComparer()
- {
- var source2 = new C[0];
- var query = (from c in _collection.AsQueryable<C>()
- select c).Except(source2, new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- public void TestFirstOrDefaultWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).FirstOrDefault();
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstOrDefaultWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).FirstOrDefault();
- Assert.IsNull(result);
- }
- [Test]
- public void TestFirstOrDefaultWithNoMatchAndProjectionToStruct()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c.X).FirstOrDefault();
- Assert.AreEqual(0, result);
- }
- [Test]
- public void TestFirstOrDefaultWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).FirstOrDefault();
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "FirstOrDefault with predicate after a projection is not supported.")]
- public void TestFirstOrDefaultWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).FirstOrDefault(y => y == 11);
- }
- [Test]
- public void TestFirstOrDefaultWithPredicateAfterWhere()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 1
- select c).FirstOrDefault(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstOrDefaultWithPredicateNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).FirstOrDefault(c => c.X == 9);
- Assert.IsNull(result);
- }
- [Test]
- public void TestFirstOrDefaultWithPredicateOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).FirstOrDefault(c => c.X == 3);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestFirstOrDefaultWithPredicateTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).FirstOrDefault(c => c.Y == 11);
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstOrDefaultWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).FirstOrDefault();
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).First();
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- [ExpectedException(typeof(InvalidOperationException))]
- public void TestFirstWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).First();
- }
- [Test]
- public void TestFirstWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).First();
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "First with predicate after a projection is not supported.")]
- public void TestFirstWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).First(y => y == 11);
- }
- [Test]
- public void TestFirstWithPredicateAfterWhere()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 1
- select c).First(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstWithPredicateNoMatch()
- {
- var ex = Assert.Throws<InvalidOperationException>(() =>
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).First(c => c.X == 9);
- });
- Assert.AreEqual(ExpectedErrorMessage.FirstEmptySequence, ex.Message);
- }
- [Test]
- public void TestFirstWithPredicateOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).First(c => c.X == 3);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestFirstWithPredicateTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).First(c => c.Y == 11);
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestFirstWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).First();
- Assert.AreEqual(2, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelector()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndElementSelector()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, c => c);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndElementSelectorAndEqualityComparer()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, c => c, new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndElementSelectorAndResultSelector()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, c => c, (c, e) => 1.0);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndElementSelectorAndResultSelectorAndEqualityComparer()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, c => c, (c, e) => e.First(), new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndEqualityComparer()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndResultSelector()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, (k, e) => 1.0);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupBy query operator is not supported.")]
- public void TestGroupByWithKeySelectorAndResultSelectorAndEqualityComparer()
- {
- var query = (from c in _collection.AsQueryable<C>()
- select c).GroupBy(c => c, (k, e) => e.First(), new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupJoin query operator is not supported.")]
- public void TestGroupJoin()
- {
- var inner = new C[0];
- var query = _collection.AsQueryable<C>().GroupJoin(inner, c => c, c => c, (c, e) => c);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The GroupJoin query operator is not supported.")]
- public void TestGroupJoinWithEqualityComparer()
- {
- var inner = new C[0];
- var query = _collection.AsQueryable<C>().GroupJoin(inner, c => c, c => c, (c, e) => c, new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Intersect query operator is not supported.")]
- public void TestIntersect()
- {
- var source2 = new C[0];
- var query = (from c in _collection.AsQueryable<C>()
- select c).Intersect(source2);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Intersect query operator is not supported.")]
- public void TestIntersectWithEqualityComparer()
- {
- var source2 = new C[0];
- var query = (from c in _collection.AsQueryable<C>()
- select c).Intersect(source2, new CEqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Join query operator is not supported.")]
- public void TestJoin()
- {
- var query = _collection.AsQueryable<C>().Join(_collection.AsQueryable<C>(), c => c.X, c => c.X, (x, y) => x);
- query.ToList(); // execute query
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The Join query operator is not supported.")]
- public void TestJoinWithEqualityComparer()
- {
- var query = _collection.AsQueryable<C>().Join(_collection.AsQueryable<C>(), c => c.X, c => c.X, (x, y) => x, new Int32EqualityComparer());
- query.ToList(); // execute query
- }
- [Test]
- public void TestLastOrDefaultWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).LastOrDefault();
- Assert.AreEqual(4, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- public void TestLastOrDefaultWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).LastOrDefault();
- Assert.IsNull(result);
- }
- [Test]
- public void TestLastOrDefaultWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).LastOrDefault();
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestLastOrDefaultWithOrderBy()
- {
- var result = (from c in _collection.AsQueryable<C>()
- orderby c.X
- select c).LastOrDefault();
- Assert.AreEqual(5, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "LastOrDefault with predicate after a projection is not supported.")]
- public void TestLastOrDefaultWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).LastOrDefault(y => y == 11);
- }
- [Test]
- public void TestLastOrDefaultWithPredicateAfterWhere()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 1
- select c).LastOrDefault(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLastOrDefaultWithPredicateNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).LastOrDefault(c => c.X == 9);
- Assert.IsNull(result);
- }
- [Test]
- public void TestLastOrDefaultWithPredicateOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).LastOrDefault(c => c.X == 3);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestLastOrDefaultWithPredicateTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).LastOrDefault(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLastOrDefaultWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).LastOrDefault();
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLastWithManyMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Last();
- Assert.AreEqual(4, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- [ExpectedException(typeof(InvalidOperationException))]
- public void TestLastWithNoMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 9
- select c).Last();
- }
- [Test]
- public void TestLastWithOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 3
- select c).Last();
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Last with predicate after a projection is not supported.")]
- public void TestLastWithPredicateAfterProjection()
- {
- var result = _collection.AsQueryable<C>().Select(c => c.Y).Last(y => y == 11);
- }
- [Test]
- public void TestLastWithPredicateAfterWhere()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.X == 1
- select c).Last(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLastWithPredicateNoMatch()
- {
- var ex = Assert.Throws<InvalidOperationException>(() =>
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Last(c => c.X == 9);
- });
- Assert.AreEqual(ExpectedErrorMessage.LastEmptySequence, ex.Message);
- }
- [Test]
- public void TestLastWithPredicateOneMatch()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Last(c => c.X == 3);
- Assert.AreEqual(3, result.X);
- Assert.AreEqual(33, result.Y);
- }
- [Test]
- public void TestLastWithPredicateTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Last(c => c.Y == 11);
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLastWithOrderBy()
- {
- var result = (from c in _collection.AsQueryable<C>()
- orderby c.X
- select c).Last();
- Assert.AreEqual(5, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- public void TestLastWithTwoMatches()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).Last();
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestLongCountEquals2()
- {
- var result = (from c in _collection.AsQueryable<C>()
- where c.Y == 11
- select c).LongCount();
- Assert.AreEqual(2L, result);
- }
- [Test]
- public void TestLongCountEquals5()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).LongCount();
- Assert.AreEqual(5L, result);
- }
- [Test]
- public void TestLongCountWithSkipAndTake()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Skip(2).Take(2).LongCount();
- Assert.AreEqual(2L, result);
- }
- [Test]
- public void TestMaxDZWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.D.Z).Max();
- Assert.AreEqual(55, result);
- }
- [Test]
- public void TestMaxDZWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Max(c => c.D.Z);
- Assert.AreEqual(55, result);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Max must be used with either Select or a selector argument, but not both.")]
- public void TestMaxWithProjectionAndSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.D).Max(d => d.Z);
- }
- [Test]
- public void TestMaxXWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.X).Max();
- Assert.AreEqual(5, result);
- }
- [Test]
- public void TestMaxXWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Max(c => c.X);
- Assert.AreEqual(5, result);
- }
- [Test]
- public void TestMaxXYWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select new { c.X, c.Y }).Max();
- Assert.AreEqual(5, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- public void TestMaxXYWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Max(c => new { c.X, c.Y });
- Assert.AreEqual(5, result.X);
- Assert.AreEqual(44, result.Y);
- }
- [Test]
- public void TestMinDZWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.D.Z).Min();
- Assert.AreEqual(11, result);
- }
- [Test]
- public void TestMinDZWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Min(c => c.D.Z);
- Assert.AreEqual(11, result);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Min must be used with either Select or a selector argument, but not both.")]
- public void TestMinWithProjectionAndSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.D).Min(d => d.Z);
- }
- [Test]
- public void TestMinXWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c.X).Min();
- Assert.AreEqual(1, result);
- }
- [Test]
- public void TestMinXWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Min(c => c.X);
- Assert.AreEqual(1, result);
- }
- [Test]
- public void TestMinXYWithProjection()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select new { c.X, c.Y }).Min();
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestMinXYWithSelector()
- {
- var result = (from c in _collection.AsQueryable<C>()
- select c).Min(c => new { c.X, c.Y });
- Assert.AreEqual(1, result.X);
- Assert.AreEqual(11, result.Y);
- }
- [Test]
- public void TestOrderByValueTypeWithObjectReturnType()
- {
- Expression<Func<C, object>> orderByClause = c => c.LX;
- var query = _collection.AsQueryable<C>().OrderBy(orderByClause);
- RunTestOrderByValueTypeWithMismatchingType(query, "(C c) => (Object)c.LX");
- }
- [Test]
- public void TestOrderByValueTypeWithIComparableReturnType()
- {
- Expression<Func<C, IComparable>> orderByClause = c => c.LX;
- var query = _collection.AsQueryable<C>().OrderBy(orderByClause);
- RunTestOrderByValueTypeWithMismatchingType(query, "(C c) => (IComparable)c.LX");
- }
- private void RunTestOrderByValueTypeWithMismatchingType(IOrderedQueryable query, string orderByString)
- {
- var mongoQuery = MongoQueryTranslator.Translate(query);
- Assert.IsInstanceOf<SelectQuery>(mongoQuery);
- var selectQuery = (SelectQuery) mongoQuery;
- Assert.AreEqual(orderByString, ExpressionFormatter.ToString(selectQuery.OrderBy[0].Key));
- }
- [Test]
- public void TestOrderByAscending()
- {
- var query = from c in _collection.AsQueryable<C>()
- orderby c.X
- select c;
- var translatedQuery = MongoQueryTranslator.Translate(query);
- Assert.IsInstanceOf<SelectQuery>(translatedQuery);
- Assert.AreSame(_collection, translatedQuery.Collection);
- Assert.AreSame(typeof(C), translatedQuery.DocumentType);
- var selectQuery = (SelectQuery)translatedQuery;
- Assert.IsNull(selectQuery.Where);
- Assert.AreEqual(1, selectQuery.OrderBy.Count);
- Assert.AreEqual("(C c) => c.X", ExpressionFormatter.ToString(selectQuery.OrderBy[0].Key));
- Assert.AreEqual(OrderByDirection.Ascending, selectQuery.OrderBy[0].Direction);
- Assert.IsNull(selectQuery.Projection);
- Assert.IsNull(selectQuery.Skip);
- Assert.IsNull(selectQuery.Take);
- Assert.IsNull(selectQuery.BuildQuery());
- var results = query.ToList();
- Assert.AreEqual(5, results.Count);
- Assert.AreEqual(1, results.First().X);
- Assert.AreEqual(5, results.Last().X);
- }
- [Test]
- public void TestOrderByAscendingThenByAscending()
- {
- var query = from…
Large files files are truncated, but you can click here to view the full file