Skip to content

Commit

Permalink
RavenDB-18957 Safe casting & Removing nullables from tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejaszyk authored and ppekrol committed Jul 14, 2022
1 parent 038e009 commit 85a3eab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
18 changes: 8 additions & 10 deletions src/Raven.Client/Documents/Linq/LinqPathProvider.cs
Expand Up @@ -82,7 +82,7 @@ public Result GetPath(Expression expression, bool isFilterActive = false)
{
var itemKey = GetValueFromExpression(callExpression.Arguments[0], callExpression.Method.GetParameters()[0].ParameterType).ToString();

if (callExpression.Object.NodeType == ExpressionType.Parameter)
if (callExpression.Object?.NodeType == ExpressionType.Parameter)
{
return new Result
{
Expand All @@ -91,16 +91,14 @@ public Result GetPath(Expression expression, bool isFilterActive = false)
Path = itemKey
};
}
else

var parent = GetPath(callExpression.Object);
return new Result
{
var parent = GetPath(callExpression.Object);
return new Result
{
MemberType = callExpression.Method.ReturnType,
IsNestedPath = true,
Path = parent.Path + "." + itemKey
};
}
MemberType = callExpression.Method.ReturnType,
IsNestedPath = true,
Path = parent.Path + "." + itemKey
};
}

if (callExpression.Method.Name == "ToString" && callExpression.Method.ReturnType == typeof(string))
Expand Down
Expand Up @@ -510,7 +510,7 @@ private bool IsMemberAccessForQuerySource(Expression node)
}
if (node.NodeType == ExpressionType.Parameter)
return true;
if (node.NodeType == ExpressionType.Call && ((MethodCallExpression)node).Method.Name == "get_Item" && ((MethodCallExpression)node).Object.NodeType == ExpressionType.Parameter)
if (node.NodeType == ExpressionType.Call && node is MethodCallExpression callExpressionNode && callExpressionNode.Method.Name == "get_Item" && callExpressionNode.Object?.NodeType == ExpressionType.Parameter)
return true;
if (node.NodeType != ExpressionType.MemberAccess)
return false;
Expand Down
35 changes: 16 additions & 19 deletions test/FastTests/Client/Query.cs
Expand Up @@ -222,12 +222,12 @@ public void Query_Dictionary_Simple()
{
using (var newSession = store.OpenSession())
{
newSession.Store(new Dictionary<string, object?> { { "Name", "John" } }, "users/1");
newSession.Store(new Dictionary<string, object?> { { "Name", "Jane" } }, "users/2");
newSession.Store(new Dictionary<string, object?> { { "Name", "Tarzan" } }, "users/3");
newSession.Store(new Dictionary<string, object> { { "Name", "John" } }, "users/1");
newSession.Store(new Dictionary<string, object> { { "Name", "Jane" } }, "users/2");
newSession.Store(new Dictionary<string, object> { { "Name", "Tarzan" } }, "users/3");
newSession.SaveChanges();

var queryResult = newSession.Query<Dictionary<string,object?>>()
var queryResult = newSession.Query<Dictionary<string,object>>()
.ToList();

Assert.Equal(queryResult.Count, 3);
Expand Down Expand Up @@ -273,23 +273,20 @@ public void Query_Dictionary_With_Where_Clause()
{
using (var newSession = store.OpenSession())
{
newSession.Store(new Dictionary<string, object?> { { "Name", "John" } }, "users/1");
newSession.Store(new Dictionary<string, object?> { { "Name", "Jane" } }, "users/2");
newSession.Store(new Dictionary<string, object?> { { "Name", "Tarzan" } }, "users/3");
newSession.Store(new Dictionary<string, object> { { "Name", "John" } }, "users/1");
newSession.Store(new Dictionary<string, object> { { "Name", "Jane" } }, "users/2");
newSession.Store(new Dictionary<string, object> { { "Name", "Tarzan" } }, "users/3");
newSession.SaveChanges();

var query = newSession.Query<Dictionary<string, object?>>()
.Where(x => ((string)x["Name"]).StartsWith("J")).ToString();

var queryResult = newSession.Query<Dictionary<string, object?>>()

var queryResult = newSession.Query<Dictionary<string, object>>()
.Where(x => ((string)x["Name"]).StartsWith("J"))
.ToList();

var queryResult2 = newSession.Query<Dictionary<string, object?>>()
var queryResult2 = newSession.Query<Dictionary<string, object>>()
.Where(x => ((string)x["Name"]).Equals("Tarzan"))
.ToList();

var queryResult3 = newSession.Query<Dictionary<string, object?>>()
var queryResult3 = newSession.Query<Dictionary<string, object>>()
.Where(x => ((string)x["Name"]).EndsWith("n"))
.ToList();

Expand All @@ -307,13 +304,13 @@ public void Query_Dictionary_With_Where_Equality_Clause()
{
using (var newSession = store.OpenSession())
{
newSession.Store(new Dictionary<string, object?> { { "Name", "Australia" } }, "continents/1");
newSession.Store(new Dictionary<string, object?> { { "Name", "Europe" } }, "continents/2");
newSession.Store(new Dictionary<string, object?> { { "Name", "America" } }, "continents/3");
newSession.Store(new Dictionary<string, object> { { "Name", "Australia" } }, "continents/1");
newSession.Store(new Dictionary<string, object> { { "Name", "Europe" } }, "continents/2");
newSession.Store(new Dictionary<string, object> { { "Name", "America" } }, "continents/3");
newSession.SaveChanges();

var queryResult = newSession.Query<Dictionary<string, object?>>()
.Where(x => ((string?)x["Name"]) == "Australia").ToList();
var queryResult = newSession.Query<Dictionary<string, object>>()
.Where(x => ((string)x["Name"]) == "Australia").ToList();

Assert.Equal(queryResult.Count, 1);
}
Expand Down

0 comments on commit 85a3eab

Please sign in to comment.