Example #1
0
        /// <summary>Visits a type binary expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitTypeBinary(System.Linq.Expressions.TypeBinaryExpression expression)
        {
            var classMappings = MappingsRepository.FindMappedClasses(expression.TypeOperand);

            if (classMappings.Any())
            {
                Remotion.Linq.Clauses.FromClauseBase sourceExpression = GetSourceExpression(expression.Expression);
                StrongEntityAccessor entityAccessor = _query.FindAllComponents <StrongEntityAccessor>().FirstOrDefault(item => item.SourceExpression == sourceExpression);
                if (entityAccessor == null)
                {
                    entityAccessor = this.GetEntityAccessor(sourceExpression);
                    _query.Elements.Add(entityAccessor);
                }

                EntityTypeConstrain typeConstrain = this.CreateTypeConstrain(expression.TypeOperand, entityAccessor.SourceExpression.FromExpression);
                _lastComponent = typeConstrain;
                if ((_currentComponent.Count > 0) && (_currentComponent.Peek() is BinaryOperatorNavigator))
                {
                    HandleComponent(typeConstrain);
                }
                else if (!entityAccessor.Elements.Contains(typeConstrain))
                {
                    entityAccessor.Elements.Add(typeConstrain);
                }
            }
            else
            {
                return(base.VisitTypeBinary(expression));
            }

            return(expression);
        }
Example #2
0
        protected override void ScenarioSetup()
        {
            MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <PropertyInfo>()))
            .Returns <IEntity, PropertyInfo>((entity, propertyInfo) => PropertyMapping.Object);
            Entity = new Entity(Iri, Context);
            EntitySource.Setup(instance => instance.Load(It.IsAny <Iri>()))
            .Returns <Iri>(iri => new[] { new Statement(iri, PredicateIri, ExpectedName) });
            EntityMapping = new Mock <IEntityMapping>(MockBehavior.Strict);
            EntityMapping.SetupGet(instance => instance.Type).Returns(typeof(IProduct));
            Converter = new Mock <IConverter>(MockBehavior.Strict);
            Converter.Setup(instance => instance.ConvertFrom(It.IsAny <Statement>()))
            .Returns <Statement>(statement => statement.Value);
            var property = new Mock <PropertyInfo>(MockBehavior.Strict);

            property.SetupGet(instance => instance.Name).Returns("Name");
            property.SetupGet(instance => instance.PropertyType).Returns(typeof(string));
            property.SetupGet(instance => instance.DeclaringType).Returns(typeof(IProduct));
            property.Setup(instance => instance.GetHashCode()).Returns(typeof(IProduct).GetProperty("Name").GetHashCode());
            property.Setup(instance => instance.Equals(It.IsAny <object>())).Returns <object>(obj => (obj as PropertyInfo)?.Name == "Name");
            PropertyMapping = new Mock <IPropertyMapping>(MockBehavior.Strict);
            PropertyMapping.SetupGet(instance => instance.EntityMapping).Returns(EntityMapping.Object);
            PropertyMapping.SetupGet(instance => instance.PropertyInfo).Returns(property.Object);
            PropertyMapping.SetupGet(instance => instance.Name).Returns("Name");
            PropertyMapping.SetupGet(instance => instance.Graph).Returns((Iri)null);
            PropertyMapping.SetupGet(instance => instance.ValueConverter).Returns(Converter.Object);
        }
        public void Setup()
        {
            _ontologies = new Mock <IOntologyProvider>();
            _ontologies.Setup(o => o.ResolveUri(It.IsAny <string>(), It.IsAny <string>()))
            .Returns((string p, string t) => GetUri(p, t));
            _container = new ServiceContainer();
            _container.RegisterFrom <ConventionsCompositionRoot>();
            _container.RegisterFrom <MappingComposition>();
            _container.RegisterFrom <AttributesMappingComposition>();
            _container.RegisterFrom <FluentMappingComposition>();
            _container.RegisterFrom <InternalComponentsCompositionRoot>();
            _container.RegisterInstance(_ontologies.Object);
            var logger = new Mock <ILogger>(MockBehavior.Strict);

            logger.Setup(instance => instance.Log(It.IsAny <LogLevel>(), It.IsAny <string>(), It.IsAny <object[]>()));
            logger.Setup(instance => instance.Log(It.IsAny <LogLevel>(), It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <object[]>()));
            _container.RegisterInstance(logger.Object);
            foreach (var mappingProviderSource in CreateMappingSources())
            {
                _container.RegisterInstance(mappingProviderSource, mappingProviderSource.GetType().FullName);
            }

            var conventions         = _container.GetInstance <IEnumerable <IConvention> >();
            var mappingModelBuilder = new MappingModelBuilder(new MappingContext(_ontologies.Object, conventions), new ConverterCatalog());

            _container.RegisterInstance(mappingModelBuilder);
            _mappingsRepository = (MappingsRepository)_container.GetInstance <IMappingsRepository>();
        }
        public void Should_fail_when_getting_ambiguous_property_mapping()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IChild>();

            // then
            mapping.Invoking(m => m.PropertyFor("Member")).ShouldThrow <AmbiguousPropertyException>();
        }
