<commit_before>import os
import shutil
import unittest
import tempfile

from unittest.mock import patch, MagicMock, mock_open
from testfixtures import TempDirectory

os.environ['LICENSE-KEY'] = '1234556'
from ..geoip import match_sha256, write, download_file  # noqa: E402


class GeoipTestCases(unittest.TestCase):

    @patch('urllib.request.urlopen')
    def test_sha256_mismatch(self, mock_urlopen='http://foo'):
        mock_sha256 = MagicMock()
        mock_sha256.getcode.return_value = 200
        mock_sha256.read.return_value = '29cd163ffacb8fef0441ab3b7c246b64db4a7148362ed35ef59a5d64f' \
                                        'bbd7f5c'.encode('utf-8')
        mock_urlopen.return_value = mock_sha256

        temp_dir = TempDirectory()
        temp_dir.write('file.txt', 'testing sha256'.encode('utf-8'))

        file_path = os.path.join(temp_dir.path, 'file.txt')
        value = match_sha256(file_path, mock_urlopen)

        self.assertEqual(False, value)

        temp_dir.cleanup()

    @patch('urllib.request.urlopen')
    def test_sha256_match(self, mock_urlopen='http://foo'):
        mock_sha256 = MagicMock()
        mock_sha256.getcode.return_value = 200
        mock_sha256.read.return_value = '78553549e63a1ad55ff0af86d3c23c7e5e8b6146d09fab87009ec769a3f' \
                                        'f8fce'.encode('utf-8')
        mock_sha256.__enter__.return_value = mock_sha256
        mock_urlopen.return_value = mock_sha256

        temp_dir = TempDirectory()
        temp_dir.write('file.txt', 'testing sha256'.encode('utf-8'))

        file_path = os.path.join(temp_dir.path, 'file.txt')
        value = match_sha256(file_path, mock_urlopen)

        self.assertEqual(True, value)

        temp_dir.cleanup()

    def test_shutil_raise(self):
        out_file = tempfile.NamedTemporaryFile(delete=False)
        existing_file_as_string = 'invalid/path/file'
        self.assertRaises(FileNotFoundError, write, out_file, existing_file_as_string)

    def test_file_content(self):
        existing_file = tempfile.NamedTemporaryFile(delete=False)
        existing_file.close()
        out_file = tempfile.NamedTemporaryFile(delete=False)
        with out_file as f:
            f.write('testing'.encode('utf-8'))
            out_file.seek(0)
            write(f, existing_file.name)

        f.close()

        with open(existing_file.name) as ex:
            with open(out_file.name) as f:
                self.assertEqual(f.read(), ex.read())

    @patch('urllib.request.urlretrieve')
    @patch('urllib.request.urlopen')
    def test_raise_value_error(self, mock_urlretrieve, mock_urlopen):
        mock_urlretrieve.return_value = MagicMock()<commit_msg>Improve test name.<commit_after>import os
import shutil
import unittest
import tempfile

from unittest.mock import patch, MagicMock, mock_open
from testfixtures import TempDirectory

os.environ['LICENSE-KEY'] = '1234556'
from ..geoip import match_sha256, write, download_file  # noqa: E402


class GeoipTestCases(unittest.TestCase):

    @patch('urllib.request.urlopen')
    def test_sha256_mismatch(self, mock_urlopen='http://foo'):
        mock_sha256 = MagicMock()
        mock_sha256.getcode.return_value = 200
        mock_sha256.read.return_value = '29cd163ffacb8fef0441ab3b7c246b64db4a7148362ed35ef59a5d64f' \
                                        'bbd7f5c'.encode('utf-8')
        mock_urlopen.return_value = mock_sha256

        temp_dir = TempDirectory()
        temp_dir.write('file.txt', 'testing sha256'.encode('utf-8'))

        file_path = os.path.join(temp_dir.path, 'file.txt')
        value = match_sha256(file_path, mock_urlopen)

        self.assertEqual(False, value)

        temp_dir.cleanup()

    @patch('urllib.request.urlopen')
    def test_sha256_match(self, mock_urlopen='http://foo'):
        mock_sha256 = MagicMock()
        mock_sha256.getcode.return_value = 200
        mock_sha256.read.return_value = '78553549e63a1ad55ff0af86d3c23c7e5e8b6146d09fab87009ec769a3f' \
                                        'f8fce'.encode('utf-8')
        mock_sha256.__enter__.return_value = mock_sha256
        mock_urlopen.return_value = mock_sha256

        temp_dir = TempDirectory()
        temp_dir.write('file.txt', 'testing sha256'.encode('utf-8'))

        file_path = os.path.join(temp_dir.path, 'file.txt')
        value = match_sha256(file_path, mock_urlopen)

        self.assertEqual(True, value)

        temp_dir.cleanup()

    def test_file_not_found_raise(self):
        out_file = tempfile.NamedTemporaryFile(delete=False)
        existing_file_as_string = 'invalid/path/file'
        self.assertRaises(FileNotFoundError, write, out_file, existing_file_as_string)

    def test_file_content(self):
        existing_file = tempfile.NamedTemporaryFile(delete=False)
        existing_file.close()
        out_file = tempfile.NamedTemporaryFile(delete=False)
        with out_file as f:
            f.write('testing'.encode('utf-8'))
            out_file.seek(0)
            write(f, existing_file.name)

        f.close()

        with open(existing_file.name) as ex:
            with open(out_file.name) as f:
                self.assertEqual(f.read(), ex.read())

    @patch('urllib.request.urlretrieve')
    @patch('urllib.request.urlopen')
    def test_raise_value_error(self, mock_urlretrieve, mock_urlopen):
        mock_urlretrieve.return_value = MagicMock()
{'fraction_non_alphanumeric': 0.10488212363096343, 'fraction_numerical': 0.053833302394653795, 'mean_word_length': 2.9734513274336285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7375231', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1748, 'n_words': 261}