|
| 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.Linq; |
| 19 | +using System.Text; |
| 20 | +using NUnit.Framework; |
| 21 | + |
| 22 | +using MongoDB.Bson; |
| 23 | +using MongoDB.Driver; |
| 24 | +using MongoDB.Driver.Linq; |
| 25 | + |
| 26 | +namespace MongoDB.DriverOnlineTests.Jira.CSharp418 |
| 27 | +{ |
| 28 | + [TestFixture] |
| 29 | + public class CSharp418Tests |
| 30 | + { |
| 31 | + public class C |
| 32 | + { |
| 33 | + public ObjectId Id; |
| 34 | + public int X; |
| 35 | + } |
| 36 | + |
| 37 | + public class D : C |
| 38 | + { |
| 39 | + public int Y; |
| 40 | + } |
| 41 | + |
| 42 | + private MongoServer _server; |
| 43 | + private MongoDatabase _database; |
| 44 | + private MongoCollection<D> _collection; |
| 45 | + |
| 46 | + [TestFixtureSetUp] |
| 47 | + public void TestFixtureSetup() |
| 48 | + { |
| 49 | + _server = Configuration.TestServer; |
| 50 | + _database = Configuration.TestDatabase; |
| 51 | + _collection = Configuration.GetTestCollection<D>(); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void TestQueryAgainstInheritedField() |
| 56 | + { |
| 57 | + _collection.Drop(); |
| 58 | + _collection.Insert(new D { X = 1, Y = 2 }); |
| 59 | + |
| 60 | + var query = from d in _collection.AsQueryable<D>() |
| 61 | + where d.X == 1 |
| 62 | + select d; |
| 63 | + |
| 64 | + var translatedQuery = MongoQueryTranslator.Translate(query); |
| 65 | + Assert.IsInstanceOf<SelectQuery>(translatedQuery); |
| 66 | + Assert.AreSame(_collection, translatedQuery.Collection); |
| 67 | + Assert.AreSame(typeof(D), translatedQuery.DocumentType); |
| 68 | + |
| 69 | + var selectQuery = (SelectQuery)translatedQuery; |
| 70 | + Assert.AreEqual("(D d) => (d.X == 1)", ExpressionFormatter.ToString(selectQuery.Where)); |
| 71 | + Assert.IsNull(selectQuery.OrderBy); |
| 72 | + Assert.IsNull(selectQuery.Projection); |
| 73 | + Assert.IsNull(selectQuery.Skip); |
| 74 | + Assert.IsNull(selectQuery.Take); |
| 75 | + |
| 76 | + Assert.AreEqual("{ \"X\" : 1 }", selectQuery.BuildQuery().ToJson()); |
| 77 | + Assert.AreEqual(1, query.ToList().Count); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments