def test_file__collision_in_renamed_files(self, move_mock, error_mock): destination = path.join(self.documents, 'Assets', 'Account1') os.makedirs(destination) # Make two different files with the same contents. file1 = path.join(self.downloads, 'ofxdownload.ofx') file2 = path.join(self.downloads, 'ofxdownload2.ofx') shutil.copyfile(file1, file2) # The importer matches both files, and attempts to move them to the same # destination filename; an error should be generated. date = datetime.date(2015, 1, 2) imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=date) imp.file_name = mock.MagicMock(return_value='mybank') file.file([imp], [file1, file2], self.documents) self.assertEqual(0, move_mock.call_count) self.assertEqual(1, error_mock.call_count) self.assertTrue(all(re.match('Collision in destination filenames', call[1][0]) for call in error_mock.mock_calls)) move_mock.reset_mock() error_mock.reset_mock() # Test the case where the importer generates two distinct filenames via # file_name() while we're at it. imp.file_name = mock.MagicMock(side_effect=['bank1', 'bank2']) file.file([imp], [file1, file2], self.documents) self.assertEqual(2, move_mock.call_count) self.assertEqual(0, error_mock.call_count)
def test_file__overwrite(self, move_mock, error_mock): date = datetime.date(2015, 1, 2) dest_filename = path.join(self.documents, 'Assets', 'Account1', '{0:%Y-%m-%d}.ofxdownload.ofx'.format(date)) os.makedirs(path.dirname(dest_filename)) with open(dest_filename, 'w'): pass imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=date) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], path.join(self.downloads, 'ofxdownload.ofx'), self.documents) self.assertEqual(0, move_mock.call_count) self.assertEqual(1, error_mock.call_count) self.assertTrue(all(re.match('Destination file .* already exist', call[1][0]) for call in error_mock.mock_calls)) move_mock.reset_mock() error_mock.reset_mock() file.file([imp], path.join(self.downloads, 'ofxdownload.ofx'), self.documents, overwrite=True) self.assertEqual(1, move_mock.call_count) self.assertEqual(0, error_mock.call_count)
def test_file__dry_run(self, move_mock, error_mock): imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=None) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], self.downloads, self.documents, mkdirs=True, dry_run=True) self.assertEqual(0, error_mock.call_count) self.assertEqual(0, move_mock.call_count)
def test_file__file_name(self, move_mock, error_mock): imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=None) imp.file_name = mock.MagicMock(return_value='final.ofx') file.file([imp], path.join(self.downloads, 'ofxdownload.ofx'), self.documents, mkdirs=True) self.assertEqual(1, move_mock.call_count) self.assertEqual(1, imp.file_date.call_count) dest_filename = path.basename(move_mock.mock_calls[0][1][1]) self.assertRegex(dest_filename, r'\d\d\d\d-\d\d-\d\d\.final\.ofx')
def test_file__date_uses_extracted(self, move_mock, error_mock): date = datetime.date(2015, 2, 3) imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=date) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], path.join(self.downloads, 'ofxdownload.ofx'), self.documents, mkdirs=True) self.assertEqual(1, move_mock.call_count) self.assertEqual(1, imp.file_date.call_count) dest_filename = path.basename(move_mock.mock_calls[0][1][1]) self.assertRegex(dest_filename, r'{}\.ofxdownload\.ofx'.format(date))
def test_file__date_uses_mtime(self, move_mock, error_mock): imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=None) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], path.join(self.downloads, 'ofxdownload.ofx'), self.documents, mkdirs=True) self.assertEqual(1, move_mock.call_count) self.assertEqual(1, imp.file_date.call_count) dest_filename = path.basename(move_mock.mock_calls[0][1][1]) self.assertEqual( '{0:%Y-%m-%d}.ofxdownload.ofx'.format(datetime.date.today()), dest_filename)
def test_file__two_importers_same_accounts(self, move_mock, error_mock): account = 'Assets:Account1' imp1 = mock.MagicMock() imp1.identify = mock.MagicMock(return_value=True) imp1.file_account = mock.MagicMock(return_value=account) imp1.file_date = mock.MagicMock(return_value=None) imp1.file_name = mock.MagicMock(return_value=None) imp2 = mock.MagicMock() imp2.identify = mock.MagicMock(return_value=True) imp2.file_account = mock.MagicMock(return_value=account) imp2.file_name = mock.MagicMock(return_value=None) file.file([imp1, imp2], self.downloads, self.documents, mkdirs=True) self.assertEqual(3, move_mock.call_count) self.assertEqual(0, error_mock.call_count)
def test_file__ambiguous_accounts(self, move_mock, error_mock): imp1 = mock.MagicMock() imp1.identify = mock.MagicMock(return_value=True) imp1.file_account = mock.MagicMock(return_value='Assets:Account1') imp1.file_date = mock.MagicMock(return_value=datetime.date.today()) imp1.file_name = mock.MagicMock(return_value='filename_account1') imp2 = mock.MagicMock() imp2.identify = mock.MagicMock(return_value=True) imp2.file_account = mock.MagicMock(return_value='Assets:Account2') imp2.file_date = mock.MagicMock(return_value=datetime.date.today()) imp2.file_name = mock.MagicMock(return_value='filename_account2') file.file([imp1, imp2], self.downloads, self.documents) self.assertEqual(0, move_mock.call_count) self.assertEqual(3, error_mock.call_count) self.assertTrue(all(re.match('Ambiguous accounts', call[1][0]) for call in error_mock.mock_calls))
def test_file__idify(self, move_mock, error_mock): filename = path.join(self.downloads, 'Some thing-To remember.OFX') with open(filename, 'w'): pass imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=None) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], filename, self.documents, mkdirs=True, idify=True) self.assertEqual(1, move_mock.call_count) self.assertEqual(1, imp.file_date.call_count) dest_filename = path.basename(move_mock.mock_calls[0][1][1]) self.assertEqual( '{0:%Y-%m-%d}.Some_thing-To_remember.OFX'.format(datetime.date.today()), dest_filename)
def test_file__dest_dir_does_not_exist(self, move_mock, error_mock): imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=True) imp.file_account = mock.MagicMock(return_value='Assets:Account1') imp.file_date = mock.MagicMock(return_value=None) imp.file_name = mock.MagicMock(return_value=None) file.file([imp], self.downloads, self.documents) self.assertEqual(0, move_mock.call_count) self.assertEqual(3, error_mock.call_count) self.assertTrue(all(re.match('Destination directory .* does not exist', call[1][0]) for call in error_mock.mock_calls)) move_mock.reset_mock() error_mock.reset_mock() file.file([imp], self.downloads, self.documents, mkdirs=True) self.assertEqual(3, move_mock.call_count) self.assertEqual(0, error_mock.call_count)
def file(self, what, *args, **kwargs): file.file(self.importers, what, *args, **kwargs)
def test_file__no_match(self, move_mock): imp = mock.MagicMock() imp.identify = mock.MagicMock(return_value=False) file.file([imp], self.downloads, self.documents) self.assertEqual(0, move_mock.call_count)