Akash Singh 3 years ago
parent
commit
a9eaf598f0
4 changed files with 124 additions and 2 deletions
  1. 1 1
      CloneXls/ChangeLog.py
  2. 1 1
      CloneXls/__init__.py
  3. BIN
      tests/TestInput/CompleteBaangtWebdemo.xlsx
  4. 122 0
      tests/test_CloneXls.py

+ 1 - 1
CloneXls/ChangeLog.py

@@ -48,7 +48,7 @@ class ChangeLog:
         self.check_log_sheet(clone_sheets, clone)
         sheets = self.get_sheets(source_sheets, clone_sheets)
         logs = self.get_change_logs(sheets, source, clone)
-        self.update_change_logs(logs, clone, clone_sheets)
+        self.update_change_logs(logs, clone)
         self.update_clone_file(source, clone, source_sheets)
         clone.save(self.clone_file)
         clone.close()

+ 1 - 1
CloneXls/__init__.py

@@ -1,5 +1,5 @@
 import os
-from ChangeLog import ChangeLog
+from .ChangeLog import ChangeLog
 
 class CloneXls:
     def __init__(self, source_file, clone_file_postfix="_baangt"):

BIN
tests/TestInput/CompleteBaangtWebdemo.xlsx


+ 122 - 0
tests/test_CloneXls.py

@@ -0,0 +1,122 @@
+from CloneXls import ChangeLog
+import CloneXls
+import os
+from pathlib import Path
+from openpyxl import load_workbook
+import string
+import random
+import sys
+
+myPath = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0, myPath + '/../')
+
+input_directory = "tests/TestInput/"
+original_file = "CompleteBaangtWebdemo.xlsx"
+input_file = "CompleteBaangtWebdemo1.xlsx"
+output_file = "CompleteBaangtWebdemo1_baangt.xlsx"
+new_prefix = "_test"
+output_file_2 = "CompleteBaangtWebdemo1_test.xlsx"
+sheet = "data"
+ignore_headers = ["password", "Country"]
+ignore_sheets = ["TestStepExecution"]
+
+with open(Path(input_directory).joinpath(original_file), "rb") as file:
+    data = file.read()
+
+with open(Path(input_directory).joinpath(input_file), "wb") as file:
+    file.write(data)
+
+
+def update_source(path):
+    wb = load_workbook(path)
+    sht = wb.get_sheet_by_name(sheet)
+    for row in range(1, sht.max_row+1):
+        sht.cell(row, 9).value = None
+    sht.cell(1, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
+    sht.cell(2, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
+    sht.cell(5, 9).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
+    sht.cell(8, 3).value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
+    wb.save(path)
+
+
+def check_source(path, ignore=False):
+    wb = load_workbook(path)
+    sht = wb.get_sheet_by_name("Change Logs")
+    if not ignore:
+        assert int(sht.cell(2, 4).value) == 1
+        assert int(sht.cell(3, 4).value) == 1
+        assert int(sht.cell(4, 4).value) == 8
+        assert sht.max_row == 4
+    else:
+        assert int(sht.cell(2, 4).value) == 1
+        assert int(sht.cell(3, 4).value) == 8
+        assert sht.max_row == 3
+
+
+def test_check_source_file():
+    try:
+        CloneXls.CloneXls("tests/TestInput/CompleteBaangtWebdemo1.xlsx")
+        assert 1 == 0
+    except BaseException:
+        assert 1 == 1
+
+
+def test_update_or_make_clone():
+    output = str(Path(input_directory).joinpath(output_file))
+    input_ = str(Path(input_directory).joinpath(input_file))
+    if os.path.exists(output):
+        os.remove(output)
+    cloneXls = CloneXls.CloneXls(input_)
+    cloneXls.update_or_make_clone()
+    assert os.path.exists(output)
+
+
+def test_update_or_make_clone_prefix():
+    output = str(Path(input_directory).joinpath(output_file_2))
+    input = str(Path(input_directory).joinpath(input_file))
+    if os.path.exists(output):
+        os.remove(output)
+    cloneXls = CloneXls.CloneXls(input, new_prefix)
+    cloneXls.update_or_make_clone()
+    assert os.path.exists(output) and new_prefix in output
+    os.remove(output)
+
+
+def test_ChangeLog_class():
+    output = str(Path(input_directory).joinpath(output_file))
+    input_ = str(Path(input_directory).joinpath(input_file))
+    changeLog = ChangeLog(input_, output)
+    assert changeLog.ignore_sheets == ['change logs'] and changeLog.ignore_headers ==[]
+
+
+def test_ChangeLog_not_case_sensitive():
+    output = str(Path(input_directory).joinpath(output_file))
+    input_ = str(Path(input_directory).joinpath(input_file))
+    changeLog = ChangeLog(
+        input_, output, ignore_headers=ignore_headers, ignore_sheets=ignore_sheets
+    )
+    assert changeLog.ignore_headers[0].islower() and changeLog.ignore_headers[1].islower() and \
+           changeLog.ignore_sheets[0].islower()
+    update_source(input_)
+    changeLog.xlsxChangeLog()
+    check_source(output, ignore=True)
+
+
+def test_ChangeLog_case_sensitive():
+    output = str(Path(input_directory).joinpath(output_file))
+    input_ = str(Path(input_directory).joinpath(input_file))
+    os.remove(output)
+    cloneXls = CloneXls.CloneXls(input_)
+    cloneXls.update_or_make_clone()
+    changeLog = ChangeLog(
+        input_, output, ignore_headers=ignore_headers, ignore_sheets=ignore_sheets, case_sensitive_ignore=True
+    )
+    assert changeLog.ignore_headers[0].islower() and not changeLog.ignore_headers[1].islower() and \
+           not changeLog.ignore_sheets[0].islower()
+    update_source(input_)
+    changeLog.xlsxChangeLog()
+    changeLog.xlsxChangeLog()
+    check_source(output)
+    os.remove(input_)
+    os.remove(output)
+