public void TestExtractSpecialTypePartitionKey()
 {
     foreach (Tuple <string, object, Func <object, object> > tuple in new[]
     {
         Tuple.Create <string, object, Func <object, object> >("Guid", Guid.NewGuid(), val => val.ToString()),
         Tuple.Create <string, object, Func <object, object> >("DateTime", DateTime.Now, val =>
         {
             string str = JsonConvert.SerializeObject(val, new JsonSerializerSettings()
             {
                 Converters = new List <JsonConverter> {
                     new IsoDateTimeConverter()
                 }
             });
             return(str.Substring(1, str.Length - 2));
         }),
         Tuple.Create <string, object, Func <object, object> >("Enum", HttpStatusCode.OK, val => (int)val),
         Tuple.Create <string, object, Func <object, object> >("CustomEnum", HttpStatusCode.OK, val => val.ToString()),
         Tuple.Create <string, object, Func <object, object> >("ResourceId", "testid", val => val),
         Tuple.Create <string, object, Func <object, object> >("CustomDateTime", new DateTime(2016, 11, 14), val => EpochDateTimeConverter.DateTimeToEpoch((DateTime)val)),
     })
     {
         SpecialPropertyDocument sd = new SpecialPropertyDocument();
         sd.GetType().GetProperty(tuple.Item1).SetValue(sd, tuple.Item2);
         PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
             Kind = PartitionKind.Hash, Paths = new Collection <string> {
                 "/" + tuple.Item1
             }
         };
         PartitionKeyInternal partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(Document.FromObject(sd), partitionKeyDefinition);
         Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { tuple.Item3(tuple.Item2) }, true), partitionKeyValue);
     }
 }
Пример #2
0
        /// <summary>
        /// Extracts effective <see cref="PartitionKeyInternal"/> from serialized document.
        /// </summary>
        /// <remarks>
        /// This code doesn't do any validation, as it assumes that IndexingPolicy is valid, as it is coming from the backend.
        /// Expected format is "/prop1/prop2/?". No array expressions are expected.
        /// </remarks>
        /// <param name="documentString">Serialized document to extract partition key value from.</param>
        /// <param name="partitionKeyDefinition">Information about partition key.</param>
        /// <returns>Instance of <see cref="PartitionKeyInternal"/>.</returns>
        public static PartitionKeyInternal ExtractPartitionKeyValue(string documentString, PartitionKeyDefinition partitionKeyDefinition)
        {
            if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
            {
                return(PartitionKeyInternal.Empty);
            }

            return(DocumentAnalyzer.ExtractPartitionKeyValue(documentString, partitionKeyDefinition, (docString) => JToken.Parse(docString)));
        }
Пример #3
0
        public void TestExtractValidNumberPartitionKey()
        {
            dynamic address = new JObject();

            address.Country = 5.5;

            dynamic document = new Document();

            document.address = address;

            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
                Kind = PartitionKind.Hash, Paths = new Collection <string> {
                    "/address/Country"
                }
            };

            PartitionKeyInternal partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, partitionKeyDefinition);

            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), partitionKeyDefinition);
            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(Document.FromObject(new Entity {
                Address = new Address {
                    Country = 5.5
                }
            })), partitionKeyDefinition);
            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(Document.FromObject(new Entity {
                Address = new Address {
                    Country = 5.5
                }
            }), partitionKeyDefinition);
            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(Document.FromObject(new DocumentEntity {
                Address = new Address {
                    Country = 5.5
                }
            })), partitionKeyDefinition);
            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(Document.FromObject(new DocumentEntity {
                Address = new Address {
                    Country = 5.5
                }
            }), partitionKeyDefinition);
            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { 5.5 }, true), partitionKeyValue);
        }
Пример #4
0
        public void TestExtractEmptyPartitionKey()
        {
            Document document = new Document();

            PartitionKeyInternal partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, null);

            Assert.AreEqual(PartitionKeyInternal.Empty, partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), null);
            Assert.AreEqual(PartitionKeyInternal.Empty, partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, new PartitionKeyDefinition());
            Assert.AreEqual(PartitionKeyInternal.Empty, partitionKeyValue);

            partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), new PartitionKeyDefinition());
            Assert.AreEqual(PartitionKeyInternal.Empty, partitionKeyValue);
        }
Пример #5
0
        /// <summary>
        /// Extracts effective <see cref="PartitionKeyInternal"/> from deserialized document.
        /// </summary>
        /// <remarks>
        /// This code doesn't do any validation, as it assumes that IndexingPolicy is valid, as it is coming from the backend.
        /// Expected format is "/prop1/prop2/?". No array expressions are expected.
        /// </remarks>
        /// <param name="document">Deserialized document to extract partition key value from.</param>
        /// <param name="partitionKeyDefinition">Information about partition key.</param>
        /// <returns>Instance of <see cref="PartitionKeyInternal"/>.</returns>
        public static PartitionKeyInternal ExtractPartitionKeyValue(Document document, PartitionKeyDefinition partitionKeyDefinition)
        {
            if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
            {
                return(PartitionKeyInternal.Empty);
            }

            if (document.GetType().IsSubclassOf(typeof(Document)))
            {
                return(DocumentAnalyzer.ExtractPartitionKeyValue(document, partitionKeyDefinition, (doc) => JToken.FromObject(doc)));
            }
            else
            {
                return(PartitionKeyInternal.FromObjectArray(
                           partitionKeyDefinition.Paths.Select(path =>
                {
                    string[] parts = PathParser.GetPathParts(path);
                    Debug.Assert(parts.Length >= 1, "Partition key component definition path is invalid.");

                    return document.GetValueByPath <object>(parts, Undefined.Value);
                }).ToArray(),
                           false));
            }
        }
Пример #6
0
        public void TestExtractUndefinedPartitionKey()
        {
            dynamic document = new Document();

            PartitionKeyDefinition[] partitionKeyDefinitions = new PartitionKeyDefinition[]
            {
                new PartitionKeyDefinition {
                    Paths = new Collection <string> {
                        "/address/Country",
                    },
                    Kind = PartitionKind.Range
                },
                new PartitionKeyDefinition {
                    Paths = new Collection <string> {
                        "/address/Country/something",
                    },
                    Kind = PartitionKind.Range
                },
                new PartitionKeyDefinition {
                    Paths = new Collection <string> {
                        "/address",
                    },
                    Kind = PartitionKind.Range
                },
            };

            foreach (PartitionKeyDefinition definition in partitionKeyDefinitions)
            {
                PartitionKeyInternal partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);

                document         = new Document();
                document.address = new JObject();

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(Document.FromObject(new Entity {
                    Address = new Address {
                        Country = new object()
                    }
                }), definition);
                Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true), partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(Document.FromObject(new Entity {
                    Address = new Address {
                        Country = new object()
                    }
                })), definition);
                Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true), partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(Document.FromObject(new DocumentEntity {
                    Address = new Address {
                        Country = new object()
                    }
                }), definition);
                Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true), partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(Document.FromObject(new DocumentEntity {
                    Address = new Address {
                        Country = new object()
                    }
                })), definition);
                Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true), partitionKeyValue);

                dynamic address = new JObject();
                address.Country = new JObject();

                document         = new Document();
                document.address = address;

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(document, definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);

                partitionKeyValue = DocumentAnalyzer.ExtractPartitionKeyValue(JsonConvert.SerializeObject(document), definition);
                Assert.AreEqual(
                    PartitionKeyInternal.FromObjectArray(new object[] { Undefined.Value }, true),
                    partitionKeyValue);
            }
        }