Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Query : Adds test coverage for custom serializers #4114

Merged
merged 23 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ namespace Microsoft.Azure.Cosmos.Linq
using System.Reflection;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.CosmosElements.Numbers;
using Microsoft.Azure.Cosmos.Serializer;
using Microsoft.Azure.Cosmos.Spatial;
using Microsoft.Azure.Cosmos.SqlObjects;
using Microsoft.Azure.Documents;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static Microsoft.Azure.Cosmos.Linq.FromParameterBindings;

// ReSharper disable UnusedParameter.Local
Expand Down Expand Up @@ -77,10 +75,9 @@ public static class LinqMethods
public const string Where = "Where";
}

private static string SqlRoot = "root";
private static string DefaultParameterName = "v";
private static bool usePropertyRef = false;
private static SqlIdentifier RootIdentifier = SqlIdentifier.Create(SqlRoot);
private static readonly string SqlRoot = "root";
private static readonly string DefaultParameterName = "v";
private static readonly bool usePropertyRef = false;

/// <summary>
/// Toplevel entry point.
Expand Down Expand Up @@ -455,12 +452,12 @@ private static SqlScalarExpression VisitBinary(BinaryExpression inputExpression,
{
if (TryMatchStringCompareTo(methodCallExpression, constantExpression, inputExpression.NodeType))
{
return ExpressionToSql.VisitStringCompareTo(methodCallExpression, constantExpression, inputExpression.NodeType, reverseNodeType, context);
return ExpressionToSql.VisitStringCompareTo(methodCallExpression, inputExpression.NodeType, reverseNodeType, context);
}

if (TryMatchStringCompare(methodCallExpression, constantExpression, inputExpression.NodeType))
{
return ExpressionToSql.VisitStringCompare(methodCallExpression, constantExpression, inputExpression.NodeType, reverseNodeType, context);
return ExpressionToSql.VisitStringCompare(methodCallExpression, inputExpression.NodeType, reverseNodeType, context);
}
}

Expand Down Expand Up @@ -613,7 +610,6 @@ private static bool TryMatchStringCompareTo(MethodCallExpression left, ConstantE

private static SqlScalarExpression VisitStringCompareTo(
MethodCallExpression left,
ConstantExpression right,
ExpressionType compareOperator,
bool reverseNodeType,
TranslationContext context)
Expand Down Expand Up @@ -690,7 +686,6 @@ private static bool TryMatchStringCompare(MethodCallExpression left, ConstantExp

private static SqlScalarExpression VisitStringCompare(
MethodCallExpression left,
ConstantExpression right,
ExpressionType compareOperator,
bool reverseNodeType,
TranslationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Microsoft.Azure.Cosmos
using Microsoft.Azure.Cosmos.ChangeFeed;
using Microsoft.Azure.Cosmos.ChangeFeed.FeedProcessing;
using Microsoft.Azure.Cosmos.ChangeFeed.Pagination;
using Microsoft.Azure.Cosmos.ChangeFeed.Utils;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Linq;
Expand All @@ -29,11 +27,8 @@ namespace Microsoft.Azure.Cosmos
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
using Microsoft.Azure.Cosmos.ReadFeed;
using Microsoft.Azure.Cosmos.ReadFeed.Pagination;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Serializer;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Routing;

/// <summary>
/// Used to perform operations on items. There are two different types of operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// This class provides a way to configure Linq Serialization Properties
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<Results>
<Result>
<Input>
<Description><![CDATA[Filter w/ DataObject initializer with constant value, camelcase = True]]></Description>
<Expression><![CDATA[query.Where(doc => (doc == new DataObjectDataMember() {NumericField = 12, StringField = "12"}))]]></Expression>
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root = {"NumericField": 12, "StringField": "12", "id": null, "Pk": null})]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select w/ DataObject initializer, camelcase = True]]></Description>
<Expression><![CDATA[query.Select(doc => new DataObjectDataMember() {NumericField = 12, StringField = "12"})]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE {"NumericField": 12, "StringField": "12", "id": null, "Pk": null}
FROM root]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}"
]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Deeper than top level reference, camelcase = True]]></Description>
<Expression><![CDATA[query.Select(doc => IIF((doc.NumericField > 12), new DataObjectDataMember() {NumericField = 12, StringField = "12"}, new DataObjectDataMember() {NumericField = 12, StringField = "12"}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE ((root["numericField"] > 12) ? {"NumericField": 12, "StringField": "12", "id": null, "Pk": null} : {"NumericField": 12, "StringField": "12", "id": null, "Pk": null})
FROM root]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}"
]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter w/ DataObject initializer with member initialization, camelcase = True]]></Description>
<Expression><![CDATA[query.Where(doc => (doc == new DataObjectDataMember() {NumericField = doc.NumericField, StringField = doc.StringField})).Select(b => "A")]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "A"
FROM root
WHERE (root = {"numericField": root["numericField"], "stringField": root["stringField"]})]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter w/ DataObject initializer with constant value, camelcase = False]]></Description>
<Expression><![CDATA[query.Where(doc => (doc == new DataObjectDataMember() {NumericField = 12, StringField = "12"}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root = {"NumericField": 12, "StringField": "12", "id": null, "Pk": null})]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select w/ DataObject initializer, camelcase = False]]></Description>
<Expression><![CDATA[query.Select(doc => new DataObjectDataMember() {NumericField = 12, StringField = "12"})]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE {"NumericField": 12, "StringField": "12", "id": null, "Pk": null}
FROM root]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}"
]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Deeper than top level reference, camelcase = False]]></Description>
<Expression><![CDATA[query.Select(doc => IIF((doc.NumericField > 12), new DataObjectDataMember() {NumericField = 12, StringField = "12"}, new DataObjectDataMember() {NumericField = 12, StringField = "12"}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE ((root["NumericField"] > 12) ? {"NumericField": 12, "StringField": "12", "id": null, "Pk": null} : {"NumericField": 12, "StringField": "12", "id": null, "Pk": null})
FROM root]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}",
"{\"NumericField\":12.0,\"StringField\":\"12\",\"id\":null,\"Pk\":null}"
]]]></Results>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Filter w/ DataObject initializer with member initialization, camelcase = False]]></Description>
<Expression><![CDATA[query.Where(doc => (doc == new DataObjectDataMember() {NumericField = doc.NumericField, StringField = doc.StringField})).Select(b => "A")]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "A"
FROM root
WHERE (root = {"NumericField": root["NumericField"], "StringField": root["StringField"]})]]></SqlQuery>
<InputData><![CDATA[[
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-True\",\"Pk\":\"Test\"}",
"{\"NumericField\":0.0,\"StringField\":\"0\",\"id\":\"0-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":1.0,\"StringField\":\"1\",\"id\":\"1-False\",\"Pk\":\"Test\"}",
"{\"NumericField\":2.0,\"StringField\":\"2\",\"id\":\"2-False\",\"Pk\":\"Test\"}"
]]]></InputData>
<Results><![CDATA[[]]]></Results>
</Output>
</Result>
</Results>
Loading
Loading