Skip to content

Commit a9c3fe6

Browse files
rstamdnickless
authored andcommitted
CSHARP-2727: Verify that string cast and queries on BsonDocument work in LINQ3.
1 parent ba453df commit a9c3fe6

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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;
17+
using System.Linq;
18+
using FluentAssertions;
19+
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization;
21+
using MongoDB.Driver.Linq;
22+
using Xunit;
23+
24+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
25+
{
26+
public class CSharp2727Tests : Linq3IntegrationTest
27+
{
28+
[Fact]
29+
public void Find_with_predicate_on_Body_should_work()
30+
{
31+
var collection = CreateCollection();
32+
var filter = new ExpressionFilterDefinition<Entity>(x => new[] { "Test1", "Test2" }.Contains((string)x.Body["name"]));
33+
34+
var serializerRegistry = BsonSerializer.SerializerRegistry;
35+
var documentSerializer = serializerRegistry.GetSerializer<Entity>();
36+
var renderedFilter = filter.Render(documentSerializer, serializerRegistry, LinqProvider.V3);
37+
38+
renderedFilter.Should().Be("{ $expr : { $in : [{ $toString : '$Body.name' }, ['Test1', 'Test2']] } }");
39+
40+
var cursor = collection.Find(filter);
41+
42+
var results = cursor.ToList();
43+
results.Select(x => x.Id).Should().Equal(1, 2);
44+
}
45+
46+
[Fact]
47+
public void Find_with_predicate_on_Caption_should_work()
48+
{
49+
var collection = CreateCollection();
50+
var filter = new ExpressionFilterDefinition<Entity>(x => new[] { "Test1", "Test2" }.Contains(x.Caption));
51+
52+
var serializerRegistry = BsonSerializer.SerializerRegistry;
53+
var documentSerializer = serializerRegistry.GetSerializer<Entity>();
54+
var renderedFilter = filter.Render(documentSerializer, serializerRegistry, LinqProvider.V3);
55+
56+
renderedFilter.Should().Be("{ Caption : { $in : ['Test1', 'Test2'] } }");
57+
58+
var cursor = collection.FindSync(filter);
59+
60+
var results = cursor.ToList();
61+
results.Select(x => x.Id).Should().Equal(1, 2);
62+
}
63+
64+
[Fact]
65+
public void Where_with_predicate_on_Body_should_work()
66+
{
67+
var collection = CreateCollection();
68+
69+
var queryable = collection
70+
.AsQueryable()
71+
.Where(x => new[] { "Test1", "Test2" }.Contains((string)x.Body["name"]));
72+
73+
var stages = Translate(collection, queryable);
74+
AssertStages(stages, "{ $match : { $expr : { $in : [{ $toString : '$Body.name' }, ['Test1', 'Test2']] } } }");
75+
76+
var results = queryable.ToList();
77+
results.Select(x => x.Id).Should().Equal(1, 2);
78+
}
79+
80+
[Fact]
81+
public void Where_with_predicate_on_Caption_should_work()
82+
{
83+
var collection = CreateCollection();
84+
85+
var queryable = collection
86+
.AsQueryable()
87+
.Where(x => new[] { "Test1", "Test2" }.Contains(x.Caption));
88+
89+
var stages = Translate(collection, queryable);
90+
AssertStages(stages, "{ $match : { Caption : { $in : ['Test1', 'Test2'] } } }");
91+
92+
var results = queryable.ToList();
93+
results.Select(x => x.Id).Should().Equal(1, 2);
94+
}
95+
96+
private IMongoCollection<Entity> CreateCollection()
97+
{
98+
var collection = GetCollection<Entity>("C");
99+
100+
CreateCollection(
101+
collection,
102+
new Entity { Id = 1, Body = BsonDocument.Parse("{ name : 'Test1' }"), Caption = "Test1" },
103+
new Entity { Id = 2, Body = BsonDocument.Parse("{ name : 'Test2' }"), Caption = "Test2" },
104+
new Entity { Id = 3, Body = BsonDocument.Parse("{ name : 'Test3' }"), Caption = "Test3" });
105+
106+
return collection;
107+
}
108+
109+
private class Entity
110+
{
111+
public int Id { get; set; }
112+
public BsonDocument Body { get; set; }
113+
public string Caption { get; set; }
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)