public void GetEntityKeyValue_MultipleKeys_DerivedType() { // Arrange var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true }; EdmEntityType baseEntityType = new EdmEntityType("NS", "Name"); baseEntityType.AddKeys(baseEntityType.AddStructuralProperty("Key1", EdmPrimitiveTypeKind.String)); baseEntityType.AddKeys(baseEntityType.AddStructuralProperty("Key2", EdmPrimitiveTypeKind.Int32)); baseEntityType.AddKeys(baseEntityType.AddStructuralProperty("Key3", EdmPrimitiveTypeKind.Boolean)); EdmEntityType derivedEntityType = new EdmEntityType("NS", "Derived", baseEntityType); EntityInstanceContext entityInstanceContext = new EntityInstanceContext(_writeContext, derivedEntityType.AsReference(), entityInstance); // Act var keyValue = ConventionsHelpers.GetEntityKeyValue(entityInstanceContext); // Assert Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue); }
public void GetEntityKeyValue_ThrowsForNullKeys() { // Arrange IStructuralTypeConfiguration structuralType = new Mock <IStructuralTypeConfiguration>().Object; var entityInstance = new { Key = (string)null }; PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) }; Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>(); entityType.Setup(e => e.Keys).Returns(keys); entityType.Setup(e => e.FullName).Returns("FullName"); // Act & Assert Assert.Throws <InvalidOperationException>( () => ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object), "Key property 'Key' of type 'FullName' is null. Key properties cannot have null values."); }
internal static string GenerateSelfLink(EntitySetConfiguration configuration, EntityInstanceContext entityContext, bool includeCast) { List <ODataPathSegment> idLinkPathSegments = new List <ODataPathSegment>(); idLinkPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet)); idLinkPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType))); if (includeCast) { idLinkPathSegments.Add(new CastPathSegment(entityContext.EntityType)); } string idLink = entityContext.Url.ODataLink(idLinkPathSegments); if (idLink == null) { return(null); } return(idLink); }
internal static Uri GenerateSelfLink(EntitySetConfiguration configuration, EntityInstanceContext entityContext, bool includeCast) { List <ODataPathSegment> editLinkPathSegments = new List <ODataPathSegment>(); editLinkPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet)); editLinkPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType))); if (includeCast) { editLinkPathSegments.Add(new CastPathSegment(entityContext.EntityType)); } string editLink = entityContext.UrlHelper.ODataLink(entityContext.PathHandler, editLinkPathSegments); if (editLink == null) { return(null); } return(new Uri(editLink)); }
public void GetEntityKeyValue_SingleKey() { // Arrange var entityInstance = new { Key = "key" }; PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key")) }; Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>(); entityType .Setup(e => e.Keys) .Returns(keys); // Act var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object); // Assert Assert.Equal("'key'", keyValue); }
public void GetEntityKeyValue_SingleKey_DifferentDataTypes(object value, object expectedValue) { // Arrange IStructuralTypeConfiguration structuralType = new Mock <IStructuralTypeConfiguration>().Object; var entityInstance = new { Key = value }; PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) }; Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>(); entityType .Setup(e => e.Keys) .Returns(keys); // Act var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object); // Assert Assert.Equal(expectedValue, keyValue); }
public void GetEntityKeyValue_DerivedType() { // Arrange var entityInstance = new { Key = "key" }; Mock <EntityTypeConfiguration> baseEntityType = new Mock <EntityTypeConfiguration>(); PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), baseEntityType.Object) }; baseEntityType.Setup(e => e.Keys).Returns(keys); Mock <EntityTypeConfiguration> derivedEntityType = new Mock <EntityTypeConfiguration>(); derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object); // Act var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, derivedEntityType.Object); // Assert Assert.Equal("'key'", keyValue); }
internal static Uri GenerateNavigationPropertyLink(EntityInstanceContext entityContext, IEdmNavigationProperty navigationProperty, EntitySetConfiguration configuration, bool includeCast) { List <ODataPathSegment> navigationPathSegments = new List <ODataPathSegment>(); navigationPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet)); navigationPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType))); if (includeCast) { navigationPathSegments.Add(new CastPathSegment(entityContext.EntityType)); } navigationPathSegments.Add(new NavigationPathSegment(navigationProperty)); string link = entityContext.Url.ODataLink(navigationPathSegments); if (link == null) { return(null); } return(new Uri(link)); }
internal static Uri GenerateActionLink(EntityInstanceContext entityContext, ActionConfiguration action) { // the entity type the action is bound to. IEntityTypeConfiguration actionEntityType = action.BindingParameter.TypeConfiguration as IEntityTypeConfiguration; Contract.Assert(actionEntityType != null, "we have already verified that binding paramter type is entity"); Dictionary <string, object> routeValues = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); routeValues.Add(LinkGenerationConstants.Controller, entityContext.EntitySet.Name); routeValues.Add(LinkGenerationConstants.BoundId, ConventionsHelpers.GetEntityKeyValue(entityContext, actionEntityType)); routeValues.Add(LinkGenerationConstants.ODataAction, action.Name); string routeName; // generate link without cast if the entityset type matches the entity type the action is bound to. if (entityContext.EntitySet.ElementType.IsOrInheritsFrom(entityContext.EdmModel.FindDeclaredType(actionEntityType.FullName))) { routeName = ODataRouteNames.InvokeBoundAction; } else { routeName = ODataRouteNames.InvokeBoundActionWithCast; routeValues.Add(LinkGenerationConstants.Entitytype, entityContext.EntityType.FullName()); } string actionLink = entityContext.UrlHelper.Link(routeName, routeValues); if (actionLink == null) { return(null); } else { return(new Uri(actionLink)); } }
public void GetUriRepresentationForValue_Works(object value, string result) { Assert.Equal( result, ConventionsHelpers.GetUriRepresentationForValue(value)); }
public void GetKeyProperty_InValidEntityType(Type type) { var key = ConventionsHelpers.GetKeyProperty(type, throwOnError: false); Assert.Null(key); }