Example #5
0
        public void Type_can_be_mapped_without_rdf_type()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IUntypedAnimal>();

            // then
            Assert.That(mapping.Classes, Is.Empty);
            Assert.That(mapping.PropertyFor("Name"), Is.Not.Null);
        }
        public void Should_respect_hiding_members()
        {
            // given, when
            var mapping = MappingsRepository.MappingFor <IHidesMember>();

            // then
            mapping.PropertyFor("MappedProperty1").Uri.Should().Be(new Uri("urn:hidden:mapping"));
            mapping.PropertyFor("MappedProperty2").Uri.Should().Be(new Uri("urn:hidden:fluent"));
        }
Example #7
0
        public void Subclass_should_inherit_parent_Class_mappings()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IHuman>();

            // when
            var classMappings = mapping.Classes;

            // then
            classMappings.Should().HaveCount(4);
        }
Example #8
0
        public void Explicit_setting_for_collection_converter_should_not_be_replaced_by_convention()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IEntityWithExplicitConverters>();

            // when
            var property = (ICollectionMapping)mapping.PropertyFor("Collection");

            // then
            property.ElementConverter.Should().BeOfType <BooleanConverter>();
        }
Example #9
0
        public void Subclass_should_inehrit_parent_generic_property_mapping()
        {
            // given
            var mapping = MappingsRepository.MappingFor <ISpecificContainer>();

            // when
            var itemsPropertyMapping = (ICollectionMapping)mapping.Properties.First(item => item.Name == "Items");

            // then
            itemsPropertyMapping.StoreAs.Should().Be(StoreAs.SimpleCollection);
        }
        public void Should_give_access_to_all_ambiguous_property_mappings()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IChild>();

            // when
            var properties = mapping.Properties;

            // then
            properties.Should().HaveCount(4);
        }
        public void Should_retain_class_inheritance_in_concrete_entities()
        {
            // given
            var mapping = MappingsRepository.MappingFor <ConcreteEntityChild>();

            // when
            var property = mapping.PropertyFor("UnMappedProperty");

            // then
            property.Uri.Should().Be(new Uri("urn:concrete:class"));
        }
        public void Should_contain_closed_generic_mappings_not_mapped_explicitly()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IGenericParent <int> >();

            // when
            var property = mapping.PropertyFor("MappedProperty2");

            // then
            property.ReturnType.Should().Be(typeof(int));
        }
        public void Should_contain_open_generic_mappings()
        {
            // given
            var mapping = MappingsRepository.MappingFor(typeof(IGenericParent <>));

            // when
            var property = mapping.PropertyFor("MappedProperty2");

            // then
            property.ReturnType.Name.Should().Be("T");
        }
        public void Should_use_open_generic_mapping_fallback()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IDerived>();

            // when
            var property = mapping.PropertyFor("MappedProperty2");

            // then
            property.Uri.Should().Be(new Uri("urn:open:mapping2"));
        }
        public void Should_fill_return_types_for_inherited_generic_properties()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IDerived>();

            // when
            var property = mapping.PropertyFor("MappedProperty2");

            // then
            property.ReturnType.Should().Be(typeof(int));
        }
        public void Should_read_all_properties_from_fluent_mapped_hierarchy_of_concrete_classes()
        {
            // given
            var mapping = MappingsRepository.MappingFor <FluentNoIEntityInnerMapChild>();

            // when
            var properties = mapping.Properties;

            // then
            properties.Should().HaveCount(2);
        }
        public void Should_inherit_properties_in_concrete_entity_class()
        {
            // given
            var mapping = MappingsRepository.MappingFor <ConcreteEntity>();

            // when
            var property = mapping.PropertyFor("MappedProperty1");

            // then
            property.Uri.Should().Be(new Uri("urn:open:mapping1"));
        }
        /// <summary>
        /// Creates a new instance of <see cref="EntityContextFactory"/>
        /// </summary>
        public EntityContextFactory()
        {
            // todo: change how defaults are set
            _namedGraphSelector = new NamedGraphSelector();
            _mappingsRepository = new MappingsRepository();
            _mappingsRepository.AddVisitor(_matcher);

            WithMappings(DefaultMappings);
            _conventions = CreateDefaultConventions().ToList();
            LogTo.Info("Created entity context factory");
        }
        public void Should_respect_mapping_override(Type type, string expectedUri)
        {
            // given
            var mapping = MappingsRepository.MappingFor(type);

            // when
            var property = mapping.PropertyFor("MappedProperty1");

            // then
            property.Uri.Should().Be(new Uri(expectedUri));
        }
