public void AllExistingProjectModelReportTemplatesAreValid() { SitkaLogger.RegisterLogger(new FirmaLogger()); List <ReportTemplateTestFailedReportTemplate> failedReportTemplates = new List <ReportTemplateTestFailedReportTemplate>(); //var reportTemplates = HttpRequestStorageForTest.DatabaseEntities.AllReportTemplates.ToList(); var reportTemplatesGroupedByTenantID = HttpRequestStorageForTest.DatabaseEntities.AllReportTemplates.GroupBy(x => x.TenantID, (key, rt) => new { TenantID = key, ReportTemplates = rt.ToList() }).ToList(); foreach (var reportTemplatesForTenant in reportTemplatesGroupedByTenantID) { var sampleProjectIDs = HttpRequestStorageForTest.DatabaseEntities.AllProjects .Where(p => p.TenantID == reportTemplatesForTenant.TenantID).Select(p => p.ProjectID).Take(10).ToList(); foreach (var reportTemplate in reportTemplatesForTenant.ReportTemplates) { ReportTemplateGenerator.ValidateReportTemplateForSelectedModelIDs(reportTemplate, sampleProjectIDs, out var reportIsValid, out var errorMessage, out var sourceCode); if (!reportIsValid) { failedReportTemplates.Add(new ReportTemplateTestFailedReportTemplate(reportTemplate, errorMessage, sourceCode)); } } } Assert.That(failedReportTemplates, Is.Empty, $"The following report templates (count: {failedReportTemplates.Count}) have failed to compile: {string.Join(", ", failedReportTemplates.Select(x => x.ErrorMessageForTest()))}. Was there any changes recently to the Docx ReportTemplating or associated models?"); }
public static void ValidateReportTemplateForSelectedModelIDs(ReportTemplate reportTemplate, List <int> selectedModelIDs, out bool reportIsValid, out string errorMessage, out string sourceCode) { errorMessage = ""; sourceCode = ""; var reportTemplateGenerator = new ReportTemplateGenerator(reportTemplate, selectedModelIDs); var tempDirectory = reportTemplateGenerator.GetCompilePath(); try { reportTemplateGenerator.Generate(); reportIsValid = true; } catch (SharpDocxCompilationException exception) { errorMessage = exception.Errors; sourceCode = exception.SourceCode; reportIsValid = false; SitkaLogger.Instance.LogDetailedErrorMessage($"There was a SharpDocxCompilationException validating a report template. Temporary template file location:\"{tempDirectory}\" Error Message: \"{errorMessage}\". Source Code: \"{sourceCode}\"", exception); } catch (Exception exception) { reportIsValid = false; // SMG 2/12/2020 submitted an issue on the SharpDocx repo https://github.com/egonl/SharpDocx/issues/13 for better exceptions to be able to refactor this out later. switch (exception.Message) { case "No end tag found for code.": errorMessage = $"CodeBlockBuilder exception: \"{exception.Message}\". Could not find a matching closing tag \"%>\" for an opening tag."; break; case "TextBlock is not terminated with '<% } %>'.": errorMessage = $"CodeBlockBuilder exception: \"{exception.Message}\"."; break; default: errorMessage = exception.Message; break; } sourceCode = exception.StackTrace; SitkaLogger.Instance.LogDetailedErrorMessage($"There was a SharpDocxCompilationException validating a report template. Temporary template file location:\"{tempDirectory}\". Error Message: \"{errorMessage}\".", exception); } }