Skip to content

Commit 8829537

Browse files
committed
implemented CSharp475 and added tests.
1 parent 711a3a3 commit 8829537

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

Driver/Linq/Translators/SelectQuery.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ public override object Execute()
173173
}
174174
else
175175
{
176-
// TODO: handle projection after OfType
177-
throw new NotSupportedException();
176+
var paramExpression = Expression.Parameter(DocumentType, "x");
177+
var convertExpression = Expression.Convert(paramExpression, _ofType);
178+
var body = ExpressionParameterReplacer.ReplaceParameter(projection.Body, projection.Parameters[0], convertExpression);
179+
projection = Expression.Lambda(body, paramExpression);
178180
}
179181
}
180182

DriverUnitTests/DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<Compile Include="GridFS\MongoGridFSSettingsTests.cs" />
127127
<Compile Include="GridFS\MongoGridFSStreamTests.cs" />
128128
<Compile Include="GridFS\MongoGridFSTests.cs" />
129+
<Compile Include="Jira\CSharp475Tests.cs" />
129130
<Compile Include="Jira\CSharp101Tests.cs" />
130131
<Compile Include="Jira\CSharp103Tests.cs" />
131132
<Compile Include="Jira\CSharp110Tests.cs" />
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright 2010-2012 10gen 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.IO;
19+
using System.Linq;
20+
using System.Runtime.Serialization;
21+
using System.Text;
22+
using NUnit.Framework;
23+
24+
using MongoDB.Bson;
25+
using MongoDB.Bson.Serialization.Attributes;
26+
using MongoDB.Driver;
27+
using MongoDB.Driver.Builders;
28+
using MongoDB.Driver.Linq;
29+
30+
namespace MongoDB.DriverUnitTests.Jira.CSharp475
31+
{
32+
[TestFixture]
33+
public class CSharp475Tests
34+
{
35+
public abstract class Base
36+
{
37+
public ObjectId Id { get; set; }
38+
39+
public string A { get; set; }
40+
}
41+
42+
public class T1 : Base
43+
{
44+
public string B { get; set; }
45+
}
46+
47+
[Test]
48+
public void ProjectAfterOfTypeTest()
49+
{
50+
var server = MongoServer.Create();
51+
var db = server.GetDatabase("csharp475");
52+
var collection = db.GetCollection<Base>("ProjectTest");
53+
collection.Drop();
54+
55+
56+
var t1 = new T1 { A = "T1.A", B = "T1.B" };
57+
collection.Insert(t1);
58+
59+
var query = from t in collection.AsQueryable().OfType<T1>() select t.B;
60+
var results = query.ToList();
61+
Assert.That(results.Count, Is.EqualTo(1));
62+
Assert.That(results[0], Is.EqualTo("T1.B"));
63+
}
64+
}
65+
}

DriverUnitTests/Linq/SelectOfTypeTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ public void TestOfTypeD()
151151
Assert.AreEqual(1, Consume(query));
152152
}
153153

154+
[Test]
155+
public void TestOfTypeDWithProjection()
156+
{
157+
var query = _collection.AsQueryable<B>().OfType<D>().Select(x => new { A = x.d });
158+
159+
var translatedQuery = MongoQueryTranslator.Translate(query);
160+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
161+
Assert.AreSame(_collection, translatedQuery.Collection);
162+
Assert.AreSame(typeof(B), translatedQuery.DocumentType);
163+
164+
var selectQuery = (SelectQuery)translatedQuery;
165+
Assert.AreEqual("(B x) => LinqToMongo.Inject({ \"_t\" : \"D\" })", ExpressionFormatter.ToString(selectQuery.Where));
166+
Assert.AreEqual(typeof(D), selectQuery.OfType);
167+
Assert.IsNull(selectQuery.OrderBy);
168+
Assert.IsNotNull(selectQuery.Projection);
169+
Assert.AreEqual("(D x) => new <>f__AnonymousType1`1(x.d)", ExpressionFormatter.ToString(selectQuery.Projection));
170+
Assert.IsNull(selectQuery.Skip);
171+
Assert.IsNull(selectQuery.Take);
172+
173+
Assert.AreEqual("{ \"_t\" : \"D\" }", selectQuery.BuildQuery().ToJson());
174+
Assert.AreEqual(1, Consume(query));
175+
}
176+
154177
[Test]
155178
public void TestWhereBGreaterThan0OfTypeCWhereCGreaterThan0()
156179
{

0 commit comments

Comments
 (0)