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

Patch: Adds public to PatchOperation<T> class for testing #3404

Merged
merged 12 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Patch/PatchOperation{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Azure.Cosmos
/// Defines PatchOperation with a value parameter.
/// </summary>
/// <typeparam name="T">Data type of value provided for PatchOperation.</typeparam>
internal abstract class PatchOperation<T> : PatchOperation
public abstract class PatchOperation<T> : PatchOperation
{
/// <summary>
/// Value parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tests
namespace Microsoft.Azure.Cosmos.Tests.Patch
decodingahmed marked this conversation as resolved.
Show resolved Hide resolved
{
using System;
using System.IO;
using Microsoft.Azure.Cosmos;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand All @@ -21,7 +22,7 @@ public void ThrowsOnNullArguement()
PatchOperation.Add(null, "1");
Assert.Fail();
}
catch(ArgumentNullException ex)
catch (ArgumentNullException ex)
{
Assert.AreEqual(ex.ParamName, "path");
}
Expand All @@ -48,7 +49,7 @@ public void ConstructPatchOperationTest()
PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, current);

dynamic complexObject = new { a = "complex", b = 12.34, c = true };
operation = PatchOperation.Add(path, complexObject);
operation = PatchOperation.Add(path, complexObject);
PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, complexObject);

operation = PatchOperation.Remove(path);
Expand All @@ -61,7 +62,7 @@ public void ConstructPatchOperationTest()
Guid guid = new Guid();
operation = PatchOperation.Set(path, guid);
PatchOperationTests.ValidateOperations(operation, PatchOperationType.Set, guid);

operation = PatchOperation.Set<object>(path, null);
PatchOperationTests.ValidateOperations<object>(operation, PatchOperationType.Set, null);
}
Expand Down Expand Up @@ -100,7 +101,7 @@ private static void ValidateOperations<T>(PatchOperation patchOperation, PatchOp

private class CustomSerializer : CosmosSerializer
{
private CosmosSerializer cosmosSerializer = new CosmosJsonDotNetSerializer();
private readonly CosmosSerializer cosmosSerializer = new CosmosJsonDotNetSerializer();

public override T FromStream<T>(Stream stream)
{
Expand All @@ -109,7 +110,7 @@ public override T FromStream<T>(Stream stream)

public override Stream ToStream<T>(T input)
{
return this.cosmosSerializer.ToStream<T>(input);
return this.cosmosSerializer.ToStream(input);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tests.Patch
decodingahmed marked this conversation as resolved.
Show resolved Hide resolved
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class PatchOperationTTests
{
private const string path = "/random";

[TestMethod]
public void CastPatchOperationTest()
{
PatchOperation operation = PatchOperation.Add(path, "test_string");
PatchOperation<string> addStringOp = AssertCanCast<string>(operation);
Assert.AreEqual("test_string", addStringOp?.Value);

DateTime dateTime = new DateTime(2022, 8, 1);
operation = PatchOperation.Add(path, dateTime);
PatchOperation<DateTime> addDateTimeOp = AssertCanCast<DateTime>(operation);
Assert.AreEqual(dateTime, addDateTimeOp?.Value);

var complexObject = new { a = "complex", b = 12.34, c = true };
operation = PatchOperation.Add(path, complexObject);
dynamic addDynamicOp = operation;
Assert.AreEqual("complex", addDynamicOp.Value.a);
Assert.AreEqual(12.34, addDynamicOp.Value.b);
Assert.AreEqual(true, addDynamicOp.Value.c);

int[] arrayObject = { 1, 2, 3 };
operation = PatchOperation.Replace(path, arrayObject);
PatchOperation<int[]> replaceOp = AssertCanCast<int[]>(operation);
Assert.AreEqual(arrayObject, replaceOp.Value);

Guid guid = new Guid();
operation = PatchOperation.Set(path, guid);
PatchOperation<Guid> setGuidOp = AssertCanCast<Guid>(operation);
Assert.AreEqual(guid, setGuidOp.Value);

operation = PatchOperation.Set<object>(path, null);
PatchOperation<object> setObjectOp = AssertCanCast<object>(operation);
Assert.AreEqual(null, setObjectOp.Value);

operation = PatchOperation.Increment(path, 7.0);
PatchOperation<double> incrementDoubleOp = AssertCanCast<double>(operation);
Assert.AreEqual(7.0, incrementDoubleOp.Value);

operation = PatchOperation.Increment(path, 40);
PatchOperation<long> incrementIntOp = AssertCanCast<long>(operation);
Assert.AreEqual(40, incrementIntOp.Value);
}

private static PatchOperation<TPatchData> AssertCanCast<TPatchData>(PatchOperation operation)
{
PatchOperation<TPatchData> castedOp = operation as PatchOperation<TPatchData>;
Assert.IsNotNull(castedOp, $"{nameof(PatchOperation)} should be castable to {nameof(PatchOperation<TPatchData>)}");

return castedOp;
}
}
}