Answer the question
In order to leave comments, you need to log in
How to lock a function in a specific place?
The essence of the problem is this
. I have a test that should check the correct operation of the function that creates the path for the file
Test code and function code
@mock.patch('uuid.uuid4', return_value='name_file.png')
def test_path_to_restaurant_image(self, fake_func):
"""
Checking that the path for the restaurant pictures is formed correctly
"""
restaurant = RestaurantFactory()
expected_value = restaurant_image_upload_handler(
restaurant, self.file_name,
)
result = os.path.join(
'restaurant',
'images',
self.file_name,
)
self.assertEqual(result, expected_value)
def restaurant_image_upload_handler(instance, filename):
unique_id = str(uuid.uuid4())
ext = filename.split('.')[-1]
return os.path.join(
'restaurant',
'images',
f'{unique_id}.{ext}'.lower(),
)
Answer the question
In order to leave comments, you need to log in
In the restaurant_image_upload_handler function, you can use the function to get the unique_id
def get_unique_id():
return str(uuid.uuid4())
...
unique_id = get_unique_id()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question