-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-3435: FilterDefinition Inject method should use root serializer #1686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp3435Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq; | ||
using MongoDB.Driver.Linq; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; | ||
|
||
public class CSharp3435Tests : LinqIntegrationTest<CSharp3435Tests.ClassFixture> | ||
{ | ||
public CSharp3435Tests(ClassFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void Where_should_work() | ||
{ | ||
var queryable = CreateQueryable() | ||
.Where(x => x.NormalizedUsername == "PAPLABROS"); | ||
|
||
var stages = Translate(Fixture.UserClaimCollection, queryable); | ||
AssertStages( | ||
stages, | ||
"{ $project : { _outer : '$$ROOT', _id : 0 } }", | ||
"{ $lookup : { from : 'Users', localField : '_outer.UserId', foreignField : '_id', as : '_inner' } }", | ||
"{ $project : { claim : '$_outer', users : '$_inner', _id : 0 } }", | ||
"{ $match : { 'claim.ClaimType' : 'Moderator' } }", | ||
"{ $project : { _v : { $arrayElemAt : ['$users', 0] }, _id : 0 } }", | ||
"{ $match : { '_v.NormalizedUsername' : 'PAPLABROS' } }"); | ||
} | ||
|
||
[Fact] | ||
public void Where_with_Inject_should_work() | ||
{ | ||
var filter = Builders<User>.Filter.Eq(x => x.NormalizedUsername, "PAPLABROS"); | ||
var queryable = CreateQueryable() | ||
.Where(x => filter.Inject()); | ||
|
||
var stages = Translate(Fixture.UserClaimCollection, queryable); | ||
AssertStages( | ||
stages, | ||
"{ $project : { _outer : '$$ROOT', _id : 0 } }", | ||
"{ $lookup : { from : 'Users', localField : '_outer.UserId', foreignField : '_id', as : '_inner' } }", | ||
"{ $project : { claim : '$_outer', users : '$_inner', _id : 0 } }", | ||
"{ $match : { 'claim.ClaimType' : 'Moderator' } }", | ||
"{ $project : { _v : { $arrayElemAt : ['$users', 0] }, _id : 0 } }", | ||
"{ $match : { '_v.NormalizedUsername' : 'PAPLABROS' } }"); | ||
} | ||
|
||
public IQueryable<User> CreateQueryable() | ||
{ | ||
var usersCollection = Fixture.UserCollection; | ||
var userClaimsCollection = Fixture.UserClaimCollection; | ||
|
||
var queryable = | ||
from claim in userClaimsCollection.AsQueryable() | ||
join user in usersCollection.AsQueryable() on claim.UserId equals user.Id into users | ||
where claim.ClaimType == "Moderator" | ||
select users.First(); | ||
|
||
// this is the equivalent method syntax | ||
// var queryable = userClaimsCollection.AsQueryable() | ||
// .GroupJoin( | ||
// usersCollection.AsQueryable(), | ||
// claim => claim.UserId, | ||
// user => user.Id, | ||
// (claim, users) => new { claim, users }) | ||
// .Where(x => x.claim.ClaimType == "Moderator") | ||
// .Select(x => x.users.First()); | ||
|
||
return queryable; | ||
} | ||
|
||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string NormalizedUsername { get; set; } | ||
} | ||
|
||
public class UserClaim | ||
{ | ||
public int Id { get; set; } | ||
public int UserId { get; set; } | ||
public string ClaimType { get; set; } | ||
} | ||
|
||
public sealed class ClassFixture : MongoDatabaseFixture | ||
{ | ||
public IMongoCollection<User> UserCollection { get; private set; } | ||
public IMongoCollection<UserClaim> UserClaimCollection { get; private set; } | ||
|
||
protected override void InitializeFixture() | ||
{ | ||
UserCollection = CreateCollection<User>("Users"); | ||
UserClaimCollection = CreateCollection<UserClaim>("UserClaims"); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: no need to store a var for serializerRegistry, we could just have BsonSerializer.SerializerRegistry directly where it is used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like to decouple as much as possible the SOURCE of the registry from the USE of it.
Intermediate variables are usually a good thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair