コード例 #1
0
        public static void Format_WithEnumPropertyThrowsArgumentOutOfRangeWithUndefinedEnum()
        {
            var mapping    = new ElasticMapping(enumFormat: EnumFormat.String);
            var memberInfo = TypeHelper.GetMemberInfo((FormatClass f) => f.DayProperty);

            Assert.Throws <ArgumentOutOfRangeException>(() => mapping.FormatValue(memberInfo, 127));
        }
コード例 #2
0
ファイル: ElasticSerializer.cs プロジェクト: hawful70/Puppy
        private void CreateBulkContentForChildDocument(EntityContextInfo entityInfo, ElasticMapping elasticMapping,
                                                       EntityContextInfo item)
        {
            var childMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(item.EntityType);

            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            _elasticCrudJsonWriter.JsonWriter.WritePropertyName("index");
            // Write the batch "index" operation header
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            // Always write to the same index
            WriteValue("_index", childMapping.GetIndexForType(entityInfo.EntityType));
            WriteValue("_type", childMapping.GetDocumentType(item.EntityType));
            WriteValue("_id", item.Id);
            WriteValue("_parent", item.RoutingDefinition.ParentId);
            if (item.RoutingDefinition.RoutingId != null && _elasticSerializerConfiguration.UserDefinedRouting)
            {
                WriteValue("_routing", item.RoutingDefinition.RoutingId);
            }
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n"); //ES requires this \n separator
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            childMapping.MapEntityValues(item, _elasticCrudJsonWriter, true);

            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n");
        }
コード例 #3
0
        public static void GetTypeSelectionCriteria()
        {
            var mapping = new ElasticMapping();

            var criteria = mapping.GetTypeSelectionCriteria(typeof(FormatClass));

            Assert.Null(criteria);
        }
コード例 #4
0
        public static void GetDocumentType(bool camelCaseTypeNames, bool pluralizeTypeNames, Type type, string expected)
        {
            var mapping = new ElasticMapping(camelCaseTypeNames: camelCaseTypeNames, pluralizeTypeNames: pluralizeTypeNames);

            var actual = mapping.GetDocumentType(type);

            Assert.Equal(expected, actual);
        }
コード例 #5
0
        public static void GetFieldName(bool camelCaseFieldNames, string prefix, string expected)
        {
            var memberInfo = MethodBase.GetCurrentMethod();
            var mapping    = new ElasticMapping(camelCaseFieldNames: camelCaseFieldNames);

            var actual = mapping.GetFieldName(prefix, memberInfo);

            Assert.Equal(expected, actual);
        }
コード例 #6
0
        public static void GetFieldName_HonorsJsonPropertyName()
        {
            var memberInfo = TypeHelper.GetMemberInfo((FormatClass f) => f.NotSoCustom);
            var mapping    = new ElasticMapping();

            var actual = mapping.GetFieldName(typeof(Sample), memberInfo);

            Assert.Equal("CustomPropertyName", actual);
        }
コード例 #7
0
        public static void FormatValue(bool lowerCaseAnalyzedFieldValues, string propertyName, object inputValue, string expected)
        {
            var memberInfo = typeof(FormatClass).GetProperty(propertyName);
            var mapping    = new ElasticMapping(lowerCaseAnalyzedFieldValues: lowerCaseAnalyzedFieldValues);

            var result = mapping.FormatValue(memberInfo, inputValue);

            Assert.Equal(expected, result.ToString(Formatting.None));
        }
コード例 #8
0
        public static void GetFieldName_Correctly_Cases_Field_Name(bool camelCaseFieldNames, string expected)
        {
            Expression <Func <Sample, int> > stringAccess = (Sample s) => s.IntegerField;
            var mapping = new ElasticMapping(camelCaseFieldNames: camelCaseFieldNames);

            var actual = mapping.GetFieldName(typeof(Sample), (MemberExpression)stringAccess.Body);

            Assert.Equal(expected, actual);
        }
コード例 #9
0
        public static void Format_WithEnumPropertyProducesIntWhenMappingSpecifiesIntFormat()
        {
            var mapping    = new ElasticMapping(enumFormat: EnumFormat.Integer);
            var memberInfo = TypeHelper.GetMemberInfo((FormatClass f) => f.DayProperty);

            var actual = mapping.FormatValue(memberInfo, (int)Day.Saturday);

            Assert.Equal((int)Day.Saturday, actual);
        }
コード例 #10
0
        public static void GetFieldName_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.GetFieldName("", (MemberExpression)null));
            Assert.Throws <NotSupportedException>(() => mapping.GetFieldName("", Expression.Field(Expression.Constant(new FieldClass {
                AField = "test"
            }), "AField")));
            Assert.Throws <ArgumentNullException>(() => mapping.GetFieldName("", (MemberInfo)null));
        }
