|
| 1 | +import logging |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pyspark.sql import functions as F |
| 5 | +from pyspark.sql.types import StructType, StructField, IntegerType, StringType, FloatType |
| 6 | + |
| 7 | +#import dbldatagen as dg |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="class") |
| 11 | +def setupSpark(): |
| 12 | + import dbldatagen as dg |
| 13 | + sparkSession = dg.SparkSingleton.getLocalInstance("unit tests") |
| 14 | + return sparkSession |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="class") |
| 18 | +def setupLogging(): |
| 19 | + #FORMAT = '%(asctime)-15s %(message)s' |
| 20 | + #logging.basicConfig(format=FORMAT) |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class TestLoggingOperation: |
| 25 | + testDataSpec = None |
| 26 | + dfTestData = None |
| 27 | + SMALL_ROW_COUNT = 100000 |
| 28 | + TINY_ROW_COUNT = 1000 |
| 29 | + column_count = 10 |
| 30 | + row_count = SMALL_ROW_COUNT |
| 31 | + |
| 32 | + def setup_log_capture(self, caplog_object): |
| 33 | + """ set up log capture fixture |
| 34 | +
|
| 35 | + Sets up log capture fixture to only capture messages after setup and only |
| 36 | + capture warnings and errors |
| 37 | +
|
| 38 | + """ |
| 39 | + caplog_object.set_level(logging.INFO) |
| 40 | + |
| 41 | + # clear messages from setup |
| 42 | + caplog_object.clear() |
| 43 | + |
| 44 | + def get_log_capture_warnings_and_errors(self, caplog_object, textFlag): |
| 45 | + """ |
| 46 | + gets count of errors containing specified text |
| 47 | +
|
| 48 | + :param caplog_object: log capture object from fixture |
| 49 | + :param textFlag: text to search for to include error or warning in count |
| 50 | + :return: count of errors containg text specified in `textFlag` |
| 51 | + """ |
| 52 | + flagged_text_warnings_and_errors = 0 |
| 53 | + for r in caplog_object.records: |
| 54 | + if (r.levelname == "WARNING" or r.levelname == "ERROR") and textFlag in r.message: |
| 55 | + flagged_text_warnings_and_errors += 1 |
| 56 | + |
| 57 | + return flagged_text_warnings_and_errors |
| 58 | + |
| 59 | + def get_log_capture_info(self, caplog_object, textFlag): |
| 60 | + """ |
| 61 | + gets count of errors containing specified text |
| 62 | +
|
| 63 | + :param caplog_object: log capture object from fixture |
| 64 | + :param textFlag: text to search for to include error or warning in count |
| 65 | + :return: count of errors containg text specified in `textFlag` |
| 66 | + """ |
| 67 | + flagged_text_info = 0 |
| 68 | + for r in caplog_object.records: |
| 69 | + if (r.levelname == "INFO") and textFlag in r.message: |
| 70 | + flagged_text_info += 1 |
| 71 | + |
| 72 | + return flagged_text_info |
| 73 | + |
| 74 | + |
| 75 | + def test_logging_operation(self, caplog): |
| 76 | + # caplog fixture captures log content |
| 77 | + self.setup_log_capture(caplog) |
| 78 | + |
| 79 | + date_format = "%Y-%m-%d %H:%M:%S" |
| 80 | + log_format = "[%(name)s]%(asctime)s %(levelname)-8s [%(module)s][%(funcName)s] TESTING1 %(message)s" |
| 81 | + formatter = logging.Formatter(log_format, date_format) |
| 82 | + handler = logging.StreamHandler() |
| 83 | + handler.setFormatter(formatter) |
| 84 | + logger = logging.getLogger(__name__) |
| 85 | + logger.setLevel(level=logging.INFO) |
| 86 | + logger.addHandler(handler) |
| 87 | + |
| 88 | + logger.warning("Info message 1") |
| 89 | + |
| 90 | + # Prints: 2023-03-08 14:18:59 INFO Info message |
| 91 | + |
| 92 | + from dbldatagen import DataGenerator |
| 93 | + |
| 94 | + logger.info("Info message 2") |
| 95 | + |
| 96 | + for h in logger.handlers: |
| 97 | + h.flush() |
| 98 | + |
| 99 | + message1_count = self.get_log_capture_warnings_and_errors(caplog, "Info message 1") |
| 100 | + assert message1_count == 1, "Should only have 1 message 1" |
| 101 | + |
| 102 | + message2_count = self.get_log_capture_info(caplog, "Info message 2") |
| 103 | + assert message2_count == 1, "Should only have 1 message 2" |
| 104 | + |
| 105 | + |
| 106 | + def test_logging_operation2(self, setupSpark, caplog): |
| 107 | + self.setup_log_capture(caplog) |
| 108 | + |
| 109 | + spark=setupSpark |
| 110 | + |
| 111 | + date_format = "%Y-%m-%d %H:%M:%S" |
| 112 | + log_format = "%(asctime)s %(levelname)-8s TESTING2 %(message)s" |
| 113 | + formatter1 = logging.Formatter(log_format, date_format) |
| 114 | + handler1 = logging.StreamHandler() |
| 115 | + handler1.setFormatter(formatter1) |
| 116 | + logger2 = logging.getLogger("test1") |
| 117 | + logger2.setLevel(level=logging.INFO) |
| 118 | + logger2.addHandler(handler1) |
| 119 | + |
| 120 | + logger2.warning("Info message 1") |
| 121 | + # Prints: 2023-03-08 14:18:59 INFO Info message |
| 122 | + |
| 123 | + from dbldatagen import DataGenerator |
| 124 | + |
| 125 | + spec = (DataGenerator(sparkSession=spark, name="test_data_set1", rows=10000, seedMethod='hash_fieldname') |
| 126 | + .withIdOutput() |
| 127 | + .withColumn("r", "float", expr="floor(rand() * 350) * (86400 + 3600)", |
| 128 | + numColumns=10) |
| 129 | + .withColumn("code1", "int", min=100, max=200) |
| 130 | + .withColumn("code2", "int", min=0, max=10) |
| 131 | + .withColumn("code3", "string", values=['a', 'b', 'c']) |
| 132 | + .withColumn("code4", "string", values=['a', 'b', 'c'], random=True) |
| 133 | + .withColumn("code5", "string", values=['a', 'b', 'c'], random=True, weights=[9, 1, 1]) |
| 134 | + ) |
| 135 | + |
| 136 | + df = spec.build() |
| 137 | + |
| 138 | + |
| 139 | + # Prints: INFO: Version : VersionInfo(major='0', minor='3', patch='1', release='', build='') |
| 140 | + logger2.info("Info message 2") |
| 141 | + |
| 142 | + for h in logger2.handlers: |
| 143 | + h.flush() |
| 144 | + |
| 145 | + message1_count = self.get_log_capture_warnings_and_errors(caplog, "Info message 1") |
| 146 | + assert message1_count == 1, "Should only have 1 message 1" |
| 147 | + |
| 148 | + message2_count = self.get_log_capture_info(caplog, "Info message 2") |
| 149 | + assert message2_count == 1, "Should only have 1 message 2" |
0 commit comments