|
| 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.Collections.Generic; |
| 17 | +using MongoDB.Bson; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Bson.Serialization.Serializers; |
| 20 | +using MongoDB.Driver.Core.Clusters; |
| 21 | +using MongoDB.Driver.Core.Misc; |
| 22 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 23 | +using MongoDB.Driver.Linq; |
| 24 | +using MongoDB.Driver.Tests.Linq.Linq3ImplementationTests; |
| 25 | +using Xunit; |
| 26 | + |
| 27 | +namespace MongoDB.Driver.Tests.Jira.CSharp624 |
| 28 | +{ |
| 29 | + public class CSharp4646Tests : Linq3IntegrationTest |
| 30 | + { |
| 31 | + [Fact] |
| 32 | + public void Watch_client_filtering_on_database_name() |
| 33 | + { |
| 34 | + RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.ChangeStreamAllChangesForCluster); |
| 35 | + var client = DriverTestConfiguration.Client; |
| 36 | + |
| 37 | + var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>() |
| 38 | + .Match(x => x.DatabaseNamespace.DatabaseName.StartsWith("MyPrefix")); |
| 39 | + |
| 40 | + var stages = RenderPipeline(pipeline); |
| 41 | + AssertStages(stages, "{ $match : { 'ns.db' : /^MyPrefix/s } }"); |
| 42 | + |
| 43 | + using var changeStream = client.Watch(pipeline); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Watch_client_filtering_on_collection_name() |
| 48 | + { |
| 49 | + RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.ChangeStreamAllChangesForCluster); |
| 50 | + var client = DriverTestConfiguration.Client; |
| 51 | + |
| 52 | + var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>() |
| 53 | + .Match(x => x.CollectionNamespace.CollectionName.StartsWith("MyPrefix")); |
| 54 | + |
| 55 | + var stages = RenderPipeline(pipeline); |
| 56 | + AssertStages(stages, "{ $match : { 'ns.coll' : /^MyPrefix/s } }"); |
| 57 | + |
| 58 | + using var changeStream = client.Watch(pipeline); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Watch_database_filtering_on_collection_name() |
| 63 | + { |
| 64 | + RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.ChangeStreamForDatabase); |
| 65 | + var client = DriverTestConfiguration.Client; |
| 66 | + var database = client.GetDatabase("test"); |
| 67 | + |
| 68 | + var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>() |
| 69 | + .Match(x => x.CollectionNamespace.CollectionName.StartsWith("MyPrefix")); |
| 70 | + |
| 71 | + var stages = RenderPipeline(pipeline); |
| 72 | + AssertStages(stages, "{ $match : { 'ns.coll' : /^MyPrefix/s } }"); |
| 73 | + |
| 74 | + // some older versions of the server require the database to exist before you can watch it |
| 75 | + CreateDatabase(database); |
| 76 | + |
| 77 | + using var changeStream = database.Watch(pipeline); |
| 78 | + } |
| 79 | + |
| 80 | + private void CreateDatabase(IMongoDatabase database) |
| 81 | + { |
| 82 | + // the easiest way to create a database is to create a collection by inserting a document |
| 83 | + var collection = database.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName); |
| 84 | + collection.InsertOne(new BsonDocument("_id", 1)); |
| 85 | + } |
| 86 | + |
| 87 | + private IList<BsonDocument> RenderPipeline(PipelineDefinition<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>> pipeline) |
| 88 | + { |
| 89 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 90 | + var documentSerializer = BsonDocumentSerializer.Instance; |
| 91 | + var changeStreamDocumentSerializer = new ChangeStreamDocumentSerializer<BsonDocument>(documentSerializer); |
| 92 | + var renderedPipeline = pipeline.Render(changeStreamDocumentSerializer, serializerRegistry, LinqProvider.V3); |
| 93 | + return renderedPipeline.Documents; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments