def test_insert_new_files(self, mock_extras, mock_psycopg2): expected = ['10', '2', '3', '4', '5'] mock_psycopg2.connect().__enter__().cursor( ).__enter__().fetchall.return_value = [('10', ), ('2', ), ('3', ), ('4', ), ('5', )] files = [ NewFile(item_id='1', file_title='one', file_stream=psycopg2.Binary(b'123123')), NewFile(item_id='2', file_title='one', file_stream=psycopg2.Binary(b'123123')), NewFile(item_id='3', file_title='one', file_stream=psycopg2.Binary(b'123123')), NewFile(item_id='4', file_title='one', file_stream=psycopg2.Binary(b'123123')), NewFile(item_id='5', file_title='one', file_stream=psycopg2.Binary(b'123123')), ] sync = FileSync() files = sync.insert_new_files(files) for bundle in zip(files, expected): self.assertEqual(bundle[0].file_id, bundle[1])
def test_update_existing_files(self, mock_extras, mock_psycopg2): mock_psycopg2.cursor.return_value.execute.fetch_all = [] files = [ File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), File(file_id='1', file_title='hello.txt', file_stream=BytesIO(b'onetwothree')), ] sync = FileSync() sync.update_existing_files(files) self.assertEqual(mock_extras.call_count, 2)
def test_transform_function_error(self, mock_rename, mock_subprocess, mock_open): mock_subprocess.side_effect = Exception('exception') sync = FileSync() result = sync.transform_file('file.txt') self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual(result[0], 'file.txt') mock_open.assert_called_with('temp_files/lowriter_in.txt', 'rb')
def test_filter_ftp_items_already_stored_no_match(self, mock_sql): mock_sql.return_value = set([]) sync = FileSync() sync.file_dict = {'item1': 'item1_1.txt'} sync.filter_ftp_items_already_stored() self.assertEqual(sync.file_dict, {'item1': 'item1_1.txt'}) mock_sql.assert_called_with( '\n SELECT file.file_title FROM file WHERE file_title SIMILAR TO %s;\n ', 'item1_1%')
def test_process_updates_pdf(self, mock_store, mock_load, mock_update): empty_stream = BytesIO(b'abc') mock_load.return_value = empty_stream files = [ VersionUpdate(file_id='1', file_name='file.pdf', item_number='1') ] sync = FileSync() sync.process_updates(files) mock_load.assert_called_with('file.pdf') mock_store.assert_not_called()
def test_files_to_be_updated_query(self, mock_sql): mock_sql.return_value = [( '1', 'item1_1.sql', )] sync = FileSync() sync.file_dict = {'item1': 'item1_2.txt', 'item2': 'item2_2.txt'} sync.files_to_be_updated() mock_sql.assert_called_with( '\n SELECT file.file_id, file.file_title FROM file WHERE file_title SIMILAR TO %s;\n ', ('(item1|item2)%'))
def test_process_updates_txt(self, mock_store, mock_load, mock_transform, mock_update): empty_stream = BytesIO(b'') mock_transform.return_value = '/temp_files/file.pdf', empty_stream mock_load.return_value = empty_stream files = [ VersionUpdate(file_id='1', file_name='file.txt', item_number='1') ] sync = FileSync() sync.process_updates(files) mock_load.assert_called_with('file.txt') mock_store.assert_called_with('file.txt', empty_stream) mock_transform.assert_called_with('file.txt')
def test_files_to_be_updated_files_to_upload(self, mock_sql): mock_sql.return_value = [ ( '1', 'item1_1.sql', ), ] sync = FileSync() sync.file_dict = {'item1': 'item1_2.txt', 'item2': 'item2_2.txt'} files_to_upload = sync.files_to_be_updated() self.assertEqual(files_to_upload[0]._asdict(), { 'item_number': 'item1', 'file_id': '1', 'file_name': 'item1_2.txt' })
def test_filter_ftp_items_already_stored_match(self, mock_sql): mock_sql.return_value = set([('item1_1.pdf', ), ('item2_1.pdf', )]) expected_sql = 'SELECT file.file_title FROM file WHERE file_title SIMILAR TO %s;' expected_args = 'item1_1%|item2_1%' sync = FileSync() sync.file_dict = OrderedDict({ 'item1': 'item1_1.txt', 'item2': 'item2_1.txt', }) sync.filter_ftp_items_already_stored() # fix assert so that order sql = mock_sql.call_args_list[0][0][0].strip() args = mock_sql.call_args_list[0][0][1] self.assertEqual(expected_sql, sql) self.assertEqual(expected_args, args) self.assertEqual(sync.file_dict, {})
def test_transform_function_silent_fail(self, mock_rename, mock_path, mock_subprocess, mock_open): mock_subprocess.call.return_value = None mock_path.return_value = False expected_call_one = [ 'lowriter', '--convert-to', 'pdf:writer_pdf_Export', 'temp_files/lowriter_in.txt', '--outdir', 'temp_files' ] expected_call_two = [ 'ps2pdf', '-dPDFSETTINGS=/ebook', 'temp_files/lowriter_in.pdf', 'temp_files/compressed.pdf' ] expected_calls = [call(expected_call_one), call(expected_call_two)] sync = FileSync() result = sync.transform_file('file.txt') self.assertEqual(mock_subprocess.call_count, 2) self.assertEqual(result[0], 'file.txt') self.assertTrue(mock_subprocess.call_args_list[0], expected_call_one) mock_subprocess.assert_has_calls(expected_calls) mock_open.assert_called_with('temp_files/lowriter_in.txt', 'rb')
def test_files_not_in_system_filter(self): sync = FileSync() items_dict = {'item4': '15', 'item5': '16'} sync.file_dict = { 'item4': 'item4_2.txt', 'item5': 'item5_2.txt', } results = [ res._asdict() for res in sync.files_not_in_system(items_dict) ] self.assertEqual(results, [ { 'item_number': 'item4', 'file_name': 'item4_2.txt', 'item_id': '15' }, { 'item_number': 'item5', 'file_name': 'item5_2.txt', 'item_id': '16' }, ])
def test_get_ftp_file_names(self, mock_ftp): mock_ftp().__enter__().nlst.return_value = ['one', 'two', 'three.txt'] sync = FileSync() result = sync.get_ftp_file_names() self.assertEqual(mock_ftp.call_count, 2) self.assertEqual(list(result), ['three.txt'])
def test_filter_ftp_dir_items_file_not_exists_in_db(self): sync = FileSync() db_names = [('1', 'item2')] sync.file_dict = {'item1': 'item1_1.txt'} self.assertEqual(list(sync.filter_ftp_dir_items(db_names)), [])