"""This module holds forms tests for the ``create_or_update_fixtures`` command."""

import hashlib
import os
import shutil

from django.conf import settings
from django.test import override_settings

from mtp.apps.common.tests.test_cases import TestCase

from ..create_or_update_fixtures import update_default_media

class CreateOrUpdateFixturesTestCase(TestCase):
    """Test case that tests the ``create_or_update_fixtures`` command."""

    # To be sure that MEDIA_ROOT is different than /mtp/media in repository
    @override_settings(MEDIA_ROOT=os.path.join(settings.PROJECT_ROOT, 'media_root'))
    def test_update_default_media(self):
        """Test ``update_default_media`` method"""

        # we clean the path of media
        shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)

        src_media_path = os.path.join(settings.PROJECT_ROOT, 'media')
        os.makedirs(src_media_path, exist_ok=True)

        # we create a file we want to udpate
        default_file_relative_path = 'default/default_file_name.png'
        default_file_full_path = os.path.join(src_media_path, default_file_relative_path)
        open(default_file_full_path, 'w').close()

        # Path of media file
        dest_file_full_path = os.path.join(
            settings.MEDIA_ROOT,
            default_file_relative_path
        )

        # The media file doesn't exist in media_root for the moment
        self.assertEqual(
            os.path.exists(dest_file_full_path),
            False
        )

        # Then we update file
        update_default_media(default_file_relative_path)

        # The media file now exist in media_root
        self.assertEqual(
            os.path.exists(dest_file_full_path),
            True
        )

        # We can update file even if it doesn't change, without error
        update_default_media(default_file_relative_path)

        # Now we change the source file
        with open(default_file_full_path, 'w') as default_file:
            default_file.write('New content')

        # and we calculate the hash of the source file
        with open(default_file_full_path, 'rb') as default_file:
            default_file_hash = hashlib.md5(default_file.read()).hexdigest()

        # and finally we calculate the hash of the dest file
        with open(dest_file_full_path, 'rb') as dest_file:
            dest_file_hash = hashlib.md5(dest_file.read()).hexdigest()

        # so now the file should be different
        self.assertNotEqual(default_file_hash, dest_file_hash)

        # then we update file
        update_default_media(default_file_relative_path)

        # We re calculate the hash of the dest file
        with open(dest_file_full_path, 'rb') as dest_file:
            dest_file_hash = hashlib.md5(dest_file.read()).hexdigest()

        # And now the file should be equal
        self.assertEqual(default_file_hash, dest_file_hash)

        # We remove default file source
        os.remove(default_file_full_path)

        # we clean the path of media
        shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)

