/// <summary> /// Gets the expected query value for the action request /// </summary> /// <param name="initialExpectedResults">Initial expected values for an action</param> /// <param name="parameterValues">Parameter values for the action</param> /// <returns>A query Value that is the expected value</returns> public QueryValue GetExpectedQueryValue(QueryValue initialExpectedResults, params QueryValue[] parameterValues) { ExceptionUtilities.CheckArgumentNotNull(initialExpectedResults, "initialExpectedResults"); QueryStructuralValue initialStructuralValue = initialExpectedResults as QueryStructuralValue; ExceptionUtilities.CheckArgumentNotNull(initialStructuralValue, "initialStructuralValue"); QueryScalarValue initialScalarValue = initialStructuralValue.GetScalarValue(this.IntegerProperty); ExceptionUtilities.CheckArgumentNotNull(initialScalarValue, "initialScalarValue"); int intPropertyValue = (int)initialScalarValue.Value; ExceptionUtilities.CheckArgumentNotNull(intPropertyValue, "intPropertyValue"); if (intPropertyValue != int.MaxValue) { initialStructuralValue.SetPrimitiveValue(this.IntegerProperty, intPropertyValue + 1); } else { initialStructuralValue.SetPrimitiveValue(this.IntegerProperty, 0); } return(initialStructuralValue); }
internal void UpdateValues(QueryStructuralValue instance, IEnumerable <NamedValue> namedValues, string propertyPath) { foreach (QueryProperty property in instance.Type.Properties) { string childPropertyPath = property.Name; if (propertyPath != null) { childPropertyPath = propertyPath + "." + property.Name; } // Skip if its an EntityType, this only handles structural types var queryEntityType = property.PropertyType as QueryEntityType; if (queryEntityType != null) { continue; } var collectionType = property.PropertyType as QueryCollectionType; var scalarType = property.PropertyType as QueryScalarType; var complexDataType = property.PropertyType as QueryComplexType; QueryEntityType collectionQueryElementType = null; if (collectionType != null) { collectionQueryElementType = collectionType.ElementType as QueryEntityType; } // Skip if its a collection of QueryEntityType if (collectionQueryElementType != null) { continue; } if (scalarType != null) { NamedValue primitivePropertyNamedValue = namedValues.SingleOrDefault(nv => nv.Name == childPropertyPath); if (primitivePropertyNamedValue != null) { instance.SetPrimitiveValue(property.Name, primitivePropertyNamedValue.Value); this.unusedNamedValuePaths.Remove(childPropertyPath); } } else if (collectionType != null) { List <NamedValue> bagNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList(); if (bagNamedValues.Any()) { this.UpdateBagProperty(instance, property, collectionType.ElementType, childPropertyPath, bagNamedValues); } else { this.UpdateBagPropertyWithNullOrEmpty(instance, property, childPropertyPath, namedValues); } } else if (complexDataType != null) { // NOTE: we cannot assert that it is complex/primitive/bag, because there may be new query types added in other assemblies that we know nothing about here QueryStructuralValue complexTypeValue = instance.GetStructuralValue(property.Name); List <NamedValue> complexInstanceNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList(); if (complexInstanceNamedValues.Any()) { if (complexTypeValue.IsNull) { complexTypeValue = complexDataType.CreateNewInstance(); } this.UpdateValues(complexTypeValue, complexInstanceNamedValues, childPropertyPath); instance.SetValue(property.Name, complexTypeValue); } else { // Check for null case List <NamedValue> exactMatches = namedValues.Where(nv => nv.Name == childPropertyPath).ToList(); ExceptionUtilities.Assert(exactMatches.Count < 2, "Should only find at most one property path {0} when looking for null value", childPropertyPath); if (exactMatches.Count == 1) { ExceptionUtilities.Assert( exactMatches[0].Value == null, "Named value at path '{0}' was unexpectedly non-null. Value was '{1}'", childPropertyPath, exactMatches[0].Value); instance.SetValue(property.Name, complexDataType.NullValue); this.unusedNamedValuePaths.Remove(childPropertyPath); } } } } }
internal void UpdateValues(QueryStructuralValue instance, IEnumerable<NamedValue> namedValues, string propertyPath) { foreach (QueryProperty property in instance.Type.Properties) { string childPropertyPath = property.Name; if (propertyPath != null) { childPropertyPath = propertyPath + "." + property.Name; } // Skip if its an EntityType, this only handles structural types var queryEntityType = property.PropertyType as QueryEntityType; if (queryEntityType != null) { continue; } var collectionType = property.PropertyType as QueryCollectionType; var scalarType = property.PropertyType as QueryScalarType; var complexDataType = property.PropertyType as QueryComplexType; QueryEntityType collectionQueryElementType = null; if (collectionType != null) { collectionQueryElementType = collectionType.ElementType as QueryEntityType; } // Skip if its a collection of QueryEntityType if (collectionQueryElementType != null) { continue; } if (scalarType != null) { NamedValue primitivePropertyNamedValue = namedValues.SingleOrDefault(nv => nv.Name == childPropertyPath); if (primitivePropertyNamedValue != null) { instance.SetPrimitiveValue(property.Name, primitivePropertyNamedValue.Value); this.unusedNamedValuePaths.Remove(childPropertyPath); } } else if (collectionType != null) { List<NamedValue> bagNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList(); if (bagNamedValues.Any()) { this.UpdateBagProperty(instance, property, collectionType.ElementType, childPropertyPath, bagNamedValues); } else { this.UpdateBagPropertyWithNullOrEmpty(instance, property, childPropertyPath, namedValues); } } else if (complexDataType != null) { // NOTE: we cannot assert that it is complex/primitive/bag, because there may be new query types added in other assemblies that we know nothing about here QueryStructuralValue complexTypeValue = instance.GetStructuralValue(property.Name); List<NamedValue> complexInstanceNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList(); if (complexInstanceNamedValues.Any()) { if (complexTypeValue.IsNull) { complexTypeValue = complexDataType.CreateNewInstance(); } this.UpdateValues(complexTypeValue, complexInstanceNamedValues, childPropertyPath); instance.SetValue(property.Name, complexTypeValue); } else { // Check for null case List<NamedValue> exactMatches = namedValues.Where(nv => nv.Name == childPropertyPath).ToList(); ExceptionUtilities.Assert(exactMatches.Count < 2, "Should only find at most one property path {0} when looking for null value", childPropertyPath); if (exactMatches.Count == 1) { ExceptionUtilities.Assert( exactMatches[0].Value == null, "Named value at path '{0}' was unexpectedly non-null. Value was '{1}'", childPropertyPath, exactMatches[0].Value); instance.SetValue(property.Name, complexDataType.NullValue); this.unusedNamedValuePaths.Remove(childPropertyPath); } } } } }