test_CloneXls.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from CloneXls import ChangeLog
  2. import CloneXls
  3. import os
  4. from pathlib import Path
  5. from openpyxl import load_workbook
  6. import string
  7. import random
  8. import sys
  9. myPath = os.path.dirname(os.path.abspath(__file__))
  10. sys.path.insert(0, myPath + '/../')
  11. input_directory = "tests/TestInput/"
  12. original_file = "CompleteBaangtWebdemo.xlsx"
  13. input_file = "CompleteBaangtWebdemo1.xlsx"
  14. output_file = "CompleteBaangtWebdemo1_baangt.xlsx"
  15. new_prefix = "_test"
  16. output_file_2 = "CompleteBaangtWebdemo1_test.xlsx"
  17. sheet = "data"
  18. ignore_headers = ["password", "Country"]
  19. ignore_sheets = ["TestStepExecution"]
  20. with open(Path(input_directory).joinpath(original_file), "rb") as file:
  21. data = file.read()
  22. with open(Path(input_directory).joinpath(input_file), "wb") as file:
  23. file.write(data) # creating a duplicate file as we will update the source file in the run for test to update change log
  24. def update_source(path): # update the file which is used to check the changelog
  25. wb = load_workbook(path)
  26. sht = wb.get_sheet_by_name(sheet)
  27. for row in range(1, sht.max_row+1):
  28. sht.cell(row, 9).value = None
  29. sht.cell(1, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
  30. sht.cell(2, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
  31. sht.cell(5, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
  32. sht.cell(8, 3).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
  33. wb.save(path)
  34. def check_source(path, case_sensitive=False):
  35. # checks the change log sheet that if their is value added and verifies it by row number in which we have changed data
  36. wb = load_workbook(path)
  37. sht = wb.get_sheet_by_name("Change Logs")
  38. if case_sensitive: # if case_sensitive test is done these 3 changes will the only ones
  39. assert int(sht.cell(2, 4).value) == 1
  40. assert int(sht.cell(3, 4).value) == 1
  41. assert int(sht.cell(4, 4).value) == 8
  42. assert sht.max_row == 4
  43. else: # if case_sensitive test is done these 2 changes will the only ones
  44. assert int(sht.cell(2, 4).value) == 1
  45. assert int(sht.cell(3, 4).value) == 8
  46. assert sht.max_row == 3
  47. def test_check_source_file():
  48. # Give a fake file path so it must throw a BaseException
  49. try:
  50. CloneXls.CloneXls("tests/TestInput/CompleteBaangtWebdemo_Fake.xlsx")
  51. assert 1 == 0
  52. except BaseException:
  53. assert 1 == 1
  54. def test_update_or_make_clone():
  55. # Checking update_or_make_clone method of CloneXls class, that will it make a clone file if their is none
  56. output = str(Path(input_directory).joinpath(output_file))
  57. input_ = str(Path(input_directory).joinpath(input_file))
  58. if os.path.exists(output):
  59. os.remove(output)
  60. cloneXls = CloneXls.CloneXls(input_)
  61. cloneXls.update_or_make_clone()
  62. assert os.path.exists(output)
  63. def test_update_or_make_clone_prefix():
  64. # testing the change prefix parameter
  65. output = str(Path(input_directory).joinpath(output_file_2))
  66. input = str(Path(input_directory).joinpath(input_file))
  67. if os.path.exists(output):
  68. os.remove(output)
  69. cloneXls = CloneXls.CloneXls(input, new_prefix)
  70. cloneXls.update_or_make_clone()
  71. assert os.path.exists(output) and new_prefix in output
  72. os.remove(output)
  73. def test_ChangeLog_class():
  74. # creating a ChangeLog class object and verifies that if it is build successfully
  75. output = str(Path(input_directory).joinpath(output_file))
  76. input_ = str(Path(input_directory).joinpath(input_file))
  77. changeLog = ChangeLog(input_, output)
  78. assert changeLog.ignore_sheets == ['change logs'] and changeLog.ignore_headers ==[]
  79. def test_ChangeLog_not_case_sensitive():
  80. # Testing ChangeLog class with ignore lists treated as non case sensitive
  81. output = str(Path(input_directory).joinpath(output_file))
  82. input_ = str(Path(input_directory).joinpath(input_file))
  83. changeLog = ChangeLog(
  84. input_, output, ignore_headers=ignore_headers, ignore_sheets=ignore_sheets
  85. )
  86. assert changeLog.ignore_headers[0].islower() and changeLog.ignore_headers[1].islower() and \
  87. changeLog.ignore_sheets[0].islower()
  88. update_source(input_)
  89. changeLog.xlsxChangeLog()
  90. check_source(output)
  91. def test_ChangeLog_case_sensitive():
  92. # Testing ChangeLog class with ignore lists treated as case sensitive
  93. output = str(Path(input_directory).joinpath(output_file))
  94. input_ = str(Path(input_directory).joinpath(input_file))
  95. os.remove(output)
  96. cloneXls = CloneXls.CloneXls(input_)
  97. cloneXls.update_or_make_clone()
  98. changeLog = ChangeLog(
  99. input_, output, ignore_headers=ignore_headers, ignore_sheets=ignore_sheets, case_sensitive_ignore=True
  100. )
  101. assert changeLog.ignore_headers[0].islower() and not changeLog.ignore_headers[1].islower() and \
  102. not changeLog.ignore_sheets[0].islower()
  103. update_source(input_)
  104. changeLog.xlsxChangeLog()
  105. changeLog.xlsxChangeLog()
  106. check_source(output, case_sensitive=True)
  107. os.remove(input_)
  108. os.remove(output)