|
| 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.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using MongoDB.Bson; |
| 19 | +using MongoDB.Bson.Serialization.Attributes; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators |
| 24 | +{ |
| 25 | + public class NegateExpressionToAggregationExpressionTranslatorTests: Linq3IntegrationTest |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void Negate_int_should_work() |
| 29 | + { |
| 30 | + var collection = CreateCollection(); |
| 31 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Int); |
| 32 | + |
| 33 | + var stages = Translate(collection, queryable); |
| 34 | + AssertStages( |
| 35 | + stages, |
| 36 | + "{ $project : { _v : { $subtract : [0, '$Int'] }, _id : 0 } }"); |
| 37 | + |
| 38 | + var results = queryable.ToList(); |
| 39 | + results.Should().Equal(10, -5, 0, int.MaxValue, -int.MaxValue); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Negate_long_should_work() |
| 44 | + { |
| 45 | + var collection = CreateCollection(); |
| 46 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Long); |
| 47 | + |
| 48 | + var stages = Translate(collection, queryable); |
| 49 | + AssertStages( |
| 50 | + stages, |
| 51 | + "{ $project : { _v : { $subtract : [0, '$Long'] }, _id : 0 } }"); |
| 52 | + |
| 53 | + var results = queryable.ToList(); |
| 54 | + results.Should().Equal(10L, -5L, 0L, long.MaxValue, -long.MaxValue); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Negate_single_should_work() |
| 59 | + { |
| 60 | + var collection = CreateCollection(); |
| 61 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Single); |
| 62 | + |
| 63 | + var stages = Translate(collection, queryable); |
| 64 | + AssertStages( |
| 65 | + stages, |
| 66 | + "{ $project : { _v : { $subtract : [0, '$Single'] }, _id : 0 } }"); |
| 67 | + |
| 68 | + var results = queryable.ToList(); |
| 69 | + results.Should().Equal(10F, -5F, 0F, float.MaxValue, -float.MaxValue); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Negate_double_should_work() |
| 74 | + { |
| 75 | + var collection = CreateCollection(); |
| 76 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Double); |
| 77 | + |
| 78 | + var stages = Translate(collection, queryable); |
| 79 | + AssertStages( |
| 80 | + stages, |
| 81 | + "{ $project : { _v : { $subtract : [0, '$Double'] }, _id : 0 } }"); |
| 82 | + |
| 83 | + var results = queryable.ToList(); |
| 84 | + results.Should().Equal(10.0, -5.0, 0.0, double.MaxValue, -double.MaxValue); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void Negate_decimal128_should_work() |
| 89 | + { |
| 90 | + var collection = CreateCollection(); |
| 91 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.DecimalAsDecimal128); |
| 92 | + |
| 93 | + var stages = Translate(collection, queryable); |
| 94 | + AssertStages( |
| 95 | + stages, |
| 96 | + "{ $project : { _v : { $subtract : [0, '$DecimalAsDecimal128'] }, _id : 0 } }"); |
| 97 | + |
| 98 | + var results = queryable.ToList(); |
| 99 | + results.Should().Equal(10.0m, -5.0m, 0.0m, decimal.MaxValue, -decimal.MaxValue); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void Negate_decimal_as_string_should_throw() |
| 104 | + { |
| 105 | + var collection = CreateCollection(); |
| 106 | + var queryable = Queryable.Select(collection.AsQueryable(), i => -i.DecimalAsString); |
| 107 | + |
| 108 | + var exception = Record.Exception(() => Translate(collection, queryable)); |
| 109 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 110 | + } |
| 111 | + |
| 112 | + private IMongoCollection<Data> CreateCollection() |
| 113 | + { |
| 114 | + var collection = GetCollection<Data>("test"); |
| 115 | + CreateCollection( |
| 116 | + collection, |
| 117 | + new Data { Id = 1, Int = -10, Double = -10, Single = -10, Long = -10, DecimalAsString = -10, DecimalAsDecimal128 = -10}, |
| 118 | + new Data { Id = 2, Int = 5, Double = 5, Single = 5, Long = 5, DecimalAsString = 5, DecimalAsDecimal128 = 5}, |
| 119 | + new Data { Id = 3, Int = 0, Double = 0, Single = 0, Long = 0, DecimalAsString = 0, DecimalAsDecimal128 = 0}, |
| 120 | + new Data { Id = 4, Int = -int.MaxValue, Double = -double.MaxValue, Single = -float.MaxValue, Long = -long.MaxValue, DecimalAsString = -decimal.MaxValue, DecimalAsDecimal128 = -decimal.MaxValue}, |
| 121 | + new Data { Id = 5, Int = int.MaxValue, Double = double.MaxValue, Single = float.MaxValue, Long = long.MaxValue, DecimalAsString = decimal.MaxValue, DecimalAsDecimal128 = decimal.MaxValue}); |
| 122 | + return collection; |
| 123 | + } |
| 124 | + |
| 125 | + private class Data |
| 126 | + { |
| 127 | + public int Id { get; set; } |
| 128 | + public int Int { get; set; } |
| 129 | + public long Long { get; set; } |
| 130 | + public float Single { get; set; } |
| 131 | + public double Double { get; set; } |
| 132 | + public decimal DecimalAsString { get; set; } |
| 133 | + [BsonRepresentation(BsonType.Decimal128)] |
| 134 | + public decimal DecimalAsDecimal128 { get; set; } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments