test_TestDataGenerator.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from baangt.TestDataGenerator.TestDataGenerator import TestDataGenerator
  2. import xlrd3 as xlrd
  3. import os
  4. import csv
  5. from pathlib import Path
  6. import pytest
  7. import baangt.base.GlobalConstants as GC
  8. from xlsxwriter.exceptions import FileCreateError
  9. ####
  10. # DO NOT CHANGE THE PATHS below. Instead execute PYTEST from baangt root directory and not from /tests
  11. ####pi
  12. # Test file path
  13. rawTestFile = "tests/0TestInput/RawTestData.xlsx"
  14. # Create an instance of TestDataGenerator object with sample input file
  15. testDataGenerator = TestDataGenerator(rawTestFile)
  16. Path(os.getcwd()).joinpath("Tests").joinpath("1TestResults").mkdir(parents=True, exist_ok=True)
  17. testOutput100xls = str(Path(os.getcwd()).joinpath("Tests").joinpath("1TestResults").joinpath("output100.xlsx"))
  18. testOutput300csv = str(Path(os.getcwd()).joinpath("Tests").joinpath("1TestResults").joinpath("output300.csv"))
  19. testOutputFullxls = str(Path(os.getcwd()).joinpath("Tests").joinpath("1TestResults").joinpath("outputFull.xlsx"))
  20. testOutputFullcsv = str(Path(os.getcwd()).joinpath("Tests").joinpath("1TestResults").joinpath("outputFull.csv"))
  21. def removeFile(file):
  22. try:
  23. os.remove(file)
  24. except Exception as e:
  25. pass
  26. def test_write_excel_100():
  27. # Tests write method of TestDataGenerator object with 100 random data in excel.
  28. removeFile(testOutput100xls)
  29. testDataGenerator.write(batch_size=100, outputfile=testOutput100xls)
  30. xl_book = xlrd.open_workbook(testOutput100xls)
  31. xl_sheet = xl_book.sheet_by_index(0)
  32. assert xl_sheet.nrows == 101
  33. print("Test 100 random data successful.")
  34. def test_write_csv_300():
  35. # Tests write method of TestDataGenerator object with 300 random data in csv.
  36. removeFile(testOutput300csv)
  37. testDataGenerator.write(OutputFormat="csv", batch_size=250, outputfile=testOutput300csv)
  38. data = []
  39. with open(testOutput300csv, 'r', encoding='utf-8-sig') as raw_file:
  40. raw = csv.reader(raw_file)
  41. for row in raw:
  42. data.append(row)
  43. assert len(data) == 251
  44. print("Test 300 random data successful.")
  45. def test_write_excel_all():
  46. removeFile(testOutputFullxls)
  47. # Takes too much time!!!!!
  48. # Tests write_excel method of TestDataGenerator object with all data.
  49. testDataGenerator.write(outputfile=testOutputFullxls)
  50. xl_book = xlrd.open_workbook(testOutputFullxls)
  51. xl_sheet = xl_book.sheet_by_index(0)
  52. print(f"Total data in excel file = {str(xl_sheet.nrows)}")
  53. def test_write_csv_all():
  54. # Tests write_csv method of TestDataGenerator
  55. removeFile(testOutputFullcsv)
  56. testDataGenerator.write(OutputFormat="csv", outputfile=testOutputFullcsv)
  57. data = []
  58. with open(testOutputFullcsv, 'r', encoding='utf-8-sig') as raw_file:
  59. raw = csv.reader(raw_file)
  60. for row in raw:
  61. data.append(row)
  62. print(f"Total data in excel file = {str(len(data))}")
  63. def test_write_to_wrong_Path():
  64. with pytest.raises(FileCreateError):
  65. testDataGenerator.write(outputfile="/franzi/fritzi/hansi.xlsx", batch_size=100)
  66. def test_rrd_simple_input():
  67. # Checks __processRrd to get dict to target data
  68. rrd_output_dict = testDataGenerator._TestDataGenerator__processRrdRre(
  69. 'Customers', 'Customer', {'Age group': ['30s', '40s'], 'Employment_status': ['employed']}
  70. )
  71. assert len(rrd_output_dict) > 0
  72. def test_rrd_target_data_all():
  73. # Checks __processRrd to get dict to for all data of matching values
  74. rrd_output_dict = testDataGenerator._TestDataGenerator__processRrdRre(
  75. 'Customers', '*', {'Age group': ['30s', '40s'], 'Employment_status': ['employed']}
  76. )
  77. assert len(rrd_output_dict) > 0
  78. def test_rrd_no_data_to_match():
  79. # Checks __processRrd to get dict to for all data of when no value of matching is given
  80. rrd_output_dict = testDataGenerator._TestDataGenerator__processRrdRre('PaymentTerms', '*', {})
  81. assert len(rrd_output_dict) > 0
  82. def test_rre_simple_input():
  83. # Checks __processRrd to get dict to target data
  84. rrd_output_dict = testDataGenerator.data_generators(
  85. "RRE_[../../examples/CompleteBaangtWebdemo.xlsx,CustomerData,[NameFirst,NameLast],[Stage:[Test]]]"
  86. )
  87. assert len(rrd_output_dict.dataList) == 5
  88. def test_rre_target_data_all():
  89. # Checks __processRrd to get dict to for all data of matching values
  90. rrd_output_dict = testDataGenerator.data_generators(
  91. "RRE_[../../examples/CompleteBaangtWebdemo.xlsx,CustomerData,*,[Stage:[Test]]]"
  92. )
  93. assert len(rrd_output_dict.dataList) == 5
  94. def test_rre_no_data_to_match():
  95. # Checks __processRrd to get dict to for all data of when no value of matching is given
  96. rrd_output_dict = testDataGenerator.data_generators(
  97. "RRE_[../../examples/CompleteBaangtWebdemo.xlsx,CustomerData,*,[]"
  98. )
  99. assert len(rrd_output_dict.dataList) == 10
  100. def test_renv():
  101. data = TestDataGenerator.get_env_variable("(USERNAME, test)")
  102. assert data
  103. def test_renv_without_default():
  104. with pytest.raises(BaseException):
  105. TestDataGenerator.get_env_variable("(URNAMEfh)")