public void CanGetChangedPropertyNames() { dynamic delta = new Delta <AddressEntity>(); IDelta idelta = delta as IDelta; // modify in the way we expect the formatter too. idelta.TrySetPropertyValue("City", "Sammamish"); Assert.Single(idelta.GetChangedPropertyNames()); Assert.Equal("City", idelta.GetChangedPropertyNames().Single()); Assert.Equal(4, idelta.GetUnchangedPropertyNames().Count()); // read the property back object city; Assert.True(idelta.TryGetPropertyValue("City", out city)); Assert.Equal("Sammamish", city); // modify the way people will through custom code delta.StreetAddress = "23213 NE 15th Ct"; string[] mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Equal(2, mods.Count()); Assert.Contains("StreetAddress", mods); Assert.Contains("City", mods); Assert.Equal("23213 NE 15th Ct", delta.StreetAddress); Assert.Equal(3, idelta.GetUnchangedPropertyNames().Count()); }
public void CannotGetChangedNestedDeltaPropertyNames() { dynamic deltaCustomer = new Delta <CustomerEntity>(); IDelta ideltaCustomer = deltaCustomer as IDelta; AddressEntity address = new AddressEntity(); // modify address.City = "Sammamish"; address.StreetAddress = "23213 NE 15th Ct"; // modify the nested property ideltaCustomer.TrySetPropertyValue("Address", address); Assert.Single(ideltaCustomer.GetChangedPropertyNames()); Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single()); Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count()); // read the not nested property back using legacy API Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out dynamic deltaAddressEntity)); Assert.IsAssignableFrom <AddressEntity>(deltaAddressEntity); AddressEntity addressEntity = deltaAddressEntity as AddressEntity; Assert.Equal("23213 NE 15th Ct", addressEntity.StreetAddress); Assert.Equal("Sammamish", addressEntity.City); // read the not nested property back using nested API Assert.False(deltaCustomer.TryGetNestedPropertyValue("Address", out dynamic deltaNestedAddress)); }
public void CanGetChangedNestedPropertyNames() { dynamic deltaCustomer = new Delta <CustomerEntity>(); IDelta ideltaCustomer = deltaCustomer as IDelta; AddressEntity Address = new AddressEntity(); Address.ID = 42; Address.StreetAddress = "23213 NE 15th Ct"; Address.City = "Sammamish"; Address.State = "WA"; Address.ZipCode = 98074; // modify in the way we expect the formatter too. ideltaCustomer.TrySetPropertyValue("Address", Address); Assert.Single(ideltaCustomer.GetChangedPropertyNames()); Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single()); Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count()); // read the property back Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out object address)); Assert.Equal(Address, address); // read the instance CustomerEntity instance = deltaCustomer.GetInstance(); Assert.Equal(Address, instance.Address); }
public void CanClear() { dynamic delta = new Delta <AddressEntity>(); delta.StreetAddress = "Test"; IDelta idelta = delta as IDelta; Assert.Single(idelta.GetChangedPropertyNames()); idelta.Clear(); Assert.Empty(idelta.GetChangedPropertyNames()); }
public void Patch_DoesNotSet_ChangedUpdatableProperties() { AddressEntity original = new AddressEntity { ID = 1, City = "Redmond", State = "WA", StreetAddress = "21110 NE 44th St", ZipCode = 98074 }; dynamic delta = new Delta <AddressEntity>(); delta.City = "Sammamish"; delta.StreetAddress = "23213 NE 15th Ct"; IDelta idelta = delta as IDelta; string[] mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Equal(2, mods.Count()); Assert.Contains("StreetAddress", mods); Assert.Contains("City", mods); delta.UpdatableProperties.Clear(); delta.UpdatableProperties.Add("City"); delta.Patch(original); // unchanged Assert.Equal(1, original.ID); Assert.Equal("WA", original.State); Assert.Equal("21110 NE 44th St", original.StreetAddress); Assert.Equal(98074, original.ZipCode); // changed Assert.Equal("Sammamish", original.City); Assert.Equal(delta.GetChangedPropertyNames(), new[] { "City" }); }
private IEnumerable <ODataProperty> CreateStructuralPropertyBag( IEnumerable <IEdmStructuralProperty> structuralProperties, EntityInstanceContext entityInstanceContext) { Contract.Assert(structuralProperties != null); Contract.Assert(entityInstanceContext != null); List <ODataProperty> properties = new List <ODataProperty>(); if (null != entityInstanceContext.EdmObject && entityInstanceContext.EdmObject.IsDeltaResource()) { IDelta deltaObject = entityInstanceContext.EdmObject as IDelta; Contract.Assert(deltaObject != null); IEnumerable <string> changedProperties = deltaObject.GetChangedPropertyNames(); structuralProperties = structuralProperties.Where(p => changedProperties.Contains(p.Name)); } foreach (IEdmStructuralProperty structuralProperty in structuralProperties) { ODataProperty property = CreateStructuralProperty(structuralProperty, entityInstanceContext); if (property != null) { properties.Add(property); } } return(properties); }
public void CannotSetNestedDeltaPropertyNameTwice() { dynamic deltaCustomer = new Delta <CustomerEntity>(); IDelta ideltaCustomer = deltaCustomer as IDelta; dynamic deltaAddress = new Delta <AddressEntity>(); IDelta ideltaAddress = deltaAddress as IDelta; // modify the nested property ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress); Assert.Single(ideltaCustomer.GetChangedPropertyNames()); Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single()); Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count()); // modify again Assert.False(ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress)); }
private ODataResourceValue CreateODataResourceValue(object graph, IEdmStructuredTypeReference expectedType, ODataSerializerContext writeContext) { List <ODataProperty> properties = new List <ODataProperty>(); ODataResourceValue resourceValue = new ODataResourceValue { TypeName = writeContext.GetEdmType(graph, graph.GetType()).FullName() }; IDelta delta = graph as IDelta; if (delta != null) { foreach (string propertyName in delta.GetChangedPropertyNames()) { SetDeltaPropertyValue(writeContext, properties, delta, propertyName); } foreach (string propertyName in delta.GetUnchangedPropertyNames()) { SetDeltaPropertyValue(writeContext, properties, delta, propertyName); } } else { HashSet <string> structuralPropertyNames = new HashSet <string>(); foreach (IEdmStructuralProperty structuralProperty in expectedType.DeclaredStructuralProperties()) { structuralPropertyNames.Add(structuralProperty.Name); } foreach (PropertyInfo property in graph.GetType().GetProperties()) { if (structuralPropertyNames.Contains(property.Name)) { object propertyValue = property.GetValue(graph); IEdmStructuredTypeReference expectedPropType = null; if (propertyValue == null) { expectedPropType = writeContext.GetEdmType(propertyValue, property.PropertyType) as IEdmStructuredTypeReference; } SetPropertyValue(writeContext, properties, expectedPropType, property.Name, propertyValue); } } } resourceValue.Properties = properties; return(resourceValue); }
public void CanGetChangedNestedDeltaPropertyNames() { dynamic deltaCustomer = new Delta <CustomerEntity>(); IDelta ideltaCustomer = deltaCustomer as IDelta; dynamic deltaAddress = new Delta <AddressEntity>(); IDelta ideltaAddress = deltaAddress as IDelta; // modify ideltaAddress.TrySetPropertyValue("City", "Sammamish"); ideltaAddress.TrySetPropertyValue("StreetAddress", "23213 NE 15th Ct"); Assert.Equal(3, ideltaAddress.GetUnchangedPropertyNames().Count()); string[] mods = ideltaAddress.GetChangedPropertyNames().ToArray(); Assert.Equal(2, mods.Length); Assert.Contains("StreetAddress", mods); Assert.Contains("City", mods); Assert.Equal("23213 NE 15th Ct", deltaAddress.StreetAddress); Assert.Equal("Sammamish", deltaAddress.City); // read the property back Assert.True(ideltaAddress.TryGetPropertyValue("City", out var city)); Assert.Equal("Sammamish", city); Assert.True(ideltaAddress.TryGetPropertyValue("StreetAddress", out var streetAddress)); Assert.Equal("23213 NE 15th Ct", streetAddress); // modify the nested property ideltaCustomer.TrySetPropertyValue("Address", ideltaAddress); Assert.Single(ideltaCustomer.GetChangedPropertyNames()); Assert.Equal("Address", ideltaCustomer.GetChangedPropertyNames().Single()); Assert.Equal(3, ideltaCustomer.GetUnchangedPropertyNames().Count()); // read the nested property back Assert.True(ideltaCustomer.TryGetPropertyValue("Address", out dynamic nestedAddress)); Assert.IsAssignableFrom <Delta <AddressEntity> >(nestedAddress); Assert.Equal("Sammamish", nestedAddress.City); Assert.Equal("23213 NE 15th Ct", nestedAddress.StreetAddress); }
public void CanGetChangedPropertyNamesButOnlyUpdatable() { dynamic delta = new Delta <AddressEntity>(); IDelta idelta = delta as IDelta; // modify in the way we expect the formatter too. idelta.TrySetPropertyValue("City", "Sammamish"); Assert.Single(idelta.GetChangedPropertyNames()); Assert.Equal("City", idelta.GetChangedPropertyNames().Single()); Assert.Equal(4, idelta.GetUnchangedPropertyNames().Count()); // read the property back object city; Assert.True(idelta.TryGetPropertyValue("City", out city)); Assert.Equal("Sammamish", city); // limit updatable properties delta.UpdatableProperties.Clear(); delta.UpdatableProperties.Add("City"); delta.UpdatableProperties.Add("StreetAddress"); // modify the way people will through custom code delta.StreetAddress = "23213 NE 15th Ct"; string[] mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Equal(2, mods.Count()); Assert.Contains("StreetAddress", mods); Assert.Contains("City", mods); Assert.Equal("23213 NE 15th Ct", delta.StreetAddress); Assert.Empty(idelta.GetUnchangedPropertyNames()); // try to modify an un-updatable property idelta.TrySetPropertyValue("State", "IA"); mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Equal(2, mods.Count()); Assert.Contains("StreetAddress", mods); Assert.Contains("City", mods); Assert.Null(delta.State); Assert.Empty(idelta.GetUnchangedPropertyNames()); // limit a property that has been updated delta.UpdatableProperties.Remove("StreetAddress"); mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Single(mods); Assert.Contains("City", mods); Assert.Null(delta.State); Assert.Empty(idelta.GetUnchangedPropertyNames()); // enable a property that has not been updated delta.UpdatableProperties.Add("State"); mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Single(mods); Assert.Contains("City", mods); Assert.Null(delta.State); Assert.Single(idelta.GetUnchangedPropertyNames()); Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single()); // enable a property that doesn't exist delta.UpdatableProperties.Add("Bogus"); mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Single(mods); Assert.Contains("City", mods); Assert.Null(delta.State); Assert.Single(idelta.GetUnchangedPropertyNames()); Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single()); // set a property that doesn't exist Assert.False(delta.TrySetPropertyValue("Bogus", "Bad")); mods = idelta.GetChangedPropertyNames().ToArray(); Assert.Single(mods); Assert.Contains("City", mods); Assert.Null(delta.State); Assert.Single(idelta.GetUnchangedPropertyNames()); Assert.Equal("State", idelta.GetUnchangedPropertyNames().Single()); }
/// <summary> /// Creates an <see cref="ODataComplexValue"/> for the object represented by <paramref name="graph"/>. /// </summary> /// <param name="graph">The value of the <see cref="ODataComplexValue"/> to be created.</param> /// <param name="complexType">The EDM complex type of the object.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataComplexValue"/>.</returns> public virtual ODataComplexValue CreateODataComplexValue(object graph, IEdmComplexTypeReference complexType, ODataSerializerContext writeContext) { if (writeContext == null) { throw Error.ArgumentNull("writeContext"); } if (graph == null || graph is NullEdmComplexObject) { return(null); } IEdmComplexObject complexObject = graph as IEdmComplexObject ?? new TypedEdmComplexObject(graph, complexType, writeContext.Model); List <IEdmProperty> settableProperties = new List <IEdmProperty>(complexType.ComplexDefinition().Properties()); if (complexObject.IsDeltaResource()) { IDelta deltaProperty = graph as IDelta; Contract.Assert(deltaProperty != null); IEnumerable <string> changedProperties = deltaProperty.GetChangedPropertyNames(); settableProperties = settableProperties.Where(p => changedProperties.Contains(p.Name)).ToList(); } List <ODataProperty> propertyCollection = new List <ODataProperty>(); foreach (IEdmProperty property in settableProperties) { IEdmTypeReference propertyType = property.Type; ODataEdmTypeSerializer propertySerializer = SerializerProvider.GetEdmTypeSerializer(propertyType); if (propertySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, propertyType.FullName(), typeof(ODataMediaTypeFormatter).Name); } object propertyValue; if (complexObject.TryGetPropertyValue(property.Name, out propertyValue)) { if (propertyValue != null && propertyType != null && propertyType.IsComplex()) { IEdmTypeReference actualType = writeContext.GetEdmType(propertyValue, propertyValue.GetType()); if (actualType != null && propertyType != actualType) { propertyType = actualType; } } var odataProperty = propertySerializer.CreateProperty(propertyValue, propertyType, property.Name, writeContext); if (odataProperty != null) { propertyCollection.Add(odataProperty); } } } // Try to add the dynamic properties if the complex type is open. if (complexType.ComplexDefinition().IsOpen) { List <ODataProperty> dynamicProperties = AppendDynamicProperties(complexObject, complexType, writeContext, propertyCollection, new string[0]); if (dynamicProperties != null) { propertyCollection.AddRange(dynamicProperties); } } string typeName = complexType.FullName(); ODataComplexValue value = new ODataComplexValue() { Properties = propertyCollection, TypeName = typeName }; AddTypeNameAnnotationAsNeeded(value, writeContext.MetadataLevel); return(value); }