How to use mock_io method in autotest

Best Python code snippet using autotest_python

test_mongo_data_set_io_reader.py

Source:test_mongo_data_set_io_reader.py Github

copy

Full Screen

1import unittest2from threading import ThreadError3from unittest.mock import patch, MagicMock4import numpy as np5from pymongo.errors import OperationFailure6from qilib.data_set import MongoDataSetIOReader, DataSet, DataArray, MongoDataSetIO7class TestMongoDataSetIOReader(unittest.TestCase):8 def test_sync_from_storage_meta_data(self):9 mock_queue = MagicMock()10 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, patch(11 'qilib.data_set.mongo_data_set_io_reader.Thread') as thread, \12 patch('qilib.data_set.mongo_data_set_io_reader.Queue', return_value=mock_queue):13 reader = MongoDataSetIOReader(name='test')14 thread.assert_called_once()15 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',16 database='qilib')17 data_set = DataSet(storage_reader=reader)18 mock_queue.get.return_value = {'updateDescription': {'updatedFields': {'metadata': {'name': 'test_name'}}}}19 data_set.sync_from_storage(-1)20 self.assertEqual('test_name', data_set.name)21 mock_queue.get.return_value = {22 'updateDescription': {'updatedFields': {'metadata.default_array_name': 'some_array'}}}23 data_set.sync_from_storage(-1)24 self.assertEqual('some_array', data_set.default_array_name)25 def test_sync_from_storage_array_update(self):26 mock_queue = MagicMock()27 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, patch(28 'qilib.data_set.mongo_data_set_io_reader.Thread') as thread, \29 patch('qilib.data_set.mongo_data_set_io_reader.Queue', return_value=mock_queue):30 reader = MongoDataSetIOReader(name='test')31 thread.assert_called_once()32 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',33 database='qilib')34 data_set = DataSet(storage_reader=reader)35 data_array = DataArray(name='test_array', label='lab', shape=(2, 2))36 data_set.add_array(data_array)37 mock_queue.get.return_value = {'updateDescription': {38 'updatedFields': {'array_updates': [[[0, 0], {'test_array': 42}], [[0, 1], {'test_array': 25}]]}}}39 data_set.sync_from_storage(-1)40 self.assertListEqual([42, 25], list(data_set.test_array[0]))41 mock_queue.get.return_value = {'updateDescription': {42 'updatedFields': {'array_updates.1': [1, {'test_array': [67, 67]}]}}}43 data_set.sync_from_storage(-1)44 self.assertListEqual([67, 67], list(data_set.test_array[1]))45 def test_sync_from_storage_array(self):46 mock_queue = MagicMock()47 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, patch(48 'qilib.data_set.mongo_data_set_io_reader.Thread') as thread, \49 patch('qilib.data_set.mongo_data_set_io_reader.Queue', return_value=mock_queue):50 mock_io.decode_numpy_array = MongoDataSetIO.decode_numpy_array51 reader = MongoDataSetIOReader(name='test')52 thread.assert_called_once()53 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',54 database='qilib')55 data_set = DataSet(storage_reader=reader)56 set_array = DataArray(name='setter', label='for_testing', is_setpoint=True,57 preset_data=np.array(range(0, 2)))58 set_array[0] = 4259 set_array[1] = 2560 data_array = DataArray(name='test_array', label='lab', unit='V', is_setpoint=False, set_arrays=[set_array],61 shape=(2,))62 update_data = {"data_arrays": {"setter": {63 "name": set_array.name,64 "label": set_array.label,65 "unit": set_array.unit,66 "is_setpoint": set_array.is_setpoint,67 "set_arrays": [array.name for array in set_array.set_arrays],68 "preset_data": MongoDataSetIO.encode_numpy_array(set_array)}}}69 mock_queue.get.return_value = {'updateDescription': {'updatedFields': update_data}}70 data_set.sync_from_storage(-1)71 update_data = {"data_arrays.test_array": {72 "name": data_array.name,73 "label": data_array.label,74 "unit": data_array.unit,75 "is_setpoint": data_array.is_setpoint,76 "set_arrays": [array.name for array in data_array.set_arrays],77 "preset_data": MongoDataSetIO.encode_numpy_array(data_array)}}78 mock_queue.get.return_value = {'updateDescription': {'updatedFields': update_data}}79 data_set.sync_from_storage(-1)80 self.assertEqual('test_array', data_set.test_array.name)81 self.assertEqual('lab', data_set.test_array.label)82 self.assertEqual('V', data_set.test_array.unit)83 self.assertFalse(data_set.test_array.is_setpoint)84 self.assertEqual('setter', data_array.set_arrays[0].name)85 self.assertListEqual([42, 25], list(data_array.set_arrays[0]))86 data_array[0] = 25587 update_data["data_arrays.test_array"]["preset_data"] = MongoDataSetIO.encode_numpy_array(data_array)88 data_set.sync_from_storage(-1)89 self.assertEqual(255, data_set.test_array[0])90 def test_sync_from_storage_error_on_queue(self):91 mock_queue = MagicMock()92 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, patch(93 'qilib.data_set.mongo_data_set_io_reader.Thread') as thread, \94 patch('qilib.data_set.mongo_data_set_io_reader.Queue', return_value=mock_queue):95 reader = MongoDataSetIOReader(name='test')96 thread.assert_called_once()97 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',98 database='qilib')99 mock_queue.get.return_value = {'thread_error': OperationFailure('Error')}100 error = ThreadError, 'Watcher thread has stopped unexpectedly.'101 self.assertRaisesRegex(*error, reader.sync_from_storage, -1)102 def test_sync_from_storage_array_update_timeout(self):103 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, patch(104 'qilib.data_set.mongo_data_set_io_reader.Thread') as thread:105 reader = MongoDataSetIOReader(name='test')106 thread.assert_called_once()107 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',108 database='qilib')109 error = TimeoutError, ''110 self.assertRaisesRegex(*error, reader.sync_from_storage, 0.001)111 def test_bind_data_set(self):112 mock_mongo_data_set_io = MagicMock()113 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO',114 return_value=mock_mongo_data_set_io) as mock_io, \115 patch('qilib.data_set.mongo_data_set_io_reader.Thread') as thread:116 mock_mongo_data_set_io.get_document.return_value = {'name': 'test',117 'metadata': {'default_array_name': 'array'}}118 reader = MongoDataSetIOReader(name='test')119 thread.assert_called_once()120 mock_io.assert_called_once_with('test', None, create_if_not_found=False, collection='data_sets',121 database='qilib')122 data_set = DataSet()123 reader.bind_data_set(data_set)124 self.assertEqual('test', data_set.name)125 self.assertEqual('array', data_set.default_array_name)126 def test_load(self):127 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO') as mock_io, \128 patch('qilib.data_set.mongo_data_set_io_reader.Thread') as thread:129 data_set = MongoDataSetIOReader.load('test', '0x2A')130 thread.assert_called_once()131 mock_io.assert_called_once_with('test', '0x2A', create_if_not_found=False, collection='data_sets',132 database='qilib')133 self.assertIsInstance(data_set.storage, MongoDataSetIOReader)134 def test_thread_and_queue(self):135 mock_queue_instance = MagicMock()136 with patch('qilib.data_set.mongo_data_set_io_reader.MongoDataSetIO'), patch(137 'qilib.data_set.mongo_data_set_io_reader.Thread') as mock_thread, \138 patch('qilib.data_set.mongo_data_set_io_reader.Queue', return_value=mock_queue_instance):139 def work(target, args):140 target(*args)141 return mock_thread142 mock_thread.side_effect = work143 iteration_error = StopIteration('Boom!')144 mock_queue_instance.put.side_effect = [iteration_error, None]145 MongoDataSetIOReader(name='test')...

