使用 Python 测试 Parquet

Test Parquet with Python

我正在尝试模拟 parquet 并断言它是使用正确的路径调用的,但无法正确模拟它。我怎样才能将 option 函数模拟为 return 我一个模拟的 parquet?

待测代码

def read_from_s3(spark, path):
    return spark.read.option('mergeSchema', 'true').parquet(path)

测试

import unittest
import mock
from src.read_from_s3 import read_from_s3


class TestReadFromS3(unittest.TestCase):
    def test_read_from_s3__called_with_correct_params(self):
        spark = mock.MagicMock()
        spark.read.option = mock.MagicMock()
        spark.read.option.parquet = mock.MagicMock()
        path = 'my_path'

        read_from_s3(spark, path)

        spark.read.option.assert_called_with('mergeSchema', 'true') # this passes
        spark.read.option.parquet.assert_called_with(path) # this fails

测试失败

AssertionError: Expected 'parquet' to have been called once. Called 0 times.

parquet 不是 spark.read.option 的属性;它是 optionreturn 值 的属性。此外,您不需要显式创建模拟;模拟上的属性查找 return 也是模拟。

def test_read_from_s3__called_with_correct_params(self):
    spark = mock.MagicMock()
    read_from_s3(spark, path)
    spark.read.option.assert_called_with('mergeSchema', 'true')
    spark.read.option.return_value.parquet.assert_called_with(path)