<commit_before>from unittest import TestCase
from mock import mock

from exifread import IfdTag

from pictures.rename import rename
from tests import helpers


def ifd_tag_from(date_time_original):
    return IfdTag(None, None, None, date_time_original, None, None)


class MockFile(object):
    def __init__(self, filename, mode):
        self.filename = filename
        self.mode = mode

    def __enter__(self):
        return self

    def __exit__(self, *args):
        pass


def create_mock_process_file(files):
    return lambda f_mock: files[f_mock.filename]


def create_mock_isfile(files):
    return lambda f: f in files


def create_mock_join():
    return lambda path, *paths: r'{}\{}'.format(path, '\\'.join(paths))


class TestRename(TestCase):
    FILES = {
        r'C:\dir\no_exif_tags.jpeg': {},

        r'C:\dir\timestamp_does_not_exist.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:10:29 15:43:56')},  # 1 check

        r'C:\dir\timestamp_does_exist.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:02:04 12:03:35')},  # 2 checks
        r'C:\dir\20160204_120335.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:02:04 12:03:35')},

        r'C:\dir\timestamp_does_exist_multiple.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},  # 4 checks
        r'C:\dir\20170103_142345.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},
        r'C:\dir\20170103_142345_1.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},
        r'C:\dir\20170103_142345_2.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')}
    }

    @mock.patch('os.rename')
    @mock.patch('os.path.isfile', side_effect=create_mock_isfile(FILES))
    @mock.patch('os.path.join', side_effect=create_mock_join())
    @mock.patch('exifread.process_file', side_effect=create_mock_process_file(FILES))
    @mock.patch('builtins.open', side_effect=MockFile)
    def test_rename(self, mock_open, mock_process_file, mock_join, mock_isfile, mock_rename):
        rename(self.FILES)

        self.assertEquals(mock_open.mock_calls, helpers.calls_from(zip(self.FILES.keys(), ['rb'] * len(self.FILES))))
        self.assertEquals(mock_process_file.call_count, len(self.FILES))
        self.assertEquals(mock_join.call_count, 7)  # number of checks
        self.assertEquals(mock_isfile.call_count, 7)  # number of checks
        self.assertEquals(mock_rename.mock_calls, helpers.calls_from([
            (r'C:\dir\timestamp_does_not_exist.jpeg', r'C:\dir\20161029_154356.jpeg'),
            (r'C:\dir\timestamp_does_exist.jpeg', r'C:\dir\20160204_120335_1.jpeg'),
            (r'C:\dir\timestamp_does_exist_multiple.jpeg', r'C:\dir\20170103_142345_3.jpeg')
        ]))<commit_msg>Testfix: Replace os.path with ntpath in tests<commit_after>import ntpath
from unittest import TestCase

from exifread import IfdTag
from mock import mock

from pictures.rename import rename
from tests import helpers


def ifd_tag_from(date_time_original):
    return IfdTag(None, None, None, date_time_original, None, None)


class MockFile(object):
    def __init__(self, filename, mode):
        self.filename = filename
        self.mode = mode

    def __enter__(self):
        return self

    def __exit__(self, *args):
        pass


def create_mock_process_file(files):
    return lambda f_mock: files[f_mock.filename]


def create_mock_isfile(files):
    return lambda f: f in files


class TestRename(TestCase):
    FILES = {
        r'C:\dir\no_exif_tags.jpeg': {},

        r'C:\dir\timestamp_does_not_exist.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:10:29 15:43:56')},  # 1 check

        r'C:\dir\timestamp_does_exist.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:02:04 12:03:35')},  # 2 checks
        r'C:\dir\20160204_120335.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2016:02:04 12:03:35')},

        r'C:\dir\timestamp_does_exist_multiple.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},  # 4 checks
        r'C:\dir\20170103_142345.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},
        r'C:\dir\20170103_142345_1.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')},
        r'C:\dir\20170103_142345_2.jpeg': {'EXIF DateTimeOriginal': ifd_tag_from('2017:01:03 14:23:45')}
    }

    @mock.patch('os.rename')
    @mock.patch('exifread.process_file', side_effect=create_mock_process_file(FILES))
    @mock.patch('builtins.open', side_effect=MockFile)
    @mock.patch('os.path.isfile', create_mock_isfile(FILES))
    @mock.patch('os.path', ntpath)
    def test_rename(self, mock_open, mock_process_file, mock_rename):
        rename(self.FILES)

        self.assertEquals(mock_open.mock_calls, helpers.calls_from(zip(self.FILES.keys(), ['rb'] * len(self.FILES))))
        self.assertEquals(mock_process_file.call_count, len(self.FILES))
        self.assertEquals(sorted(mock_rename.mock_calls), sorted(helpers.calls_from([
            (r'C:\dir\timestamp_does_not_exist.jpeg', r'C:\dir\20161029_154356.jpeg'),
            (r'C:\dir\timestamp_does_exist.jpeg', r'C:\dir\20160204_120335_1.jpeg'),
            (r'C:\dir\timestamp_does_exist_multiple.jpeg', r'C:\dir\20170103_142345_3.jpeg')
        ])))
{'fraction_non_alphanumeric': 0.14400152584398246, 'fraction_numerical': 0.07781804310509251, 'mean_word_length': 4.265060240963855, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2601930', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1856, 'n_words': 298}