Skip to content

Commit eea6c74

Browse files
committed
CSHARP-5730: Added new tests for Select.
1 parent f4e62c6 commit eea6c74

File tree

1 file changed

+90
-5
lines changed

1 file changed

+90
-5
lines changed

tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5730Tests.cs

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public CSharp5730Tests(ClassFixture fixture)
4343
[InlineData(10, "{ $match : { A : { $lte : 'B' } } }", new int[] { 1, 2, 3 })]
4444
[InlineData(11, "{ $match : { A : { $lte : 'B' } } }", new int[] { 1, 2, 3 })]
4545
[InlineData(12, "{ $match : { A : { $gte : 'B' } } }", new int[] { 3, 4, 5, 6 })]
46-
public void Where_String_Compare_field_to_constant_should_work(int scenario, string expectedStage, int[] expectedResults)
46+
public void Where_String_Compare_field_to_constant_should_work(int scenario, string expectedStage, int[] expectedIds)
4747
{
4848
var collection = Fixture.Collection;
4949

@@ -65,7 +65,7 @@ public void Where_String_Compare_field_to_constant_should_work(int scenario, str
6565
_ => throw new ArgumentException($"Invalid scenario: {scenario}.")
6666
};
6767

68-
Assert(collection, queryable, expectedStage, expectedResults);
68+
AssertIds(collection, queryable, expectedStage, expectedIds);
6969
}
7070

