Beispiel #1
0
    def test_scan(self, walk_mock, shuffle_mock):
        """
        Test random pick

        :param MagicMock walk_mock   : mock for os.walk
        :param MagicMock shuffle_mock: mock for random.shuffle
        """
        walk_mock.return_value = [['', [], [
            'myphoto1.jpg',
            'myphoto2.jpg',
            'myphoto3.jpg',
            'myphoto4.jpg'
        ]]]

        shuffle_mock.side_effect = self._shuffle_mock_side_effect
        order_picked_mock = Mock()
        order_picked_mock.side_effect = self._order_picked_side_effect

        sut = RandomPicker('', 2)
        sut._order_picked = order_picked_mock
        sut.initialize()
        sut.scan()

        shuffle_mock.assert_called_once_with([
            PickerPhotoStub('myphoto3.jpg'),
            PickerPhotoStub('myphoto2.jpg'),
            PickerPhotoStub('myphoto4.jpg'),
            PickerPhotoStub('myphoto1.jpg')
        ])

        self.assertEqual(
            ['myphoto3.jpg', 'myphoto2.jpg'],
            sut.picked_file_paths
        )
Beispiel #2
0
    def test_initialize_multiple_and_excluded_paths(self, excluded_paths,
                                                    expected_files_to_scan,
                                                    walk_mock,
                                                    expanduser_mock):
        """
        Test initialize method with multiple and excluded paths

        :param list excluded_paths: excluded paths
        :param list expected_files_to_scan: expected files to scan
        :param MagicMock walk_mock: mock for walk function
        :param MagicMock expanduser_mock: mock for expanduser function
        """
        expanduser_mock.side_effect = self.expanduser_side_effect

        walk_mock.side_effect = [
            [['/mypath1', [], []],
             ['/mypath1/folder1', [], ['myphoto1.jpg', 'myphoto2.JPEG']]],
            [['/home/user/mypath2', [], ['myphoto3.png']],
             ['/home/user/mypath2/folder1', [], ['myphoto4.png']]]
        ]

        sut = DummyPicker(['/mypath1', '~/mypath2'], 20, 0, None,
                          excluded_paths)
        sut.initialize()

        walk_mock.assert_has_calls(
            [mock.call('/mypath1'),
             mock.call('/home/user/mypath2')])

        self.assertEqual(
            [PickerPhotoStub(filepath) for filepath in expected_files_to_scan],
            sut.files_to_scan)
Beispiel #3
0
    def _shuffle_mock_side_effect(self, files):
        """
        Side effect for shuffle mock

        :param list files: file list
        """

        self.assertEqual(files, [
            PickerPhotoStub('myphoto1.jpg'),
            PickerPhotoStub('myphoto2.jpg'),
            PickerPhotoStub('myphoto3.jpg'),
            PickerPhotoStub('myphoto4.jpg')
        ])

        del files[:]
        files.append(PickerPhoto('myphoto3.jpg'))
        files.append(PickerPhoto('myphoto2.jpg'))
        files.append(PickerPhoto('myphoto4.jpg'))
        files.append(PickerPhoto('myphoto1.jpg'))
Beispiel #4
0
    def _random_shuffle_side_effect(self, photos):
        """
        Side effect for random shuffle

        :param list photos: list of PickerPhotoStub objects
        """
        self.assertEqual([
            PickerPhotoStub('myfile1.jpg', '2018-04-07 23:32:21'),
            PickerPhotoStub('myfile2.jpg', '2018-05-05 18:32:21'),
            PickerPhotoStub('myfile3.jpg', '2018-05-05 12:32:21'),
            PickerPhotoStub('myfile4.jpg', '2018-05-03 15:32:21'),
        ], photos)

        photo1 = photos[0]
        photo2 = photos[1]
        photo3 = photos[2]
        photo4 = photos[3]
        del photos[:]
        photos.append(photo3)
        photos.append(photo1)
        photos.append(photo4)
        photos.append(photo2)
Beispiel #5
0
    def test_scan(self, order_parameter, expected_picked, shuffle_mock):
        """
        Test scan method

        :param MagicMock shuffle_mock: mock for shuffle method
        :param int order_parameter: parameter given to constructor for ordering
        :param list expected_picked: expected sorted list
        """
        internal_scan_mock = Mock()
        internal_scan_mock.return_value = [
            PickerPhotoStub('myfile1.jpg', '2018-04-07 23:32:21'),
            PickerPhotoStub('myfile2.jpg', '2018-05-05 18:32:21'),
            PickerPhotoStub('myfile3.jpg', '2018-05-05 12:32:21'),
            PickerPhotoStub('myfile4.jpg', '2018-05-03 15:32:21'),
        ]

        shuffle_mock.side_effect = self._random_shuffle_side_effect

        sut = DummyPicker('', 0, order_parameter)
        sut._scan = internal_scan_mock
        sut.scan()

        self.assertEqual(expected_picked, sut.picked_file_paths)
