public async Task TestProjectionTrait() { string testName = "TestProjectionTrait"; string entityName = testName; CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName); CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json"); string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); CdmEntityDefinition entTestProjectionTrait = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}", manifest); Assert.IsNotNull(entTestProjectionTrait); CdmEntityDefinition resolvedTestProjectionTrait = await TestUtils.GetResolvedEntity(corpus, entTestProjectionTrait, new List <string> { }); Assert.IsNotNull(resolvedTestProjectionTrait); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, entityName, resolvedTestProjectionTrait); { // Attribute Name Assert.AreEqual("TestProjectionAttribute", ((CdmTypeAttributeDefinition)resolvedTestProjectionTrait.Attributes[0]).Name); // Trait Name Assert.AreEqual("does.haveDefault", ((CdmTypeAttributeDefinition)resolvedTestProjectionTrait.Attributes[0]).AppliedTraits[3].NamedReference); // Trait Name Assert.AreEqual("testTrait", ((CdmTypeAttributeDefinition)resolvedTestProjectionTrait.Attributes[0]).AppliedTraits[4].NamedReference); // Trait Param Name Assert.AreEqual("testTraitParam1", ((CdmArgumentDefinition)((CdmTypeAttributeDefinition)resolvedTestProjectionTrait.Attributes[0]).AppliedTraits[4].Arguments[0]).ResolvedParameter.Name); // Trait Param Default Value Assert.AreEqual("TestTrait Param 1 DefaultValue", ((CdmArgumentDefinition)((CdmTypeAttributeDefinition)resolvedTestProjectionTrait.Attributes[0]).AppliedTraits[4].Arguments[0]).Value); } }
/// <summary> /// Print the relationship /// </summary> /// <param name="relationship"></param> /// <returns></returns> private static string PrintRelationship(CdmE2ERelationship relationship) { StringBuilder bldr = new StringBuilder(); if (!string.IsNullOrWhiteSpace(relationship?.Name)) { bldr.AppendLine($" Name: {relationship.Name}"); } bldr.AppendLine($" FromEntity: {relationship.FromEntity}"); bldr.AppendLine($" FromEntityAttribute: {relationship.FromEntityAttribute}"); bldr.AppendLine($" ToEntity: {relationship.ToEntity}"); bldr.AppendLine($" ToEntityAttribute: {relationship.ToEntityAttribute}"); if (relationship.ExhibitsTraits != null && relationship.ExhibitsTraits.Count != 0) { bldr.AppendLine($" ExhibitsTraits:"); var orderedAppliedTraits = relationship.ExhibitsTraits.AllItems.ToList().OrderBy(x => x.NamedReference); foreach (CdmTraitReference trait in orderedAppliedTraits) { bldr.AppendLine($" {trait.NamedReference}"); foreach (CdmArgumentDefinition args in trait.Arguments) { AttributeContextUtil attrCtxUtil = new AttributeContextUtil(); bldr.AppendLine($" {attrCtxUtil.GetArgumentValuesAsString(args)}"); } } } bldr.AppendLine(); Console.WriteLine(bldr.ToString()); return(bldr.ToString()); }
/// <summary> /// A function to validate if the attribute context tree & traits generated for a resolved entity is the same as the expected and saved attribute context tree & traits for a test case /// </summary> /// <param name="corpus"></param> /// <param name="expectedOutputPath"></param> /// <param name="entityName"></param> /// <param name="resolvedEntity"></param> public static void ValidateAttributeContext(CdmCorpusDefinition corpus, string expectedOutputPath, string entityName, CdmEntityDefinition resolvedEntity) { if (resolvedEntity.AttributeContext != null) { AttributeContextUtil attrCtxUtil = new AttributeContextUtil(); string actualText = attrCtxUtil.GetAttributeContextStrings(resolvedEntity, resolvedEntity.AttributeContext); string expectedStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, $"AttrCtx_{entityName}.txt")); string expectedText = File.ReadAllText(expectedStringFilePath); Assert.AreEqual(expectedText, actualText); } }
/// <summary> /// Common test code for these test cases /// </summary> /// <param name="testName"></param> /// <param name="entityName"></param> private void TestRun(string testName, string entityName) { CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName); string inputFolder = TestHelper.GetInputFolderPath(testsSubpath, testName); string expectedOutputFolder = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); string actualOutputFolder = TestHelper.GetActualOutputFolderPath(testsSubpath, testName); if (!Directory.Exists(actualOutputFolder)) { Directory.CreateDirectory(actualOutputFolder); } CdmManifestDefinition manifest = corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json").GetAwaiter().GetResult(); Assert.IsNotNull(manifest); CdmEntityDefinition entity = corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}", manifest).GetAwaiter().GetResult(); Assert.IsNotNull(entity); CdmEntityDefinition resolvedEntity = TestUtils.GetResolvedEntity(corpus, entity, new List <string> { "referenceOnly" }).GetAwaiter().GetResult(); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputFolder, entityName, resolvedEntity); corpus.CalculateEntityGraphAsync(manifest).GetAwaiter().GetResult(); manifest.PopulateManifestRelationshipsAsync().GetAwaiter().GetResult(); string actualRelationshipsString = ListRelationships(corpus, entity, actualOutputFolder, entityName); string expectedRelationshipsStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputFolder, $"REL_{entityName}.txt")); string expectedRelationshipsString = File.ReadAllText(expectedRelationshipsStringFilePath); Assert.AreEqual(expectedRelationshipsString, actualRelationshipsString); CdmFolderDefinition outputFolder = corpus.Storage.FetchRootFolder("output"); outputFolder.Documents.Add(manifest); string manifestFileName = $"saved.manifest.cdm.json"; manifest.SaveAsAsync(manifestFileName, saveReferenced: true).GetAwaiter().GetResult(); string actualManifestPath = Path.Combine(actualOutputFolder, manifestFileName); if (!File.Exists(actualManifestPath)) { Assert.Fail("Unable to save manifest with relationship"); } else { CdmManifestDefinition savedManifest = corpus.FetchObjectAsync <CdmManifestDefinition>($"output:/{manifestFileName}").GetAwaiter().GetResult(); string actualSavedManifestRel = GetRelationshipStrings(savedManifest.Relationships); string expectedSavedManifestRel = File.ReadAllText(Path.Combine(expectedOutputFolder, $"MANIFEST_REL_{entityName}.txt")); Assert.AreEqual(expectedSavedManifestRel, actualSavedManifestRel); } }
/// <summary> /// Loads an entity, resolves it, and then validates the generated attribute contexts /// </summary> private async Task LoadEntityForResolutionOptionAndSave(CdmCorpusDefinition corpus, string testName, string entityName, List <string> resOpts) { string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); string fileNameSuffix = TestUtils.GetResolutionOptionNameSuffix(resOpts); CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}"); CdmEntityDefinition resolvedEntity = await TestUtils.GetResolvedEntity(corpus, entity, resOpts, true); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, $"{entityName}{fileNameSuffix}", resolvedEntity); }
public async Task TestAppliedTraitsInAttributes() { CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestAppliedTraitsInAttributes"); string expectedOutputFolder = TestHelper.GetExpectedOutputFolderPath(testsSubpath, "TestAppliedTraitsInAttributes"); CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/Sales.cdm.json/Sales"); CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> { "referenceOnly" }); await AttributeContextUtil.ValidateAttributeContext(expectedOutputFolder, "Sales", resolvedEntity); }
private async Task LoadEntityForResolutionOptionAndSave(string testName, string entityName, List <string> resOpts) { CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName); CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json"); string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); string fileNameSuffix = GetResolutionOptionNameSuffix(resOpts); CdmEntityDefinition entSalesForeignKeyProjection = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}", manifest); Assert.IsNotNull(entSalesForeignKeyProjection); CdmEntityDefinition resolvedSalesForeignKeyProjection = await SaveResolved(corpus, manifest, testName, entSalesForeignKeyProjection, resOpts); Assert.IsNotNull(resolvedSalesForeignKeyProjection); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, $"{entityName}{fileNameSuffix}", resolvedSalesForeignKeyProjection); }
public async Task TestEntityProjection() { string testName = "TestEntityProjection"; string entityName = testName; CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName); CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json"); string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); CdmEntityDefinition entTestEntityProjection = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}", manifest); Assert.IsNotNull(entTestEntityProjection); CdmEntityDefinition resolvedTestEntityProjection = await TestUtils.GetResolvedEntity(corpus, entTestEntityProjection, new List <string> { }); Assert.IsNotNull(resolvedTestEntityProjection); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, entityName, resolvedTestEntityProjection); }
/// <summary> /// Loads an entity, resolves it, and then validates the generated attribute contexts /// </summary> private async Task LoadEntityForResolutionOptionAndSave(string testName, string entityName, List <string> resOpts) { string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName); string fileNameSuffix = ProjectionTestUtils.GetResolutionOptionNameSuffix(resOpts); CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName); corpus.Storage.Mount("expected", new LocalAdapter(expectedOutputPath)); CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json"); CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}"); Assert.IsNotNull(entity); CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, resOpts, true); Assert.IsNotNull(resolvedEntity); await ValidateResolvedAttributes(corpus, resolvedEntity, entityName, fileNameSuffix); AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, $"{entityName}{fileNameSuffix}", resolvedEntity); }
/// <summary> /// A function to validate if the attribute context tree & traits generated for a resolved entity is the same as the expected and saved attribute context tree & traits for a test case /// </summary> /// <param name="corpus"></param> /// <param name="expectedOutputPath"></param> /// <param name="entityName"></param> /// <param name="resolvedEntity"></param> public static void ValidateAttributeContext(CdmCorpusDefinition corpus, string expectedOutputPath, string entityName, CdmEntityDefinition resolvedEntity) { if (resolvedEntity.AttributeContext != null) { AttributeContextUtil attrCtxUtil = new AttributeContextUtil(); // Expected string expectedStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, $"AttrCtx_{entityName}.txt")); string expectedText = File.ReadAllText(expectedStringFilePath); // Actual string actualStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, "..", "ActualOutput", $"AttrCtx_{entityName}.txt")); // Save Actual AttrCtx_*.txt and Resolved_*.cdm.json string actualText = attrCtxUtil.GetAttributeContextStrings(resolvedEntity, resolvedEntity.AttributeContext); File.WriteAllText(actualStringFilePath, actualText); resolvedEntity.InDocument.SaveAsAsync($"Resolved_{entityName}.cdm.json", saveReferenced: false).GetAwaiter().GetResult(); // Test if Actual is Equal to Expected Assert.AreEqual(expectedText.Replace("\r\n", "\n"), actualText.Replace("\r\n", "\n")); } }