|
| 1 | +import logging |
| 2 | +from unittest.mock import Mock, PropertyMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +import dbldatagen as dg |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="class") |
| 9 | +def setupLogging(): |
| 10 | + FORMAT = '%(asctime)-15s %(message)s' |
| 11 | + logging.basicConfig(format=FORMAT) |
| 12 | + |
| 13 | + |
| 14 | +class TestSharedEnv: |
| 15 | + """Tests to simulate testing under a Unity Catalog shared environment. In a Unity Catalog shared environment with |
| 16 | + the 14.x versions of the Databricks runtime, the sparkSession object does not support use of the sparkContext |
| 17 | + attribute to get the default parallelism. In this case, we want to catch errors and return a default of |
| 18 | + 200 as the default number of partitions. This is the same as the default parallelism in many versions of Spark. |
| 19 | +
|
| 20 | +
|
| 21 | + """ |
| 22 | + SMALL_ROW_COUNT = 100000 |
| 23 | + COLUMN_COUNT = 10 |
| 24 | + |
| 25 | + @pytest.fixture(scope="class") |
| 26 | + def sparkSession(self, setupLogging): |
| 27 | + spark = dg.SparkSingleton.getLocalInstance("unit tests") |
| 28 | + return spark |
| 29 | + |
| 30 | + @pytest.fixture(scope="class") |
| 31 | + def sharedSparkSession(self, setupLogging): |
| 32 | + spark = Mock(wraps=dg.SparkSingleton.getLocalInstance("unit tests")) |
| 33 | + del spark.sparkContext |
| 34 | + return spark |
| 35 | + |
| 36 | + @pytest.fixture(scope="class") |
| 37 | + def sparkSessionNullContext(self, setupLogging): |
| 38 | + |
| 39 | + class MockSparkSession: |
| 40 | + def __init__(self): |
| 41 | + self.sparkContext = None |
| 42 | + |
| 43 | + spark = MockSparkSession() |
| 44 | + return spark |
| 45 | + |
| 46 | + def test_getDefaultParallelism(self, sparkSession): |
| 47 | + """Test that the default parallelism is returned when the sparkSession object supports use of the |
| 48 | + sparkContext attribute to get the default parallelism. |
| 49 | +
|
| 50 | + :param sparkSession: The sparkSession object to use for the test. |
| 51 | + """ |
| 52 | + defaultParallelism = dg.DataGenerator._getDefaultSparkParallelism(sparkSession) |
| 53 | + assert defaultParallelism == sparkSession.sparkContext.defaultParallelism |
| 54 | + |
| 55 | + def test_getSharedDefaultParallelism(self, sharedSparkSession): |
| 56 | + """Test that the default parallelism is returned when the sparkSession object supports use of the |
| 57 | + sparkContext attribute to get the default parallelism, but that a constant is return when the `sparkContext` |
| 58 | + attribute is not available. |
| 59 | + """ |
| 60 | + defaultParallelism = dg.DataGenerator._getDefaultSparkParallelism(sharedSparkSession) |
| 61 | + assert defaultParallelism == dg.SPARK_DEFAULT_PARALLELISM |
| 62 | + |
| 63 | + def test_getNullContextDefaultParallelism(self, sparkSessionNullContext): |
| 64 | + """Test that the default parallelism is returned when the sparkSession object supports use of the |
| 65 | + sparkContext attribute to get the default parallelism. |
| 66 | +
|
| 67 | + :param sparkSession: The sparkSession object to use for the test. |
| 68 | + """ |
| 69 | + defaultParallelism = dg.DataGenerator._getDefaultSparkParallelism(sparkSessionNullContext) |
| 70 | + assert defaultParallelism == dg.SPARK_DEFAULT_PARALLELISM |
| 71 | + |
| 72 | + def test_mocked_shared_session1(self, sharedSparkSession): |
| 73 | + # validate that accessing the sparkContext on the shared spark session raises an exception |
| 74 | + with pytest.raises(Exception) as excinfo: |
| 75 | + context = sharedSparkSession.sparkContext |
| 76 | + |
| 77 | + assert "sparkContext" in str(excinfo.value) |
| 78 | + |
| 79 | + def test_null_context_spark_session(self, sparkSessionNullContext): |
| 80 | + # validate that accessing the sparkContext on the shared spark session raises an exception |
| 81 | + context = sparkSessionNullContext.sparkContext |
| 82 | + assert context is None |
0 commit comments