test_icopy.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import sys
  3. myPath = os.path.dirname(os.path.abspath(__file__))
  4. sys.path.insert(0, myPath + '/../')
  5. from icopy2xls import Mover
  6. input_file_1 = "tests/TestInput/Financial Sample.xlsx"
  7. input_file_2 = "tests/TestInput/Financial Sample_1.xlsx"
  8. input_sheet = "Sheet1"
  9. destination_file = "tests/TestInput/Financial Sample - Output.xlsx"
  10. output_file = "tests/TestOutput/Financial Sample - Result.xlsx"
  11. output_sheet = "Sheet1"
  12. if os.path.exists(output_file):
  13. os.remove(output_file)
  14. with open(destination_file, 'rb')as file:
  15. data = file.read()
  16. with open(output_file, 'wb')as file:
  17. file.write(data)
  18. mover = Mover(
  19. source_file_path=input_file_1,
  20. source_sheet=input_sheet,
  21. destination_file_path=output_file,
  22. destination_sheet=output_sheet
  23. )
  24. def test_basic():
  25. mover.move()
  26. sht = mover.read_xlsx(output_file, output_sheet)
  27. assert sht.nrows == 1401 and sht.ncols == 12
  28. def test_filters():
  29. mover.move(filters={"Country": "Canada"})
  30. sht = mover.read_xlsx(output_file, output_sheet)
  31. assert sht.nrows == 1541 and sht.ncols == 12
  32. def test_filters_multiple():
  33. mover.move(filters={"Country": "Canada", "Segment": "Government"})
  34. sht = mover.read_xlsx(output_file, output_sheet)
  35. assert sht.nrows == 1601 and sht.ncols == 12
  36. def test_filters_value_list():
  37. mover.move(filters={"Country": ["Canada", "France"]})
  38. sht = mover.read_xlsx(output_file, output_sheet)
  39. assert sht.nrows == 1881 and sht.ncols == 12
  40. def test_process_lines():
  41. lines = "3,7-13,25,300-310,1"
  42. processed = mover.process_lines(lines)
  43. expected = [1, 3, 7, 8, 9, 10, 11, 12, 13, 25, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310]
  44. assert len(processed) == 21 and sorted(processed) == expected
  45. def test_lines():
  46. move = Mover(
  47. source_file_path=input_file_1,
  48. source_sheet=input_sheet,
  49. destination_file_path=output_file,
  50. destination_sheet=output_sheet,
  51. lines="1-10"
  52. )
  53. move.move()
  54. sht = mover.read_xlsx(output_file, output_sheet)
  55. assert sht.nrows == 1891 and sht.ncols == 12
  56. def test_add_missing():
  57. move = Mover(
  58. source_file_path=input_file_1,
  59. source_sheet=input_sheet,
  60. destination_file_path=output_file,
  61. destination_sheet=output_sheet,
  62. lines="1-10"
  63. )
  64. move.move(add_missing_columns=True)
  65. sht = mover.read_xlsx(output_file, output_sheet)
  66. assert sht.nrows == 1901 and sht.ncols == 16
  67. def test_multiple_inputs():
  68. move = Mover(
  69. source_file_path=[input_file_1, input_file_2],
  70. source_sheet=input_sheet,
  71. destination_file_path=output_file,
  72. destination_sheet=output_sheet,
  73. lines="10,21-30"
  74. )
  75. move.move()
  76. sht = mover.read_xlsx(output_file, output_sheet)
  77. assert sht.nrows == 1923 and sht.ncols == 16
  78. def test_read_xlsx():
  79. sht = mover.read_xlsx(output_file, output_sheet)
  80. assert sht.nrows == 1923 and sht.ncols == 16