|
| 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.Bson.Serialization.Attributes; |
| 24 | +using MongoDB.Driver; |
| 25 | +using MongoDB.Driver.Linq; |
| 26 | + |
| 27 | +namespace MongoDB.DriverUnitTests.Linq |
| 28 | +{ |
| 29 | + [TestFixture] |
| 30 | + public class SelectNullableTests |
| 31 | + { |
| 32 | + private enum E { None, A, B }; |
| 33 | + |
| 34 | + private class C |
| 35 | + { |
| 36 | + public ObjectId Id { get; set; } |
| 37 | + [BsonElement("e")] |
| 38 | + [BsonRepresentation(BsonType.String)] |
| 39 | + public E? E { get; set; } |
| 40 | + [BsonElement("x")] |
| 41 | + public int? X { get; set; } |
| 42 | + } |
| 43 | + |
| 44 | + private MongoServer _server; |
| 45 | + private MongoDatabase _database; |
| 46 | + private MongoCollection<C> _collection; |
| 47 | + |
| 48 | + [TestFixtureSetUp] |
| 49 | + public void Setup() |
| 50 | + { |
| 51 | + _server = Configuration.TestServer; |
| 52 | + _database = Configuration.TestDatabase; |
| 53 | + _collection = Configuration.GetTestCollection<C>(); |
| 54 | + |
| 55 | + _collection.Drop(); |
| 56 | + _collection.Insert(new C { E = null }); |
| 57 | + _collection.Insert(new C { E = E.A }); |
| 58 | + _collection.Insert(new C { E = E.B }); |
| 59 | + _collection.Insert(new C { X = null }); |
| 60 | + _collection.Insert(new C { X = 1 }); |
| 61 | + _collection.Insert(new C { X = 2 }); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void TestWhereEEqualsA() |
| 66 | + { |
| 67 | + var query = from c in _collection.AsQueryable<C>() |
| 68 | + where c.E == E.A |
| 69 | + select c; |
| 70 | + |
| 71 | + var translatedQuery = MongoQueryTranslator.Translate(query); |
| 72 | + Assert.IsInstanceOf<SelectQuery>(translatedQuery); |
| 73 | + Assert.AreSame(_collection, translatedQuery.Collection); |
| 74 | + Assert.AreSame(typeof(C), translatedQuery.DocumentType); |
| 75 | + |
| 76 | + var selectQuery = (SelectQuery)translatedQuery; |
| 77 | + Assert.AreEqual("(C c) => ((Nullable<Int32>)c.E == (Nullable<Int32>)1)", ExpressionFormatter.ToString(selectQuery.Where)); |
| 78 | + Assert.IsNull(selectQuery.OrderBy); |
| 79 | + Assert.IsNull(selectQuery.Projection); |
| 80 | + Assert.IsNull(selectQuery.Skip); |
| 81 | + Assert.IsNull(selectQuery.Take); |
| 82 | + |
| 83 | + Assert.AreEqual("{ \"e\" : \"A\" }", selectQuery.BuildQuery().ToJson()); |
| 84 | + Assert.AreEqual(1, Consume(query)); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void TestWhereEEqualsNull() |
| 89 | + { |
| 90 | + var query = from c in _collection.AsQueryable<C>() |
| 91 | + where c.E == null |
| 92 | + select c; |
| 93 | + |
| 94 | + var translatedQuery = MongoQueryTranslator.Translate(query); |
| 95 | + Assert.IsInstanceOf<SelectQuery>(translatedQuery); |
| 96 | + Assert.AreSame(_collection, translatedQuery.Collection); |
| 97 | + Assert.AreSame(typeof(C), translatedQuery.DocumentType); |
| 98 | + |
| 99 | + var selectQuery = (SelectQuery)translatedQuery; |
| 100 | + Assert.AreEqual("(C c) => ((Nullable<Int32>)c.E == (Nullable<Int32>)null)", ExpressionFormatter.ToString(selectQuery.Where)); |
| 101 | + Assert.IsNull(selectQuery.OrderBy); |
| 102 | + Assert.IsNull(selectQuery.Projection); |
| 103 | + Assert.IsNull(selectQuery.Skip); |
| 104 | + Assert.IsNull(selectQuery.Take); |
| 105 | + |
| 106 | + Assert.AreEqual("{ \"e\" : null }", selectQuery.BuildQuery().ToJson()); |
| 107 | + Assert.AreEqual(4, Consume(query)); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void TestWhereXEquals1() |
| 112 | + { |
| 113 | + var query = from c in _collection.AsQueryable<C>() |
| 114 | + where c.X == 1 |
| 115 | + select c; |
| 116 | + |
| 117 | + var translatedQuery = MongoQueryTranslator.Translate(query); |
| 118 | + Assert.IsInstanceOf<SelectQuery>(translatedQuery); |
| 119 | + Assert.AreSame(_collection, translatedQuery.Collection); |
| 120 | + Assert.AreSame(typeof(C), translatedQuery.DocumentType); |
| 121 | + |
| 122 | + var selectQuery = (SelectQuery)translatedQuery; |
| 123 | + Assert.AreEqual("(C c) => (c.X == (Nullable<Int32>)1)", ExpressionFormatter.ToString(selectQuery.Where)); |
| 124 | + Assert.IsNull(selectQuery.OrderBy); |
| 125 | + Assert.IsNull(selectQuery.Projection); |
| 126 | + Assert.IsNull(selectQuery.Skip); |
| 127 | + Assert.IsNull(selectQuery.Take); |
| 128 | + |
| 129 | + Assert.AreEqual("{ \"x\" : 1 }", selectQuery.BuildQuery().ToJson()); |
| 130 | + Assert.AreEqual(1, Consume(query)); |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public void TestWhereXEqualsNull() |
| 135 | + { |
| 136 | + var query = from c in _collection.AsQueryable<C>() |
| 137 | + where c.X == null |
| 138 | + select c; |
| 139 | + |
| 140 | + var translatedQuery = MongoQueryTranslator.Translate(query); |
| 141 | + Assert.IsInstanceOf<SelectQuery>(translatedQuery); |
| 142 | + Assert.AreSame(_collection, translatedQuery.Collection); |
| 143 | + Assert.AreSame(typeof(C), translatedQuery.DocumentType); |
| 144 | + |
| 145 | + var selectQuery = (SelectQuery)translatedQuery; |
| 146 | + Assert.AreEqual("(C c) => (c.X == (Nullable<Int32>)null)", ExpressionFormatter.ToString(selectQuery.Where)); |
| 147 | + Assert.IsNull(selectQuery.OrderBy); |
| 148 | + Assert.IsNull(selectQuery.Projection); |
| 149 | + Assert.IsNull(selectQuery.Skip); |
| 150 | + Assert.IsNull(selectQuery.Take); |
| 151 | + |
| 152 | + Assert.AreEqual("{ \"x\" : null }", selectQuery.BuildQuery().ToJson()); |
| 153 | + Assert.AreEqual(4, Consume(query)); |
| 154 | + } |
| 155 | + |
| 156 | + private int Consume<T>(IQueryable<T> query) |
| 157 | + { |
| 158 | + var count = 0; |
| 159 | + foreach (var c in query) |
| 160 | + { |
| 161 | + count++; |
| 162 | + } |
| 163 | + return count; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments