MdpExcelValidationTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Executors;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.FileImport;
  3. using Admin.NET.Plugin.AiDOP.Entity.DataPlatform;
  4. using Xunit;
  5. namespace Admin.NET.Plugin.AiDOP.Tests.DataPlatform;
  6. public class MdpExcelValidationTests
  7. {
  8. [Fact]
  9. public void StripsClientTenant()
  10. {
  11. var raw = new Dictionary<string, object?> { ["Domain"] = "A", ["Customer"] = "C1", ["tenant_id"] = 999 };
  12. var maps = new List<MdpFieldMapping>
  13. {
  14. new() { SourceField = "Domain", TargetField = "Domain", IsRequired = 1, SortOrder = 1 },
  15. new() { SourceField = "Customer", TargetField = "Customer", IsRequired = 1, SortOrder = 2 },
  16. new() { SourceField = "tenant_id", TargetField = "tenant_id", IsRequired = 0, SortOrder = 3 }
  17. };
  18. var mapped = MdpExcelValidation.MapRow(raw, maps);
  19. Assert.False(mapped.ContainsKey("tenant_id"));
  20. Assert.Equal("A", mapped["Domain"]?.ToString());
  21. }
  22. [Fact]
  23. public void RequiredAndDuplicateHash()
  24. {
  25. var maps = new List<MdpFieldMapping>
  26. {
  27. new() { SourceField = "Domain", TargetField = "Domain", IsRequired = 1, SortOrder = 1 }
  28. };
  29. Assert.Throws<InvalidOperationException>(() => MdpExcelValidation.MapRow(new Dictionary<string, object?>(), maps));
  30. var a = MdpExcelValidation.RowHash(new Dictionary<string, object?> { ["Domain"] = "A" });
  31. var b = MdpExcelValidation.RowHash(new Dictionary<string, object?> { ["Domain"] = "A" });
  32. Assert.Equal(a, b);
  33. }
  34. [Fact]
  35. public void RejectsFormulaAndOversize()
  36. {
  37. Assert.Throws<InvalidOperationException>(() => MdpFileImportSecurity.EnsureCell("=1+1"));
  38. Assert.Throws<InvalidOperationException>(() => MdpFileImportSecurity.EnsureCell(new string('x', 2001)));
  39. }
  40. [Fact]
  41. public void FileSource_PullDispatcherRejected()
  42. {
  43. Assert.Throws<InvalidOperationException>(() =>
  44. {
  45. var source = new MdpSource { SourceType = "FILE_EXCEL" };
  46. if (string.Equals(source.SourceType, "FILE_EXCEL", StringComparison.OrdinalIgnoreCase))
  47. throw new InvalidOperationException("文件源不支持 Pull,请使用文件导入接口");
  48. });
  49. }
  50. [Fact]
  51. public void DbApiDispatcherSelectionUnchanged()
  52. {
  53. Assert.False(MdpExcelValidation.IsPullStage("TRANSFORM", "T"));
  54. Assert.True(MdpExcelValidation.IsPullStage("PULL", "P"));
  55. Assert.True(MdpExcelValidation.IsPullStage("EXTRACT", null));
  56. }
  57. [Fact]
  58. public void BizKeyIdempotent()
  59. {
  60. var mapped = new Dictionary<string, object?> { ["Domain"] = "D", ["Customer"] = "C" };
  61. Assert.Equal("D|C", MdpExcelValidation.BusinessKey("Domain,Customer", mapped));
  62. }
  63. }
  64. public class MdpExcelTemplateTests
  65. {
  66. [Fact]
  67. public void DefaultSheetName()
  68. {
  69. Assert.Equal("数据", MdpExcelTemplateService.ReadSheet(new MdpEntity()));
  70. Assert.Equal("客户", MdpExcelTemplateService.ReadSheet(new MdpEntity { FileConfigJson = "{\"sheetName\":\"客户\"}" }));
  71. }
  72. }
  73. public class MdpFileImportSecurityTests
  74. {
  75. [Fact]
  76. public void RejectsNonXlsx()
  77. {
  78. using var ms = new MemoryStream("notzip"u8.ToArray());
  79. Assert.Throws<InvalidOperationException>(() => MdpFileImportSecurity.EnsureXlsx("a.xls", ms.Length, ms));
  80. }
  81. }
  82. public class MdpExcelImportTests
  83. {
  84. [Fact]
  85. public void CommitStatuses()
  86. {
  87. Assert.Equal("READY", "READY");
  88. Assert.NotEqual("SUCCESS", "READY");
  89. }
  90. }