Beispiel #1
0
        private static MappingManager MakeMapper(Type t)
        {
            // See https://github.com/mausch/SolrNet/blob/master/Documentation/Overriding-mapper.md
            var mapper = new MappingManager();

            foreach (var prop in t.GetProperties())
            {
                foreach (Attribute attribute in prop.GetCustomAttributes(true))
                {
                    if (attribute is FieldAttribute)
                    {
                        var    fa        = attribute as FieldAttribute;
                        string fieldName = string.IsNullOrWhiteSpace(fa.FieldName) ? prop.Name : fa.FieldName;
                        mapper.Add(prop, fieldName, fa.Boost);
                    }
                    else if (attribute is SolrFieldAttribute)
                    {
                        var    fa        = attribute as SolrFieldAttribute;
                        string fieldName = string.IsNullOrWhiteSpace(fa.FieldName) ? prop.Name : fa.FieldName;
                        mapper.Add(prop, fieldName, fa.Boost);
                    }
                    if (attribute is UniqueKeyAttribute)
                    {
                        var uka = attribute as UniqueKeyAttribute;
                        mapper.SetUniqueKey(prop);
                    }
                    if (attribute is SolrUniqueKeyAttribute)
                    {
                        var uka = attribute as SolrUniqueKeyAttribute;
                        mapper.SetUniqueKey(prop);
                    }
                }
            }
            return(mapper);
        }
Beispiel #2
0
        public void Inherited_doesnt_admit_duplicates2()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(InheritedEntity).GetProperty("Id"), "id2");
            mgr.Add(typeof(Entity).GetProperty("Id"), "id");
        }
Beispiel #3
0
        public void Add_duplicate_property_overwrites()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(Entity).GetProperty("Id"), "id");
            mgr.Add(typeof(Entity).GetProperty("Id"), "id2");
            var fields = mgr.GetFields(typeof(Entity));

            Assert.AreEqual(1, fields.Count);
            Assert.AreEqual("id2", fields.First().Value.FieldName);
        }
Beispiel #4
0
        public void Inherited()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(Entity).GetProperty("Id"), "id");
            mgr.Add(typeof(InheritedEntity).GetProperty("Description"), "desc");
            var entityFields = mgr.GetFields(typeof(Entity));

            Assert.AreEqual(1, entityFields.Count);
            var inheritedEntityFields = mgr.GetFields(typeof(InheritedEntity));

            Assert.AreEqual(2, inheritedEntityFields.Count);
        }
Beispiel #5
0
        public void InheritedEntityMappedUsingReflectedTypeOnly()
        {
            var mappingManager = new MappingManager(true);
            var idProperty     = typeof(ComplexEntity <FrenchSchemaLanguage>).GetProperty("Id");

            mappingManager.Add(idProperty, "id");
            mappingManager.SetUniqueKey(idProperty);
            mappingManager.Add(typeof(ComplexEntity <FrenchSchemaLanguage>).GetProperty("Description"), "description");

            var registeredType = mappingManager.GetRegisteredTypes();

            Assert.Equal(1, registeredType.Count);
            Assert.Single(registeredType, typeof(ComplexEntity <FrenchSchemaLanguage>));
        }
Beispiel #6
0
        public void InheritedEntityMappedNotUsingReflectedTypeOnly()
        {
            var mappingManager = new MappingManager(false); // Same as "new MappingManager();"
            var idProperty     = typeof(ComplexEntity <FrenchSchemaLanguage>).GetProperty("Id");

            mappingManager.Add(idProperty, "id");
            mappingManager.SetUniqueKey(idProperty);
            mappingManager.Add(typeof(ComplexEntity <FrenchSchemaLanguage>).GetProperty("Description"), "description");

            var registeredType = mappingManager.GetRegisteredTypes();

            Assert.Equal(2, registeredType.Count);
            Assert.Collection(registeredType, type => Assert.Equal(typeof(BaseComplexEntity <FrenchSchemaLanguage>), type),
                              type => Assert.Equal(typeof(ComplexEntity <FrenchSchemaLanguage>), type));
        }
Beispiel #7
0
 public void AddProperty3_doesnt_admit_null()
 {
     Assert.Throws <ArgumentNullException>(() => {
         var mgr = new MappingManager();
         mgr.Add(typeof(Entity).GetProperties()[0], null);
     });
 }
Beispiel #8
0
 public void AddProperty2_doesnt_admit_null()
 {
     Assert.Throws <ArgumentNullException>(() => {
         var mgr = new MappingManager();
         mgr.Add(null, "");
     });
 }