コード例 #11
0
        public static void GetTypeExistsCriteria()
        {
            var mapping = new ElasticMapping();

            var criteria = mapping.GetTypeExistsCriteria(typeof(FormatClass));

            var exists = Assert.IsType <ExistsCriteria>(criteria);

            Assert.Equal("integerValue", exists.Field);
        }
コード例 #12
0
ファイル: IndexMappings.cs プロジェクト: hawful70/Puppy
        /// <summary>
        ///     Create a new index for the parent document
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="mappingDefinition"> mapping definitions for the index type </param>
        private void IndexCreateCreatePropertyMappingForEntityForParentDocument(EntityContextInfo entityInfo,
                                                                                ElasticMapping elasticMapping, MappingDefinition mappingDefinition, ElasticJsonWriter elasticCrudJsonWriter)
        {
            var itemType = elasticMapping.GetDocumentType(entityInfo.EntityType);

            if (_processedItems.Contains("_mapping" + itemType))
            {
                return;
            }
            _processedItems.Add("_mapping" + itemType);

            //elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticCrudJsonWriter.JsonWriter.WritePropertyName("mappings");
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticCrudJsonWriter.JsonWriter.WritePropertyName(itemType);
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            mappingDefinition.Source.WriteJson(elasticCrudJsonWriter);
            mappingDefinition.All.WriteJson(elasticCrudJsonWriter);

            if (entityInfo.RoutingDefinition.RoutingId != null && _elasticSerializerConfiguration.UserDefinedRouting)
            {
                CreateForceRoutingMappingForDocument(elasticCrudJsonWriter);
            }

            if (entityInfo.RoutingDefinition.ParentId != null)
            {
                CreateParentMappingForDocument(
                    elasticCrudJsonWriter,
                    elasticMapping.GetDocumentType(entityInfo.ParentEntityType));
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, entityInfo, elasticMapping);
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            if (_elasticSerializerConfiguration.ProcessChildDocumentsAsSeparateChildIndex)
            {
                if (elasticMapping.ChildIndexEntities.Count > 0)
                {
                    elasticMapping.SaveChildObjectsAsWellAsParent = false;
                    foreach (var item in elasticMapping.ChildIndexEntities)
                    {
                        IndexCreateCreatePropertyMappingForChildDocument(elasticCrudJsonWriter, entityInfo,
                                                                         elasticMapping, item, mappingDefinition);
                    }
                }
            }

            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            //elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            //CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(), mappingDefinition.Index, itemType);
        }
コード例 #13
0
        public void ConstructorWithAllArgsSetsPropertiesFromParameters()
        {
            var       expectedMapping  = new ElasticMapping();
            var       expectedLog      = new SpyLog();
            const int expectedAttempts = 5;
            var       expectedTimeout  = TimeSpan.FromSeconds(21.3);

            var context = new TestableElasticContext(expectedMapping, expectedLog, expectedAttempts, expectedTimeout);

            Assert.NotNull(context.Connection);
            Assert.Equal(expectedMapping, context.Mapping);
            Assert.NotNull(context.Provider);
            Assert.NotNull(context.Requests);
            Assert.Equal(expectedLog, context.Log);
            var retryPolicy = Assert.IsType <RetryPolicy>(context.RetryPolicy);

            Assert.Equal(expectedAttempts, retryPolicy.MaxAttempts);
        }
コード例 #14
0
ファイル: IndexMappings.cs プロジェクト: hawful70/Puppy
        /// <summary>
        ///     Create a new index for the parent document
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="mappingDefinition"> mapping definitions for the index type </param>
        private void CreatePropertyMappingForEntityForParentDocument(EntityContextInfo entityInfo,
                                                                     ElasticMapping elasticMapping, MappingDefinition mappingDefinition)
        {
            var itemType = elasticMapping.GetDocumentType(entityInfo.EntityType);

            if (_processedItems.Contains("_mapping" + itemType))
            {
                return;
            }
            _processedItems.Add("_mapping" + itemType);

            var elasticCrudJsonWriter = new ElasticJsonWriter();

            elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            elasticCrudJsonWriter.JsonWriter.WritePropertyName(itemType);
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            mappingDefinition.Source.WriteJson(elasticCrudJsonWriter);
            mappingDefinition.All.WriteJson(elasticCrudJsonWriter);

            if (entityInfo.RoutingDefinition.RoutingId != null && _elasticSerializerConfiguration.UserDefinedRouting)
            {
                CreateForceRoutingMappingForDocument(elasticCrudJsonWriter);
            }

            if (entityInfo.RoutingDefinition.ParentId != null)
            {
                CreateParentMappingForDocument(
                    elasticCrudJsonWriter,
                    elasticMapping.GetDocumentType(entityInfo.ParentEntityType));
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, entityInfo, elasticMapping);

            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();

            CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(), mappingDefinition.Index,
                                                         itemType);
        }
コード例 #15
0
        [ExcludeFromCodeCoverage] // Expression isn't "executed"
        public static void GetFieldName_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.GetFieldName("", null));
        }