Beispiel #6
0
    def test_scan(self, photos_count, photos_to_retrieve, calls_data,
                  expected_indexes, shuffle_mock):
        """
        Test scan method

        :param int photos_count: photos count for the test
        :param int photos_to_retrieve: photos to retrieve count
        :param list calls_data: data for building expected shuffle call list
        :param list expected_indexes: indexes of the exoected picked photos
        :param MagicMock shuffle_mock: mock for random.shuffle
        """

        shuffle_mock.side_effect = self._shuffle_mock_side_effect

        sut = SmartPicker('', photos_to_retrieve)
        filenames_returned = []
        for i in range(1, photos_count + 1):
            filenames_returned.append(PickerPhoto(
                'myphoto{i}.jpg'.format(i=i)))

        build_method_mock = Mock()
        build_method_mock.return_value = filenames_returned
        sut._build_photos_to_select_list = build_method_mock
        sut.scan()

        i = 0
        call = []
        calls = []
        while len(calls_data):
            i += 1
            call.append(PickerPhotoStub('myphoto{i}.jpg'.format(i=i)))
            if i == calls_data[0]:
                calls.append(mock.call(call))
                call = []
                calls_data.pop(0)

        shuffle_mock.assert_has_calls(calls)

        expected_picked = []
        for i in expected_indexes:
            expected_picked.append('myphoto{i}.jpg'.format(i=i))

        self.assertEqual(expected_picked, sut.picked_file_paths)
Beispiel #7
0
    def test_initialize(self, patterns, expected_files_to_scan, walk_mock):
        """
        Test initialize method

        :param list|None patterns: patterns passed to the constructor
        :param list expected_files_to_scan: list that should be in
                                            the _files_to_scan property
        :param MagicMock walk_mock: mock for walk function
        """

        walk_mock.return_value = [[
            '', [], ['myphoto1.jpg', 'myphoto2.JPEG', 'myphoto3.png']
        ]]

        sut = DummyPicker('mypath', 20, patterns=patterns)
        sut.initialize()

        walk_mock.assert_called_with('mypath')
        self.assertEqual(
            [PickerPhotoStub(filepath) for filepath in expected_files_to_scan],
            sut.files_to_scan)
Beispiel #8
0
    def test_select(self, walk_mock, image_open_mock):
        """
        Test select

        :param MagicMock walk_mock: mock for walk method
        :param MagicMock image_open_mock: mock for PIL Image mock method
        """

        walk_mock.return_value = [[
            '', [],
            [
                'myphoto1.jpg', 'myphoto2.jpg', 'myphoto3.jpg', 'myphoto4.jpg',
                'myphoto5.jpg'
            ]
        ]]

        image_mock1 = Mock(spec=JpegImageFile)
        image_mock1._getexif.return_value = {
            36865: 'myData',
            36867: '2017-05-01 23:50:00'
        }

        image_mock2 = Mock(spec=JpegImageFile)
        image_mock2._getexif.return_value = None

        image_mock3 = Mock(spec=JpegImageFile)
        image_mock3._getexif.return_value = {
            36867: '2017-05-01 23:49:50',
            36882: 'myOtherData'
        }

        image_mock4 = Mock(spec=JpegImageFile)
        image_mock4._getexif.return_value = {
            36864: 'myOtherData',
            36867: '2017-05-01 23:55:00',
            36888: 'anotherData'
        }

        image_mock5 = Mock()

        image_open_mock.side_effect = [
            image_mock1, image_mock2, image_mock3, image_mock4, image_mock5
        ]

        expected_ordered = [
            PickerPhotoStub('myphoto4.jpg'),
            PickerPhotoStub('myphoto1.jpg'),
            PickerPhotoStub('myphoto3.jpg')
        ]
        expected_selected = ['myphoto4.jpg', 'myphoto1.jpg']

        select_mock = Mock()
        select_mock.return_value = [
            PickerPhoto(filepath) for filepath in expected_selected
        ]

        order_picked_mock = Mock()
        order_picked_mock.side_effect = self._order_picked_side_effect

        sut = DummyPicker('', 0)
        sut._select = select_mock
        sut._order_picked = order_picked_mock
        sut.initialize()
        sut.scan()

        select_mock.assert_called_once_with(expected_ordered)

        self.assertEqual(expected_selected, sut.picked_file_paths)