public void CreateComplexWithInvalidPropertiesThrows(List <Tuple <Type, string> > properties) { Exception exception = null; "When create a dynamic type".When( () => { exception = Record.Exception(() => AggregationTypesGenerator.CreateType(properties, Context, false)); }); "Then Invalid Operation Exception is thrown".Then(() => exception.Should().BeOfType <InvalidOperationException>()); }
/// <summary> /// Helper method to create a new dynamic type for the group-by key. /// </summary> /// <param name="transformation">The group-by query.</param> /// <returns>The type of the key which was dynamically generated.</returns> internal Type GetGroupByKeyType(ApplyGroupbyClause transformation) { Contract.Assert(transformation != null); Contract.Assert(transformation.SelectedStatements != null); Contract.Assert(transformation.SelectedStatements.Any()); var keyProperties = new List <Tuple <Type, string> >(); var selectedStatementsDictionary = GetSelectedStatementsDictionary(transformation.SelectedStatements); foreach (var statement in selectedStatementsDictionary) { // simple property var statementString = statement.Value.First(); if ((statement.Value.Count() == 1) && (statementString == statement.Key)) { string samplingMethod, alias, samplingProperty; GroupByImplementation.GetSamplingMethod(statementString, out samplingMethod, out alias, out samplingProperty); if (samplingMethod != null) { var pi = this.Context.ElementClrType.GetProperty(samplingProperty); if (pi == null) { throw new ArgumentException(string.Format("Entity does not contain {0}", samplingProperty)); } var implementation = SamplingMethodsImplementations.GetAggregationImplementation(samplingMethod); var samplingType = implementation.GetResultType(pi.PropertyType); keyProperties.Add(new Tuple <Type, string>(samplingType, alias)); } else { var propName = statementString.TrimMethodCall().Split(' ').First(); var pi = this.Context.ElementClrType.GetProperty(propName); if (pi == null) { throw new ArgumentException(string.Format("Entity does not contain {0}", propName)); } keyProperties.Add(new Tuple <Type, string>(pi.PropertyType, pi.Name)); } } else { // complex property var propName = statement.Key.TrimMethodCall(); var pi = this.Context.ElementClrType.GetProperty(propName); if (pi == null) { throw new ArgumentException(string.Format("Entity does not contain {0}", propName)); } var newPropertyType = this.GenerateComplexType(pi.PropertyType, statement.Value); keyProperties.Add(new Tuple <Type, string>(newPropertyType, propName)); } } return(AggregationTypesGenerator.CreateType(keyProperties.Distinct(new TypeStringTupleComapere()).ToList(), Context, true)); }
public void CreateComplexTypeTest(List <Tuple <Type, string> > properties) { Type newType = null; "When create a dynamic type".When( () => { newType = AggregationTypesGenerator.CreateType(properties, Context, false); }); "Then the new type should exist".Then(() => newType.Should().NotBeNull()); "Then the new type namespace should be ODataAggregation.DynamicTypes".Then(() => newType.Namespace.Should().BeEquivalentTo("ODataAggregation.DynamicTypes")); "Then the new type should have 3 properties".Then(() => newType.GetProperties().Count().Should().Be(12)); "Then the new type should be added as en complex type to the model".Then(() => (Context.Model.FindDeclaredType(newType.FullName) as EdmComplexType).Should().NotBeNull()); }
/// <summary> /// Continue the recursive operation of creating the Group-By key. /// </summary> /// <param name="declaringType">The type based on which we are going to create the new type.</param> /// <param name="segments">The select segments that declare what to create.</param> /// <returns>A new type.</returns> private Type GenerateComplexType(Type declaringType, IEnumerable <string> segments) { Contract.Assert(declaringType != null); Contract.Assert(segments != null); var keyProperties = new List <Tuple <Type, string> >(); var selectedStatementsDictionary = GetSelectedStatementsDictionary(segments); foreach (var statement in selectedStatementsDictionary) { // simple property var statementString = statement.Value.First(); if ((statement.Value.Count() == 1) && (statementString == statement.Key)) { string samplingMethod, alias, samplingProperty; GroupByImplementation.GetSamplingMethod(statementString.TrimMethodCallSufix(), out samplingMethod, out alias, out samplingProperty); if (samplingMethod != null) { var pi = declaringType.GetProperty(samplingProperty); var implementation = SamplingMethodsImplementations.GetAggregationImplementation(samplingMethod); var samplingType = implementation.GetResultType(pi.PropertyType); keyProperties.Add(new Tuple <Type, string>(samplingType, alias)); } else { statementString = statementString.Split(' ').First().TrimMethodCallSufix(); var pi = declaringType.GetProperty(statementString); keyProperties.Add(new Tuple <Type, string>(pi.PropertyType, pi.Name)); } } else { // complex property var key = statement.Key.Split(' ').First().TrimMethodCallSufix(); var pi = declaringType.GetProperty(key); var newPropertyType = GenerateComplexType(pi.PropertyType, statement.Value); keyProperties.Add(new Tuple <Type, string>(newPropertyType, key)); } } return(AggregationTypesGenerator.CreateType(keyProperties.Distinct(new TypeStringTupleComapere()).ToList(), Context, false)); }