Skip to content

Commit d57fe7f

Browse files
committed
CSHARP-1892: Code review changes and added more tests.
1 parent d1d2127 commit d57fe7f

File tree

4 files changed

+117
-14
lines changed

4 files changed

+117
-14
lines changed

src/MongoDB.Driver/Linq/Expressions/FieldExpression.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2015 MongoDB Inc.
1+
/* Copyright 2015-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -50,15 +50,6 @@ public FieldExpression(Expression document, string fieldName, IBsonSerializer se
5050
_original = original;
5151
}
5252

53-
public FieldExpression(FieldExpression other, IBsonSerializer serializer)
54-
{
55-
Ensure.IsNotNull(other, nameof(other));
56-
_document = other.Document;
57-
_fieldName = other.FieldName;
58-
_original = other.Original;
59-
_serializer = Ensure.IsNotNull(serializer, nameof(serializer));
60-
}
61-
6253
public Expression Document
6354
{
6455
get { return _document; }
@@ -104,6 +95,16 @@ public FieldExpression Update(Expression document, Expression original)
10495
return this;
10596
}
10697

98+
public FieldExpression WithSerializer(IBsonSerializer serializer)
99+
{
100+
Ensure.IsNotNull(serializer, nameof(serializer));
101+
return new FieldExpression(
102+
_document,
103+
_fieldName,
104+
serializer,
105+
_original);
106+
}
107+
107108
protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
108109
{
109110
return visitor.VisitField(this);

src/MongoDB.Driver/Linq/Processors/SerializationBinder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2015-2016 MongoDB Inc.
1+
/* Copyright 2015-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -217,9 +217,7 @@ protected override Expression VisitUnary(UnaryExpression node)
217217
case ExtensionExpressionType.Document:
218218
return new DocumentExpression(serializer);
219219
case ExtensionExpressionType.Field:
220-
return new FieldExpression(
221-
(FieldExpression)serializationExpression,
222-
serializer);
220+
return ((FieldExpression)serializationExpression).WithSerializer(serializer);
223221
case ExtensionExpressionType.FieldAsDocument:
224222
return new FieldAsDocumentExpression(
225223
((FieldAsDocumentExpression)serializationExpression).Expression,
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright 2017 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.Collections.Generic;
18+
using System.Linq;
19+
using FluentAssertions;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq
23+
{
24+
public class MongoQueryableWithDotNotationTests
25+
{
26+
[Fact]
27+
public void Where_with_ExtraInfo_Type_and_ExtraInfo_NotNullableType_should_render_correctly()
28+
{
29+
var subject = CreateSubject();
30+
31+
var result = subject.Where(c => c.ExtraInfo.Type == 1 && c.ExtraInfo.NotNullableType == 1);
32+
33+
result.ToString().Should().Be("aggregate([{ \"$match\" : { \"ExtraInfo.Type\" : 1, \"ExtraInfo.NotNullableType\" : 1 } }])");
34+
}
35+
36+
[Fact]
37+
public void Where_with_ExtraInfo_Type_should_render_correctly()
38+
{
39+
var subject = CreateSubject();
40+
41+
var result = subject.Where(c => c.ExtraInfo.Type == null);
42+
43+
result.ToString().Should().Be("aggregate([{ \"$match\" : { \"ExtraInfo.Type\" : null } }])");
44+
}
45+
46+
[Fact]
47+
public void Where_with_ExtraInfo_Type_with_Value_should_render_correctly()
48+
{
49+
var subject = CreateSubject();
50+
51+
var result = subject.Where(c => c.ExtraInfo.Type.Value == 2);
52+
53+
result.ToString().Should().Be("aggregate([{ \"$match\" : { \"ExtraInfo.Type\" : 2 } }])");
54+
}
55+
56+
[Fact]
57+
public void Where_with_ExtraInfo_Type_with_Value_and_nullable_variable_should_render_correctly()
58+
{
59+
var subject = CreateSubject();
60+
int? infoType = 3;
61+
62+
var result = subject.Where(c => c.ExtraInfo.Type.Value == infoType);
63+
64+
result.ToString().Should().Be("aggregate([{ \"$match\" : { \"ExtraInfo.Type\" : 3 } }])");
65+
}
66+
67+
[Fact]
68+
public void Where_with_Contains_should_render_correctly()
69+
{
70+
var subject = CreateSubject();
71+
var list = new List<int>
72+
{
73+
4, 5
74+
};
75+
76+
var result = subject.Where(c => list.Contains(c.ExtraInfo.Type.Value) || list.Contains(c.ExtraInfo.NotNullableType));
77+
78+
result.ToString().Should().Be("aggregate([{ \"$match\" : { \"$or\" : [{ \"ExtraInfo.Type\" : { \"$in\" : [4, 5] } }, { \"ExtraInfo.NotNullableType\" : { \"$in\" : [4, 5] } }] } }])");
79+
}
80+
81+
// private methods
82+
private IQueryable<Car> CreateSubject()
83+
{
84+
var client = new MongoClient("mongodb://hostnotneeded");
85+
var database = client.GetDatabase("test");
86+
var collection = database.GetCollection<Car>("test");
87+
return collection.AsQueryable();
88+
}
89+
90+
// nested types
91+
private class Car
92+
{
93+
public Guid Id { get; set; }
94+
public ExtraInfo ExtraInfo { get; set; }
95+
}
96+
97+
private class ExtraInfo
98+
{
99+
public int NotNullableType { get; set; }
100+
public int? Type { get; set; }
101+
}
102+
}
103+
}

tests/MongoDB.Driver.Tests/MongoDB.Driver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<Compile Include="Linq\MongoQueryableIntComparedToDoubleWithStringRepresentationTests.cs" />
138138
<Compile Include="Linq\MongoQueryableIntComparedToNullableIntWithStringRepresentationTests.cs" />
139139
<Compile Include="Linq\MongoQueryableNullableEnumComparedToNullableEnumWithStringRepresentationTests.cs" />
140+
<Compile Include="Linq\MongoQueryableWithDotNotationTests.cs" />
140141
<Compile Include="Linq\Translators\AggregateGroupTranslatorTests.cs" />
141142
<Compile Include="Linq\Translators\AggregateProjectTranslatorTests.cs" />
142143
<Compile Include="Linq\IntegrationTestBase.cs" />

0 commit comments

Comments
 (0)