|
| 1 | +/* Copyright 2010-present MongoDB 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 FluentAssertions; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.Serialization; |
| 22 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation; |
| 24 | +using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators; |
| 25 | +using Xunit.Abstractions; |
| 26 | + |
| 27 | +namespace MongoDB.Driver.Tests |
| 28 | +{ |
| 29 | + public abstract class LinqIntegrationTest<TFixture> : IntegrationTest<TFixture> |
| 30 | + where TFixture : MongoDatabaseFixture |
| 31 | + { |
| 32 | + public LinqIntegrationTest(TFixture fixture, Action<RequireServer> requireServerCheck = null) |
| 33 | + : base(fixture, requireServerCheck) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + protected void AssertStages(IEnumerable<BsonDocument> stages, params string[] expectedStages) |
| 38 | + { |
| 39 | + AssertStages(stages, (IEnumerable<string>)expectedStages); |
| 40 | + } |
| 41 | + |
| 42 | + protected void AssertStages(IEnumerable<BsonDocument> stages, IEnumerable<string> expectedStages) |
| 43 | + { |
| 44 | + stages.Should().Equal(expectedStages.Select(json => BsonDocument.Parse(json))); |
| 45 | + } |
| 46 | + |
| 47 | + protected static List<BsonDocument> Translate<TDocument, TResult>(IMongoCollection<TDocument> collection, IAggregateFluent<TResult> aggregate) => |
| 48 | + Translate(collection, aggregate, out _); |
| 49 | + |
| 50 | + protected static List<BsonDocument> Translate<TDocument, TResult>(IMongoCollection<TDocument> collection, IAggregateFluent<TResult> aggregate, out IBsonSerializer<TResult> outputSerializer) |
| 51 | + { |
| 52 | + var pipelineDefinition = ((AggregateFluent<TDocument, TResult>)aggregate).Pipeline; |
| 53 | + var documentSerializer = collection.DocumentSerializer; |
| 54 | + var translationOptions = aggregate.Options?.TranslationOptions.AddMissingOptionsFrom(collection.Database.Client.Settings.TranslationOptions); |
| 55 | + return Translate(pipelineDefinition, documentSerializer, translationOptions, out outputSerializer); |
| 56 | + } |
| 57 | + |
| 58 | + // in this overload the collection argument is used only to infer the TDocument type |
| 59 | + protected List<BsonDocument> Translate<TDocument, TResult>(IMongoCollection<TDocument> collection, IQueryable<TResult> queryable) |
| 60 | + { |
| 61 | + return Translate<TDocument, TResult>(queryable); |
| 62 | + } |
| 63 | + |
| 64 | + // in this overload the collection argument is used only to infer the TDocument type |
| 65 | + protected List<BsonDocument> Translate<TDocument, TResult>(IMongoCollection<TDocument> collection, IQueryable<TResult> queryable, out IBsonSerializer<TResult> outputSerializer) |
| 66 | + { |
| 67 | + return Translate<TDocument, TResult>(queryable, out outputSerializer); |
| 68 | + } |
| 69 | + |
| 70 | + protected static List<BsonDocument> Translate<TResult>(IMongoDatabase database, IAggregateFluent<TResult> aggregate) |
| 71 | + { |
| 72 | + var pipelineDefinition = ((AggregateFluent<NoPipelineInput, TResult>)aggregate).Pipeline; |
| 73 | + var translationOptions = aggregate.Options?.TranslationOptions.AddMissingOptionsFrom(database.Client.Settings.TranslationOptions); |
| 74 | + return Translate(pipelineDefinition, NoPipelineInputSerializer.Instance, translationOptions); |
| 75 | + } |
| 76 | + |
| 77 | + // in this overload the database argument is used only to infer the NoPipelineInput type |
| 78 | + protected List<BsonDocument> Translate<TResult>(IMongoDatabase database, IQueryable<TResult> queryable) |
| 79 | + { |
| 80 | + return Translate<NoPipelineInput, TResult>(queryable); |
| 81 | + } |
| 82 | + |
| 83 | + protected List<BsonDocument> Translate<TDocument, TResult>(IQueryable<TResult> queryable) |
| 84 | + { |
| 85 | + return Translate<TDocument, TResult>(queryable, out _); |
| 86 | + } |
| 87 | + |
| 88 | + protected List<BsonDocument> Translate<TDocument, TResult>(IQueryable<TResult> queryable, out IBsonSerializer<TResult> outputSerializer) |
| 89 | + { |
| 90 | + var provider = (MongoQueryProvider<TDocument>)queryable.Provider; |
| 91 | + var translationOptions = provider.GetTranslationOptions(); |
| 92 | + var executableQuery = ExpressionToExecutableQueryTranslator.Translate<TDocument, TResult>(provider, queryable.Expression, translationOptions); |
| 93 | + var stages = executableQuery.Pipeline.Ast.Stages; |
| 94 | + outputSerializer = (IBsonSerializer<TResult>)executableQuery.Pipeline.OutputSerializer; |
| 95 | + return stages.Select(s => s.Render().AsBsonDocument).ToList(); |
| 96 | + } |
| 97 | + |
| 98 | + protected static List<BsonDocument> Translate<TDocument, TResult>( |
| 99 | + PipelineDefinition<TDocument, TResult> pipelineDefinition, |
| 100 | + IBsonSerializer<TDocument> documentSerializer, |
| 101 | + ExpressionTranslationOptions translationOptions) => |
| 102 | + Translate(pipelineDefinition, documentSerializer, translationOptions, out _); |
| 103 | + |
| 104 | + protected static List<BsonDocument> Translate<TDocument, TResult>( |
| 105 | + PipelineDefinition<TDocument, TResult> pipelineDefinition, |
| 106 | + IBsonSerializer<TDocument> documentSerializer, |
| 107 | + ExpressionTranslationOptions translationOptions, |
| 108 | + out IBsonSerializer<TResult> outputSerializer) |
| 109 | + { |
| 110 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 111 | + documentSerializer ??= serializerRegistry.GetSerializer<TDocument>(); |
| 112 | + var renderedPipeline = pipelineDefinition.Render(new(documentSerializer, serializerRegistry, translationOptions: translationOptions)); |
| 113 | + outputSerializer = renderedPipeline.OutputSerializer; |
| 114 | + return renderedPipeline.Documents.ToList(); |
| 115 | + } |
| 116 | + |
| 117 | + protected BsonDocument Translate<TDocument>(IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filterDefinition) |
| 118 | + { |
| 119 | + var documentSerializer = collection.DocumentSerializer; |
| 120 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 121 | + var translationOptions = collection.Database.Client.Settings.TranslationOptions; |
| 122 | + return filterDefinition.Render(new(documentSerializer, serializerRegistry, translationOptions: translationOptions)); |
| 123 | + } |
| 124 | + |
| 125 | + protected BsonDocument TranslateFilter<TDocument>(IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter) |
| 126 | + { |
| 127 | + var documentSerializer = collection.DocumentSerializer; |
| 128 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 129 | + var translationOptions = collection.Database.Client.Settings.TranslationOptions; |
| 130 | + return filter.Render(new(documentSerializer, serializerRegistry, translationOptions: translationOptions)); |
| 131 | + } |
| 132 | + |
| 133 | + protected BsonDocument TranslateFindFilter<TDocument, TProjection>(IMongoCollection<TDocument> collection, IFindFluent<TDocument, TProjection> find) |
| 134 | + { |
| 135 | + var filterDefinition = ((FindFluent<TDocument, TProjection>)find).Filter; |
| 136 | + var translationOptions = collection.Database.Client.Settings.TranslationOptions; |
| 137 | + return filterDefinition.Render(new(collection.DocumentSerializer, BsonSerializer.SerializerRegistry, translationOptions: translationOptions)); |
| 138 | + } |
| 139 | + |
| 140 | + protected BsonDocument TranslateFindProjection<TDocument, TProjection>( |
| 141 | + IMongoCollection<TDocument> collection, |
| 142 | + IFindFluent<TDocument, TProjection> find) => |
| 143 | + TranslateFindProjection(collection, find, out _); |
| 144 | + |
| 145 | + protected BsonDocument TranslateFindProjection<TDocument, TProjection>( |
| 146 | + IMongoCollection<TDocument> collection, |
| 147 | + IFindFluent<TDocument, TProjection> find, |
| 148 | + out IBsonSerializer<TProjection> projectionSerializer) |
| 149 | + { |
| 150 | + var projection = ((FindFluent<TDocument, TProjection>)find).Options.Projection; |
| 151 | + var translationOptions = find.Options?.TranslationOptions.AddMissingOptionsFrom(collection.Database.Client.Settings.TranslationOptions); |
| 152 | + return TranslateFindProjection(collection, projection, translationOptions, out projectionSerializer); |
| 153 | + } |
| 154 | + |
| 155 | + protected BsonDocument TranslateFindProjection<TDocument, TProjection>( |
| 156 | + IMongoCollection<TDocument> collection, |
| 157 | + ProjectionDefinition<TDocument, TProjection> projection, |
| 158 | + ExpressionTranslationOptions translationOptions) => |
| 159 | + TranslateFindProjection(collection, projection, translationOptions, out _); |
| 160 | + |
| 161 | + protected BsonDocument TranslateFindProjection<TDocument, TProjection>( |
| 162 | + IMongoCollection<TDocument> collection, |
| 163 | + ProjectionDefinition<TDocument, TProjection> projection, |
| 164 | + ExpressionTranslationOptions translationOptions, |
| 165 | + out IBsonSerializer<TProjection> projectionSerializer) |
| 166 | + { |
| 167 | + var documentSerializer = collection.DocumentSerializer; |
| 168 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 169 | + var renderedProjection = projection.Render(new(documentSerializer, serializerRegistry, translationOptions: translationOptions, renderForFind: true)); |
| 170 | + projectionSerializer = renderedProjection.ProjectionSerializer; |
| 171 | + return renderedProjection.Document; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments