0

I have created test case for generating the image path and added the function in the models for generating the image path

I got the following error AssertionError: 'uploads\recipe\test-uuid.jpg' != 'uploads/recipe/test-uuid.jpg'

  • uploads\recipe\test-uuid.jpg ? ^ ^
  • uploads/recipe/test-uuid.jpg ? ^ ^
@patch('core.models.uuid.uuid4')

    def test_recipe_file_name_uuid(self,mock_uuid):
        '''test generating image path'''

        uuid="test-uuid"
        mock_uuid.return_value=uuid
        file_path=models.recipe_image_file_path(None,"example.jpg")
        self.assertEqual(file_path,f'uploads/recipe/{uuid}.jpg')

and the recipe_image_file_path in the models file is as follows

def recipe_image_file_path(instance,filename):
    '''generating a file path for the new recipe image'''

    ext=os.path.splitext(filename)[1]
    filename=f'{uuid.uuid4()}{ext}'

    return os.path.join('uploads','recipe',filename)

When I run test case I want to match the assertEqual(file_path,f'uploads/recipe/{uuid}.jpg')

  • use from pathlib import Path, instead of os.path, https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f – Jisson May 24 '23 at 05:51
  • Pathlib is the correct answer - @Jisson should add this as a (slightly more detailed than the comment) answer so it can be accepted. Don't try and work out the system and the seperators as the other answer says ... – michjnich May 24 '23 at 06:44

1 Answers1

0

Looks like you are running code in Windows. Windows and Linux has different path separators

  • Windows: \
  • Linux: /

To be able run your code on both OSes you have to change f'uploads/recipe/{uuid}.jpg' part to

path_to_compare = os.path.join('uploads', 'recipe', f'{uuid}.jpg')

or

path_to_compare = f'uploads{os.sep}recipe{os.sep}{uuid}.jpg'

or

from pathlib import Path
path_to_compare = str(Path.joinpath(Path('uploads'), 'recipe', f'{uuid}.jpg'))

And final assertion check should be looks like:

self.assertEqual(file_path, path_to_compare)

Or, if you want to use Path instead of strings you have to update return value and asssertion check like:

def recipe_image_file_path(instance,filename):
    ...
    return Path('uploads') / 'recipe' / filename

and

self.assertEqual(file_path, Path('uploads') / 'recipe' / f'{uuid}.jpg')
rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • Yeah, don't do this. Use Pathlib. – michjnich May 24 '23 at 06:45
  • @michjnich This answer is about `How to fix AssertionError` but not what is better `os` or `pathlib` – rzlvmp May 24 '23 at 06:57
  • @michjnich I added `pathlib` solution. But personally I can't see reason why `os` can't be used here or why `pathlib` is better **in this case** – rzlvmp May 24 '23 at 07:08
  • The assertion error exists because your path is not matching. If you set a path using pathlib it can be OS independant and you know it will be correct whatever OS you are on. Added benefit - easier to read than using `os.path.join` – michjnich May 24 '23 at 07:55
  • @michjnich `If you set a path using pathlib it can be OS independent` → all three variants in this answer are OS independent. – rzlvmp May 24 '23 at 08:07
  • If you really prefer `f'uploads{os.sep}recipe{os.sep}{uuid}.jpg'` to `"uploads" / "recipe" / f"{uuid}.jpg"` then go for it. I'm not trying to evangelise here, just stating what is the general recommendation these days, and what will make life easier for you. – michjnich May 24 '23 at 09:04
  • When I try pathlib I got the same path but it adds prefix of windows path and results in the error can you solve this for me AssertionError: WindowsPath('uploads/recipe/test-uuid.jpg') != 'uploads/recipe/test-uuid.jpg' – Narendra yadav May 24 '23 at 11:05
  • @Narendrayadav I added details in my answer. If you updated your code according my answer `AssertionError: WindowsPath('uploads/recipe/test-uuid.jpg') != 'uploads/recipe/test-uuid.jpg'` will never be happen. – rzlvmp May 24 '23 at 12:21