7171
[Theory]
@@ -81,7 +81,7 @@ public void Where_String_Compare_field_to_constant_should_work(int scenario, str
8181
[InlineData(10, "{ $match : { B : { $gte : 'A' } } }", new int[] { 1, 2, 3, 4, 5, 6 })]
8282
[InlineData(11, "{ $match : { B : { $gte : 'A' } } }", new int[] { 1, 2, 3, 4, 5, 6 })]
8383
[InlineData(12, "{ $match : { B : { $lte : 'A' } } }", new int[] { 1, 3 })]
84-
public void Where_String_Compare_constant_to_field_should_work(int scenario, string expectedStage, int[] expectedResults)
84+
public void Where_String_Compare_constant_to_field_should_work(int scenario, string expectedStage, int[] expectedIds)
8585
{
8686
var collection = Fixture.Collection;
8787

@@ -103,16 +103,101 @@ public void Where_String_Compare_constant_to_field_should_work(int scenario, str
103103
_ => throw new ArgumentException($"Invalid scenario: {scenario}.")
104104
};
105105

106+
AssertIds(collection, queryable, expectedStage, expectedIds);
107+
}
108+
109+
[Theory]
110+
[InlineData( 1, "{ $project : { _v : { $eq : [{ $cmp : ['$A', 'B'] }, -1] }, _id : 0 } }", new bool[] { true, true, false, false, false, false })]
111+
[InlineData( 2, "{ $project : { _v : { $eq : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { false, false, true, false, false, false })]
112+
[InlineData( 3, "{ $project : { _v : { $eq : [{ $cmp : ['$A', 'B'] }, 1] }, _id : 0 } }", new bool[] { false, false, false, true, true, true })]
113+
[InlineData( 4, "{ $project : { _v : { $ne : [{ $cmp : ['$A', 'B'] }, -1] }, _id : 0 } }", new bool[] { false, false, true, true, true, true })]
114+
[InlineData( 5, "{ $project : { _v : { $ne : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { true, true, false, true, true, true })]
115+
[InlineData( 6, "{ $project : { _v : { $ne : [{ $cmp : ['$A', 'B'] }, 1] }, _id : 0 } }", new bool[] { true, true, true, false, false, false })]
116+
[InlineData( 7, "{ $project : { _v : { $gt : [{ $cmp : ['$A', 'B'] }, -1] }, _id : 0 } }", new bool[] { false, false, true, true, true, true })]
117+
[InlineData( 8, "{ $project : { _v : { $gt : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { false, false, false, true, true, true })]
118+
[InlineData( 9, "{ $project : { _v : { $lt : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { true, true, false, false, false, false })]
119+
[InlineData( 10, "{ $project : { _v : { $lt : [{ $cmp : ['$A', 'B'] }, 1] }, _id : 0 } }", new bool[] { true, true, true, false, false, false })]
120+
[InlineData( 11, "{ $project : { _v : { $lte : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { true, true, true, false, false, false })]
121+
[InlineData( 12, "{ $project : { _v : { $gte : [{ $cmp : ['$A', 'B'] }, 0] }, _id : 0 } }", new bool[] { false, false, true, true, true, true })]
122+
public void Select_String_Compare_field_to_constant_should_work(int scenario, string expectedStage, bool[] expectedResults)
123+
{
124+
var collection = Fixture.Collection;
125+
126+
var queryable = scenario switch
127+
{
128+
// Compare field to constant
129+
1 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") == -1),
130+
2 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") == 0),
131+
3 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") == 1),
132+
4 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") != -1),
133+
5 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") != 0),
134+
6 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") != 1),
135+
7 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") > -1),
136+
8 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") > 0),
137+
9 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") < 0),
138+
10 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") < 1),
139+
11 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") <= 0),
140+
12 => collection.AsQueryable().Select(x => string.Compare(x.A, "B") >= 0),
141+
_ => throw new ArgumentException($"Invalid scenario: {scenario}.")
142+
};
143+
106144
Assert(collection, queryable, expectedStage, expectedResults);
107145
}
108146

109-
private void Assert(IMongoCollection<C> collection, IQueryable<C> queryable, string expectedStage, int[] expectedResults)
147+
[Theory]
148+
[InlineData( 1, "{ $project : { _v : { $eq : [{ $cmp : ['A', '$B'] }, -1] }, _id : 0 } }", new bool[] { false, true, false, true, true, true })]
149+
[InlineData( 2, "{ $project : { _v : { $eq : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { true, false, true, false, false, false })]
150+
[InlineData( 3, "{ $project : { _v : { $eq : [{ $cmp : ['A', '$B'] }, 1] }, _id : 0 } }", new bool[] { false, false, false, false, false, false })]
151+
[InlineData( 4, "{ $project : { _v : { $ne : [{ $cmp : ['A', '$B'] }, -1] }, _id : 0 } }", new bool[] { true, false, true, false, false, false })]
152+
[InlineData( 5, "{ $project : { _v : { $ne : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { false, true, false, true, true, true })]
153+
[InlineData( 6, "{ $project : { _v : { $ne : [{ $cmp : ['A', '$B'] }, 1] }, _id : 0 } }", new bool[] { true, true, true, true, true, true })]
154+
[InlineData( 7, "{ $project : { _v : { $gt : [{ $cmp : ['A', '$B'] }, -1] }, _id : 0 } }", new bool[] { true, false, true, false, false, false })]
155+
[InlineData( 8, "{ $project : { _v : { $gt : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { false, false, false, false, false, false })]
156+
[InlineData( 9, "{ $project : { _v : { $lt : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { false, true, false, true, true, true })]
157+
[InlineData( 10, "{ $project : { _v : { $lt : [{ $cmp : ['A', '$B'] }, 1] }, _id : 0 } }", new bool[] { true, true, true, true, true, true })]
158+
[InlineData( 11, "{ $project : { _v : { $lte : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { true, true, true, true, true, true })]
159+
[InlineData( 12, "{ $project : { _v : { $gte : [{ $cmp : ['A', '$B'] }, 0] }, _id : 0 } }", new bool[] { true, false, true, false, false, false })]
160+
public void Select_String_Compare_constant_to_field_should_work(int scenario, string expectedStage, bool[] expectedResults)
161+
{
162+
var collection = Fixture.Collection;
163+
164+
var queryable = scenario switch
165+
{
166+
// Compare field to constant
167+
1 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) == -1),
168+
2 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) == 0),
169+
3 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) == 1),
170+
4 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) != -1),
171+
5 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) != 0),
172+
6 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) != 1),
173+
7 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) > -1),
174+
8 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) > 0),
175+
9 => collection.AsQueryable().Select(x => string.Compare ("A", x.B) < 0),
176+
10 => collection.AsQueryable().Select(x => string.Compare("A", x.B) < 1),
177+
11 => collection.AsQueryable().Select(x => string.Compare("A", x.B) <= 0),
178+
12 => collection.AsQueryable().Select(x => string.Compare("A", x.B) >= 0),
179+
_ => throw new ArgumentException($"Invalid scenario: {scenario}.")
180+
};
181+
182+
Assert(collection, queryable, expectedStage, expectedResults);
183+
}
184+
185+
private void Assert<TResult>(IMongoCollection<C> collection, IQueryable<TResult> queryable, string expectedStage, TResult[] expectedResults)
186+
{
187+
var stages = Translate(collection, queryable);
188+
AssertStages(stages, expectedStage);
189+
190+
var results = queryable.ToList();
191+
results.Should().Equal(expectedResults);
192+
}
193+
194+
private void AssertIds(IMongoCollection<C> collection, IQueryable<C> queryable, string expectedStage, int[] expectedIds)
110195
{
111196
var stages = Translate(collection, queryable);
112197
AssertStages(stages, expectedStage);
113198

114199
var results = queryable.ToList();
115-
results.Select(x => x.Id).Should().Equal(expectedResults);
200+
results.Select(x => x.Id).Should().Equal(expectedIds);
116201
}
117202

118203
public class C

0 commit comments

Comments
 (0)