public async Task TestGetPartitionKeyValueFromStreamAsync() { Mock <CosmosContainerCore> containerMock = new Mock <CosmosContainerCore>(); CosmosContainerCore container = containerMock.Object; containerMock.Setup(e => e.GetPartitionKeyPathTokensAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(new string[] { "pk" })); DateTime dateTime = new DateTime(2019, 05, 15, 12, 1, 2, 3, DateTimeKind.Utc); Guid guid = Guid.NewGuid(); //Test supported types List <dynamic> supportedTypesToTest = new List <dynamic> { new { pk = true }, new { pk = false }, new { pk = byte.MaxValue }, new { pk = sbyte.MaxValue }, new { pk = short.MaxValue }, new { pk = ushort.MaxValue }, new { pk = int.MaxValue }, new { pk = uint.MaxValue }, new { pk = long.MaxValue }, new { pk = ulong.MaxValue }, new { pk = float.MaxValue }, new { pk = double.MaxValue }, new { pk = decimal.MaxValue }, new { pk = char.MaxValue }, new { pk = "test" }, new { pk = dateTime }, new { pk = guid }, }; foreach (dynamic poco in supportedTypesToTest) { object pk = await container.GetPartitionKeyValueFromStreamAsync(new CosmosJsonSerializerCore().ToStream(poco)); if (pk is bool) { Assert.AreEqual(poco.pk, (bool)pk); } else if (pk is double) { if (poco.pk is float) { Assert.AreEqual(poco.pk, Convert.ToSingle(pk)); } else if (poco.pk is double) { Assert.AreEqual(poco.pk, Convert.ToDouble(pk)); } else if (poco.pk is decimal) { Assert.AreEqual(Convert.ToDouble(poco.pk), (double)pk); } } else if (pk is string) { if (poco.pk is DateTime) { Assert.AreEqual(poco.pk.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"), (string)pk); } else { Assert.AreEqual(poco.pk.ToString(), (string)pk); } } } //Unsupported types should throw List <dynamic> unsupportedTypesToTest = new List <dynamic> { new { pk = new { test = "test" } }, new { pk = new int[] { 1, 2, 3 } }, new { pk = new ArraySegment <byte>(new byte[] { 0 }) }, }; foreach (dynamic poco in unsupportedTypesToTest) { await Assert.ThrowsExceptionAsync <ArgumentException>(async() => { await container.GetPartitionKeyValueFromStreamAsync(new CosmosJsonSerializerCore().ToStream(poco)); }); } //null should return Undefined object pkValue = await container.GetPartitionKeyValueFromStreamAsync(new CosmosJsonSerializerCore().ToStream(new { pk = (object)null })); Assert.AreEqual(Cosmos.PartitionKey.NonePartitionKeyValue, pkValue); }
public async Task TestNestedPartitionKeyValueFromStreamAsync() { Mock <CosmosContainerCore> containerMock = new Mock <CosmosContainerCore>(); CosmosContainerCore container = containerMock.Object; containerMock.Setup(e => e.GetPartitionKeyPathTokensAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(new string[] { "a", "b", "c" })); List <dynamic> invalidNestedItems = new List <dynamic> { new // a/b/d (leaf invalid) { id = Guid.NewGuid().ToString(), a = new { b = new { d = "pk1", } } }, new // a/d/c (middle invalid) { id = Guid.NewGuid().ToString(), a = new { d = new { c = "pk1", } } }, new // nested/a/b/c (root invalid) { id = Guid.NewGuid().ToString(), nested = new { a = new { b = new { c = "pk1", } } } }, new // nested/a/b/c/d (root & tail invalid) { id = Guid.NewGuid().ToString(), nested = new { a = new { b = new { c = new { d = "pk1" } } } } } }; foreach (dynamic poco in invalidNestedItems) { object pk = await container.GetPartitionKeyValueFromStreamAsync(new CosmosJsonSerializerCore().ToStream(poco)); Assert.IsTrue(object.ReferenceEquals(Cosmos.PartitionKey.NonePartitionKeyValue, pk)); } }