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: Added custom serializer coverage tests to ExpressionToSQL.cs #3722

Merged
17 commits merged into from
May 9, 2023
Merged
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using global::Azure.Core.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand All @@ -30,6 +34,102 @@ public void DateTimeKindIsPreservedTest()
Assert.AreEqual("(a[\"StartDate\"] <= \"2022-05-26\")", sql);
}

// Ignored for now because the test is failing in current production implementation of Cosmos SDK
[TestMethod, Ignore]
public void EnumIsPreservedAsINTest()
{
// Arrange
CosmosLinqSerializerOptions options = new()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options

Not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed if the code that was removed in #3749 is re-introduced

{
// CustomCosmosSerializer = new TestCustomJsonSerializer()
};

// Act
TestEnum[] values = new[] { TestEnum.One, TestEnum.Two };
Expression<Func<TestEnumDocument, bool>> expr = a => values.Contains(a.Value);

string sql = SqlTranslator.TranslateExpression(expr.Body, options);

// Assert
Assert.AreEqual("(a[\"Value\"] IN (\"One\", \"Two\"))", sql);
}

[TestMethod]
public void EnumIsPreservedAsEQUALSTest()
{
// Arrange
CosmosLinqSerializerOptions options = new()
{
// CustomCosmosSerializer = new TestCustomJsonSerializer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before.

};

// Act
TestEnum statusValue = TestEnum.One;
Expression<Func<TestEnumDocument, bool>> expr = a => a.Value == statusValue;

string sql = SqlTranslator.TranslateExpression(expr.Body, options);

// Assert
Assert.AreEqual("(a[\"Value\"] = \"One\")", sql);
}

[TestMethod]
public void EnumIsPreservedAsEXPRESSIONTest()
{
// Arrange
CosmosLinqSerializerOptions options = new()
{
// CustomCosmosSerializer = new TestCustomJsonSerializer()
};

// Act

// Get status constant
ConstantExpression status = Expression.Constant(TestEnum.One);

// Get member access expression
ParameterExpression arg = Expression.Parameter(typeof(TestEnumNewtonsoftDocument), "a");

// Access the value property
MemberExpression docValueExpression = Expression.MakeMemberAccess(
arg,
typeof(TestEnumNewtonsoftDocument).GetProperty(nameof(TestEnumNewtonsoftDocument.Value))!
);

// Create comparison expression
BinaryExpression expression = Expression.Equal(
docValueExpression,
status
);

// Create lambda expression
Expression<Func<TestEnumNewtonsoftDocument, bool>> lambda =
Expression.Lambda<Func<TestEnumNewtonsoftDocument, bool>>(expression, arg);

string sql = SqlTranslator.TranslateExpression(lambda.Body, options);

// Assert
Assert.AreEqual("(a[\"Value\"] = \"One\")", sql);
}

enum TestEnum
{
One,
Two,
Three,
}

class TestEnumDocument
{
public TestEnum Value { get; set; }
}

class TestEnumNewtonsoftDocument
{
[JsonConverter(typeof(StringEnumConverter))]
public TestEnum Value { get; set; }
}

class TestDocument
{
[JsonConverter(typeof(DateJsonConverter))]
Expand All @@ -50,5 +150,53 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
}
}
}

/// <remarks>
// See: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs
/// </remarks>
class TestCustomJsonSerializer : CosmosSerializer
{
private readonly JsonObjectSerializer systemTextJsonSerializer;

public static readonly System.Text.Json.JsonSerializerOptions JsonOptions = new()
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
Converters = {
new System.Text.Json.Serialization.JsonStringEnumConverter(),
}
};

public TestCustomJsonSerializer()
{
this.systemTextJsonSerializer = new JsonObjectSerializer(JsonOptions);
}

public override T FromStream<T>(Stream stream)
{
if (stream.CanSeek && stream.Length == 0)
{
stream.Dispose();
return default!;
}

if (typeof(Stream).IsAssignableFrom(typeof(T)))
return (T)(object)stream;

using (stream)
{
return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default)!;
}
}

public override Stream ToStream<T>(T input)
{
MemoryStream stream = new ();

this.systemTextJsonSerializer.Serialize(stream, input, typeof(T), default);
stream.Position = 0;
return stream;
}
}
}
}