From 829d884239bcab7c678ec3b71c78ab20c93d178f Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Mon, 19 Oct 2020 15:49:51 -0700 Subject: [PATCH] added unit tests for continuation token serialization --- .../FeedToken/ChangeFeedIteratorCoreTests.cs | 2 +- ...geFeedStateCosmosElementSerializerTests.cs | 70 +++++++++++++++++++ .../FeedRangeCosmosElementSerializerTests.cs | 51 ++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ChangeFeed/ChangeFeedStateCosmosElementSerializerTests.cs create mode 100644 Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/FeedRange/FeedRangeCosmosElementSerializerTests.cs diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/FeedToken/ChangeFeedIteratorCoreTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/FeedToken/ChangeFeedIteratorCoreTests.cs index 37892a4481..ebaa41dffb 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/FeedToken/ChangeFeedIteratorCoreTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/FeedToken/ChangeFeedIteratorCoreTests.cs @@ -148,7 +148,7 @@ await feedIterator.ReadNextAsync(this.cancellationToken)) /// /// [TestMethod] - //[Timeout(30000)] + [Timeout(30000)] public async Task ChangeFeedIteratorCore_PartitionKey_ReadAll() { int totalCount = 0; diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ChangeFeed/ChangeFeedStateCosmosElementSerializerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ChangeFeed/ChangeFeedStateCosmosElementSerializerTests.cs new file mode 100644 index 0000000000..6a36424169 --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ChangeFeed/ChangeFeedStateCosmosElementSerializerTests.cs @@ -0,0 +1,70 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos.Tests.ChangeFeed +{ + using System; + using Microsoft.Azure.Cosmos.ChangeFeed.Pagination; + using Microsoft.Azure.Cosmos.CosmosElements; + using Microsoft.Azure.Cosmos.Query.Core.Monads; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public sealed class ChangeFeedStateCosmosElementSerializerTests + { + [TestMethod] + public void Now() + { + ChangeFeedState now = ChangeFeedState.Now(); + CosmosElement cosmosElement = ChangeFeedStateCosmosElementSerializer.ToCosmosElement(now); + TryCatch monadicState = ChangeFeedStateCosmosElementSerializer.MonadicFromCosmosElement(cosmosElement); + Assert.IsTrue(monadicState.Succeeded); + Assert.IsTrue(monadicState.Result is ChangeFeedStateNow); + } + + [TestMethod] + public void Beginning() + { + ChangeFeedState beginning = ChangeFeedState.Beginning(); + CosmosElement cosmosElement = ChangeFeedStateCosmosElementSerializer.ToCosmosElement(beginning); + TryCatch monadicState = ChangeFeedStateCosmosElementSerializer.MonadicFromCosmosElement(cosmosElement); + Assert.IsTrue(monadicState.Succeeded); + Assert.IsTrue(monadicState.Result is ChangeFeedStateBeginning); + } + + [TestMethod] + public void Time() + { + DateTime startTime = DateTime.MinValue.ToUniversalTime(); + ChangeFeedState time = ChangeFeedState.Time(startTime); + CosmosElement cosmosElement = ChangeFeedStateCosmosElementSerializer.ToCosmosElement(time); + TryCatch monadicState = ChangeFeedStateCosmosElementSerializer.MonadicFromCosmosElement(cosmosElement); + Assert.IsTrue(monadicState.Succeeded); + if (!(monadicState.Result is ChangeFeedStateTime stateTime)) + { + Assert.Fail(); + return; + } + + Assert.AreEqual(stateTime.StartTime, startTime); + } + + [TestMethod] + public void Continuation() + { + CosmosString continuation = CosmosString.Create("asdf"); + ChangeFeedState time = ChangeFeedState.Continuation(continuation); + CosmosElement cosmosElement = ChangeFeedStateCosmosElementSerializer.ToCosmosElement(time); + TryCatch monadicState = ChangeFeedStateCosmosElementSerializer.MonadicFromCosmosElement(cosmosElement); + Assert.IsTrue(monadicState.Succeeded); + if (!(monadicState.Result is ChangeFeedStateContinuation changeFeedContinuation)) + { + Assert.Fail(); + return; + } + + Assert.AreEqual(changeFeedContinuation.ContinuationToken, continuation); + } + } +} diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/FeedRange/FeedRangeCosmosElementSerializerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/FeedRange/FeedRangeCosmosElementSerializerTests.cs new file mode 100644 index 0000000000..bfcfed19d7 --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/FeedRange/FeedRangeCosmosElementSerializerTests.cs @@ -0,0 +1,51 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos.Tests.FeedRange +{ + using Microsoft.Azure.Cosmos.CosmosElements; + using Microsoft.Azure.Cosmos.Query.Core.Monads; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public sealed class FeedRangeCosmosElementSerializerTests + { + [TestMethod] + public void LogicalPartitionKey() + { + Cosmos.PartitionKey somePartitionKey = new Cosmos.PartitionKey(42); + FeedRangeInternal feedRange = new FeedRangePartitionKey(somePartitionKey); + AssertRoundTrip(feedRange); + } + + [TestMethod] + public void PhysicalPartitionKeyRangeId() + { + int physicalPkRangeId = 0; + FeedRangeInternal feedRange = new FeedRangePartitionKeyRange(physicalPkRangeId.ToString()); + AssertRoundTrip(feedRange); + } + + [TestMethod] + public void EffectivePartitionKeyRange() + { + Microsoft.Azure.Documents.Routing.Range range = new Microsoft.Azure.Documents.Routing.Range( + min: "A", + max: "B", + isMinInclusive: true, + isMaxInclusive: false); + + FeedRangeInternal feedRange = new FeedRangeEpk(range); + AssertRoundTrip(feedRange); + } + + private static void AssertRoundTrip(FeedRangeInternal feedRange) + { + CosmosElement cosmosElement = FeedRangeCosmosElementSerializer.ToCosmosElement(feedRange); + TryCatch monad = FeedRangeCosmosElementSerializer.MonadicCreateFromCosmosElement(cosmosElement); + Assert.IsTrue(monad.Succeeded); + Assert.AreEqual(feedRange.ToJsonString(), monad.Result.ToJsonString()); + } + } +}