Example #20
0
        public void Explicit_setting_for_collection_should_not_be_replaced_by_conventions(
            string propertyName, StoreAs asExpected)
        {
            // given
            var mapping = MappingsRepository.MappingFor <IEntityWithCollections>();

            // when
            var property = (ICollectionMapping)mapping.PropertyFor(propertyName);

            // then
            property.StoreAs.Should().Be(asExpected);
        }
Example #21
0
        public void Mapped_type_should_contain_inherited_properties()
        {
            // given
            var mapping = MappingsRepository.MappingFor <ICarnivore>();

            // when
            var propertyMapping = mapping.PropertyFor("Name");

            // then
            Assert.That(propertyMapping.Name, Is.EqualTo("Name"));
            Assert.That(propertyMapping.Uri, Is.EqualTo(new Uri("http://example/livingThings#name")));
        }
Example #22
0
        public void Multimapping_should_not_throw_when_getting_derived_properties()
        {
            // given
            IEntityMapping herbivoreMapping = MappingsRepository.MappingFor <IHerbivore>();
            IEntityMapping carnivoreMapping = MappingsRepository.MappingFor <ICarnivore>();

            // when
            var multiMapping = new MultiMapping(herbivoreMapping, carnivoreMapping);

            // then
            Assert.DoesNotThrow(() => multiMapping.PropertyFor("Name"));
        }
Example #23
0
        public void Mapping_should_allow_dictionary_with_default_key_value()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IEntityWithDictionary>();

            // then
            var propertyMapping = mapping.PropertyFor("SettingsDefault");

            propertyMapping.Should().BeAssignableTo <IDictionaryMapping>();
            propertyMapping.As <IDictionaryMapping>().KeyPredicate.Should().Be(Vocabularies.Rdf.predicate);
            propertyMapping.As <IDictionaryMapping>().ValuePredicate.Should().Be(Vocabularies.Rdf.@object);
        }
        public void Mapping_of_class_derived_from_generic_class_should_contain_parent_collections_mapped_in_open_generic()
        {
            // given
            var mapping = MappingsRepository.MappingFor <IImplementsGenericWithInt>();

            // when
            var propertyMapping = mapping.PropertyFor("Collection");

            // then
            propertyMapping.Name.Should().Be("Collection");
            propertyMapping.Uri.Should().Be(new Uri("urn:generic:collection"));
        }
