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);
     }
 }
Exemplo n.º 2
0
        public void TestVariousTypes()
        {
            string json = @"[""aa"", null, true, false, {}, 5, 5.5]";
            PartitionKeyInternal partitionKey = PartitionKeyInternal.FromJsonString(json);

            Assert.AreEqual(PartitionKeyInternal.FromObjectArray(new object[] { "aa", null, true, false, Undefined.Value, 5, 5.5 }, true), partitionKey);

            Assert.AreEqual(@"[""aa"",null,true,false,{},5.0,5.5]", partitionKey.ToJsonString());
        }
Exemplo n.º 3
0
        public void TestRangeEffectivePartitionKey()
        {
            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
                Paths = new Collection <string> {
                    "/A", "/B", "/C", "/E", "/F", "/G"
                }, Kind = PartitionKind.Range
            };
            PartitionKeyInternal partitionKey = PartitionKeyInternal.FromObjectArray(new object[] { 2, true, false, null, Undefined.Value, "Привет!" }, true);
            string effectivePartitionKey      = partitionKey.GetEffectivePartitionKeyString(partitionKeyDefinition);

            Assert.AreEqual("05C0000302010008D1A0D281D1B9D1B3D1B6D2832200", effectivePartitionKey);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void TestHashEffectivePartitionKey()
        {
            Assert.AreEqual(
                PartitionKeyInternal.InclusiveMinimum.GetEffectivePartitionKeyString(new PartitionKeyDefinition()),
                PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey);

            Assert.AreEqual(
                PartitionKeyInternal.ExclusiveMaximum.GetEffectivePartitionKeyString(new PartitionKeyDefinition()),
                PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey);

            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
                Paths = new Collection <string> {
                    "/A", "/B", "/C", "/E", "/F", "/G"
                }, Kind = PartitionKind.Hash
            };
            PartitionKeyInternal partitionKey = PartitionKeyInternal.FromObjectArray(new object[] { 2, true, false, null, Undefined.Value, "Привет!" }, true);
            string effectivePartitionKey      = partitionKey.GetEffectivePartitionKeyString(partitionKeyDefinition);

            Assert.AreEqual("05C1D19581B37C05C0000302010008D1A0D281D1B9D1B3D1B6D2832200", effectivePartitionKey);
        }
Exemplo n.º 6
0
        internal static PartitionKeyInternal ExtractPartitionKeyValue <T>(T data, PartitionKeyDefinition partitionKeyDefinition, Func <T, JToken> convertToJToken)
        {
            return(PartitionKeyInternal.FromObjectArray(
                       partitionKeyDefinition.Paths.Select(path =>
            {
                string[] parts = PathParser.GetPathParts(path);
                Debug.Assert(parts.Length >= 1, "Partition key component definition path is invalid.");

                JToken token = convertToJToken(data);

                foreach (string part in parts)
                {
                    if (token == null)
                    {
                        break;
                    }

                    token = token[part];
                }

                return token != null ? token.ToObject <object>() : Undefined.Value;
            }).ToArray(),
                       false));
        }
Exemplo n.º 7
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));
            }
        }
Exemplo n.º 8
0
 public void TestInvalidPartitionKeyValueNonStrict()
 {
     Assert.AreEqual(
         PartitionKeyInternal.FromObjectArray(new object[] { 2, true, Undefined.Value }, true),
         PartitionKeyInternal.FromObjectArray(new object[] { 2, true, new StringBuilder() }, false));
 }
Exemplo n.º 9
0
 public void TestInvalidPartitionKeyValue()
 {
     PartitionKeyInternal.FromObjectArray(new object[] { 2, true, new StringBuilder() }, true);
 }
Exemplo n.º 10
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);
            }
        }