|
| 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 | +#if NET6_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Linq; |
| 21 | +using FluentAssertions; |
| 22 | +using MongoDB.Bson; |
| 23 | +using MongoDB.Bson.Serialization.Attributes; |
| 24 | +using MongoDB.Driver.Linq; |
| 25 | +using MongoDB.Driver.TestHelpers; |
| 26 | +using Xunit; |
| 27 | + |
| 28 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators |
| 29 | +{ |
| 30 | + public class NewDictionaryExpressionToAggregationExpressionTranslatorTests : LinqIntegrationTest<NewDictionaryExpressionToAggregationExpressionTranslatorTests.ClassFixture> |
| 31 | + { |
| 32 | + public NewDictionaryExpressionToAggregationExpressionTranslatorTests(ClassFixture fixture) |
| 33 | + : base(fixture) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void NewDictionary_with_KeyValuePairs_should_translate() |
| 39 | + { |
| 40 | + var collection = Fixture.Collection; |
| 41 | + |
| 42 | + var queryable = collection.AsQueryable() |
| 43 | + .Select(d => new Dictionary<string, string>( |
| 44 | + new[] { new KeyValuePair<string, string>("A", d.A), new KeyValuePair<string, string>("B", d.B) })); |
| 45 | + |
| 46 | + var stages = Translate(collection, queryable); |
| 47 | + |
| 48 | + AssertStages(stages, "{ $project : { _v : { A : '$A', B: '$B' }, _id : 0 } }"); |
| 49 | + |
| 50 | + var result = queryable.Single(); |
| 51 | + result.Should().Equal(new Dictionary<string, string>{ ["A"] = "a", ["B"] = "b" }); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void NewDictionary_with_KeyValuePairs_Create_should_translate() |
| 56 | + { |
| 57 | + var collection = Fixture.Collection; |
| 58 | + |
| 59 | + var queryable = collection.AsQueryable() |
| 60 | + .Select(d => new Dictionary<string, string>( |
| 61 | + new[] { KeyValuePair.Create("A", d.A), KeyValuePair.Create("B", d.B) })); |
| 62 | + |
| 63 | + var stages = Translate(collection, queryable); |
| 64 | + |
| 65 | + AssertStages(stages, "{ $project : { _v : { A : '$A', B: '$B' }, _id : 0 } }"); |
| 66 | + |
| 67 | + var result = queryable.Single(); |
| 68 | + result.Should().Equal(new Dictionary<string, string>{ ["A"] = "a", ["B"] = "b" }); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void NewDictionary_with_KeyValuePairs_should_translate_Guid_as_string_key() |
| 73 | + { |
| 74 | + var collection = Fixture.Collection; |
| 75 | + |
| 76 | + var queryable = collection.AsQueryable() |
| 77 | + .Select(d => new Dictionary<Guid, string>( |
| 78 | + new[] { new KeyValuePair<Guid, string>(d.GuidAsString, d.A) })); |
| 79 | + |
| 80 | + var stages = Translate(collection, queryable); |
| 81 | + |
| 82 | + AssertStages(stages, "{ $project : { _v : { $arrayToObject : [[{ k : '$GuidAsString', v : '$A' }]] }, _id : 0 } }"); |
| 83 | + |
| 84 | + var result = queryable.Single(); |
| 85 | + result.Should().Equal(new Dictionary<Guid, string>{ [Guid.Parse("3E9AE467-9705-4C17-9655-EE7730BCC2EE")] = "a" }); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void NewDictionary_with_KeyValuePairs_should_translate_dynamic_array() |
| 91 | + { |
| 92 | + var collection = Fixture.Collection; |
| 93 | + |
| 94 | + var queryable = collection.AsQueryable() |
| 95 | + .Select(d => new Dictionary<string, string>( |
| 96 | + d.Items.Select(i => new KeyValuePair<string, string>(i.P, i.W)))); |
| 97 | + |
| 98 | + var stages = Translate(collection, queryable); |
| 99 | + |
| 100 | + AssertStages(stages, "{ $project : { _v : { $arrayToObject : { $map: { input: '$Items', as: 'i', in: { k: '$$i.P', v: '$$i.W' } } } }, _id : 0 } }"); |
| 101 | + |
| 102 | + var result = queryable.Single(); |
| 103 | + result.Should().Equal(new Dictionary<string, string>{ ["x"] = "y" }); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void NewDictionary_with_KeyValuePairs_throws_on_non_string_key() |
| 108 | + { |
| 109 | + var collection = Fixture.Collection; |
| 110 | + |
| 111 | + var queryable = collection.AsQueryable() |
| 112 | + .Select(d => new Dictionary<int, string>( |
| 113 | + new[] { new KeyValuePair<int, string>(42, d.A) })); |
| 114 | + |
| 115 | + var exception = Record.Exception(() => queryable.ToList()); |
| 116 | + |
| 117 | + exception.Should().NotBeNull(); |
| 118 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 119 | + } |
| 120 | + |
| 121 | + public class C |
| 122 | + { |
| 123 | + public string A { get; set; } |
| 124 | + |
| 125 | + public string B { get; set; } |
| 126 | + |
| 127 | + [BsonRepresentation(BsonType.String)] |
| 128 | + public Guid GuidAsString { get; set; } |
| 129 | + |
| 130 | + public Item[] Items { get; set; } |
| 131 | + } |
| 132 | + |
| 133 | + public class Item |
| 134 | + { |
| 135 | + public string P { get; set; } |
| 136 | + |
| 137 | + public string W { get; set; } |
| 138 | + } |
| 139 | + |
| 140 | + public sealed class ClassFixture : MongoCollectionFixture<C> |
| 141 | + { |
| 142 | + protected override IEnumerable<C> InitialData => |
| 143 | + [ |
| 144 | + new C |
| 145 | + { |
| 146 | + A = "a", |
| 147 | + B = "b", |
| 148 | + GuidAsString = Guid.Parse("3E9AE467-9705-4C17-9655-EE7730BCC2EE"), |
| 149 | + Items = [ new Item { P = "x", W = "y" } ] |
| 150 | + }, |
| 151 | + ]; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | +#endif |
0 commit comments