public static KeyExpression ConvertToKeyExpression(ResourceInstanceKey key, Workspace workspace) { ResourceContainer container = workspace.ServiceContainer.ResourceContainers[key.ResourceSetName]; if (container == null) { return(null); } ResourceType type = container.ResourceTypes.FirstOrDefault(rt => rt.Name == key.ResourceTypeName); if (type == null) { return(null); } return(key.CreateKeyExpression(container, type)); }
internal static ResourceInstanceKey CreateUniqueKey(ResourceContainer container, ResourceType resType, KeyExpressions relatedForeignKeys, KeyExpressions existingKeys) { KeyExpressions possibleRelatedForeignKeys = new KeyExpressions(); Workspace workspace = container.Workspace; int keysGenerated = 0; bool keyTrying = true; ResourceInstanceKey resourceInstanceKey = null; do { possibleRelatedForeignKeys = new KeyExpressions(); resourceInstanceKey = TryCreateUniqueResourceInstanceKey(container, resType, possibleRelatedForeignKeys); KeyExpression keyExpression = resourceInstanceKey.CreateKeyExpression(container, resType); // need to make sure its not a duplicate // if (existingKeys == null) { KeyedResourceInstance o = workspace.GetSingleResourceByKey(keyExpression); if (o == null) { keyTrying = false; } } else { keyTrying = existingKeys.Contains(keyExpression); } keysGenerated++; if (keysGenerated > 25) { throw new Microsoft.Test.ModuleCore.TestFailedException("Unable to create a unique key"); } }while (keyTrying); relatedForeignKeys.Add(possibleRelatedForeignKeys); return(resourceInstanceKey); }
internal KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys, bool populateNavProps) { KeyExpressions relatedForeignKeys = new KeyExpressions(); ResourceInstanceKey key = ResourceInstanceUtil.CreateUniqueKey(container, this, relatedForeignKeys, existingKeys); List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); // populate the non-key, non-navigation properties // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { if (p.Facets.IsIdentity) { continue; } ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } if (populateNavProps) { // populate the navigation properties, but don't go by key, as some foreign keys MAY NOT HAVE ASSOCIATED NAVIGATION PROPERTIES // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation)) { // find a key for this navigation property KeyExpression navPropKey = null; foreach (KeyExpression keyExp in relatedForeignKeys) { //if (p.Type.Equals(keyExp.ResourceType) // || p.Type is ResourceType && (p.Type as ResourceType).BaseTypes.Contains(keyExp.ResourceType)) if (p.OtherAssociationEnd.ResourceType.Equals(keyExp.ResourceType)) { navPropKey = keyExp; break; } } ResourceContainer associatedContainer = container.FindDefaultRelatedContainer(p); if (navPropKey == null) { if (p.OtherAssociationEnd.ResourceType.Key.Properties.OfType <ResourceProperty>() .Where(rp => rp.ForeignKeys.Any()) .Any(rp => rp.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType.Equals(this))) { // this association has a fk back to the current type, so it cannot be based on any existing entity AstoriaTestLog.WriteLineIgnore("Skipping nav prop '{0}.{1}' due to foreign key constraint on entity being created", this.Name, p.Name); continue; } else { navPropKey = associatedContainer.Workspace.GetRandomExistingKey(associatedContainer, p.OtherAssociationEnd.ResourceType); } } if (navPropKey != null) { if (p.OtherAssociationEnd.Multiplicity == Multiplicity.Many) { properties.Add(ResourceInstanceUtil.CreateCollectionInstanceProperty(new KeyExpressions(navPropKey), navPropKey.ResourceContainer, p)); } else { properties.Add(ResourceInstanceUtil.CreateRefInstanceProperty(navPropKey, navPropKey.ResourceContainer, p)); } } } } return(ResourceInstanceUtil.CreateKeyedResourceInstance(key.CreateKeyExpression(container, this), container, properties.ToArray())); }