Beispiel #9
0
        public void MappingTypesAreCompatibleWithSolrTypesRule_with_nonexistant_rule()
        {
            var rule           = new MappingTypesAreCompatibleWithSolrTypesRule(new[] { new StringSolrFieldTypeChecker() });
            var mappingManager = new MappingManager();

            mappingManager.Add(typeof(SchemaMappingTestDocument).GetProperty("Name"));
            var validations = rule.Validate(typeof(SchemaMappingTestDocument), new SolrSchema(), mappingManager).ToList();
        }
        public void MappedPropertyForWhichSolrFieldExistsInSchemaShouldNotReturnError()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("ID"), "id");
            mgr.SetUniqueKey(typeof(SchemaMappingTestDocument).GetProperty("ID"));
            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("Name"), "name");

            var schemaManager = new MappingValidator(mgr, new[] { new MappedPropertiesIsInSolrSchemaRule() });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.Empty(validationResults);
        }
Beispiel #11
0
        public void Inherited_gets_id_property_correctly2()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(InheritedEntity).GetProperty("Id"), "id");

            Assert.IsTrue(mgr.GetFields(typeof(InheritedEntity)).ContainsKey("id"), "InheritedEntity contains id field");
            Assert.IsTrue(mgr.GetFields(typeof(Entity)).ContainsKey("id"), "Entity contains id field");
        }
Beispiel #12
0
        public void AddAndGet()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(Entity).GetProperty("Id"), "id");
            var fields = mgr.GetFields(typeof(Entity));

            Assert.AreEqual(1, fields.Count);
        }
Beispiel #13
0
        public void GetRegistered()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(Entity).GetProperty("Id"), "id");
            var types = mgr.GetRegisteredTypes();

            Assert.AreEqual(1, types.Count);
            Assert.Contains(types, typeof(Entity));
        }
        public void MappedPropertiesIsInSolrSchemaRule_ignores_score()
        {
            var rule   = new MappedPropertiesIsInSolrSchemaRule();
            var mapper = new MappingManager();

            mapper.Add(typeof(SchemaMappingTestDocument).GetProperty("Score"), "score");
            var results = rule.Validate(typeof(SchemaMappingTestDocument), new SolrSchema(), mapper).ToList();

            Assert.NotNull(results);
            Assert.Empty(results);
        }
Beispiel #15
0
        public void Add_property_only()
        {
            var mgr      = new MappingManager();
            var property = typeof(Entity).GetProperty("Id");

            mgr.Add(property);
            var fields = mgr.GetFields(typeof(Entity));

            Assert.AreEqual(1, fields.Count);
            Assert.AreEqual("Id", fields.First().Value.FieldName);
        }
Beispiel #16
0
        public void EmptyEnumThrows()
        {
            var mapper = new MappingManager();

            mapper.Add(typeof(TestDocWithEnum).GetProperty("En"), "basicview");
            var docParser = GetDocumentParser <TestDocWithEnum>(mapper);
            var parser    = new ResultsResponseParser <TestDocWithEnum>(docParser);
            var xml       = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.response.xml");
            var results   = new SolrQueryResults <TestDocWithEnum>();

            parser.Parse(xml, results);
        }
Beispiel #17
0
        public void UniqueKey_Set_and_get()
        {
            var mgr      = new MappingManager();
            var property = typeof(Entity).GetProperty("Id");

            mgr.Add(property, "id");
            mgr.SetUniqueKey(property);
            var key = mgr.GetUniqueKey(typeof(Entity));

            Assert.AreEqual(property, key.Property);
            Assert.AreEqual("id", key.FieldName);
        }
Beispiel #18
0
        public void ParseDocumentWithMappingManager()
        {
            var mapper = new MappingManager();

            mapper.Add(typeof(TestDocumentWithoutAttributes).GetProperty("Id"), "id");
            var parser  = GetDocumentParser <TestDocumentWithoutAttributes>(mapper);
            var xml     = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.response.xml");
            var docNode = xml.XPathSelectElement("response/result/doc");
            var doc     = parser.ParseDocument(docNode).ToList();

            Assert.IsNotNull(doc);
            Assert.AreEqual(123456, doc[0].Id);
        }