Full Screen

Full Screen

test_main.py

Source:test_main.py Github

copy

Full Screen

...26 patch.start()27 def tearDown(self):28 for patch in self.patchers:29 patch.stop()30 def mock_io(self, body: str="", status: int=0):31 mock_io = mock.MagicMock()32 mock_io.read = lambda: body.encode("utf8")33 mock_io.channel.recv_exit_status.return_value = 034 return mock_io35class TestMain(BaseTestMain):36 argv = "lfsccm"37 def test_main_no_action(self):38 self.assertEqual(-1, main.main())39@mock.patch("paramiko.client.SSHClient.connect")40@mock.patch("paramiko.client.SSHClient.exec_command")41class TestMainCheckFiles(BaseTestMain):42 # FIXME: use something like optparse to add options in anywhare43 # in the args, and handle syntax errors to notify what was wrong44 # with in the errors.45 argv = "lfsccm --files=/sample/file:ro:0 check-files"46 @mock.patch("sys.argv", ["lfsccm", "check-files"])47 def test_check_files_no_files_specified(self, mock_exec, mock_connect):48 # FIXME: exit 1 because no requremet args found49 self.assertEqual(0, main.main())50 def test_check_files_with_args(self, mock_exec, mock_connect):51 mock_exec.return_value = [52 None, self.mock_io(), self.mock_io()]53 self.assertEqual(0, main.main())54 def test_check_files_not_exist(self, mock_exec, mock_connect):55 failed_io = self.mock_io("No such file or directory")56 failed_io.channel.recv_exit_status.return_value = 157 mock_exec.return_value = [58 None, failed_io, self.mock_io()]59 self.assertEqual(1, main.main())60@mock.patch("paramiko.client.SSHClient.connect")61@mock.patch("paramiko.client.SSHClient.exec_command")62class TestMainROAvailable(BaseTestMain):63 argv = "lfsccm check-ro-available"64 def test_check_ro_available(self, mock_exec, mock_connect):65 mock_exec.return_value = [66 None, self.mock_io("Lustre version: 2.16.0"), self.mock_io()]67 self.assertEqual(0, main.main())68 def test_check_ro_available_not_supported(69 self, mock_exec, mock_connect):70 # lustre 2.14 doesn't support RO mode of PCC71 mock_exec.return_value = [72 None, self.mock_io("Lustre version: 2.14.0"), self.mock_io()]73 self.assertEqual(1, main.main())74@mock.patch("paramiko.client.SSHClient.connect")75@mock.patch("paramiko.client.SSHClient.exec_command")76class TestMainSSHConnection(BaseTestMain):77 argv = "lfsccm check-ssh-connection"78 def test_check_ssh_connection(self, mock_exec, mock_connect):79 mock_exec.return_value = [80 None, self.mock_io(), self.mock_io()]81 self.assertEqual(0, main.main())...

