Skip to content

Commit 345aaae

Browse files
committed
CSHARP-4057: Verify that issue is not present in LINQ3.
1 parent c66a4e6 commit 345aaae

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.Linq;
17+
using FluentAssertions;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
21+
{
22+
public class CSharp4057Tests : Linq3IntegrationTest
23+
{
24+
[Fact]
25+
public void Aggregate_Project_should_work()
26+
{
27+
var collection = CreateProductsCollection();
28+
29+
var aggregate = collection.Aggregate()
30+
.Sort(Builders<Product>.Sort.Ascending(p => p.Id))
31+
.Project(
32+
augmentedProductType => new ProductTypeSearchResult
33+
{
34+
IsExternalUrl = string.IsNullOrEmpty(augmentedProductType.ShopUrl ?? "")
35+
});
36+
37+
var stages = Translate(collection, aggregate);
38+
AssertStages(
39+
stages,
40+
"{ $sort : { _id : 1 } }",
41+
"{ $project : { IsExternalUrl : { $in : [{ $ifNull : ['$ShopUrl', ''] }, [null, '']] }, _id : 0 } }");
42+
43+
var results = aggregate.ToList();
44+
results.Select(r => r.IsExternalUrl).Should().Equal(true, true, false);
45+
}
46+
47+
[Fact]
48+
public void Queryable_Select()
49+
{
50+
var collection = CreateProductsCollection();
51+
52+
var queryable = collection.AsQueryable()
53+
.OrderBy(p => p.Id)
54+
.Select(
55+
augmentedProductType => new ProductTypeSearchResult
56+
{
57+
IsExternalUrl = string.IsNullOrEmpty(augmentedProductType.ShopUrl ?? "")
58+
});
59+
60+
var stages = Translate(collection, queryable);
61+
AssertStages(
62+
stages,
63+
"{ $sort : { _id : 1 } }",
64+
"{ $project : { IsExternalUrl : { $in : [{ $ifNull : ['$ShopUrl', ''] }, [null, '']] }, _id : 0 } }");
65+
66+
var results = queryable.ToList();
67+
results.Select(r => r.IsExternalUrl).Should().Equal(true, true, false);
68+
}
69+
70+
private IMongoCollection<Product> CreateProductsCollection()
71+
{
72+
var collection = GetCollection<Product>();
73+
74+
var documents = new[]
75+
{
76+
new Product { Id = 1, ShopUrl = null },
77+
new Product { Id = 2, ShopUrl = "" },
78+
new Product { Id = 3, ShopUrl = "abc" }
79+
};
80+
CreateCollection(collection, documents);
81+
82+
return collection;
83+
}
84+
85+
private class Product
86+
{
87+
public int Id { get; set; }
88+
public string ShopUrl { get; set; }
89+
}
90+
91+
private class ProductTypeSearchResult
92+
{
93+
public bool IsExternalUrl { get; set; }
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)