public void ProjectAsWrapper_Element_ProjectedValueContainsInstance_IfSelectionIsAll(string select)
        {
            // Arrange
            Customer customer             = new Customer();
            ODataQueryOptionParser parser = new ODataQueryOptionParser(
                _model.Model,
                _model.Customer,
                _model.Customers,
                new Dictionary <string, string> {
                { "$select", select }, { "$expand", "Orders" }
            });
            SelectExpandClause selectExpand = parser.ParseSelectAndExpand();
            Expression         source       = Expression.Constant(customer);

            // Act
            Expression projection = _binder.ProjectAsWrapper(source, selectExpand, _model.Customer);

            // Assert
            Assert.Equal(ExpressionType.MemberInit, projection.NodeType);
            Assert.NotEmpty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "Instance"));
            SelectExpandWrapper <Customer> customerWrapper = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper <Customer>;

            Assert.Same(customer, customerWrapper.Instance);
        }
Ejemplo n.º 2
0
        public void ToDictionary_ContainsAllProperties_FromContainer()
        {
            // Arrange
            EdmModel      model      = new EdmModel();
            EdmEntityType entityType = new EdmEntityType("NS", "Name");

            model.AddElement(entityType);
            model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
            entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
            MockPropertyContainer container = new MockPropertyContainer();

            container.Properties.Add("Property", 42);
            SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity>
            {
                Container = container,
                ModelID   = ModelContainer.GetModelID(model)
            };

            // Act
            var result = wrapper.ToDictionary();

            // Assert
            Assert.Equal(42, result["Property"]);
        }
Ejemplo n.º 3
0
        public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull()
        {
            // Arrange
            EdmModel      model      = new EdmModel();
            EdmEntityType entityType = new EdmEntityType("NS", "Name");

            model.AddElement(entityType);
            model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
            entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
            IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
            SelectExpandWrapper <TestEntity> testWrapper = new SelectExpandWrapper <TestEntity>
            {
                Instance = new TestEntity {
                    SampleProperty = 42
                },
                ModelID = ModelContainer.GetModelID(model)
            };

            // Act
            var result = testWrapper.ToDictionary();

            // Assert
            Assert.Equal(42, result["SampleProperty"]);
        }
Ejemplo n.º 4
0
        public void TryGetValue_PropertyAliased_IfAnnotationSet()
        {
            // Arrange
            _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntityWithAlias)));
            _model.Model.SetAnnotationValue(
                _model.CustomerName,
                new ClrPropertyInfoAnnotation(typeof(TestEntityWithAlias).GetProperty("SampleProperty")));
            object expectedPropertyValue = new object();
            SelectExpandWrapper <TestEntityWithAlias> wrapper = new SelectExpandWrapper <TestEntityWithAlias> {
                ModelID = _modelID
            };

            wrapper.Instance = new TestEntityWithAlias {
                SampleProperty = expectedPropertyValue
            };

            // Act
            object value;
            bool   result = wrapper.TryGetPropertyValue("Name", out value);

            // Assert
            Assert.True(result);
            Assert.Same(expectedPropertyValue, value);
        }
Ejemplo n.º 5
0
        public void ProjectAsWrapper_NullExpandedProperty_HasNullValueInProjectedWrapper()
        {
            // Arrange
            IPropertyMapper mapper = new IdentityPropertyMapper();
            Order           order  = new Order();
            ExpandedNavigationSelectItem expandItem = new ExpandedNavigationSelectItem(
                new ODataExpandPath(new NavigationPropertySegment(_model.Order.NavigationProperties().Single(), navigationSource: _model.Customers)),
                _model.Customers,
                selectExpandOption: null);
            SelectExpandClause selectExpand = new SelectExpandClause(new SelectItem[] { expandItem }, allSelected: true);
            Expression         source       = Expression.Constant(order);

            _model.Model.SetAnnotationValue(_model.Order, new DynamicPropertyDictionaryAnnotation(typeof(Order).GetProperty("OrderProperties")));

            // Act
            Expression projection = _binder.ProjectAsWrapper(source, selectExpand, _model.Order, _model.Orders);

            // Assert
            SelectExpandWrapper <Order> projectedOrder = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper <Order>;

            Assert.NotNull(projectedOrder);
            Assert.Contains("Customer", projectedOrder.Container.ToDictionary(mapper).Keys);
            Assert.Null(projectedOrder.Container.ToDictionary(mapper)["Customer"]);
        }
Ejemplo n.º 6
0
        public void Property_Instance_RoundTrips()
        {
            SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity>();

            Assert.Reflection.Property(wrapper, w => w.Instance, expectedDefaultValue: null, allowNull: true, roundTripTestValue: new TestEntity());
        }