Full Screen

Full Screen

test_visualisation.py

Source:test_visualisation.py Github

copy

Full Screen

1"""2Unit tests for visualisation.py module3"""4import unittest5from unittest import mock6from sentiment_comparison import visualisation7class TestVisualisation(unittest.TestCase):8 """ Class for visualisation.py unit tests """9 @mock.patch('sentiment_comparison.visualisation.io')10 @mock.patch('sentiment_comparison.visualisation.plt')11 def test_make_plot__when_number_of_keywords_less_than_four(self, mock_plt, mock_io):12 """ Tests if make_plot function calls methods with appropriate arguments when number of keywords13 less than four """14 sentiments_with_names = {'keyword1': 0.3, 'keyword2': 0.7, 'keyword3': -0.9}15 dpi = 8016 fig_size = (5, 5)17 result = visualisation.make_plot(sentiments_with_names=sentiments_with_names)18 self.assert_called_appropriate_methods(mock_plt, mock_io, dpi, fig_size, result)19 @mock.patch('sentiment_comparison.visualisation.io')20 @mock.patch('sentiment_comparison.visualisation.plt')21 def test_make_plot__when_number_of_keywords_less_than_eight(self, mock_plt, mock_io):22 """ Tests if make_plot function calls methods with appropriate arguments when number of keywords23 greater than four and less than eight """24 sentiments_with_names = {'keyword1': 0.0, 'keyword2': 0.2, 'keyword3': -0.3, 'keyword4': 0.6, 'keyword5': -0.1}25 dpi = 8026 fig_size = (10, 5)27 result = visualisation.make_plot(sentiments_with_names=sentiments_with_names)28 self.assert_called_appropriate_methods(mock_plt, mock_io, dpi, fig_size, result)29 def assert_called_appropriate_methods(self, mock_plt, mock_io, dpi, fig_size, result):30 mock_plt.clf.assert_called_once()31 mock_plt.figure.assert_called_once_with(dpi=dpi, figsize=fig_size)32 mock_plt.bar.assert_called_once()33 mock_plt.title.assert_called_once()34 mock_io.BytesIO.assert_called_once()35 mock_plt.savefig.assert_called_once_with(mock_io.BytesIO.return_value, format='png')36 mock_io.BytesIO.return_value.seek.assert_called_once_with(0)37 self.assertEqual(mock_io.BytesIO.return_value, result[0])38 self.assertEqual(fig_size[0] * dpi, result[1])...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful