@@ -4492,36 +4492,36 @@ public void TestWhereSEndsWithAbcNot()
4492
4492
[ Test ]
4493
4493
public void TestWhereSIndexOfAnyBC ( )
4494
4494
{
4495
- var collection = _database . GetCollection ( "temp" ) ;
4496
- collection . Drop ( ) ;
4497
- collection . Insert ( new C { S = "bxxx" } ) ;
4498
- collection . Insert ( new C { S = "xbxx" } ) ;
4499
- collection . Insert ( new C { S = "xxbx" } ) ;
4500
- collection . Insert ( new C { S = "xxxb" } ) ;
4501
- collection . Insert ( new C { S = "bxbx" } ) ;
4502
- collection . Insert ( new C { S = "xbbx" } ) ;
4503
- collection . Insert ( new C { S = "xxbb" } ) ;
4495
+ var tempCollection = _database . GetCollection ( "temp" ) ;
4496
+ tempCollection . Drop ( ) ;
4497
+ tempCollection . Insert ( new C { S = "bxxx" } ) ;
4498
+ tempCollection . Insert ( new C { S = "xbxx" } ) ;
4499
+ tempCollection . Insert ( new C { S = "xxbx" } ) ;
4500
+ tempCollection . Insert ( new C { S = "xxxb" } ) ;
4501
+ tempCollection . Insert ( new C { S = "bxbx" } ) ;
4502
+ tempCollection . Insert ( new C { S = "xbbx" } ) ;
4503
+ tempCollection . Insert ( new C { S = "xxbb" } ) ;
4504
4504
4505
4505
var query1 =
4506
- from c in collection . AsQueryable < C > ( )
4506
+ from c in tempCollection . AsQueryable < C > ( )
4507
4507
where c . S . IndexOfAny ( new char [ ] { 'b' , 'c' } ) == 2
4508
4508
select c ;
4509
4509
Assert . AreEqual ( 2 , Consume ( query1 ) ) ;
4510
4510
4511
4511
var query2 =
4512
- from c in collection . AsQueryable < C > ( )
4512
+ from c in tempCollection . AsQueryable < C > ( )
4513
4513
where c . S . IndexOfAny ( new char [ ] { 'b' , 'c' } , 1 ) == 2
4514
4514
select c ;
4515
4515
Assert . AreEqual ( 3 , Consume ( query2 ) ) ;
4516
4516
4517
4517
var query3 =
4518
- from c in collection . AsQueryable < C > ( )
4518
+ from c in tempCollection . AsQueryable < C > ( )
4519
4519
where c . S . IndexOfAny ( new char [ ] { 'b' , 'c' } , 1 , 1 ) == 2
4520
4520
select c ;
4521
4521
Assert . AreEqual ( 0 , Consume ( query3 ) ) ;
4522
4522
4523
4523
var query4 =
4524
- from c in collection . AsQueryable < C > ( )
4524
+ from c in tempCollection . AsQueryable < C > ( )
4525
4525
where c . S . IndexOfAny ( new char [ ] { 'b' , 'c' } , 1 , 2 ) == 2
4526
4526
select c ;
4527
4527
Assert . AreEqual ( 3 , Consume ( query4 ) ) ;
@@ -4599,36 +4599,36 @@ where c.S.IndexOfAny(new char[] { 'b', '-', 'c' }, 1, 2) == 1
4599
4599
[ Test ]
4600
4600
public void TestWhereSIndexOfB ( )
4601
4601
{
4602
- var collection = _database . GetCollection ( "temp" ) ;
4603
- collection . Drop ( ) ;
4604
- collection . Insert ( new C { S = "bxxx" } ) ;
4605
- collection . Insert ( new C { S = "xbxx" } ) ;
4606
- collection . Insert ( new C { S = "xxbx" } ) ;
4607
- collection . Insert ( new C { S = "xxxb" } ) ;
4608
- collection . Insert ( new C { S = "bxbx" } ) ;
4609
- collection . Insert ( new C { S = "xbbx" } ) ;
4610
- collection . Insert ( new C { S = "xxbb" } ) ;
4602
+ var tempCollection = _database . GetCollection ( "temp" ) ;
4603
+ tempCollection . Drop ( ) ;
4604
+ tempCollection . Insert ( new C { S = "bxxx" } ) ;
4605
+ tempCollection . Insert ( new C { S = "xbxx" } ) ;
4606
+ tempCollection . Insert ( new C { S = "xxbx" } ) ;
4607
+ tempCollection . Insert ( new C { S = "xxxb" } ) ;
4608
+ tempCollection . Insert ( new C { S = "bxbx" } ) ;
4609
+ tempCollection . Insert ( new C { S = "xbbx" } ) ;
4610
+ tempCollection . Insert ( new C { S = "xxbb" } ) ;
4611
4611
4612
4612
var query1 =
4613
- from c in collection . AsQueryable < C > ( )
4613
+ from c in tempCollection . AsQueryable < C > ( )
4614
4614
where c . S . IndexOf ( 'b' ) == 2
4615
4615
select c ;
4616
4616
Assert . AreEqual ( 2 , Consume ( query1 ) ) ;
4617
4617
4618
4618
var query2 =
4619
- from c in collection . AsQueryable < C > ( )
4619
+ from c in tempCollection . AsQueryable < C > ( )
4620
4620
where c . S . IndexOf ( 'b' , 1 ) == 2
4621
4621
select c ;
4622
4622
Assert . AreEqual ( 3 , Consume ( query2 ) ) ;
4623
4623
4624
4624
var query3 =
4625
- from c in collection . AsQueryable < C > ( )
4625
+ from c in tempCollection . AsQueryable < C > ( )
4626
4626
where c . S . IndexOf ( 'b' , 1 , 1 ) == 2
4627
4627
select c ;
4628
4628
Assert . AreEqual ( 0 , Consume ( query3 ) ) ;
4629
4629
4630
4630
var query4 =
4631
- from c in collection . AsQueryable < C > ( )
4631
+ from c in tempCollection . AsQueryable < C > ( )
4632
4632
where c . S . IndexOf ( 'b' , 1 , 2 ) == 2
4633
4633
select c ;
4634
4634
Assert . AreEqual ( 3 , Consume ( query4 ) ) ;
@@ -4706,35 +4706,35 @@ where c.S.IndexOf('b', 1, 2) == 1
4706
4706
[ Test ]
4707
4707
public void TestWhereSIndexOfXyz ( )
4708
4708
{
4709
- var collection = _database . GetCollection ( "temp" ) ;
4710
- collection . Drop ( ) ;
4711
- collection . Insert ( new C { S = "xyzaaa" } ) ;
4712
- collection . Insert ( new C { S = "axyzaa" } ) ;
4713
- collection . Insert ( new C { S = "aaxyza" } ) ;
4714
- collection . Insert ( new C { S = "aaaxyz" } ) ;
4715
- collection . Insert ( new C { S = "aaaaxy" } ) ;
4716
- collection . Insert ( new C { S = "xyzxyz" } ) ;
4709
+ var tempCollection = _database . GetCollection ( "temp" ) ;
4710
+ tempCollection . Drop ( ) ;
4711
+ tempCollection . Insert ( new C { S = "xyzaaa" } ) ;
4712
+ tempCollection . Insert ( new C { S = "axyzaa" } ) ;
4713
+ tempCollection . Insert ( new C { S = "aaxyza" } ) ;
4714
+ tempCollection . Insert ( new C { S = "aaaxyz" } ) ;
4715
+ tempCollection . Insert ( new C { S = "aaaaxy" } ) ;
4716
+ tempCollection . Insert ( new C { S = "xyzxyz" } ) ;
4717
4717
4718
4718
var query1 =
4719
- from c in collection . AsQueryable < C > ( )
4719
+ from c in tempCollection . AsQueryable < C > ( )
4720
4720
where c . S . IndexOf ( "xyz" ) == 3
4721
4721
select c ;
4722
4722
Assert . AreEqual ( 1 , Consume ( query1 ) ) ;
4723
4723
4724
4724
var query2 =
4725
- from c in collection . AsQueryable < C > ( )
4725
+ from c in tempCollection . AsQueryable < C > ( )
4726
4726
where c . S . IndexOf ( "xyz" , 1 ) == 3
4727
4727
select c ;
4728
4728
Assert . AreEqual ( 2 , Consume ( query2 ) ) ;
4729
4729
4730
4730
var query3 =
4731
- from c in collection . AsQueryable < C > ( )
4731
+ from c in tempCollection . AsQueryable < C > ( )
4732
4732
where c . S . IndexOf ( "xyz" , 1 , 4 ) == 3
4733
4733
select c ;
4734
4734
Assert . AreEqual ( 0 , Consume ( query3 ) ) ; // substring isn't long enough to match
4735
4735
4736
4736
var query4 =
4737
- from c in collection . AsQueryable < C > ( )
4737
+ from c in tempCollection . AsQueryable < C > ( )
4738
4738
where c . S . IndexOf ( "xyz" , 1 , 5 ) == 3
4739
4739
select c ;
4740
4740
Assert . AreEqual ( 2 , Consume ( query4 ) ) ;
@@ -4926,6 +4926,36 @@ where Regex.IsMatch(c.S, "^abc", RegexOptions.IgnoreCase)
4926
4926
Assert . AreEqual ( 1 , Consume ( query ) ) ;
4927
4927
}
4928
4928
4929
+ [ Test ]
4930
+ public void TestWhereSIsNullOrEmpty ( )
4931
+ {
4932
+ var tempCollection = _database . GetCollection ( "temp" ) ;
4933
+ tempCollection . Drop ( ) ;
4934
+ tempCollection . Insert ( new C ( ) ) ; // serialized document will have no "s" field
4935
+ tempCollection . Insert ( new BsonDocument ( "s" , BsonNull . Value ) ) ; // work around [BsonIgnoreIfNull] on S
4936
+ tempCollection . Insert ( new C { S = "" } ) ;
4937
+ tempCollection . Insert ( new C { S = "x" } ) ;
4938
+
4939
+ var query = from c in tempCollection . AsQueryable < C > ( )
4940
+ where string . IsNullOrEmpty ( c . S )
4941
+ select c ;
4942
+
4943
+ var translatedQuery = MongoQueryTranslator . Translate ( query ) ;
4944
+ Assert . IsInstanceOf < SelectQuery > ( translatedQuery ) ;
4945
+ Assert . AreSame ( tempCollection , translatedQuery . Collection ) ;
4946
+ Assert . AreSame ( typeof ( C ) , translatedQuery . DocumentType ) ;
4947
+
4948
+ var selectQuery = ( SelectQuery ) translatedQuery ;
4949
+ Assert . AreEqual ( "(C c) => String.IsNullOrEmpty(c.S)" , ExpressionFormatter . ToString ( selectQuery . Where ) ) ;
4950
+ Assert . IsNull ( selectQuery . OrderBy ) ;
4951
+ Assert . IsNull ( selectQuery . Projection ) ;
4952
+ Assert . IsNull ( selectQuery . Skip ) ;
4953
+ Assert . IsNull ( selectQuery . Take ) ;
4954
+
4955
+ Assert . AreEqual ( "{ \" $or\" : [{ \" s\" : { \" $type\" : 10 } }, { \" s\" : \" \" }] }" , selectQuery . BuildQuery ( ) . ToJson ( ) ) ;
4956
+ Assert . AreEqual ( 2 , Consume ( query ) ) ;
4957
+ }
4958
+
4929
4959
[ Test ]
4930
4960
public void TestWhereSLengthEquals3 ( )
4931
4961
{
0 commit comments