/// <summary> /// Generates an open entity set containing entities with different amounts of primitive open properties. /// </summary> /// <param name="model">The entity model. The method will modify the model and call Fixup().</param> /// <param name="withTypeNames">True if the payloads should specify type names.</param> /// <param name="primitiveValueFilter">A function for excluding primitives which are unsupported for open properties.</param> /// <returns>The open entity set containing entities with primitive open properties.</returns> public static ODataPayloadElement CreateOpenEntitySetInstance(EdmModel model, bool withTypeNames, Func <PrimitiveValue, bool> primitiveValueFilter) { ExceptionUtilities.CheckArgumentNotNull(model, "model"); ExceptionUtilities.CheckArgumentNotNull(primitiveValueFilter, "primitiveValueFilter"); var edmEntityType = new EdmEntityType("TestModel", "OpenEntityType", baseType: null, isAbstract: false, isOpen: true); model.AddElement(edmEntityType); edmEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String, true); var container = new EdmEntityContainer("TestModel", "DefaultContainer"); container.AddEntitySet("OpenEntityTypes", edmEntityType); model.AddElement(container); var feed = PayloadBuilder.EntitySet().WithTypeAnnotation(edmEntityType); string entityTypeName = withTypeNames ? edmEntityType.FullName() : null; var primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(true).Where(p => primitiveValueFilter(p)); int idx = 0; for (int i = 0; i <= primitiveValues.Count(); ++i) { var entityInstance = PayloadBuilder.Entity(entityTypeName).PrimitiveProperty("Name", Guid.NewGuid().ToString()); foreach (var property in primitiveValues.Take(i)) { entityInstance.PrimitiveProperty("Property" + (idx++), property.ClrValue); } feed.Add(entityInstance); } return(feed); }
private static PrimitiveCollection GetPrimitiveCollection(IRandomNumberGenerator random) { TestValues.CreatePrimitiveValuesWithMetadata(true); PrimitiveValue val = random.ChooseFrom(TestValues.CreatePrimitiveValuesWithMetadata(true)); int numItems = random.NextFromRange(1, 3); var newItems = Enumerable.Range(0, numItems).Select(x => TestValues.GetDifferentPrimitiveValue(val)); var payload = new PrimitiveCollection(newItems.ToArray()); return(payload); }
/// <summary> /// Creates a set of interesting primitive value test descriptors along with metadata. /// </summary> /// <param name="settings">The settings for payload reader test descriptor to use.</param> /// <param name="fullSet">true if all available primitive values should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting test descriptors.</returns> internal static IEnumerable <PayloadReaderTestDescriptor> CreatePrimitiveValueTestDescriptors( PayloadReaderTestDescriptor.Settings settings, bool fullSet = true) { EdmModel model = new EdmModel().Fixup(); IEnumerable <PrimitiveValue> primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet); return(primitiveValues.Select(c => new PayloadReaderTestDescriptor(settings) { PayloadElement = c, PayloadEdmModel = model })); }
private static PrimitiveValue GetPrimitiveValue(IRandomNumberGenerator random, EdmModel model = null) { var payload = random.ChooseFrom(TestValues.CreatePrimitiveValuesWithMetadata(true)); if (model != null) { EdmEntityType entity = model.FindDeclaredType("AllPrimitiveTypesEntity") as EdmEntityType; if (entity == null) { entity = new EdmEntityType("TestModel", "AllPrimitiveTypesEntity"); entity.AddStructuralProperty("StringPropNullable", EdmPrimitiveTypeKind.String, true); entity.AddStructuralProperty("StringProp", EdmPrimitiveTypeKind.String, false); entity.AddStructuralProperty("Int32PropNullable", EdmPrimitiveTypeKind.Int32, true); entity.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32, false); entity.AddStructuralProperty("BooleanPropNullable", EdmPrimitiveTypeKind.Boolean, true); entity.AddStructuralProperty("BooleanProp", EdmPrimitiveTypeKind.Boolean, false); entity.AddStructuralProperty("BytePropNullable", EdmPrimitiveTypeKind.Byte, true); entity.AddStructuralProperty("ByteProp", EdmPrimitiveTypeKind.Byte, false); entity.AddStructuralProperty("SBytePropNullable", EdmPrimitiveTypeKind.SByte, true); entity.AddStructuralProperty("SByteProp", EdmPrimitiveTypeKind.SByte, false); entity.AddStructuralProperty("Int16PropNullable", EdmPrimitiveTypeKind.Int16, true); entity.AddStructuralProperty("Int16Prop", EdmPrimitiveTypeKind.Int16, false); entity.AddStructuralProperty("DecimalPropNullable", EdmPrimitiveTypeKind.Decimal, true); entity.AddStructuralProperty("DecimalProp", EdmPrimitiveTypeKind.Decimal, false); entity.AddStructuralProperty("SinglePropNullable", EdmPrimitiveTypeKind.Single, true); entity.AddStructuralProperty("SingleProp", EdmPrimitiveTypeKind.Single, false); entity.AddStructuralProperty("Int64PropNullable", EdmPrimitiveTypeKind.Int64, true); entity.AddStructuralProperty("Int64Prop", EdmPrimitiveTypeKind.Int64, false); entity.AddStructuralProperty("DoublePropNullable", EdmPrimitiveTypeKind.Double, true); entity.AddStructuralProperty("DoubleProp", EdmPrimitiveTypeKind.Double, false); entity.AddStructuralProperty("BinaryPropNullable", EdmPrimitiveTypeKind.Binary, true); entity.AddStructuralProperty("BinaryProp", EdmPrimitiveTypeKind.Binary, false); entity.AddStructuralProperty("GuidPropNullable", EdmPrimitiveTypeKind.Guid, true); entity.AddStructuralProperty("GuidProp", EdmPrimitiveTypeKind.Guid, false); model.AddElement(entity); } payload.WithTypeAnnotation(entity); } return(payload); }
/// <summary> /// Creates a set of interesting parameter values along with metadata /// </summary> /// <param name="model">The method creates complex and entity types for the complex and entity values and adds them to the model. The model cannot be null.</param> /// <param name="fullSet">true if all available parameter values should be returned, false if only the most interesting subset should be returned.</param> /// <returns>List of interesting parameter values.</returns> public static IEnumerable <ComplexInstance> CreateParameterValues(EdmModel model, bool fullSet = true) { ExceptionUtilities.CheckArgumentNotNull(model, "model"); List <ODataPayloadElement> payloadValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet).OfType <ODataPayloadElement>() .Concat(TestValues.CreateComplexValues(model, true /*withTypeNames*/, fullSet).OfType <ODataPayloadElement>()) .Concat(TestValues.CreatePrimitiveCollections(true /*withTypeNames*/, fullSet).OfType <ODataPayloadElement>()) .Concat(TestValues.CreateComplexCollections(model, true /*withTypeNames*/, fullSet).OfType <ODataPayloadElement>()) // // TODO: ODataLib test item: Add new ODataPayloadElement for parameters payload // We currently use ComplexInstance as a temporary solution until we have the new type in place. The new type need to support Entity and Feed. //.Concat(TestEntityInstances.CreateEntityInstanceTestDescriptors(model, true /*withTypeNames*/).Select(d => d.PayloadElement).OfType<ODataPayloadElement>()) //.Concat(TestFeeds.CreateEntitySetTestDescriptors(model, true /*withTypeNames*/).Select(d => d.PayloadElement).OfType<ODataPayloadElement>()) .ToList(); int functionImportCount = 0; IEnumerable <ComplexInstance> parameterPayloads = payloadValues.Combinations(1).Select(payloadElements => PayloadElementsToParameterPayload(payloadElements, model, "FunctionImport" + ++functionImportCount)) .Concat(payloadValues.Subsets(2, 5).Select(payloadElements => PayloadElementsToParameterPayload(payloadElements, model, "FunctionImport" + ++functionImportCount))); return(parameterPayloads); }
public static ComplexInstance GetComplexInstanceWithManyPrimitiveProperties(EdmModel model) { var primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(true).Where( (v) => { var edmPrimitiveType = v.Annotations.OfType <EntityModelTypeAnnotation>().Single().EdmModelType as IEdmPrimitiveTypeReference; var edmPrimitiveKind = edmPrimitiveType.PrimitiveKind(); if (edmPrimitiveKind != EdmPrimitiveTypeKind.None) { return(edmPrimitiveKind != EdmPrimitiveTypeKind.DateTimeOffset); } return(false); }).ToArray(); string typeName = "ComplexTypeWithManyPrimitiveProperties"; var complexType = model.ComplexType(typeName); for (int i = 0; i < primitiveValues.Count(); ++i) { complexType.Property("property" + i, primitiveValues[i].GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); } var complexValue = PayloadBuilder.ComplexValue("TestModel." + typeName).WithTypeAnnotation(complexType); for (int j = 0; j < primitiveValues.Count(); ++j) { complexValue.PrimitiveProperty("property" + j, primitiveValues[j].ClrValue); } return(complexValue); }
/// <summary> /// Creates a set of interesting entity instances along with metadata. /// </summary> /// <param name="settings">The test descriptor settings to use.</param> /// <param name="model">If non-null, the method creates complex types for the complex values and adds them to the model.</param> /// <param name="withTypeNames">true if the payloads should specify type names.</param> /// <returns>List of test descriptors with interesting entity instances as payload.</returns> public static IEnumerable <PayloadTestDescriptor> CreateEntityInstanceTestDescriptors( EdmModel model, bool withTypeNames) { IEnumerable <PrimitiveValue> primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet: false); IEnumerable <ComplexInstance> complexValues = TestValues.CreateComplexValues(model, withTypeNames, fullSet: false); IEnumerable <NamedStreamInstance> streamReferenceValues = TestValues.CreateStreamReferenceValues(fullSet: false); IEnumerable <PrimitiveMultiValue> primitiveMultiValues = TestValues.CreatePrimitiveCollections(withTypeNames, fullSet: false); IEnumerable <ComplexMultiValue> complexMultiValues = TestValues.CreateComplexCollections(model, withTypeNames, fullSet: false); IEnumerable <NavigationPropertyInstance> navigationProperties = TestValues.CreateDeferredNavigationLinks(); // NOTE we have to copy the EntityModelTypeAnnotation on the primitive value to the NullPropertyInstance for null values since the // NullPropertyInstance does not expose a value. We will later copy it back to the value we generate for the null property. IEnumerable <PropertyInstance> primitiveProperties = primitiveValues.Select((pv, ix) => PayloadBuilder.Property("PrimitiveProperty" + ix, pv).CopyAnnotation <PropertyInstance, EntityModelTypeAnnotation>(pv)); IEnumerable <PropertyInstance> complexProperties = complexValues.Select((cv, ix) => PayloadBuilder.Property("ComplexProperty" + ix, cv)); IEnumerable <PropertyInstance> primitiveMultiValueProperties = primitiveMultiValues.Select((pmv, ix) => PayloadBuilder.Property("PrimitiveMultiValueProperty" + ix, pmv)); IEnumerable <PropertyInstance> complexMultiValueProperties = complexMultiValues.Select((cmv, ix) => PayloadBuilder.Property("ComplexMultiValueProperty" + ix, cmv)); PropertyInstance[][] propertyMatrix = new PropertyInstance[6][]; propertyMatrix[0] = primitiveProperties.ToArray(); propertyMatrix[1] = complexProperties.ToArray(); propertyMatrix[2] = streamReferenceValues.ToArray(); propertyMatrix[3] = primitiveMultiValueProperties.ToArray(); propertyMatrix[4] = complexMultiValueProperties.ToArray(); propertyMatrix[5] = navigationProperties.ToArray(); IEnumerable <PropertyInstance[]> propertyCombinations = propertyMatrix.ColumnCombinations(0, 1, 6); int count = 0; foreach (PropertyInstance[] propertyCombination in propertyCombinations) { // build the entity type, add it to the model EdmEntityType generatedEntityType = null; string typeName = "PGEntityType" + count; EdmEntityContainer container = null; EdmEntitySet entitySet = null; if (model != null) { // generate a new type with the auto-generated name, check that no type with this name exists and add the default key property to it. Debug.Assert(model.FindDeclaredType(typeName) == null, "Entity type '" + typeName + "' already exists."); generatedEntityType = new EdmEntityType("TestModel", typeName); generatedEntityType.AddKeys(generatedEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); model.AddElement(generatedEntityType); container = model.EntityContainer as EdmEntityContainer; if (container == null) { container = new EdmEntityContainer("TestModel", "DefaultNamespace"); model.AddElement(container); } entitySet = container.AddEntitySet(typeName, generatedEntityType); } EntityInstance entityInstance = PayloadBuilder.Entity("TestModel." + typeName) .Property("Id", PayloadBuilder.PrimitiveValue(count).WithTypeAnnotation(EdmCoreModel.Instance.GetInt32(false))); for (int i = 0; i < propertyCombination.Length; ++i) { PropertyInstance currentProperty = propertyCombination[i]; entityInstance.Add(currentProperty); if (model != null) { if (entitySet == null) { entitySet = container.FindEntitySet(typeName) as EdmEntitySet; } switch (currentProperty.ElementType) { case ODataPayloadElementType.ComplexProperty: ComplexProperty complexProperty = (ComplexProperty)currentProperty; generatedEntityType.AddStructuralProperty(complexProperty.Name, complexProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; case ODataPayloadElementType.PrimitiveProperty: PrimitiveProperty primitiveProperty = (PrimitiveProperty)currentProperty; if (primitiveProperty.Value == null) { generatedEntityType.AddStructuralProperty( primitiveProperty.Name, PayloadBuilder.PrimitiveValueType(null)); } else { generatedEntityType.AddStructuralProperty(primitiveProperty.Name, primitiveProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); } break; case ODataPayloadElementType.NamedStreamInstance: NamedStreamInstance streamProperty = (NamedStreamInstance)currentProperty; generatedEntityType.AddStructuralProperty(streamProperty.Name, EdmPrimitiveTypeKind.Stream); break; case ODataPayloadElementType.EmptyCollectionProperty: throw new NotImplementedException(); case ODataPayloadElementType.NavigationPropertyInstance: NavigationPropertyInstance navigationProperty = (NavigationPropertyInstance)currentProperty; var navProperty = generatedEntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { ContainsTarget = false, Name = navigationProperty.Name, Target = generatedEntityType, TargetMultiplicity = EdmMultiplicity.One }); entitySet.AddNavigationTarget(navProperty, entitySet); break; case ODataPayloadElementType.ComplexMultiValueProperty: ComplexMultiValueProperty complexMultiValueProperty = (ComplexMultiValueProperty)currentProperty; generatedEntityType.AddStructuralProperty(complexMultiValueProperty.Name, complexMultiValueProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; case ODataPayloadElementType.PrimitiveMultiValueProperty: PrimitiveMultiValueProperty primitiveMultiValueProperty = (PrimitiveMultiValueProperty)currentProperty; generatedEntityType.AddStructuralProperty(primitiveMultiValueProperty.Name, primitiveMultiValueProperty.Value.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType); break; default: throw new NotSupportedException("Unsupported element type found : " + propertyCombination[i].ElementType); } } } if (generatedEntityType != null) { entityInstance.AddAnnotation(new EntityModelTypeAnnotation(generatedEntityType.ToTypeReference(true))); } yield return(new PayloadTestDescriptor() { PayloadElement = entityInstance, PayloadEdmModel = model }); count++; } }