123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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) # creating a duplicate file as we will update the source file in the run for test to update change log
- def update_source(path): # update the file which is used to check the changelog
- 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, case_sensitive=False):
- # checks the change log sheet that if their is value added and verifies it by row number in which we have changed data
- wb = load_workbook(path)
- sht = wb.get_sheet_by_name("Change Logs")
- if case_sensitive: # if case_sensitive test is done these 3 changes will the only ones
- 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: # if case_sensitive test is done these 2 changes will the only ones
- 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():
- # Give a fake file path so it must throw a BaseException
- try:
- CloneXls.CloneXls("tests/TestInput/CompleteBaangtWebdemo_Fake.xlsx")
- assert 1 == 0
- except BaseException:
- assert 1 == 1
- def test_update_or_make_clone():
- # Checking update_or_make_clone method of CloneXls class, that will it make a clone file if their is none
- 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():
- # testing the change prefix parameter
- 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():
- # creating a ChangeLog class object and verifies that if it is build successfully
- 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():
- # Testing ChangeLog class with ignore lists treated as non 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)
- def test_ChangeLog_case_sensitive():
- # Testing ChangeLog class with ignore lists treated as 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, case_sensitive=True)
- os.remove(input_)
- os.remove(output)
|