Skip to content

Commit dd0cae3

Browse files
committed
CSHARP-4328: Verify First in filter works in LINQ3.
1 parent 1c4c52b commit dd0cae3

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Jira/CSharp2003Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Find_BitsAllClear_should_work()
2929
var mask = E.E2 | E.E4;
3030
var find = collection.Find(x => (x.E & mask) == 0);
3131

32-
var filter = TranslateFilter(collection, find);
32+
var filter = Translate(collection, find.Filter);
3333
filter.Should().Be("{ E : { $bitsAllClear : 6 } }");
3434

3535
var results = find.ToList().OrderBy(x => x.Id).ToList();
@@ -43,7 +43,7 @@ public void Find_BitsAllSet_should_work()
4343
var mask = E.E2 | E.E4;
4444
var find = collection.Find(x => (x.E & mask) == mask);
4545

46-
var filter = TranslateFilter(collection, find);
46+
var filter = Translate(collection, find.Filter);
4747
filter.Should().Be("{ E : { $bitsAllSet : 6 } }");
4848

4949
var results = find.ToList().OrderBy(x => x.Id).ToList();
@@ -57,7 +57,7 @@ public void Find_BitsAnyClear_should_work()
5757
var mask = E.E2 | E.E4;
5858
var find = collection.Find(x => (x.E & mask) != mask);
5959

60-
var filter = TranslateFilter(collection, find);
60+
var filter = Translate(collection, find.Filter);
6161
filter.Should().Be("{ E : { $bitsAnyClear : 6 } }");
6262

6363
var results = find.ToList().OrderBy(x => x.Id).ToList();
@@ -71,7 +71,7 @@ public void Find_BitsAnySet_should_work()
7171
var mask = E.E2 | E.E4;
7272
var find = collection.Find(x => (x.E & mask) != 0);
7373

74-
var filter = TranslateFilter(collection, find);
74+
var filter = Translate(collection, find.Filter);
7575
filter.Should().Be("{ E : { $bitsAnySet : 6 } }");
7676

7777
var results = find.ToList().OrderBy(x => x.Id).ToList();
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.Driver.Linq;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
23+
{
24+
public class CSharp4328Tests : Linq3IntegrationTest
25+
{
26+
[Fact]
27+
public void Filter_using_First_should_work()
28+
{
29+
var collection = CreateCollection();
30+
var startTargetDeliveryDate = new DateTime(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc);
31+
var endTargetDeliveryDate = new DateTime(2022, 12, 31, 0, 0, 0, DateTimeKind.Utc);
32+
var country = "USA";
33+
34+
var filterBuilder = Builders<SubscriptionRepositorySubscriptionModel>.Filter;
35+
var statusFilter = filterBuilder.Where(x => x.SubscriptionStatus == SubscriptionStatus.Active);
36+
var dateFilter = filterBuilder.Where(
37+
x =>
38+
x.UpcomingOrders.First().NextTargetDeliveryDate >= startTargetDeliveryDate &&
39+
x.UpcomingOrders.First().NextTargetDeliveryDate <= endTargetDeliveryDate);
40+
var filter = filterBuilder.And(statusFilter, dateFilter);
41+
if (!string.IsNullOrEmpty(country))
42+
{
43+
filter = filterBuilder.And(filter, filterBuilder.Where(x => x.ShippingAddress.CountryCode == country));
44+
}
45+
46+
var renderedFilter = Translate(collection, filter);
47+
renderedFilter.Should().Be(
48+
@"
49+
{
50+
SubscriptionStatus : 1,
51+
'UpcomingOrders.0.NextTargetDeliveryDate' : {
52+
$gte : ISODate('2022-01-01T00:00:00Z'),
53+
$lte : ISODate('2022-12-31T00:00:00Z')
54+
},
55+
'ShippingAddress.CountryCode' : 'USA'
56+
}");
57+
58+
var result = collection.Distinct(x => x.CustomerId, filter).ToList();
59+
result.Should().Equal(2);
60+
}
61+
62+
private IMongoCollection<SubscriptionRepositorySubscriptionModel> CreateCollection()
63+
{
64+
var collection = GetCollection<SubscriptionRepositorySubscriptionModel>();
65+
66+
var documents = new SubscriptionRepositorySubscriptionModel[]
67+
{
68+
new SubscriptionRepositorySubscriptionModel
69+
{
70+
Id = 1,
71+
CustomerId = 1,
72+
SubscriptionStatus = SubscriptionStatus.Inactive,
73+
UpcomingOrders = new Order[0],
74+
ShippingAddress = null
75+
},
76+
new SubscriptionRepositorySubscriptionModel
77+
{
78+
Id = 2,
79+
CustomerId = 2,
80+
SubscriptionStatus = SubscriptionStatus.Active,
81+
UpcomingOrders = new Order[] { new Order { NextTargetDeliveryDate = new DateTime(2022, 12, 1, 0, 0, 0, DateTimeKind.Utc) } },
82+
ShippingAddress = new Address { CountryCode = "USA" }
83+
}
84+
85+
};
86+
CreateCollection(collection, documents);
87+
88+
return collection;
89+
}
90+
91+
private class SubscriptionRepositorySubscriptionModel
92+
{
93+
public int Id { get; set; }
94+
public int CustomerId { get; set; }
95+
public SubscriptionStatus SubscriptionStatus { get; set; }
96+
public Order[] UpcomingOrders { get; set; }
97+
public Address ShippingAddress { get; set; }
98+
}
99+
100+
private class Order
101+
{
102+
public DateTime NextTargetDeliveryDate { get; set; }
103+
}
104+
105+
private class Address
106+
{
107+
public string CountryCode { get; set; }
108+
}
109+
110+
private enum SubscriptionStatus
111+
{
112+
Inactive = 0,
113+
Active = 1
114+
}
115+
}
116+
}

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Linq3IntegrationTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ protected List<BsonDocument> Translate<TDocument, TResult>(IQueryable<TResult> q
107107
return stages.Select(s => s.Render().AsBsonDocument).ToList();
108108
}
109109

110-
protected BsonDocument TranslateFilter<TDocument>(IMongoCollection<TDocument> collection, IFindFluent<TDocument, TDocument> find)
110+
protected BsonDocument Translate<TDocument>(IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filterDefinition)
111111
{
112-
var filterDefinition = find.Filter;
113112
var documentSerializer = collection.DocumentSerializer;
114113
var serializerRegistry = BsonSerializer.SerializerRegistry;
115114
var linqProvider = collection.Database.Client.Settings.LinqProvider;

0 commit comments

Comments
 (0)