I have the following project structure
repo
|
|--utils
|----hudi.py
|----__init__.py
|--tests
|----test_hudi.py
|----__init__.py
I want to test the class in hudi.py
, which is:
class Partitions:
def __init__(self, bucket, table)
self.bucket = bucket
self.table = table
def get_partitions(self):
return [
f"s3://{self.bucket}/{self.table}{i}"
for i in list(range(10))
if self.path_exists(i)
]
def path_exists(self, i):
s3_paths = code using external libraries
if s3_paths:
return True
else:
return False
I have written the code for testing in the file test_hudi.py
as follow:
from utils import hudi
class TestHudi(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.hudi_partitions = hudi.Partitions(
table="signal",
bucket="bkt_name",
)
def test_get_partitions(self):
p = self.hudi_partitions.get_partitions()
assert p = ["s3://bkt_name/signal1"]
For being able to execute the test as above, I need to mock the function path_exist
, and make its returned value true, so I have tried to mock it with the following patches:
@mock.patch("utils.hudi.path_exists", return_value=True)
@mock.patch("utils.hudi.partitions.path_exists", return_value=True)
@mock.patch("hudi_partitions.path_exists", return_value=True)
@mock.patch("hudi.partitions.path_exists", return_value=True)
@mock.patch("hudi.path_exists", return_value=True)
And none of them work.
Is there any way I can mock the function path_exists? Thanks!