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> /// Get the corpus path for each attribute context in the tree and build a string collection that we can /// compare with the expected attribute context corpus path collection. /// </summary> /// <param name="attribContext"></param> private void GetContentDeclaredPath(CdmAttributeContext attribContext) { if (attribContext != null && attribContext.Contents != null && attribContext.Contents.Count > 0) { for (int i = 0; i < attribContext.Contents.Count; i++) { string str; if ((attribContext.Contents[i] is CdmAttributeReference)) { CdmAttributeReference ar = (CdmAttributeReference)attribContext.Contents[i]; str = ar.AtCorpusPath; bldr.AppendLine(str); } else { CdmAttributeContext ac = (CdmAttributeContext)attribContext.Contents[i]; str = ac.AtCorpusPath; bldr.AppendLine(str); GetContentDeclaredPath(ac); } } } }
public static void ValidateAttributeContext(AttributeContextExpectedValue expected, CdmAttributeContext actual) { if (expected == null || actual == null) { Assert.IsNull(expected); Assert.IsNull(actual); return; } Assert.AreEqual(expected.Type, actual.Type.ToString(), ignoreCase: true); Assert.AreEqual(expected.Name, actual.Name); if (actual.Parent != null) { Assert.AreEqual(expected.Parent, actual.Parent.NamedReference); } if (expected.Definition != null && actual.Definition != null) { Assert.AreEqual(expected.Definition, actual.Definition.NamedReference); } int expCount = 0; if (expected.Contexts != null && expected.Contexts.Count > 0) { expCount += expected.Contexts.Count; } if (expected.ContextStrings != null && expected.ContextStrings.Count > 0) { expCount += expected.ContextStrings.Count; } Assert.AreEqual(expCount, actual.Contents.Count); for (int i = 0, ac = 0, acs = 0; i < actual.Contents.Count; i++) { if (actual.Contents.AllItems[i].GetType() == typeof(CdmAttributeContext)) { ValidateAttributeContext(expected.Contexts[ac++], (CdmAttributeContext)actual.Contents.AllItems[i]); } else if (actual.Contents.AllItems[i].GetType() == typeof(CdmAttributeReference)) { string exp = expected.ContextStrings[acs++]; CdmAttributeReference act = (CdmAttributeReference)actual.Contents.AllItems[i]; Assert.AreEqual(exp, act.NamedReference); } else { throw new NotImplementedException("ValidateAttributeContext: typeof(Unknown)"); } } }
public async Task TestReadingIsPrimaryKey() { var testInputPath = TestHelper.GetInputFolderPath(testsSubpath, "TestReadingIsPrimaryKey"); CdmCorpusDefinition corpus = new CdmCorpusDefinition(); corpus.SetEventCallback(new EventCallback { Invoke = CommonDataModelLoader.ConsoleStatusReport }, CdmStatusLevel.Warning); corpus.Storage.Mount("local", new LocalAdapter(testInputPath)); corpus.Storage.DefaultNamespace = "local"; // Read from an unresolved entity schema. CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership.cdm.json/TeamMembership"); CdmAttributeGroupReference attributeGroupRef = (CdmAttributeGroupReference)entity.Attributes[0]; CdmAttributeGroupDefinition attributeGroup = (CdmAttributeGroupDefinition)attributeGroupRef.ExplicitReference; CdmTypeAttributeDefinition typeAttribute = (CdmTypeAttributeDefinition)attributeGroup.Members[0]; Assert.IsTrue((bool)typeAttribute.IsPrimaryKey); // Check that the trait "is.identifiedBy" is created with the correct argument. CdmTraitReference isIdentifiedBy1 = typeAttribute.AppliedTraits[1]; Assert.AreEqual("is.identifiedBy", isIdentifiedBy1.NamedReference); Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", isIdentifiedBy1.Arguments[0].Value); // Read from a resolved entity schema. CdmEntityDefinition resolvedEntity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership_Resolved.cdm.json/TeamMembership"); CdmTypeAttributeDefinition resolvedTypeAttribute = (CdmTypeAttributeDefinition)resolvedEntity.Attributes[0]; Assert.IsTrue((bool)resolvedTypeAttribute.IsPrimaryKey); // Check that the trait "is.identifiedBy" is created with the correct argument. CdmTraitReference isIdentifiedBy2 = resolvedTypeAttribute.AppliedTraits[6]; Assert.AreEqual("is.identifiedBy", isIdentifiedBy2.NamedReference); CdmAttributeReference argumentValue = isIdentifiedBy2.Arguments[0].Value; Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", argumentValue.NamedReference); }
public async Task TestReadingIsPrimaryKey() { CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestReadingIsPrimaryKey"); var resOpt = new ResolveOptions() { StrictValidation = true }; // Read from an unresolved entity schema. CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership.cdm.json/TeamMembership", null, resOpt); CdmAttributeGroupReference attributeGroupRef = (CdmAttributeGroupReference)entity.Attributes[0]; CdmAttributeGroupDefinition attributeGroup = (CdmAttributeGroupDefinition)attributeGroupRef.ExplicitReference; CdmTypeAttributeDefinition typeAttribute = (CdmTypeAttributeDefinition)attributeGroup.Members[0]; Assert.IsTrue((bool)typeAttribute.IsPrimaryKey); // Check that the trait "is.identifiedBy" is created with the correct argument. CdmTraitReference isIdentifiedBy1 = typeAttribute.AppliedTraits[1]; Assert.AreEqual("is.identifiedBy", isIdentifiedBy1.NamedReference); Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", isIdentifiedBy1.Arguments[0].Value); // Read from a resolved entity schema. CdmEntityDefinition resolvedEntity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership_Resolved.cdm.json/TeamMembership", null, resOpt); CdmTypeAttributeDefinition resolvedTypeAttribute = (CdmTypeAttributeDefinition)resolvedEntity.Attributes[0]; Assert.IsTrue((bool)resolvedTypeAttribute.IsPrimaryKey); // Check that the trait "is.identifiedBy" is created with the correct argument. CdmTraitReference isIdentifiedBy2 = resolvedTypeAttribute.AppliedTraits[6]; Assert.AreEqual("is.identifiedBy", isIdentifiedBy2.NamedReference); CdmAttributeReference argumentValue = isIdentifiedBy2.Arguments[0].Value; Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", argumentValue.NamedReference); }
public static dynamic ToData(CdmAttributeReference instance, ResolveOptions resOpt, CopyOptions options) { return(CdmObjectRefPersistence.ToData(instance, resOpt, options)); }