|
| 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 System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using FluentAssertions; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.Serialization; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Tests |
| 25 | +{ |
| 26 | + public class FilterDefinitionRenderContextTests |
| 27 | + { |
| 28 | + [Fact] |
| 29 | + public void FilterDefinitionRenderContext_dollarForm_default_value_should_be_false() |
| 30 | + { |
| 31 | + FilterDefinitionRenderContext.RenderDollarForm.Should().BeFalse(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task FilterDefinitionRenderContext_should_be_scoped_to_task() |
| 36 | + { |
| 37 | + bool? renderDollarFormValueObservedByTask = null; |
| 38 | + var taskReadyToRenderEvent = new ManualResetEventSlim(); |
| 39 | + var unblockTaskEvent = new ManualResetEventSlim(); |
| 40 | + var rendererTask = Task.Run(RendererTask); |
| 41 | + |
| 42 | + // Wait for task to set RenderDollarForm = true; |
| 43 | + taskReadyToRenderEvent.Wait(); |
| 44 | + |
| 45 | + // Try to 'override' RenderDollarForm value and unblock the task |
| 46 | + FilterDefinitionRenderContext.RenderDollarForm = false; |
| 47 | + unblockTaskEvent.Set(); |
| 48 | + |
| 49 | + // Wait fot task to finish and set renderDollarFormValueObservedByTask |
| 50 | + await rendererTask; |
| 51 | + |
| 52 | + renderDollarFormValueObservedByTask.Should().Be(true); |
| 53 | + FilterDefinitionRenderContext.RenderDollarForm.Should().Be(false); |
| 54 | + |
| 55 | + void RendererTask() |
| 56 | + { |
| 57 | + using var renderContext = FilterDefinitionRenderContext.StartRender(true); |
| 58 | + |
| 59 | + taskReadyToRenderEvent.Set(); |
| 60 | + unblockTaskEvent.Wait(); |
| 61 | + |
| 62 | + renderDollarFormValueObservedByTask = FilterDefinitionRenderContext.RenderDollarForm; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [MemberData(nameof(Correct_form_should_be_rendered_test_cases))] |
| 68 | + public void Correct_form_should_be_rendered(FilterDefinition<BsonDocument> filterDefinition, bool renderDollarForm, string expectedFilter) |
| 69 | + { |
| 70 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 71 | + var documentSerializer = serializerRegistry.GetSerializer<BsonDocument>(); |
| 72 | + var expectedFilterDocument = BsonDocument.Parse(expectedFilter); |
| 73 | + |
| 74 | + using var renderContext = FilterDefinitionRenderContext.StartRender(renderDollarForm); |
| 75 | + var actualFilter = filterDefinition.Render(documentSerializer, serializerRegistry); |
| 76 | + |
| 77 | + actualFilter.Should().Be(expectedFilterDocument); |
| 78 | + } |
| 79 | + |
| 80 | + public static IEnumerable<object[]> Correct_form_should_be_rendered_test_cases() |
| 81 | + { |
| 82 | + return new object[][] |
| 83 | + { |
| 84 | + // $eq |
| 85 | + new object[] { Eq("a", 1), false, "{ a : 1 }" }, |
| 86 | + new object[] { Eq("a", 1), true, "{ a: { $eq: 1 } }" }, |
| 87 | + |
| 88 | + // $and |
| 89 | + new object[] { Gt("a", 1) & Gt("b", 2), false, "{ a : { $gt : 1 }, b : { $gt : 2 } }" }, |
| 90 | + new object[] { Gt("a", 1) & Gt("b", 2), true, "{ $and: [ { a : { $gt : 1 }}, { b : { $gt : 2 }} ] }" }, |
| 91 | + new object[] { Gt("a", 1) & Gt("b", 2) & Gt("c", 3), false, "{ a : { $gt : 1 }, b : { $gt : 2 }, c : { $gt : 3 } }" }, |
| 92 | + new object[] { Gt("a", 1) & Gt("b", 2) & Gt("c", 3), true, "{ $and: [ { a : { $gt : 1 }}, { b : { $gt : 2 }}, { c : { $gt : 3 }} ] }" }, |
| 93 | + |
| 94 | + // nested $eq |
| 95 | + new object[] { Eq("a", 1) | Eq("b", 2), false, "{ $or: [{ a : 1 }, { b : 2 }] }" }, |
| 96 | + new object[] { Eq("a", 1) | Eq("b", 2), true, "{ $or: [{ a : { $eq: 1 }}, { b : { $eq : 2 }}] }" }, |
| 97 | + new object[] { !(Eq("a", 1) | Eq("b", 2)), false, "{ $nor: [ { a : 1 }, { b : 2 }] }" }, |
| 98 | + new object[] { !(Eq("a", 1) | Eq("b", 2)), true, "{ $nor: [{ a : { $eq: 1 }}, { b : { $eq: 2 }}] }" }, |
| 99 | + |
| 100 | + // nested $and |
| 101 | + new object[] { Gt("a", 1) & Gt("b", 2) | Gt("c", 3) & Gt("d", 4), false, "{ $or: [{ a : { $gt : 1 }, b : { $gt : 2 }}, { c : { $gt : 3 }, d : { $gt : 4 }}] }" }, |
| 102 | + new object[] { Gt("a", 1) & Gt("b", 2) | Gt("c", 3) & Gt("d", 4), true, "{ $or: [{ $and: [ { a : { $gt : 1 }}, { b : { $gt : 2 }} ] }, { $and: [ { c : { $gt : 3 }}, { d : { $gt : 4 }} ] }] }" }, |
| 103 | + |
| 104 | + // $eq and $and |
| 105 | + new object[] { Eq("a", 1) & Eq("b", 2), false, "{ a : 1, b : 2 }" }, |
| 106 | + new object[] { Eq("a", 1) & Eq("b", 2), true, "{ $and: [{ a: { $eq: 1 }}, { b: { $eq: 2 }}] }" }, |
| 107 | + |
| 108 | + // $eq and $and nested |
| 109 | + new object[] { !(Eq("a", 1) & Eq("b", 2)), false, "{ $nor: [{ a : 1, b : 2 }] }" }, |
| 110 | + new object[] { !(Eq("a", 1) & Eq("b", 2)), true, "{ $nor: [{ $and: [{ a: { $eq: 1 }}, { b: { $eq: 2 }}] }] }" }, |
| 111 | + |
| 112 | + // always dollar form |
| 113 | + new object[] { Eq("a", 1) & Eq("a", 2), false, "{ $and : [{ a : 1 }, { a : 2 }] }" }, |
| 114 | + new object[] { Eq("a", 1) & Eq("a", 2), true, "{ $and : [{ a : { $eq: 1 } }, { a : { $eq: 2 } }] }" }, |
| 115 | + new object[] { Gt("a", 1) & Gt("a", 2), false, "{ $and : [{ a : { $gt : 1 } }, { a : { $gt : 2 } }] }" }, |
| 116 | + new object[] { Gt("a", 1) & Gt("a", 2), true, "{ $and : [{ a : { $gt : 1 } }, { a : { $gt : 2 } }] }" }, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + private static FilterDefinition<BsonDocument> Eq(string field, int value) => GetBuilder().Eq(field, value); |
| 121 | + private static FilterDefinition<BsonDocument> Gt(string field, int value) => GetBuilder().Gt(field, value); |
| 122 | + |
| 123 | + private static FilterDefinitionBuilder<BsonDocument> GetBuilder() => new(); |
| 124 | + } |
| 125 | +} |
0 commit comments