public async Task TestAttributesThatAreReplaced() { CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestAttributesThatAreReplaced"); corpus.Storage.Mount("cdm", new LocalAdapter(TestHelper.SchemaDocumentsPath)); CdmEntityDefinition extendedEntity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/extended.cdm.json/extended"); CdmEntityDefinition resExtendedEnt = await extendedEntity.CreateResolvedEntityAsync("resExtended"); // the attribute from the base class should be merged with the attribute // from the extended class into a single attribute Assert.AreEqual(1, resExtendedEnt.Attributes.Count); // check that traits from the base class merged with the traits from the extended class CdmAttributeItem attribute = resExtendedEnt.Attributes[0]; // base trait Assert.AreNotEqual(-1, attribute.AppliedTraits.IndexOf("means.identity.brand")); // extended trait Assert.AreNotEqual(-1, attribute.AppliedTraits.IndexOf("means.identity.company.name")); // make sure the attribute context and entity foreign key were maintained correctly CdmAttributeContext foreignKeyForBaseAttribute = ((resExtendedEnt.AttributeContext.Contents[1] as CdmAttributeContext).Contents[1] as CdmAttributeContext); Assert.AreEqual(foreignKeyForBaseAttribute.Name, "_generatedAttributeSet"); CdmAttributeReference fkReference = ((foreignKeyForBaseAttribute.Contents[0] as CdmAttributeContext).Contents[0] as CdmAttributeContext).Contents[0] as CdmAttributeReference; Assert.AreEqual("resExtended/hasAttributes/regardingObjectId", fkReference.NamedReference); }
/// <summary> /// Validates that the supporting attribute has the "is.addedInSupportOf" and "is.virtual.attribute" traits /// </summary> /// <param name="supportingAttribute"></param> /// <param name="fromAttribute"></param> private void ValidateInSupportOfAttribute(CdmAttributeItem supportingAttribute, string fromAttribute, bool checkVirtualTrait = true) { CdmTraitReference inSupportOfTrait = supportingAttribute.AppliedTraits.Item("is.addedInSupportOf"); Assert.IsNotNull(inSupportOfTrait); Assert.AreEqual(1, inSupportOfTrait.Arguments.Count); Assert.AreEqual(fromAttribute, inSupportOfTrait.Arguments[0].Value); if (checkVirtualTrait) { Assert.IsNotNull(supportingAttribute.AppliedTraits.Item("is.virtual.attribute"), "Missing is.virtual.attribute traits"); } }
internal CdmAttributeItem AddAttributeDef(CdmAttributeItem attributeDef) { this.Members.Add(attributeDef); return(attributeDef); }
public async Task TestUpdateRelationships() { var expectedRels = JToken.Parse(TestHelper.GetExpectedOutputFileContent(testsSubpath, "TestUpdateRelationships", "expectedRels.json")).ToObject <List <E2ERelationship> >(); string tempFromFilePath = "fromEntTemp.cdm.json"; string tempFromEntityPath = "local:/fromEntTemp.cdm.json/fromEnt"; string tempToEntityPath = "local:/toEnt.cdm.json/toEnt"; // Initialize corpus and entity files CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestUpdateRelationships"); CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>("local:/main.manifest.cdm.json"); CdmManifestDefinition manifestNoToEnt = await corpus.FetchObjectAsync <CdmManifestDefinition>("local:/mainNoToEnt.manifest.cdm.json"); CdmEntityDefinition fromEnt = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/fromEnt.cdm.json/fromEnt"); await fromEnt.InDocument.SaveAsAsync(tempFromFilePath, options : new CopyOptions() { IsTopLevelDocument = false }); async Task reloadFromEntity() { await fromEnt.InDocument.SaveAsAsync(tempFromFilePath, options : new CopyOptions() { IsTopLevelDocument = false }); // fetch again to reset the cache await corpus.FetchObjectAsync <CdmEntityDefinition>(tempFromEntityPath, null, false, true); } try { // 1. test when entity attribute is removed await corpus.CalculateEntityGraphAsync(manifest); await manifest.PopulateManifestRelationshipsAsync(); // check that the relationship has been created correctly VerifyRelationships(manifest, expectedRels); // now remove the entity attribute, which removes the relationship CdmAttributeItem removedAttribute = fromEnt.Attributes[0]; fromEnt.Attributes.RemoveAt(0); await reloadFromEntity(); await corpus.CalculateEntityGraphAsync(manifest); await manifest.PopulateManifestRelationshipsAsync(); // check that the relationship has been removed VerifyRelationships(manifest, new List <E2ERelationship>()); // 2. test when the to entity is removed // restore the entity to the original state fromEnt.Attributes.Add(removedAttribute); await reloadFromEntity(); await corpus.CalculateEntityGraphAsync(manifest); await manifest.PopulateManifestRelationshipsAsync(); // check that the relationship has been created correctly VerifyRelationships(manifest, expectedRels); // remove the to entity fromEnt.Attributes.RemoveAt(0); await reloadFromEntity(); // fetch again to reset the cache await corpus.FetchObjectAsync <CdmEntityDefinition>(tempToEntityPath, null, false, true); await corpus.CalculateEntityGraphAsync(manifestNoToEnt); await manifestNoToEnt.PopulateManifestRelationshipsAsync(); // check that the relationship has been removed VerifyRelationships(manifestNoToEnt, new List <E2ERelationship>()); } finally { // clean up created files created string fromPath = corpus.Storage.CorpusPathToAdapterPath($"local:/{tempFromFilePath}"); File.Delete(fromPath); } }