Beispiel #19
0
        private void SetupSolr()
        {
            Startup.InitContainer();

            Startup.Container.Remove <IReadOnlyMappingManager>();
            var mapper = new MappingManager();

            mapper.Add(typeof(Entity).GetProperty("Description"), "name");
            mapper.Add(typeof(Entity).GetProperty("Id"), "id");
            mapper.Add(typeof(Entity).GetProperty("Tags"), "cat");
            Startup.Container.Register <IReadOnlyMappingManager>(c => mapper);

            Startup.Container.Remove <ISolrDocumentPropertyVisitor>();
            var propertyVisitor = new DefaultDocumentVisitor(mapper, Startup.Container.GetInstance <ISolrFieldParser>());

            Startup.Container.Register <ISolrDocumentPropertyVisitor>(c => propertyVisitor);

            Startup.Init <Entity>("http://localhost:8983/solr");
            var solr = ServiceLocator.Current.GetInstance <ISolrOperations <Entity> >();

            solr.Delete(SolrQuery.All).Commit();
        }
        public void ParseDocumentWithMappingManager()
        {
            var mapper = new MappingManager();

            mapper.Add(typeof(TestDocumentWithoutAttributes).GetProperty("Id"), "id");
            var parser  = new SolrDocumentResponseParser <TestDocumentWithoutAttributes>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator <TestDocumentWithoutAttributes>());
            var xml     = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.response.xml");
            var docNode = xml.XPathSelectElement("response/result/doc");
            var doc     = parser.ParseDocument(docNode, mapper.GetFields(typeof(TestDocumentWithoutAttributes)));

            Assert.IsNotNull(doc);
            Assert.AreEqual(123456, doc.Id);
        }
        public void DictionaryFields_are_ignored()
        {
            var rule   = new MappedPropertiesIsInSolrSchemaRule();
            var mapper = new MappingManager();

            mapper.Add(typeof(SchemaMappingTestDocument).GetProperty("DynamicMapped"), "ma_*");
            var schema    = new SolrSchema();
            var fieldType = new SolrFieldType("string", "solr.StrField");

            schema.SolrFields.Add(new SolrField("ma_uaua", fieldType));
            var results = rule.Validate(typeof(SchemaMappingTestDocument), new SolrSchema(), mapper).ToList();

            Assert.Empty(results);
        }
Beispiel #22
0
        public void MultivaluedSolrFieldMappedToCollectionShouldNotReturnError()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("NameCollection"), "name");

            var schemaManager = new MappingValidator(mgr, new[] { new MultivaluedMappedToCollectionRule() });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaMultiValuedName.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.AreEqual(0, validationResults.Count);
        }
Beispiel #23
0
        public void CollectionMappedToNonMultiValuedFolrFieldShouldReturnError()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("NameCollection"), "author");

            var schemaManager = new MappingValidator(mgr, new[] { new MultivaluedMappedToCollectionRule() });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaMultiValuedName.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.Single(validationResults);
            testOutputHelper.WriteLine(validationResults[0].Message);
        }
        public void RequiredSolrFieldForWhichNoCopyFieldExistsShouldReturnError()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("ID"), "id");
            mgr.SetUniqueKey(typeof(SchemaMappingTestDocument).GetProperty("ID"));

            var schemaManager = new MappingValidator(mgr, new[] { new RequiredFieldsAreMappedRule() });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.Equal(1, validationResults.Count);
        }
        public void NonMatchingUniqueKeyMappingShouldReturnError()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("Name"), "name");
            mgr.SetUniqueKey(typeof(SchemaMappingTestDocument).GetProperty("Name"));

            var schemaManager = new MappingValidator(mgr, new[] { new UniqueKeyMatchesMappingRule() });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.AreEqual(1, validationResults.Count);
        }
Beispiel #26
0
        public void StringMappedToIntShouldReturnError()
        {
            var mappingTypesCompatibleRule = new MappingTypesAreCompatibleWithSolrTypesRule(new[] { new StringSolrFieldTypeChecker() });

            var mgr = new MappingManager();

            mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("FieldNotSolrSchema"), "popularity");

            var schemaManager = new MappingValidator(mgr, new[] { mappingTypesCompatibleRule });

            var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaMappingTypes.xml");
            var solrSchemaParser  = new SolrSchemaParser();
            var schema            = solrSchemaParser.Parse(schemaXmlDocument);

            var validationResults = schemaManager.EnumerateValidationResults(typeof(SchemaMappingTestDocument), schema).ToList();

            Assert.Equal(1, validationResults.Count);
        }
        public void SchemaNull_MappingNotNull_generates_error()
        {
            var rule       = new UniqueKeyMatchesMappingRule();
            var mapper     = new MappingManager();
            var idProperty = typeof(SchemaMappingTestDocument).GetProperty("ID");

            mapper.Add(idProperty);
            mapper.SetUniqueKey(idProperty);
            var validations = rule.Validate(typeof(SchemaMappingTestDocument), new SolrSchema(), mapper).ToList();

            Assert.IsNotNull(validations);
            Assert.AreEqual(1, validations.Count);
            foreach (var v in validations)
            {
                Console.WriteLine("{0}: {1}", v.GetType(), v.Message);
            }
            Assert.IsInstanceOf <ValidationError>(validations[0]);
        }
Beispiel #28
0
        public void AddProperty3_doesnt_admit_null()
        {
            var mgr = new MappingManager();

            mgr.Add(typeof(Entity).GetProperties()[0], null);
        }
Beispiel #29
0
        public void AddProperty2_doesnt_admit_null()
        {
            var mgr = new MappingManager();

            mgr.Add(null, "");
        }