コード例 #16
0
ファイル: ElasticSerializer.cs プロジェクト: hawful70/Puppy
        private void CreateBulkContentForParentDocument(EntityContextInfo entityInfo, ElasticMapping elasticMapping)
        {
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            _elasticCrudJsonWriter.JsonWriter.WritePropertyName("index");
            // Write the batch "index" operation header
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();
            WriteValue("_index", elasticMapping.GetIndexForType(entityInfo.EntityType));
            WriteValue("_type", elasticMapping.GetDocumentType(entityInfo.EntityType));
            WriteValue("_id", entityInfo.Id);
            if (entityInfo.RoutingDefinition.ParentId != null &&
                _elasticSerializerConfiguration.ProcessChildDocumentsAsSeparateChildIndex)
            {
                WriteValue("_parent", entityInfo.RoutingDefinition.ParentId);
            }
            if (entityInfo.RoutingDefinition.RoutingId != null &&
                _elasticSerializerConfiguration.UserDefinedRouting)
            {
                WriteValue("_routing", entityInfo.RoutingDefinition.RoutingId);
            }
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n"); //ES requires this \n separator
            _elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticMapping.MapEntityValues(entityInfo, _elasticCrudJsonWriter, true);

            _elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            _elasticCrudJsonWriter.JsonWriter.WriteRaw("\n");
        }
コード例 #17
0
        [ExcludeFromCodeCoverage] // Expression isn't "executed"
        public static void FormatValue_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.FormatValue(null, "value"));
        }
コード例 #18
0
        public static void GetTypeSelectionCriteria_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.GetTypeSelectionCriteria(null));
        }
コード例 #19
0
        public static void GetDocumentType_GuardClause()
        {
            var mapping = new ElasticMapping();

            Assert.Throws <ArgumentNullException>(() => mapping.GetDocumentType(null));
        }
コード例 #20
0
ファイル: IndexMappings.cs プロジェクト: hawful70/Puppy
 private void ProccessPropertyMappingsWithoutTypeName(ElasticJsonWriter elasticCrudJsonWriter,
                                                      EntityContextInfo entityInfo, ElasticMapping elasticMapping)
 {
     //"properties": {
     elasticCrudJsonWriter.JsonWriter.WritePropertyName("properties");
     elasticCrudJsonWriter.JsonWriter.WriteStartObject();
     elasticMapping.MapEntityValues(entityInfo, elasticCrudJsonWriter, true, CreatePropertyMappings);
     elasticCrudJsonWriter.JsonWriter.WriteEndObject();
 }
コード例 #21
0
ファイル: IndexMappings.cs プロジェクト: hawful70/Puppy
        /// <summary>
        ///     Create a new mapping for the child type in the parent document index
        /// </summary>
        /// <param name="entityInfo">       </param>
        /// <param name="elasticMapping">   </param>
        /// <param name="item">             </param>
        /// <param name="mappingDefinition"> definition for the type mappings </param>
        private void CreatePropertyMappingForChildDocument(EntityContextInfo entityInfo, ElasticMapping elasticMapping,
                                                           EntityContextInfo item, MappingDefinition mappingDefinition)
        {
            var childMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(item.EntityType);

            var parentMapping =
                _elasticSerializerConfiguration.ElasticMappingResolver.GetElasticSearchMapping(item.ParentEntityType);

            var childType  = childMapping.GetDocumentType(item.EntityType);
            var parentType = parentMapping.GetDocumentType(item.ParentEntityType);

            var processedId = childType + "_" + parentType;

            if (_processedItems.Contains(childType))
            {
                var test = CommandTypes.Find(t => t.StartsWith(childType));
                if (test != processedId)
                {
                    throw new ElasticException("InitMappings: Not supported, child documents can only have one parent");
                }
                return;
            }
            _processedItems.Add(childType);
            CommandTypes.Add(processedId);

            var elasticCrudJsonWriter = new ElasticJsonWriter();

            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            elasticCrudJsonWriter.JsonWriter.WritePropertyName(childType);
            elasticCrudJsonWriter.JsonWriter.WriteStartObject();

            mappingDefinition.Source.WriteJson(elasticCrudJsonWriter);
            mappingDefinition.All.WriteJson(elasticCrudJsonWriter);

            CreateParentMappingForDocument(
                elasticCrudJsonWriter,
                elasticMapping.GetDocumentType(item.ParentEntityType));

            if (item.RoutingDefinition.RoutingId != null && _elasticSerializerConfiguration.UserDefinedRouting)
            {
                CreateForceRoutingMappingForDocument(elasticCrudJsonWriter);
            }

            ProccessPropertyMappingsWithoutTypeName(elasticCrudJsonWriter, item, childMapping);
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            elasticCrudJsonWriter.JsonWriter.WriteEndObject();
            CreateMappingCommandForTypeWithExistingIndex(elasticCrudJsonWriter.GetJsonString(),
                                                         elasticMapping.GetIndexForType(entityInfo.EntityType), childMapping.GetDocumentType(item.EntityType));
        }