public void TestWhereBTypeEqualsD() { var query = from b in _collection.AsQueryable <B>() where b.GetType() == typeof(D) select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(B b) => (b.GetType() == typeof(D))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(null, selectQuery.OfType); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"_t.0\" : { \"$exists\" : false }, \"_t\" : \"D\" }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); }
public void TestWhereEEqualsA() { if (TestEnvironment.IsMono) { // This doesn't pass on Mono 3.2.5. Not sure why, but // we are rewriting most of our linq provider, so // it's not essential to fix this yet. return; } var query = from c in _collection.AsQueryable <C>() where c.E == E.A 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.AreEqual("(C c) => ((Nullable<Int32>)c.E == (Nullable<Int32>)1)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"e\" : \"A\" }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); }
public void TestWhereBTypeEqualsC() { var query = from b in __collection.AsQueryable <B>() where b.GetType() == typeof(C) select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(B b) => (b.GetType() == typeof(C))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Equal(null, selectQuery.OfType); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t\" : { \"$size\" : 2 }, \"_t.0\" : \"B\", \"_t.1\" : \"C\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(1, Consume(query)); }
public void TestQueryAgainstInheritedField() { _collection.Drop(); _collection.Insert(new D { X = 1, Y = 2 }); var query = from d in _collection.AsQueryable <D>() where d.X == 1 select d; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(D), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(D d) => (d.X == 1)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"X\" : 1 }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, query.ToList().Count); }
public void TestWhereBIsB() { var query = from b in __collection.AsQueryable <B>() where b is B select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(B b) => (b is B)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Equal(null, selectQuery.OfType); // OfType ignored because <T> was the same as <TDocument> Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t\" : \"B\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(3, Consume(query)); }
public void TestWhereBTypeEqualsB() { if (__server.BuildInfo.Version >= new Version(2, 0, 0)) { var query = from b in __collection.AsQueryable <B>() where b.GetType() == typeof(B) select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(B b) => (b.GetType() == typeof(B))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Equal(null, selectQuery.OfType); // OfType ignored because <T> was the same as <TDocument> Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t.0\" : { \"$exists\" : false }, \"_t\" : \"B\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(1, Consume(query)); } }
public void TestWhereBIsC() { var query = from b in _collection.AsQueryable <B>() where b is C select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(B b) => (b is C)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(null, selectQuery.OfType); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"_t\" : \"C\" }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); // should match 2 but for that you need to use the hierarchical discriminator }
public void TestWhereBTypeEqualsB() { var query = from b in _collection.AsQueryable <B>() where b.GetType() == typeof(B) select b; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(B b) => (b.GetType() == typeof(B))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(null, selectQuery.OfType); // OfType ignored because <T> was the same as <TDocument> Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(3, Consume(query)); }
public void TestQueryWithProjectionHasIndexDocumentHint() { var query = __collection.AsQueryable().WithIndex(new BsonDocument("x", 1)).Select(o => o.a); var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(query); Assert.Equal(new BsonDocument("x", 1), selectQuery.IndexHint.AsBsonDocument); Assert.Equal("(B o) => o.a", ExpressionFormatter.ToString(selectQuery.Projection)); }
public void TestQueryWithProjectionHasIndexNameHint() { var query = __collection.AsQueryable().WithIndex("i").Select(o => o.a); var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(query); Assert.Equal("i", selectQuery.IndexHint.AsString); Assert.Equal("(B o) => o.a", ExpressionFormatter.ToString(selectQuery.Projection)); }
/// <inheritdoc /> public string Format(object obj, IFormatter formatter) { Expression value = (Expression)obj; ExpressionFormatter visitor = new ExpressionFormatter(formatter); visitor.Visit(value); return(visitor.ToString()); }
// public static methods /// <summary> /// Gets the serialization info for the node utilizing precalculated serialization information. /// </summary> /// <param name="node">The expression.</param> /// <param name="serializationInfoCache">The serialization info cache.</param> /// <returns>BsonSerializationInfo for the expression.</returns> public static BsonSerializationInfo GetSerializationInfo(Expression node, Dictionary <Expression, BsonSerializationInfo> serializationInfoCache) { BsonSerializationInfo serializationInfo; if (!TryGetSerializationInfo(node, serializationInfoCache, out serializationInfo)) { string message = string.Format("Unable to determine the serialization information for the expression: {0}.", ExpressionFormatter.ToString(node)); throw new NotSupportedException(message); } return(serializationInfo); }
// public static methods /// <summary> /// Gets the serialization info for the node utilizing precalculated serialization information. /// </summary> /// <param name="node">The expression.</param> /// <param name="serializationInfoCache">The serialization info cache.</param> /// <returns>BsonSerializationInfo for the expression.</returns> public static BsonSerializationInfo GetSerializationInfo(Expression node, Dictionary <Expression, BsonSerializationInfo> serializationInfoCache) { var finder = new BsonSerializationInfoFinder(serializationInfoCache); var serializationInfo = finder.Visit(node); if (serializationInfo == null) { string message = string.Format("Unable to determine the serialization information for the expression: {0}.", ExpressionFormatter.ToString(node)); throw new NotSupportedException(message); } return(serializationInfo); }
public void CastTest() { var server = MongoServer.Create(); var db = server.GetDatabase("test"); var collection = db.GetCollection <Base>("castTest"); collection.Drop(); var t1 = new T1 { Id = Guid.NewGuid(), A = "T1.A", B = "T1.B" }; var t2 = new T2 { Id = Guid.NewGuid(), A = "T2.A" }; collection.Insert(t1); collection.Insert(t2); var query = from t in collection.AsQueryable() where t is T1 && ((T1)t).B == "T1.B" select t; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(collection, translatedQuery.Collection); Assert.AreSame(typeof(Base), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(Base t) => ((t is T1) && ((T1)t.B == \"T1.B\"))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"_t\" : \"T1\", \"B\" : \"T1.B\" }", selectQuery.BuildQuery().ToString()); var results = query.ToList(); Assert.That(results.Count, Is.EqualTo(1)); Assert.That(results[0], Is.InstanceOf(typeof(T1))); Assert.That(results[0].A, Is.EqualTo("T1.A")); }
public void CastTest() { #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && BsonDefaults.GuidRepresentation != GuidRepresentation.Unspecified) { var db = LegacyTestConfiguration.Database; var collection = db.GetCollection <Base>("castTest"); collection.Drop(); var t1 = new T1 { Id = Guid.NewGuid(), A = "T1.A", B = "T1.B" }; var t2 = new T2 { Id = Guid.NewGuid(), A = "T2.A" }; collection.Insert(t1); collection.Insert(t2); var query = from t in collection.AsQueryable() where t is T1 && ((T1)t).B == "T1.B" select t; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(collection, translatedQuery.Collection); Assert.Same(typeof(Base), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(Base t) => ((t is T1) && ((T1)t.B == \"T1.B\"))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t\" : \"T1\", \"B\" : \"T1.B\" }", selectQuery.BuildQuery().ToString()); var results = query.ToList(); Assert.Equal(1, results.Count); Assert.IsType <T1>(results[0]); Assert.Equal("T1.A", results[0].A); } #pragma warning restore 618 }
public void TestWhereJContainsKeyX() { var query = from c in _collection.AsQueryable<C>() where c.J.Contains("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.AreEqual("(C c) => c.J.Contains(\"x\")", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); var ex = Assert.Throws<NotSupportedException>(() => { selectQuery.BuildQuery(); }); Assert.AreEqual("Contains in a LINQ query is only supported for DictionaryRepresentation ArrayOfDocuments or Document, not ArrayOfArrays.", ex.Message); }
public void TestWhereDContainsKeyZ() { var query = from c in _collection.AsQueryable<C>() where c.D.ContainsKey("z") 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.AreEqual("(C c) => c.D.ContainsKey(\"z\")", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"D.z\" : { \"$exists\" : true } }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(0, query.ToList().Count()); }
public void TestOfTypeD() { var query = _collection.AsQueryable <B>().OfType <D>(); var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(B x) => LinqToMongo.Inject({ \"_t\" : \"D\" })", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(typeof(D), selectQuery.OfType); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"_t\" : \"D\" }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); }
public void TestOfTypeC() { var query = __collection.AsQueryable <B>().OfType <C>(); var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(B x) => LinqToMongo.Inject({ \"_t\" : \"C\" })", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Equal(typeof(C), selectQuery.OfType); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t\" : \"C\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(1, Consume(query)); // should match 2 but for that you need to use the hierarchical discriminator }
public void TestOfTypeDWithProjection() { var query = __collection.AsQueryable <B>().OfType <D>().Select(x => new { A = x.d }); var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(B x) => LinqToMongo.Inject({ \"_t\" : \"D\" })", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Equal(typeof(D), selectQuery.OfType); Assert.Null(selectQuery.OrderBy); Assert.NotNull(selectQuery.Projection); Assert.Equal("(D x) => new __AnonymousType<Int32>(x.d)", ExpressionFormatter.ToString(selectQuery.Projection)); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"_t\" : \"D\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(1, Consume(query)); }
public void TestWhereXEqualsNull() { var query = from c in __collection.AsQueryable <C>() where c.X == null select c; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(C), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(C c) => (c.X == (Nullable<Int32>)null)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"x\" : null }", selectQuery.BuildQuery().ToJson()); Assert.Equal(4, Consume(query)); }
public void TestWhereEEqualsA() { var query = from c in _collection.AsQueryable <C>() where c.E == E.A 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.AreEqual("(C c) => ((Nullable<Int32>)c.E == (Nullable<Int32>)1)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"e\" : \"A\" }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); }
public void TestWhereIContainsKeyZ() { var query = from c in __collection.AsQueryable <C>() where c.I.Contains("z") select c; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(C), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(C c) => c.I.Contains(\"z\")", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"I.k\" : \"z\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(0, query.ToList().Count()); }
public void TestWhereEEqualsA() { RequireEnvironment.Check().EnvironmentVariable("MONO"); // Does not pass on Mono 3.2.5. Excluding for now. var query = from c in __collection.AsQueryable <C>() where c.E == E.A select c; var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsType <SelectQuery>(translatedQuery); Assert.Same(__collection, translatedQuery.Collection); Assert.Same(typeof(C), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.Equal("(C c) => ((Nullable<Int32>)c.E == (Nullable<Int32>)1)", ExpressionFormatter.ToString(selectQuery.Where)); Assert.Null(selectQuery.OrderBy); Assert.Null(selectQuery.Projection); Assert.Null(selectQuery.Skip); Assert.Null(selectQuery.Take); Assert.Equal("{ \"e\" : \"A\" }", selectQuery.BuildQuery().ToJson()); Assert.Equal(1, Consume(query)); }
public void TestWhereBGreaterThan0OfTypeCWhereCGreaterThan0() { var query = _collection.AsQueryable <B>().Where(b => b.b > 0).OfType <C>().Where(c => c.c > 0); var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(C c) => (((c.b > 0) && LinqToMongo.Inject({ \"_t\" : \"C\" })) && (c.c > 0))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(typeof(C), selectQuery.OfType); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"b\" : { \"$gt\" : 0 }, \"_t\" : \"C\", \"c\" : { \"$gt\" : 0 } }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(1, Consume(query)); // should match 2 but for that you need to use the hierarchical discriminator }
public void TestOfTypeCWhereCGreaterThan0() { var query = _collection.AsQueryable <B>().OfType <C>().Where(c => c.c > 0); var translatedQuery = MongoQueryTranslator.Translate(query); Assert.IsInstanceOf <SelectQuery>(translatedQuery); Assert.AreSame(_collection, translatedQuery.Collection); Assert.AreSame(typeof(B), translatedQuery.DocumentType); var selectQuery = (SelectQuery)translatedQuery; Assert.AreEqual("(C c) => (LinqToMongo.Inject({ \"_t\" : \"C\" }) && (c.c > 0))", ExpressionFormatter.ToString(selectQuery.Where)); Assert.AreEqual(typeof(C), selectQuery.OfType); Assert.IsNull(selectQuery.OrderBy); Assert.IsNull(selectQuery.Projection); Assert.IsNull(selectQuery.Skip); Assert.IsNull(selectQuery.Take); Assert.AreEqual("{ \"_t\" : \"C\", \"c\" : { \"$gt\" : 0 } }", selectQuery.BuildQuery().ToJson()); Assert.AreEqual(2, Consume(query)); }