Example #25
0
        public void Explicit_setting_for_dictionary_value_converter_should_not_be_replaced_by_convention()
        {
            // given
            var generatedEntityType = GetDynamicType("RomanticWeb.TestEntities.IEntityWithExplicitConverters_Dictionary_Entry, RomanticWeb.TestEntities");
            var mapping             = MappingsRepository.MappingFor(generatedEntityType);

            // when
            var property = mapping.PropertyFor("Value");

            // then
            property.Converter.Should().BeOfType <BooleanConverter>();
        }
 protected override void ScenarioSetup()
 {
     SourceContext = new DefaultEntityContext(EntitySource.Object, MappingsRepository.Object, ChangeDetector.Object);
     Source        = SourceContext.Create <IProduct>(new Iri("source"));
     MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <PropertyInfo>()))
     .Returns <IEntity, PropertyInfo>((entity, propertyInfo) =>
     {
         var result = new Mock <IPropertyMapping>(MockBehavior.Strict);
         result.SetupGet(instance => instance.PropertyInfo).Returns(propertyInfo);
         return(result.Object);
     });
 }
Example #27
0
 protected override void ScenarioSetup()
 {
     base.ScenarioSetup();
     Calls = 0;
     Context.UnmappedPropertyEncountered += (sender, e) =>
     {
         if (Calls++ == 0)
         {
             e.PropertyMapping = PropertyMapping.Object;
         }
     };
     MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <Iri>(), It.IsAny <Iri>())).Returns((IPropertyMapping)null);
 }
Example #28
0
        public void Mapped_type_should_have_mapped_class()
        {
            // given
            var mapping = MappingsRepository.MappingFor <ICarnivore>();

            // when
            var classMappings = mapping.Classes.ToList();

            // then
            classMappings.Should().HaveCount(2);
            classMappings.Should()
            .Contain(c => c.IsInherited && c.IsMatch(new[] { new Uri("http://example/livingThings#Animal") }));
            classMappings.Should()
            .Contain(c => !c.IsInherited && c.IsMatch(new[] { new Uri("http://example/livingThings#Carnivore") }));
        }
Example #29
0
 protected override void ScenarioSetup()
 {
     MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <PropertyInfo>()))
     .Returns <IEntity, PropertyInfo>((entity, propertyInfo) =>
     {
         var result = new Mock <IPropertyMapping>(MockBehavior.Strict);
         result.SetupGet(instance => instance.PropertyInfo).Returns(propertyInfo);
         return(result.Object);
     });
     Product             = Context.Create <IProduct>(new Iri("test"));
     Product.Description = "Product description";
     Product.Name        = "Product name";
     Product.Ordinal     = 1;
     Product.Price       = 3.14159;
     Product.Categories.Add("category 1");
     Product.Categories.Add("category 2");
     Product.Comments.Add("comment 1");
     Product.Comments.Add("comment 2");
 }
        public void Setup()
        {
            _ontologies = new Mock<IOntologyProvider>();
            _ontologies.Setup(o => o.ResolveUri(It.IsAny<string>(), It.IsAny<string>()))
                       .Returns((string p, string t) => GetUri(p, t));

            _mappingsRepository = new MappingsRepository();
            foreach (var mappingSource in CreateMappingSources())
            {
                MappingsRepository.AddSource(GetType().Assembly, mappingSource);
            }

            IEnumerable<IConvention> conventions = new IConvention[]
                                                       {
                                                           new DefaultDictionaryKeyPredicateConvention(),
                                                           new DefaultDictionaryValuePredicateConvention(),
                                                           new CollectionStorageConvention(),
                                                           new RdfListConvention()
                                                       };
            MappingsRepository.RebuildMappings(new MappingContext(_ontologies.Object, conventions));
        }
