0

I have Python code that works locally and when run on Databricks. For saving the results, a different function is used depending on where to code is run.

On Databricks, several things are automatically initialized when running a notebook; among these is the pyspark library.

Therefore, in order to make my code also work locally, I import pyspark like so:

if "DATABRICKS_RUNTIME_VERSION" in os.environ:
    from pyspark.sql import functions as F

def save_results_to_databricks(...):
    # do stuff like F.col("relevant")

Now, when writing a test, I would patch F like so:

class TestSaveResultsToDatabricks(unittest.TestCase):
    @patch("path.to.module.F")
    def test_save_results_to_databricks(self, MockFunctions):
        # test stuff

However, this will throw an error:

AttributeError: does not have the attribute 'F'

So, how do I patch a function that is not available locally?

3
  • You could try skipping the test if the import is missing: stackoverflow.com/q/36377625/14536215 Commented Jan 15 at 10:00
  • can you post code that I can run to reproduce this locally? Tried importing with import pyspark.sql.functions instead of from pyspark.sql import functions as F ? It's kinda a catch 22. F isn't in the module being patched until import statement is executed, which happens only when the test runs, and mocking is done before tests start, hence the error you get. But pyspark.sql.functions should be available and patchable before tests are started. See the gotcha in case it's not clear. Commented Jan 15 at 21:01
  • Also, there is nothing stopping you from creating a local SparkSession that's used during unit tests. Just create a fixture and pass it around. @pytest.fixture(scope='session') def test_spark(): rerurn SparkSession.builder.getOrCreate() Commented Jan 15 at 21:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.