|
1 | | -using System.Linq; |
| 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.Driver.Linq; |
2 | 19 | using Xunit; |
3 | 20 |
|
4 | 21 | namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
5 | 22 | { |
6 | 23 | public class CSharp4316Tests : Linq3IntegrationTest |
7 | 24 | { |
8 | 25 | [Fact] |
9 | | - public void Nullable_type_translate_should_work() |
| 26 | + public void Value_and_HasValue_should_work_when_properties_on_Nullable_type() |
10 | 27 | { |
11 | | - var collection = GetCollection<ModelContainNullableType>(); |
12 | | - var fluent = collection.Aggregate() |
13 | | - .Group(x => new { Some = x.Some.Value, x.AnotherKeyGroup, x.Some.HasValue }, x => x.Select(t => t)); |
14 | | - |
15 | | - var stages = Translate(collection, fluent); |
16 | | - var expected = new [] |
17 | | - { |
18 | | - "{ $group : {'_id' : { Some : '$Some', AnotherKeyGroup : '$AnotherKeyGroup', HasValue : { $ne : ['$Some', null] }}, __agg0 : { '$push' : '$$ROOT'}}}", |
19 | | - "{ $project : { '_v' : '$__agg0', _id : 0 } }" |
20 | | - }; |
21 | | - |
22 | | - AssertStages(stages, expected); |
| 28 | + var collection = CreateCollection(); |
| 29 | + var matchStage = "{ $match : { 'ActualNullable' : { $ne : null } } }"; |
| 30 | + var projectStage = "{ $project : { Id : '$_id', Value : '$ActualNullable', HasValue : { $ne : ['$ActualNullable', null] }, _id : 0 } }"; |
| 31 | + |
| 32 | + var queryable = collection.AsQueryable() |
| 33 | + .Where(x => x.ActualNullable.HasValue) |
| 34 | + .Select(x => new { x.Id, x.ActualNullable.Value, x.ActualNullable.HasValue }); |
| 35 | + |
| 36 | + var stages = Translate(collection, queryable); |
| 37 | + AssertStages(stages, matchStage, projectStage); |
| 38 | + |
| 39 | + var results = queryable.ToList(); |
| 40 | + results.Select(r => r.Id).Should().Equal(2); |
23 | 41 | } |
24 | 42 |
|
25 | 43 | [Fact] |
26 | | - public void Type_contains_property_value_translate_should_work() |
| 44 | + public void Value_and_HasValue_should_work_when_properties_not_on_Nullable_type() |
27 | 45 | { |
28 | | - var collection = GetCollection<ModelWithoutNullable>(); |
29 | | - var fluent = collection.Aggregate() |
30 | | - .Group(x => new { Key1 = x.Property.Value, Key2 = x.SomeData, Key3 = x.Property.HasValue}, x => x.Select(t => t)); |
31 | | - |
32 | | - var stages = Translate(collection, fluent); |
33 | | - var expected = new [] |
34 | | - { |
35 | | - "{ $group : {'_id' : { Key1 : '$Property.Value', Key2 : '$SomeData', Key3 : '$Property.HasValue' }, __agg0 : { '$push' : '$$ROOT'}}}", |
36 | | - "{ $project : { '_v' : '$__agg0', _id : 0 } }" |
37 | | - }; |
38 | | - AssertStages(stages, expected); |
| 46 | + var collection = CreateCollection(); |
| 47 | + var matchStage = "{ $match : { 'OnlyLooksLikeNullable.HasValue' : true } }"; |
| 48 | + var projectStage = "{ $project : { Id : '$_id', Value : '$OnlyLooksLikeNullable.Value', HasValue : '$OnlyLooksLikeNullable.HasValue', _id : 0 } }"; |
| 49 | + |
| 50 | + var queryable = collection |
| 51 | + .AsQueryable() |
| 52 | + .Where(x => x.OnlyLooksLikeNullable.HasValue) |
| 53 | + .Select(x => new { x.Id, Value = x.OnlyLooksLikeNullable.Value, HasValue = x.OnlyLooksLikeNullable.HasValue }); |
| 54 | + |
| 55 | + var stages = Translate(collection, queryable); |
| 56 | + AssertStages(stages, matchStage, projectStage); |
| 57 | + |
| 58 | + var results = queryable.ToList(); |
| 59 | + results.Select(r => r.Id).Should().Equal(1); |
39 | 60 | } |
40 | 61 |
|
41 | | - public class ModelContainNullableType |
| 62 | + private IMongoCollection<C> CreateCollection() |
42 | 63 | { |
43 | | - public int? Some { get; set; } |
44 | | - public float AnotherKeyGroup { get; set; } |
| 64 | + var collection = GetCollection<C>("C"); |
| 65 | + |
| 66 | + CreateCollection( |
| 67 | + collection, |
| 68 | + new C { Id = 1, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = "SomeValue", HasValue = true }, ActualNullable = null }, |
| 69 | + new C { Id = 2, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = null, HasValue = false }, ActualNullable = true }); |
45 | 70 |
|
| 71 | + return collection; |
46 | 72 | } |
47 | 73 |
|
48 | | - public class ModelWithoutNullable |
| 74 | + private class C |
49 | 75 | { |
50 | | - public CustomTypeLikeNullable Property { get; set; } |
51 | | - public int SomeData { get; set; } |
| 76 | + public int Id { get; set; } |
| 77 | + public OnlyLooksLikeNullable OnlyLooksLikeNullable { get; set; } |
| 78 | + public bool? ActualNullable { get; set; } |
52 | 79 | } |
53 | 80 |
|
54 | | - public class CustomTypeLikeNullable |
| 81 | + private class OnlyLooksLikeNullable |
55 | 82 | { |
56 | 83 | public string Value { get; set; } |
57 | 84 | public bool HasValue { get; set; } |
|
0 commit comments