Example #31
0
        protected override void ScenarioSetup()
        {
            Converter = new Mock <IConverter>(MockBehavior.Strict);
            Converter.Setup(instance => instance.ConvertTo(It.IsAny <Iri>(), It.IsAny <Iri>(), It.IsAny <object>(), null))
            .Returns <Iri, Iri, object, Iri>((subject, predicate, value, graph) => new Statement(subject, predicate, String.Format(CultureInfo.InvariantCulture, "{0}", value), graph));
            MappingsRepository.Setup(instance => instance.FindEntityMappingFor(It.IsAny <IEntity>(), It.IsAny <Type>()))
            .Returns <IEntity, Type>((entity, type) =>
            {
                var entityMapping = new Mock <IEntityMapping>(MockBehavior.Strict);
                var classMapping  = new Mock <IStatementMapping>(MockBehavior.Strict);
                classMapping.SetupGet(instance => instance.Graph).Returns((Iri)null);
                classMapping.SetupGet(instance => instance.Term).Returns(new Iri(type.Name.Substring(1)));
                entityMapping.SetupGet(instance => instance.Classes).Returns(new[] { classMapping.Object });
                return(entityMapping.Object);
            });
            MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <PropertyInfo>()))
            .Returns <IEntity, PropertyInfo>((entity, property) =>
            {
                var propertyMapping = new Mock <IPropertyMapping>(MockBehavior.Strict);
                propertyMapping.SetupGet(instance => instance.ValueConverter).Returns(Converter.Object);
                propertyMapping.SetupGet(instance => instance.Term).Returns(new Iri(property.Name.ToLower()));
                propertyMapping.SetupGet(instance => instance.Graph).Returns((Iri)null);
                propertyMapping.SetupGet(instance => instance.PropertyInfo).Returns(property);
                if (property.PropertyType.IsAnEnumerable())
                {
                    var collectionMapping = propertyMapping.As <ICollectionMapping>();
                    collectionMapping.SetupGet(instance => instance.StoreAs)
                    .Returns(property.PropertyType.IsAList() ? CollectionStorageModel.LinkedList : CollectionStorageModel.Simple);
                    return(collectionMapping.Object);
                }

                return(propertyMapping.Object);
            });
            RetractedStatements = new Dictionary <IEntity, ISet <Statement> >();
            AddedStatements     = new Dictionary <IEntity, ISet <Statement> >();
            Context             = new Mock <DefaultEntityContext>(null, MappingsRepository.Object, Detector);
            Context.SetupGet(instance => instance.Mappings).Returns(MappingsRepository.Object);
            Entity = new Entity(new Iri("test"), Context.Object);
        }
        protected override void ScenarioSetup()
        {
            CallCounter = new Dictionary <Iri, int>();
            Entity      = new Entity(Iri, Context);
            EntitySource.Setup(instance => instance.Load(It.IsAny <Iri>())).Returns <Iri>(SetupStatements);
            var entityMapping = new Mock <IEntityMapping>(MockBehavior.Strict);

            entityMapping.SetupGet(instance => instance.Type).Returns(typeof(IComplexEntity));
            Mock <ICollectionMapping>[] properties =
            {
                SetupPropertyMapping(entityMapping, "Ordinals"),
                SetupPropertyMapping(entityMapping, "Related"),
                SetupPropertyMapping(entityMapping, "Floats"),
                SetupPropertyMapping(entityMapping, "Doubles"),
                SetupPropertyMapping(entityMapping, "Other")
            };
            MappingsRepository.Setup(instance => instance.FindEntityMappingFor(It.IsAny <IEntity>(), It.IsAny <Iri>(), null))
            .Returns <IEntity, Iri, Iri>((entity, iri, graph) => entityMapping.Object);
            MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <Iri>(), null))
            .Returns <IEntity, Iri, Iri>((entity, iri, graph) => properties.Where(property => property.Object.Term == iri).Select(property => property.Object).First());
            MappingsRepository.Setup(instance => instance.FindPropertyMappingFor(It.IsAny <IEntity>(), It.IsAny <PropertyInfo>()))
            .Returns <IEntity, PropertyInfo>((entity, propertyInfo) => properties.Where(property => property.Object.PropertyInfo == propertyInfo).Select(property => property.Object).First());
        }