|
| 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.Linq; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Driver.Linq; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 23 | +{ |
| 24 | + public class CSharp2472Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + public void Projection_of_ArrayOfDocuments_dictionary_keys_and_values_should_work() |
| 28 | + { |
| 29 | + var collection = CreateCollection(); |
| 30 | + var equipmentId = 1; |
| 31 | + var startDate = new DateTime(2022, 01, 01, 0, 0, 0, DateTimeKind.Utc); |
| 32 | + var endDate = new DateTime(2022, 01, 02, 0, 0, 0, DateTimeKind.Utc); |
| 33 | + |
| 34 | + var queryable = collection |
| 35 | + .AsQueryable() |
| 36 | + .Where( |
| 37 | + q => q.equipment_id == equipmentId |
| 38 | + && q.timestamp >= startDate |
| 39 | + && q.timestamp <= endDate |
| 40 | + ) |
| 41 | + .GroupBy(g => g.timestamp) |
| 42 | + .Select(p => new MyDTO |
| 43 | + { |
| 44 | + timestamp = p.Key, |
| 45 | + sqrt_calc = (decimal)Math.Sqrt( |
| 46 | + p.Sum(x => (double)x.my_decimal_value) |
| 47 | + ) |
| 48 | + }) |
| 49 | + .OrderBy(q => q.timestamp); |
| 50 | + |
| 51 | + var stages = Translate(collection, queryable); |
| 52 | + AssertStages( |
| 53 | + stages, |
| 54 | + "{ $match : { equipment_id : 1, timestamp : { $gte : ISODate('2022-01-01T00:00:00Z'), $lte : ISODate('2022-01-02T00:00:00Z') } } }", |
| 55 | + "{ $group : { _id : '$timestamp', __agg0 : { $sum : { $toDouble : '$my_decimal_value' } } } }", |
| 56 | + "{ $project : { timestamp : '$_id', sqrt_calc : { $toDecimal : { $sqrt : '$__agg0' } }, _id : 0 } }", |
| 57 | + "{ $sort : { timestamp : 1 } }"); |
| 58 | + |
| 59 | + var result = queryable.Single(); |
| 60 | + result.timestamp.Should().Be(new DateTime(2022, 01, 01, 12, 0, 0, DateTimeKind.Utc)); |
| 61 | + result.sqrt_calc.Should().Be(2M); |
| 62 | + } |
| 63 | + |
| 64 | + private IMongoCollection<C> CreateCollection() |
| 65 | + { |
| 66 | + var collection = GetCollection<C>("C"); |
| 67 | + |
| 68 | + CreateCollection( |
| 69 | + collection, |
| 70 | + new C { Id = 1, equipment_id = 1, timestamp = new DateTime(2022, 01, 01, 12, 0, 0, DateTimeKind.Utc), my_decimal_value = 4M }); |
| 71 | + |
| 72 | + return collection; |
| 73 | + } |
| 74 | + |
| 75 | + private class C |
| 76 | + { |
| 77 | + public int Id { get; set; } |
| 78 | + public int equipment_id { get; set; } |
| 79 | + public DateTime timestamp { get; set; } |
| 80 | + public decimal my_decimal_value { get; set; } |
| 81 | + } |
| 82 | + |
| 83 | + private class MyDTO |
| 84 | + { |
| 85 | + public DateTime timestamp { get; set; } |
| 86 | + public decimal sqrt_calc { get; set; } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments