DynamicWriterTests.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using ClosedXML.Excel;
  2. using ExcelORM;
  3. namespace ExcelORMTests;
  4. public class DynamicWriterTests
  5. {
  6. private const string DifficultFile = "testFiles/dynamicDifficult.xlsx";
  7. private const string MultipleSheetsFile = "testFiles/multipleSheets.xlsx";
  8. [Fact]
  9. public void Write()
  10. {
  11. var testFile = Path.GetRandomFileName();
  12. testFile = Path.ChangeExtension(testFile, "xlsx");
  13. var reader = new ExcelDynamicReader(DifficultFile);
  14. var results = reader.Read().ToArray();
  15. Assert.NotEmpty(results);
  16. var writer = new ExcelDynamicWriter();
  17. writer.Write(results);
  18. writer.SaveAs(testFile);
  19. var savedReader = new ExcelDynamicReader(testFile);
  20. var savedResults = savedReader.Read().ToArray();
  21. Assert.NotEmpty(savedResults);
  22. Assert.True(results.First().SequenceEqual(savedResults.First()));
  23. Assert.True(results.Last().SequenceEqual(savedResults.Last()));
  24. File.Delete(testFile);
  25. }
  26. [Fact]
  27. public void WriteAll()
  28. {
  29. var testFile = Path.GetRandomFileName();
  30. testFile = Path.ChangeExtension(testFile, "xlsx");
  31. var reader = new ExcelDynamicReader(MultipleSheetsFile);
  32. var results = reader.ReadAll().ToArray();
  33. Assert.NotEmpty(results);
  34. var writer = new ExcelDynamicWriter();
  35. writer.WriteAll(results);
  36. writer.SaveAs(testFile);
  37. var savedReader = new ExcelDynamicReader(testFile);
  38. var savedResults = savedReader.ReadAll().ToArray();
  39. Assert.NotEmpty(savedResults);
  40. Assert.Equal(results.First().Name, savedResults.First().Name);
  41. Assert.Equal(results.First().Cells?.Count(), savedResults.First().Cells?.Count());
  42. Assert.Equal(results.Last().Name, savedResults.Last().Name);
  43. Assert.Equal(results.Last().Cells?.Count(), savedResults.Last().Cells?.Count());
  44. File.Delete(testFile);
  45. }
  46. [Fact]
  47. public void WriteReadInMemory()
  48. {
  49. using var readWorkbook = new XLWorkbook(DifficultFile);
  50. var reader = new ExcelDynamicReader(readWorkbook);
  51. var results = reader.Read().ToArray();
  52. Assert.NotEmpty(results);
  53. using var writeWorkbook = new XLWorkbook();
  54. var writer = new ExcelDynamicWriter(writeWorkbook);
  55. writer.Write(results);
  56. var savedReader = new ExcelDynamicReader(writeWorkbook);
  57. var savedResults = savedReader.Read().ToArray();
  58. Assert.NotEmpty(savedResults);
  59. Assert.True(results.First().SequenceEqual(savedResults.First()));
  60. Assert.True(results.Last().SequenceEqual(savedResults.Last()));
  61. }
  62. }