Skip to content

Commit c9eba24

Browse files
staywellandycraiggwilson
authored andcommitted
Unit tests for ToLowerInvariant, ToUpperInvariant
Unit tests were added for the invariant versions of the linq string methods.
1 parent a5aa944 commit c9eba24

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed

DriverUnitTests/Linq/SelectQueryTests.cs

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6014,6 +6014,305 @@ where c.S.ToUpper() != null
60146014
Assert.AreEqual(2, Consume(query));
60156015
}
60166016

6017+
[Test]
6018+
public void TestWhereSTrimStartTrimEndToLowerInvariantContainsXyz()
6019+
{
6020+
var query = from c in _collection.AsQueryable<C>()
6021+
where c.S.TrimStart(' ', '.', '-', '\t').TrimEnd().ToLowerInvariant().Contains("xyz")
6022+
select c;
6023+
6024+
var translatedQuery = MongoQueryTranslator.Translate(query);
6025+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6026+
Assert.AreSame(_collection, translatedQuery.Collection);
6027+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6028+
6029+
var selectQuery = (SelectQuery)translatedQuery;
6030+
Assert.AreEqual("(C c) => c.S.TrimStart(Char[]:{ ' ', '.', '-', '\t' }).TrimEnd(Char[]:{ }).ToLowerInvariant().Contains(\"xyz\")", ExpressionFormatter.ToString(selectQuery.Where));
6031+
Assert.IsNull(selectQuery.OrderBy);
6032+
Assert.IsNull(selectQuery.Projection);
6033+
Assert.IsNull(selectQuery.Skip);
6034+
Assert.IsNull(selectQuery.Take);
6035+
6036+
Assert.AreEqual("{ \"s\" : /^[\\ \\.\\-\\t]*.*xyz.*\\s*$/is }", selectQuery.BuildQuery().ToJson());
6037+
Assert.AreEqual(1, Consume(query));
6038+
}
6039+
6040+
[Test]
6041+
public void TestWhereSToLowerInvariantEqualsConstantLowerCaseValue()
6042+
{
6043+
var query = from c in _collection.AsQueryable<C>()
6044+
where c.S.ToLowerInvariant() == "abc"
6045+
select c;
6046+
6047+
var translatedQuery = MongoQueryTranslator.Translate(query);
6048+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6049+
Assert.AreSame(_collection, translatedQuery.Collection);
6050+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6051+
6052+
var selectQuery = (SelectQuery)translatedQuery;
6053+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() == \"abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6054+
Assert.IsNull(selectQuery.OrderBy);
6055+
Assert.IsNull(selectQuery.Projection);
6056+
Assert.IsNull(selectQuery.Skip);
6057+
Assert.IsNull(selectQuery.Take);
6058+
6059+
Assert.AreEqual("{ \"s\" : /^abc$/i }", selectQuery.BuildQuery().ToJson());
6060+
Assert.AreEqual(1, Consume(query));
6061+
}
6062+
6063+
[Test]
6064+
public void TestWhereSToLowerInvariantDoesNotEqualConstantLowerCaseValue()
6065+
{
6066+
var query = from c in _collection.AsQueryable<C>()
6067+
where c.S.ToLowerInvariant() != "abc"
6068+
select c;
6069+
6070+
var translatedQuery = MongoQueryTranslator.Translate(query);
6071+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6072+
Assert.AreSame(_collection, translatedQuery.Collection);
6073+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6074+
6075+
var selectQuery = (SelectQuery)translatedQuery;
6076+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() != \"abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6077+
Assert.IsNull(selectQuery.OrderBy);
6078+
Assert.IsNull(selectQuery.Projection);
6079+
Assert.IsNull(selectQuery.Skip);
6080+
Assert.IsNull(selectQuery.Take);
6081+
6082+
Assert.AreEqual("{ \"s\" : { \"$not\" : /^abc$/i } }", selectQuery.BuildQuery().ToJson());
6083+
Assert.AreEqual(4, Consume(query));
6084+
}
6085+
6086+
[Test]
6087+
public void TestWhereSToLowerInvariantEqualsConstantMixedCaseValue()
6088+
{
6089+
var query = from c in _collection.AsQueryable<C>()
6090+
where c.S.ToLowerInvariant() == "Abc"
6091+
select c;
6092+
6093+
var translatedQuery = MongoQueryTranslator.Translate(query);
6094+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6095+
Assert.AreSame(_collection, translatedQuery.Collection);
6096+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6097+
6098+
var selectQuery = (SelectQuery)translatedQuery;
6099+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() == \"Abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6100+
Assert.IsNull(selectQuery.OrderBy);
6101+
Assert.IsNull(selectQuery.Projection);
6102+
Assert.IsNull(selectQuery.Skip);
6103+
Assert.IsNull(selectQuery.Take);
6104+
6105+
Assert.AreEqual("{ \"_id\" : { \"$type\" : -1 } }", selectQuery.BuildQuery().ToJson());
6106+
Assert.AreEqual(0, Consume(query));
6107+
}
6108+
6109+
[Test]
6110+
public void TestWhereSToLowerInvariantDoesNotEqualConstantMixedCaseValue()
6111+
{
6112+
var query = from c in _collection.AsQueryable<C>()
6113+
where c.S.ToLowerInvariant() != "Abc"
6114+
select c;
6115+
6116+
var translatedQuery = MongoQueryTranslator.Translate(query);
6117+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6118+
Assert.AreSame(_collection, translatedQuery.Collection);
6119+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6120+
6121+
var selectQuery = (SelectQuery)translatedQuery;
6122+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() != \"Abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6123+
Assert.IsNull(selectQuery.OrderBy);
6124+
Assert.IsNull(selectQuery.Projection);
6125+
Assert.IsNull(selectQuery.Skip);
6126+
Assert.IsNull(selectQuery.Take);
6127+
6128+
Assert.AreEqual("{ }", selectQuery.BuildQuery().ToJson());
6129+
Assert.AreEqual(5, Consume(query));
6130+
}
6131+
6132+
[Test]
6133+
public void TestWhereSToLowerInvariantEqualsNullValue()
6134+
{
6135+
var query = from c in _collection.AsQueryable<C>()
6136+
where c.S.ToLowerInvariant() == null
6137+
select c;
6138+
6139+
var translatedQuery = MongoQueryTranslator.Translate(query);
6140+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6141+
Assert.AreSame(_collection, translatedQuery.Collection);
6142+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6143+
6144+
var selectQuery = (SelectQuery)translatedQuery;
6145+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() == null)", ExpressionFormatter.ToString(selectQuery.Where));
6146+
Assert.IsNull(selectQuery.OrderBy);
6147+
Assert.IsNull(selectQuery.Projection);
6148+
Assert.IsNull(selectQuery.Skip);
6149+
Assert.IsNull(selectQuery.Take);
6150+
6151+
Assert.AreEqual("{ \"s\" : null }", selectQuery.BuildQuery().ToJson());
6152+
Assert.AreEqual(3, Consume(query));
6153+
}
6154+
6155+
[Test]
6156+
public void TestWhereSToLowerInvariantDoesNotEqualNullValue()
6157+
{
6158+
var query = from c in _collection.AsQueryable<C>()
6159+
where c.S.ToLowerInvariant() != null
6160+
select c;
6161+
6162+
var translatedQuery = MongoQueryTranslator.Translate(query);
6163+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6164+
Assert.AreSame(_collection, translatedQuery.Collection);
6165+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6166+
6167+
var selectQuery = (SelectQuery)translatedQuery;
6168+
Assert.AreEqual("(C c) => (c.S.ToLowerInvariant() != null)", ExpressionFormatter.ToString(selectQuery.Where));
6169+
Assert.IsNull(selectQuery.OrderBy);
6170+
Assert.IsNull(selectQuery.Projection);
6171+
Assert.IsNull(selectQuery.Skip);
6172+
Assert.IsNull(selectQuery.Take);
6173+
6174+
Assert.AreEqual("{ \"s\" : { \"$ne\" : null } }", selectQuery.BuildQuery().ToJson());
6175+
Assert.AreEqual(2, Consume(query));
6176+
}
6177+
6178+
[Test]
6179+
public void TestWhereSToUpperInvariantEqualsConstantLowerCaseValue()
6180+
{
6181+
var query = from c in _collection.AsQueryable<C>()
6182+
where c.S.ToUpperInvariant() == "abc"
6183+
select c;
6184+
6185+
var translatedQuery = MongoQueryTranslator.Translate(query);
6186+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6187+
Assert.AreSame(_collection, translatedQuery.Collection);
6188+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6189+
6190+
var selectQuery = (SelectQuery)translatedQuery;
6191+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() == \"abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6192+
Assert.IsNull(selectQuery.OrderBy);
6193+
Assert.IsNull(selectQuery.Projection);
6194+
Assert.IsNull(selectQuery.Skip);
6195+
Assert.IsNull(selectQuery.Take);
6196+
6197+
Assert.AreEqual("{ \"_id\" : { \"$type\" : -1 } }", selectQuery.BuildQuery().ToJson());
6198+
Assert.AreEqual(0, Consume(query));
6199+
}
6200+
6201+
[Test]
6202+
public void TestWhereSToUpperInvariantDoesNotEqualConstantLowerCaseValue()
6203+
{
6204+
var query = from c in _collection.AsQueryable<C>()
6205+
where c.S.ToUpperInvariant() != "abc"
6206+
select c;
6207+
6208+
var translatedQuery = MongoQueryTranslator.Translate(query);
6209+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6210+
Assert.AreSame(_collection, translatedQuery.Collection);
6211+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6212+
6213+
var selectQuery = (SelectQuery)translatedQuery;
6214+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() != \"abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6215+
Assert.IsNull(selectQuery.OrderBy);
6216+
Assert.IsNull(selectQuery.Projection);
6217+
Assert.IsNull(selectQuery.Skip);
6218+
Assert.IsNull(selectQuery.Take);
6219+
6220+
Assert.AreEqual("{ }", selectQuery.BuildQuery().ToJson());
6221+
Assert.AreEqual(5, Consume(query));
6222+
}
6223+
6224+
[Test]
6225+
public void TestWhereSToUpperInvariantEqualsConstantMixedCaseValue()
6226+
{
6227+
var query = from c in _collection.AsQueryable<C>()
6228+
where c.S.ToUpperInvariant() == "Abc"
6229+
select c;
6230+
6231+
var translatedQuery = MongoQueryTranslator.Translate(query);
6232+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6233+
Assert.AreSame(_collection, translatedQuery.Collection);
6234+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6235+
6236+
var selectQuery = (SelectQuery)translatedQuery;
6237+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() == \"Abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6238+
Assert.IsNull(selectQuery.OrderBy);
6239+
Assert.IsNull(selectQuery.Projection);
6240+
Assert.IsNull(selectQuery.Skip);
6241+
Assert.IsNull(selectQuery.Take);
6242+
6243+
Assert.AreEqual("{ \"_id\" : { \"$type\" : -1 } }", selectQuery.BuildQuery().ToJson());
6244+
Assert.AreEqual(0, Consume(query));
6245+
}
6246+
6247+
[Test]
6248+
public void TestWhereSToUpperInvariantDoesNotEqualConstantMixedCaseValue()
6249+
{
6250+
var query = from c in _collection.AsQueryable<C>()
6251+
where c.S.ToUpperInvariant() != "Abc"
6252+
select c;
6253+
6254+
var translatedQuery = MongoQueryTranslator.Translate(query);
6255+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6256+
Assert.AreSame(_collection, translatedQuery.Collection);
6257+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6258+
6259+
var selectQuery = (SelectQuery)translatedQuery;
6260+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() != \"Abc\")", ExpressionFormatter.ToString(selectQuery.Where));
6261+
Assert.IsNull(selectQuery.OrderBy);
6262+
Assert.IsNull(selectQuery.Projection);
6263+
Assert.IsNull(selectQuery.Skip);
6264+
Assert.IsNull(selectQuery.Take);
6265+
6266+
Assert.AreEqual("{ }", selectQuery.BuildQuery().ToJson());
6267+
Assert.AreEqual(5, Consume(query));
6268+
}
6269+
6270+
[Test]
6271+
public void TestWhereSToUpperInvariantEqualsNullValue()
6272+
{
6273+
var query = from c in _collection.AsQueryable<C>()
6274+
where c.S.ToUpperInvariant() == null
6275+
select c;
6276+
6277+
var translatedQuery = MongoQueryTranslator.Translate(query);
6278+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6279+
Assert.AreSame(_collection, translatedQuery.Collection);
6280+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6281+
6282+
var selectQuery = (SelectQuery)translatedQuery;
6283+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() == null)", ExpressionFormatter.ToString(selectQuery.Where));
6284+
Assert.IsNull(selectQuery.OrderBy);
6285+
Assert.IsNull(selectQuery.Projection);
6286+
Assert.IsNull(selectQuery.Skip);
6287+
Assert.IsNull(selectQuery.Take);
6288+
6289+
Assert.AreEqual("{ \"s\" : null }", selectQuery.BuildQuery().ToJson());
6290+
Assert.AreEqual(3, Consume(query));
6291+
}
6292+
6293+
[Test]
6294+
public void TestWhereSToUpperInvariantDoesNotEqualNullValue()
6295+
{
6296+
var query = from c in _collection.AsQueryable<C>()
6297+
where c.S.ToUpperInvariant() != null
6298+
select c;
6299+
6300+
var translatedQuery = MongoQueryTranslator.Translate(query);
6301+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
6302+
Assert.AreSame(_collection, translatedQuery.Collection);
6303+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
6304+
6305+
var selectQuery = (SelectQuery)translatedQuery;
6306+
Assert.AreEqual("(C c) => (c.S.ToUpperInvariant() != null)", ExpressionFormatter.ToString(selectQuery.Where));
6307+
Assert.IsNull(selectQuery.OrderBy);
6308+
Assert.IsNull(selectQuery.Projection);
6309+
Assert.IsNull(selectQuery.Skip);
6310+
Assert.IsNull(selectQuery.Take);
6311+
6312+
Assert.AreEqual("{ \"s\" : { \"$ne\" : null } }", selectQuery.BuildQuery().ToJson());
6313+
Assert.AreEqual(2, Consume(query));
6314+
}
6315+
60176316
[Test]
60186317
public void TestWhereSystemProfileInfoDurationGreatherThan10Seconds()
60196318
{

0 commit comments

Comments
 (0)