Ejemplo n.º 1
0
        /// <summary>
        /// Validates if the attribute context of the resolved entity matches the expected output.
        /// </summary>
        /// <param name="directives"></param>
        /// <param name="expectedOutputPath"></param>
        /// <param name="entityName"></param>
        /// <param name="resolvedEntity"></param>
        /// <param name="updateExpectedOutput">If true, will update the expected output txt files for all the tests that are ran.</param>
        private static async Task ValidateAttributeContext(List <string> directives, string expectedOutputPath, string entityName, CdmEntityDefinition resolvedEntity, bool updateExpectedOutput = false)
        {
            if (resolvedEntity.AttributeContext == null)
            {
                throw new Exception("ValidateAttributeContext called with not resolved entity.");
            }

            string fileNamePrefix = $"AttrCtx_{entityName}";
            string fileNameSuffix = GetResolutionOptionNameSuffix(directives);

            // Get actual text
            AttributeContextUtil attrCtxUtil = new AttributeContextUtil();
            string actualText = attrCtxUtil.GetAttributeContextStrings(resolvedEntity);
            string expectedStringFilePath;

            if (updateExpectedOutput)
            {
                expectedStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, $"{fileNamePrefix}{fileNameSuffix}.txt"));

                if (directives.Count > 0)
                {
                    string defaultFileNameSuffix = GetResolutionOptionNameSuffix(new List <string>()
                    {
                    });
                    string defaultStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, $"{fileNamePrefix}{defaultFileNameSuffix}.txt"));
                    string defaultText           = File.Exists(defaultStringFilePath) ? File.ReadAllText(defaultStringFilePath) : null;

                    if (actualText.Equals(defaultText))
                    {
                        File.Delete(expectedStringFilePath);
                    }
                    else
                    {
                        File.WriteAllText(expectedStringFilePath, actualText);
                    }
                }
                else
                {
                    File.WriteAllText(expectedStringFilePath, actualText);
                }
            }
            else
            {
                // Actual
                string actualStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, "..", TestHelper.GetTestActualOutputFolderName(), $"{fileNamePrefix}{fileNameSuffix}.txt"));

                // Save Actual AttrCtx_*.txt and Resolved_*.cdm.json
                File.WriteAllText(actualStringFilePath, actualText);
                await resolvedEntity.InDocument.SaveAsAsync($"{resolvedEntity.EntityName}{fileNameSuffix}.cdm.json", saveReferenced : false);

                // Expected
                string expectedFileNameSuffix = GetResolutionOptionNameSuffix(directives, expectedOutputPath, entityName);
                expectedStringFilePath = Path.GetFullPath(Path.Combine(expectedOutputPath, $"{fileNamePrefix}{expectedFileNameSuffix}.txt"));
                string expectedText = File.ReadAllText(expectedStringFilePath);

                // Test if Actual is Equal to Expected
                Assert.AreEqual(expectedText.Replace("\r\n", "\n"), actualText.Replace("\r\n", "\n"));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads an entity, resolves it, and then validates the generated attribute contexts.
        /// </summary>
        public static async Task LoadEntityForResolutionOptionAndSave(CdmCorpusDefinition corpus, string testName, string testsSubpath, string entityName, List <string> resOpts)
        {
            string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName);
            string fileNameSuffix     = GetResolutionOptionNameSuffix(resOpts);

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await GetResolvedEntity(corpus, entity, resOpts, true);

            AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, $"{entityName}{fileNameSuffix}", resolvedEntity);
        }