def test_valid_input(reader_container): """ :type reader_container: mock.Mock """ builtin_open_patcher = patch('__builtin__.open') builtin_open_mock = builtin_open_patcher.start() builtin_open_mock.return_value = 123 reader_container.is_format_supported.return_value = True reader_container.get.return_value = reader = Mock(InputReader) with patch.multiple( 'lib.analysis_transactions', _get_writer=DEFAULT, _write_output=DEFAULT, analysis_transactions_with_reader_writer=DEFAULT) as values: values['_get_writer'].return_value = writer = Mock(OutputWriter) analysis_transactions('input.txt', 'output.xml') values['_get_writer'].assert_called_once_with('output.xml') values['_write_output'].assert_called_once() values[ 'analysis_transactions_with_reader_writer'].assert_called_once_with( reader, writer) reader.open.assert_called_once_with(123) reader_container.is_format_supported.assert_called_once_with('txt') builtin_open_patcher.stop()
def test_analysis_transactions_valid_parameters(repository): command1 = Mock(TransactionCommand) command2 = Mock(TransactionCommand) repository.get_formatted_transactions.return_value = sample_output = [ (2, 3.0) ] with patch.multiple('lib.analysis_transactions', _get_reader=DEFAULT, _get_writer=DEFAULT, _write_output=DEFAULT) as values: values['_get_reader'].return_value = [command1, command2] values['_get_writer'].return_value = writer = Mock(OutputWriter) writer.generate.return_value = sample_content = 'Sample generated output' analysis_transactions('input.txt', 'output.txt') command1.apply_to.assert_called_once_with(repository) command2.apply_to.assert_called_once_with(repository) writer.generate.assert_called_once_with(sample_output) values['_get_writer'].assert_called_once_with('output.txt') values['_write_output'].assert_called_once_with( 'output.txt', sample_content) repository.get_formatted_transactions.assert_called_once_with()
def test_valid_input(reader_container): """ :type reader_container: mock.Mock """ builtin_open_patcher = patch('__builtin__.open') builtin_open_mock = builtin_open_patcher.start() builtin_open_mock.return_value = 123 reader_container.is_format_supported.return_value = True reader_container.get.return_value = reader = Mock(InputReader) with patch.multiple('lib.analysis_transactions', _get_writer=DEFAULT, _write_output=DEFAULT, analysis_transactions_with_reader_writer=DEFAULT) as values: values['_get_writer'].return_value = writer = Mock(OutputWriter) analysis_transactions('input.txt', 'output.xml') values['_get_writer'].assert_called_once_with('output.xml') values['_write_output'].assert_called_once() values['analysis_transactions_with_reader_writer'].assert_called_once_with(reader, writer) reader.open.assert_called_once_with(123) reader_container.is_format_supported.assert_called_once_with('txt') builtin_open_patcher.stop()
def test_invalid_input(reader_container): """ :type reader_container: mock.Mock """ reader_container.is_format_supported.return_value = False with pytest.raises(FileFormatNotSupported): analysis_transactions('input.txt', 'output.xml') reader_container.is_format_supported.assert_called_once_with('txt')
def test_invalid_output(writer_container): """ :type writer_container: mock.Mock """ writer_container.is_format_supported.return_value = False with patch('lib.analysis_transactions._get_reader') as _get_reader: with pytest.raises(FileFormatNotSupported): analysis_transactions('input.txt', 'output.xml') _get_reader.assert_called_once_with('input.txt') writer_container.is_format_supported.assert_called_once_with('xml')
def test_valid_output(writer_container): writer_container.is_format_supported.return_value = True writer_container.get.return_value = writer = Mock(OutputWriter) with patch.multiple('lib.analysis_transactions', _get_reader=DEFAULT, _write_output=DEFAULT, analysis_transactions_with_reader_writer=DEFAULT) as values: values['_get_reader'].return_value = reader = Mock(InputReader) analysis_transactions('input.txt', 'output.xml') values['_get_reader'].assert_called_once_with('input.txt') values['_write_output'].assert_called_once() values['analysis_transactions_with_reader_writer'].assert_called_once_with(reader, writer) writer_container.is_format_supported.assert_called_once_with('xml')
def test_valid_output(writer_container): writer_container.is_format_supported.return_value = True writer_container.get.return_value = writer = Mock(OutputWriter) with patch.multiple( 'lib.analysis_transactions', _get_reader=DEFAULT, _write_output=DEFAULT, analysis_transactions_with_reader_writer=DEFAULT) as values: values['_get_reader'].return_value = reader = Mock(InputReader) analysis_transactions('input.txt', 'output.xml') values['_get_reader'].assert_called_once_with('input.txt') values['_write_output'].assert_called_once() values[ 'analysis_transactions_with_reader_writer'].assert_called_once_with( reader, writer) writer_container.is_format_supported.assert_called_once_with('xml')
def test_analysis_transactions_valid_parameters(repository): command1 = Mock(TransactionCommand) command2 = Mock(TransactionCommand) repository.get_formatted_transactions.return_value = sample_output = [(2, 3.0)] with patch.multiple('lib.analysis_transactions', _get_reader=DEFAULT, _get_writer=DEFAULT, _write_output=DEFAULT) as values: values['_get_reader'].return_value = [command1, command2] values['_get_writer'].return_value = writer = Mock(OutputWriter) writer.generate.return_value = sample_content = 'Sample generated output' analysis_transactions('input.txt', 'output.txt') command1.apply_to.assert_called_once_with(repository) command2.apply_to.assert_called_once_with(repository) writer.generate.assert_called_once_with(sample_output) values['_get_writer'].assert_called_once_with('output.txt') values['_write_output'].assert_called_once_with('output.txt', sample_content) repository.get_formatted_transactions.assert_called_once_with()
from lib.analysis_transactions import analysis_transactions from lib.transaction_repository import repository __author__ = 'Hossein Zolfi <*****@*****.**>' analysis_transactions('samples/sample_input.csv', '/tmp/generated_output.csv') repository.clear() analysis_transactions('samples/sample_input.xml', '/tmp/generated_output.xml') repository.clear() analysis_transactions('samples/sample_input.json', '/tmp/generated_output.json') repository.clear() analysis_transactions('samples/sample_input.txt', '/tmp/generated_output.txt')