def test_hello_world(self): file_name = "jvpm/javafiles/HelloWorld.class" header_class_object = packages.jvpm_opcodes.HeaderClass(name=file_name) header_class_object.get_magic() header_class_object.get_minor() header_class_object.get_major() get_cp = header_class_object.get_const_pool() p_translator = packages.pool_translate.PoolTranslate( get_cp, header_class_object.skips_in_constant_pool, name=file_name) pool = p_translator.translate_pool() access_flags = header_class_object.get_access_flags() this_class = header_class_object.get_this_class() super_class = header_class_object.get_super_class() get_ic = header_class_object.get_interfaces_count() header_class_object.get_interface( ) # no method built yet but should just be index in constant pool get_fc = header_class_object.get_field_count() header_class_object.get_field( ) # no method built yet but should just be variable table opcodes = header_class_object.get_methods_count() opcodes = header_class_object.get_methods(pool) # **************************************************************************************** dict_search_object = packages.jvpm_opcodes.OpCodes(opcodes, pool) dict_search_object.dict_search() sys.stdout.assert_has_calls([ call.write('java/lang/System.in:Ljava/io/InputStream;'), call.write('\n') ])
def test_player_count(self): with patch('builtins.input', mock_valid_input_p_count): while not StopIteration: p, b = self.p.player_count() self.assertIsInstance(p, int) self.assertIsInstance(b, int) self.assertTrue(2 <= p <= 6) self.assertTrue(0 <= b <= p) with patch('builtins.input', mock_invalid_input_p_count), patch('sys.stdout') as out: p, b = self.p.player_count() self.assertEqual(p, 6) self.assertEqual(b, 6) errs = [] for e in get_ips_iter(): if e != "6": errs.append( call.write( f"\nError: {e} is an invalid entry for player count.\n" )) errs.append(call.write('\n')) for e in get_ibs_iter(): if e != "6": errs.append( call.write( f"\nError: {e} is an invalid entry for bot count.\n" )) errs.append(call.write('\n')) out.assert_has_calls(errs)
def test_show(self): common.show("Hello, world!", file=self.file) assert [ call.write("Hello, world!"), call.write("\n"), ] == self.file.mock_calls
def test_show(self): common.show("Hello, world!", file=self.file) assert [ call.write("Hello, world!"), call.write("\n"), ] == self.file.mock_calls
def test_wakeup_sleeping(self, sleep, sleeping): ctrlr = PWMController(self.bus, 0) self.bus.read_byte_data.return_value = 0 container = Mock() container.attach_mock(sleep, "sleep") container.attach_mock(self.bus.write_byte_data, "write") self.bus.reset_mock() sleeping.return_value = False ctrlr.wakeup() self.bus.write_byte_data.assert_not_called() sleeping.return_value = True ctrlr.wakeup() self.bus.write_byte_data.assert_called_with(0, 0, 0) container.reset_mock() self.bus.read_byte_data.return_value = 0b10110101 ctrlr.wakeup() calls = [ call.write(0, 0, 0b00100101), call.sleep(ANY), call.write(0, 0, 0b10100101), ] container.assert_has_calls(calls) self.assertGreaterEqual(sleep.call_args[0][0], 500e-6)
def test_show_after_1_indent_2_dedent(self): common.indent() common.dedent() common.dedent() common.show("|\n", file=self.file, color=None) assert [call.write("|\n"), call.write("\n")] == self.file.mock_calls
def assert_power_button_press_release_emitted_write_and_sync(self, calls): expected_calls = [ call.write(ecodes.EV_KEY, ecodes.KEY_POWER, 1), call.write(ecodes.EV_KEY, ecodes.KEY_POWER, 0), call.syn(), ] self.assertEquals(expected_calls, calls)
def test_show_after_indent(self): common.indent() common.show("|\n", file=self.file) assert [ call.write(" |\n"), call.write("\n"), ] == self.file.mock_calls
def test_show_after_indent(self): common.indent() common.show("|\n", file=self.file) assert [ call.write(" |\n"), call.write("\n"), ] == self.file.mock_calls
def assert_finger_move_emitted_write_and_syn(self, touch, slot, x, y): expected_calls = [ call.write(ecodes.EV_ABS, ecodes.ABS_MT_SLOT, slot), call.write(ecodes.EV_ABS, ecodes.ABS_MT_POSITION_X, x), call.write(ecodes.EV_ABS, ecodes.ABS_MT_POSITION_Y, y), call.syn() ] self.assertEqual(expected_calls, touch._device.mock_calls)
def test_serialize_no_body_to_stream(self): s = ICAPResponse(http=HTTPResponse()) stream = MagicMock() Serializer(s, 'asdf', is_options=False).serialize_to_stream(stream) calls = stream.mock_calls[-2:] print(calls) assert calls == [call.write(b'\r\n'), call.write(b'HTTP/1.1 200 OK\r\n\r\n')]
def test_log(mock_stderr, mock_stdout): tshuttle.helpers.log("message") tshuttle.helpers.log("abc") tshuttle.helpers.log("message 1\n") tshuttle.helpers.log("message 2\nline2\nline3\n") tshuttle.helpers.log("message 3\nline2\nline3") assert mock_stdout.mock_calls == [ call.flush(), call.flush(), call.flush(), call.flush(), call.flush(), ] assert mock_stderr.mock_calls == [ call.write('prefix: message\n'), call.flush(), call.write('prefix: abc\n'), call.flush(), call.write('prefix: message 1\n'), call.flush(), call.write('prefix: message 2\n'), call.write(' line2\n'), call.write(' line3\n'), call.flush(), call.write('prefix: message 3\n'), call.write(' line2\n'), call.write(' line3\n'), call.flush(), ]
def test_manage_account(self, stdout, check_call, filter, delete, create): mbm.__main__.manage_account(namespace({'action': 'new', 'name': None, 'type': None, })) sys.exit.assert_called_with(2) mbm.__main__.manage_account(namespace({'action': 'new', 'name': 'acc1', 'type': None, })) create.assert_called_with("acc1", account_type=None) mbm.__main__.controller.global_conf.accounts = {'acc1': 'fake'} mbm.__main__.manage_account(namespace({'action': 'list', 'name': None, 'type': None, })) stdout.assert_has_calls([call.write('acc1'), call.write('\n')]) mbm.__main__.manage_account(namespace({'action': 'edit', 'name': 'acc1', 'type': None, })) filter.assert_called_with(['acc1']) check_call.assert_called check_call.side_effect = FileNotFoundError mbm.__main__.manage_account(namespace({'action': 'edit', 'name': 'acc1', 'type': None, })) sys.exit.assert_called_with(1) sys.exit.reset_mock() check_call.side_effect = subprocess.CalledProcessError( cmd='edit', returncode=1) mbm.__main__.manage_account(namespace({'action': 'edit', 'name': 'acc1', 'type': None, })) sys.exit.assert_called_with(1) sys.exit.reset_mock() check_call.side_effect = None mbm.__main__.manage_account(namespace({'action': 'delete', 'name': 'acc1', 'type': None, })) delete.assert_called_with('acc1') create.side_effect = mbm.config.AccountException mbm.__main__.manage_account(namespace({'action': 'new', 'name': 'acc1', 'type': None, })) sys.exit.assert_called_with(2) sys.exit.reset_mock()
def assert_finger_up_emitted_write_and_syn(self, touch, slot): lift_tracking_id = -1 release_value = 0 expected_calls = [ call.write(ecodes.EV_ABS, ecodes.ABS_MT_SLOT, slot), call.write(ecodes.EV_ABS, ecodes.ABS_MT_TRACKING_ID, lift_tracking_id), call.write(ecodes.EV_KEY, ecodes.BTN_TOUCH, release_value), call.syn() ] self.assertEqual(expected_calls, touch._device.mock_calls)
def test_fail_immediately_if_deployment_failed(self): get_deployment_state_mock = MagicMock(side_effect=[ self.create_deployment_state('deploymentFailure', {'failure': 'test only'}), ]) url_mock = MagicMock(return_value='/deployments/events') conductr_host = '10.0.0.1' conductr_host_mock = MagicMock(return_value=conductr_host) get_events_mock = MagicMock() stdout = MagicMock() stderr = MagicMock() deployment_id = 'a101449418187d92c789d1adc240b6d6' resolved_version = { 'org': 'typesafe', 'repo': 'bundle', 'package_name': 'cassandra', 'compatibility_version': 'v1', 'digest': 'abcdef', 'resolver': bintray_resolver.__name__ } dcos_mode = False args = MagicMock( **{ 'dcos_mode': dcos_mode, 'long_ids': False, 'wait_timeout': 10, 'conductr_auth': self.conductr_auth, 'server_verification_file': self.server_verification_file }) with patch('conductr_cli.conduct_url.url', url_mock), \ patch('conductr_cli.conduct_url.conductr_host', conductr_host_mock), \ patch('conductr_cli.bundle_deploy.get_deployment_state', get_deployment_state_mock), \ patch('conductr_cli.sse_client.get_events', get_events_mock): logging_setup.configure_logging(args, stdout, stderr) self.assertRaises(ContinuousDeliveryError, bundle_deploy.wait_for_deployment_complete, deployment_id, resolved_version, args) self.assertEqual(get_deployment_state_mock.call_args_list, [call(deployment_id, args)]) url_mock.assert_not_called() conductr_host_mock.assert_not_called() get_events_mock.assert_not_called() self.assertEqual(stdout.method_calls, [ call.write('Deploying cassandra:v1-abcdef'), call.write('\n'), call.flush() ])
def test_apartments_to_csv(self): """Tests if found apartments are saved in the output file""" apartments = [ { 'Apartment': 'A.0.03', 'Size': '75.56', 'Rooms': '3', 'Floor': '0', 'Status': 'sold', 'Link': 'https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.03.pdf' }, { 'Apartment': 'A.0.05', 'Size': '81.7', 'Rooms': '3', 'Floor': '0', 'Status': 'free', 'Link': 'https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.05.pdf' }, ] headers = ['Apartment', 'Size', 'Rooms', 'Floor', 'Status', 'Link'] mock_op = mock_open() with patch('builtins.open', mock_op) as mocked_file: forestclub.apartments_to_csv(mocked_file, apartments, headers) # print(mock_op.mock_calls) expected_calls = [ call.write('Apartment,Size,Rooms,Floor,Status,Link\r\n'), call.write( 'A.0.03,75.56,3,0,sold,https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.03.pdf\r\n' ), call.write( 'A.0.05,81.7,3,0,free,https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.05.pdf\r\n' ), ] mock_op().assert_has_calls(calls=expected_calls)
def test_periodic_check_between_events(self): count_installations_mock = MagicMock(side_effect=[0, 0, 1]) url_mock = MagicMock(return_value='/bundle-events/endpoint') conductr_host = '10.0.0.1' conductr_host_mock = MagicMock(return_value=conductr_host) get_events_mock = MagicMock(return_value=[ create_heartbeat_event(), create_test_event('bundleInstallationAdded'), create_heartbeat_event(), create_heartbeat_event(), create_heartbeat_event(), create_test_event('bundleInstallationAdded') ]) stdout = MagicMock() is_tty_mock = MagicMock(return_value=True) bundle_id = 'a101449418187d92c789d1adc240b6d6' dcos_mode = True args = MagicMock(**{ 'dcos_mode': dcos_mode, 'wait_timeout': 10, 'conductr_auth': self.conductr_auth, 'server_verification_file': self.server_verification_file }) with patch('conductr_cli.conduct_url.url', url_mock), \ patch('conductr_cli.conduct_url.conductr_host', conductr_host_mock), \ patch('conductr_cli.bundle_installation.count_installations', count_installations_mock), \ patch('conductr_cli.sse_client.get_events', get_events_mock), \ patch('sys.stdout.isatty', is_tty_mock): logging_setup.configure_logging(args, stdout) bundle_installation.wait_for_installation(bundle_id, args) self.assertEqual(count_installations_mock.call_args_list, [ call(bundle_id, args), call(bundle_id, args), call(bundle_id, args) ]) url_mock.assert_called_with('bundles/events', args) conductr_host_mock.assert_called_with(args) get_events_mock.assert_called_with(dcos_mode, conductr_host, '/bundle-events/endpoint', auth=self.conductr_auth, verify=self.server_verification_file) self.assertEqual(stdout.method_calls, [ call.write('Bundle a101449418187d92c789d1adc240b6d6 waiting to be installed'), call.write('\n'), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 still waiting to be installed\r'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 still waiting to be installed\n'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 installed'), call.write('\n'), call.flush(), ])
def test_stats_to_csv(self): """Tests if apartments stats are saved in the output file""" apartments = [ { 'Apartment': 'A.0.03', 'Size': '75.56', 'Rooms': '3', 'Floor': '0', 'Status': 'sold', 'Link': 'https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.03.pdf' }, { 'Apartment': 'A.0.05', 'Size': '81.7', 'Rooms': '3', 'Floor': '0', 'Status': 'free', 'Link': 'https://www.forestclub.com.pl/wp-content/uploads/2019/04/A.0.05.pdf' }, ] mock_op = mock_open() with patch('builtins.open', mock_op) as mocked_file: stat = os.stat_result(tuple(0 for _ in range(10))) with patch('os.stat', return_value=stat): forestclub.stats_to_csv(mocked_file, apartments) # print(mock_op.mock_calls) stats_date = datetime.date.today() expected_calls = [ call.write('Date,Flats total,Flats free,Flats sold\r\n'), call.write(f'{stats_date},2,1,1\r\n'), ] mock_op().assert_has_calls(calls=expected_calls)
def test_periodic_check_between_events(self): count_installations_mock = MagicMock(side_effect=[0, 0, 1]) url_mock = MagicMock(return_value='/bundle-events/endpoint') conductr_host = '10.0.0.1' conductr_host_mock = MagicMock(return_value=conductr_host) get_events_mock = MagicMock(return_value=[ create_heartbeat_event(), create_test_event('bundleInstallationAdded'), create_heartbeat_event(), create_heartbeat_event(), create_heartbeat_event(), create_test_event('bundleInstallationAdded') ]) stdout = MagicMock() bundle_id = 'a101449418187d92c789d1adc240b6d6' dcos_mode = True args = MagicMock(**{ 'dcos_mode': dcos_mode, 'wait_timeout': 10, 'conductr_auth': self.conductr_auth, 'server_verification_file': self.server_verification_file }) with patch('conductr_cli.conduct_url.url', url_mock), \ patch('conductr_cli.conduct_url.conductr_host', conductr_host_mock), \ patch('conductr_cli.bundle_installation.count_installations', count_installations_mock), \ patch('conductr_cli.sse_client.get_events', get_events_mock): logging_setup.configure_logging(args, stdout) bundle_installation.wait_for_installation(bundle_id, args) self.assertEqual(count_installations_mock.call_args_list, [ call(bundle_id, args), call(bundle_id, args), call(bundle_id, args) ]) url_mock.assert_called_with('bundles/events', args) conductr_host_mock.assert_called_with(args) get_events_mock.assert_called_with(dcos_mode, conductr_host, '/bundle-events/endpoint', auth=self.conductr_auth, verify=self.server_verification_file) self.assertEqual(stdout.method_calls, [ call.write('Bundle a101449418187d92c789d1adc240b6d6 waiting to be installed'), call.write('\n'), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 still waiting to be installed\r'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 still waiting to be installed\n'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 installed'), call.write('\n'), call.flush(), ])
def test_check__checksum_same__part_differ__cache_new(self, nowflt, fp): # Prepare test self.key = self.test_check__checksum_same__part_differ__cache_new.__name__ different_part = 'p,457,3,46,2' nowflt.return_value = self.now + 1 self.cache.fp = fp self.cache.cache_dict[self.key] = self.cache_entry self.cache.count += 1 # Execute test result = self.cache.check(self.key, self.path, different_part) # Evaluate internal state (attributes values) self.assertTrue( result, ASSERT_INVALID_RETURNED_VALUE_FMT.format('sr_cache.check')) expected = {self.key: self.cache_entry} self.assertDictEqual( expected, self.cache.cache_dict, ASSERT_INVALID_VALUE_FMT.format('sr_cache.cache_dict')) self.assertEqual(self.count + 2, self.cache.count, ASSERT_INVALID_VALUE_FMT.format('sr_cache.count')) # Evaluate external calls expected = [ call.write( WRITE_LINE_FMT.format(self.key, nowflt.return_value, self.file, different_part)) ] self.assertEqual(expected, fp.mock_calls, ASSERT_MOCK_CALLS)
def test_check__checksum_same__value_same__cache_hit(self, nowflt, fp): # Prepare test self.key = self.test_check__checksum_same__value_same__cache_hit.__name__ nowflt.return_value = self.now + 1 self.cache.fp = fp self.cache.cache_dict[self.key] = self.cache_entry self.cache.count += 1 # Execute test result = self.cache.check(self.key, self.path, self.part) # Evaluate internal state (attributes values) self.assertFalse( result, ASSERT_INVALID_RETURNED_VALUE_FMT.format('sr_cache.check')) assert_msg = ASSERT_INVALID_VALUE_FMT.format('sr_cache.cache_hit') self.assertEqual(self.cache_entry.popitem()[0], self.cache.cache_hit, assert_msg) expected = {self.key: self.cache_entry} self.assertDictEqual( expected, self.cache.cache_dict, ASSERT_INVALID_VALUE_FMT.format('sr_cache.cache_dict')) self.assertEqual(self.count + 2, self.cache.count, ASSERT_INVALID_VALUE_FMT.format('sr_cache.count')) # Evaluate external calls expected = [ call.write( WRITE_LINE_FMT.format(self.key, nowflt.return_value, self.file, self.part)) ] self.assertEqual(expected, fp.mock_calls, ASSERT_MOCK_CALLS)
def test_HelloWorld(self, mock_patch): jvpm.HelloWorld.HelloWorld() self.assertEqual(mock_patch.mock_calls, [ call.write('Hello world'), call.write('Team 5 is:'), call.write('Brian Pedersen'), call.write('Jake Schnorr'), call.write('Megan Stucky'), call.write('James Thomas'), call.write('Nate Roberts'), call.write('John Shapiro'), call.write('Kelly Trujillo'), ])
def test_log_storage(*mocks): open_mock = Mock() with patch('builtins.open', open_mock): metric = Metric(name='foo', value=8) log_storage = LogStorage(output_filename='mocked_file_name.log') log_storage.store([metric]) assert open_mock.return_value.write.call_count == 2 assert open_mock.return_value.method_calls[0] == call.write('foo 8 1\n')
def test_release_pressed_keys_with_pressed_keys(self): expected_calls = [ call.write(ecodes.EV_KEY, ecodes.ecodes.get('KEY_A'), self._RELEASE_VALUE), call.syn(), call.write(ecodes.EV_KEY, ecodes.ecodes.get('KEY_B'), self._RELEASE_VALUE), call.syn() ] keyboard = self.get_keyboard_with_mocked_backend() self.press_key_and_reset_mock(keyboard, 'KEY_A') self.press_key_and_reset_mock(keyboard, 'KEY_B') keyboard.release_pressed_keys() self.assertEqual(expected_calls, keyboard._device.mock_calls)
def assert_emitted_write_and_syn(self, keyboard, key, value): key_ecode = ecodes.ecodes.get(key) expected_calls = [ call.write(ecodes.EV_KEY, key_ecode, value), call.syn() ] self.assertEqual(expected_calls, keyboard._device.mock_calls)
def test_main(mock_get_method, mock_setup_daemon, mock_rewrite_etc_hosts): stdin, stdout = setup_daemon() mock_setup_daemon.return_value = stdin, stdout mock_get_method("not_auto").name = "test" mock_get_method.reset_mock() sshuttle.firewall.main("not_auto", False) assert mock_rewrite_etc_hosts.mock_calls == [ call({'1.2.3.3': 'existing'}, 1024), call({}, 1024), ] assert stdout.mock_calls == [ call.write('READY test\n'), call.flush(), call.write('STARTED\n'), call.flush() ] assert mock_setup_daemon.mock_calls == [call()] assert mock_get_method.mock_calls == [ call('not_auto'), call().is_supported(), call().is_supported().__bool__(), call().setup_firewall( 1024, 1026, [(AF_INET6, u'2404:6800:4004:80c::33')], AF_INET6, [(AF_INET6, 64, False, u'2404:6800:4004:80c::', 0, 0), (AF_INET6, 128, True, u'2404:6800:4004:80c::101f', 80, 80)], True, None, '0x01'), call().setup_firewall( 1025, 1027, [(AF_INET, u'1.2.3.33')], AF_INET, [(AF_INET, 24, False, u'1.2.3.0', 8000, 9000), (AF_INET, 32, True, u'1.2.3.66', 8080, 8080)], True, None, '0x01'), call().restore_firewall(1024, AF_INET6, True, None), call().restore_firewall(1025, AF_INET, True, None), ]
def test_wait_timeout_all_events(self): get_scale_mock = MagicMock(return_value=0) url_mock = MagicMock(return_value='/bundle-events/endpoint') conductr_host = '10.0.0.1' conductr_host_mock = MagicMock(return_value=conductr_host) get_events_mock = MagicMock(return_value=[ self.create_test_event('bundleExecutionAdded'), self.create_test_event('bundleExecutionAdded'), self.create_test_event('bundleExecutionAdded') ]) stdout = MagicMock() bundle_id = 'a101449418187d92c789d1adc240b6d6' dcos_mode = False args = MagicMock(**{ 'dcos_mode': dcos_mode, 'wait_timeout': 10, 'conductr_auth': self.conductr_auth, 'server_verification_file': self.server_verification_file }) with patch('conductr_cli.conduct_url.url', url_mock), \ patch('conductr_cli.conduct_url.conductr_host', conductr_host_mock), \ patch('conductr_cli.bundle_scale.get_scale', get_scale_mock), \ patch('conductr_cli.sse_client.get_events', get_events_mock): logging_setup.configure_logging(args, stdout) self.assertRaises(WaitTimeoutError, bundle_scale.wait_for_scale, bundle_id, 3, args) self.assertEqual(get_scale_mock.call_args_list, [ call(bundle_id, args), call(bundle_id, args), call(bundle_id, args), call(bundle_id, args) ]) url_mock.assert_called_with('bundles/events', args) conductr_host_mock.assert_called_with(args) get_events_mock.assert_called_with(dcos_mode, conductr_host, '/bundle-events/endpoint', auth=self.conductr_auth, verify=self.server_verification_file) self.assertEqual(stdout.method_calls, [ call.write('Bundle a101449418187d92c789d1adc240b6d6 waiting to reach expected scale 3'), call.write('\n'), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 has scale 0, expected 3\r'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 has scale 0, expected 3.\r'), call.write(''), call.flush(), call.write('Bundle a101449418187d92c789d1adc240b6d6 has scale 0, expected 3..\r'), call.write(''), call.flush() ])
def test_extract(client, mo): doc = {"field1": "stuff", "field2": "things"} doc_data = serialize_doc(doc) client.count.return_value = {"count": 1001} client.search.return_value = { "_scroll_id": "uohialjrknf", "_shards": { "successful": 1, "total": 1, "skipped": 0 }, "hits": { "hits": [{ "_index": "test", "_id": "0", "_score": 0, "_source": doc }] } } def set_corp_size(*args, **kwargs): path = args[0] mockstat = mock.Mock() if ".bz2" in path: mockstat.st_size = 500 else: mockstat.st_size = 1000 return mockstat client.scroll.return_value = {} index = "test" outdir = "/abs/outpath/to/tracks/" with mock.patch("os.stat") as osstat: osstat.side_effect = set_corp_size res = corpus.extract(client, outdir, index) assert mo.call_count == 4 mo.assert_has_calls([ call("/abs/outpath/to/tracks/test-documents.json", "wb"), call("/abs/outpath/to/tracks/test-documents.json.bz2", "wb"), call("/abs/outpath/to/tracks/test-documents-1k.json", "wb"), call("/abs/outpath/to/tracks/test-documents-1k.json.bz2", "wb") ], any_order=True) assert res == { "filename": "test-documents.json.bz2", "path": "/abs/outpath/to/tracks/test-documents.json.bz2", "compressed_bytes": 500, "index_name": "test", "doc_count": 1001, "uncompressed_bytes": 1000 } file_mock = mo.return_value file_mock.assert_has_calls([call.write(doc_data)])
def test_train(return_predictions_batch): batch_size = 2 num_items = sum(len(batch) for batch in return_predictions_batch) return_predictions_batch_copy = copy.deepcopy(return_predictions_batch) formatter = VowpalWabbitDummyFormatter() common_features = '|a user1' items_features = ['|b item{}'.format(i) for i in range(num_items)] weights = [random.random() for _ in range(num_items)] labels = [random.random() for _ in range(num_items)] def get_next_prediction(): if return_predictions_batch_copy[0]: return str(return_predictions_batch_copy[0].pop(0)) else: return_predictions_batch_copy.pop(0) return get_next_prediction() vw_process = Mock( stdin=Mock(), stdout=Mock( readline=Mock(side_effect=lambda: bytes(get_next_prediction() + '\n', encoding='utf-8')) ) ) popen = Mock( return_value=vw_process ) with patch('subwabbit.blocking.subprocess.Popen', new=popen): model = VowpalWabbitProcess( formatter=formatter, batch_size=batch_size, vw_args=[] ) assert model.vw_process == vw_process model.train(common_features, iter(items_features), iter(labels), iter(weights)) expected_calls = [] for i, item_features in enumerate(return_predictions_batch): items_from = i * batch_size items_to = i * batch_size + batch_size expected_calls.append( call.write( bytes( '\n'.join([ formatter.get_formatted_example(common_features, item_features, label, weight) for item_features, label, weight in zip( items_features[items_from:items_to], labels[items_from:items_to], weights[items_from:items_to]) ]) + '\n', encoding='utf-8' ) ) ) expected_calls.append(call.flush()) vw_process.stdin.assert_has_calls(expected_calls) assert model.unprocessed_batch_sizes == []
def test_store_token_filename(self): filename = 'path/to/open/dummyqi.rc' os.makedirs = MagicMock() json.load = MagicMock() os.environ.get = MagicMock() os.environ.get.return_value = None warnings = MagicMock() existing_token = 'secret' new_token = 'other' json.load.return_value = {'token': existing_token} with patch("builtins.open", mock_open()) as mock_file: with patch('os.makedirs', os.makedirs): with patch('warnings.warn', warnings): store_account(new_token, filename) # store token, while one exists warnings.assert_called_once( ) # warning printed to use overwrite=True mock_file.assert_called_once() mock_file.assert_called_with( filename, 'r') # no token written, only read once store_account(new_token, filename, overwrite=True) warnings.assert_called_once() # still 1, no new warning mock_file.assert_called_with(filename, 'w') # token is written handle = mock_file() all_calls = handle.mock_calls self.assertIn([ call.write('{'), call.write('\n '), call.write('"token"'), call.write(': '), call.write('"' + new_token + '"'), call.write('\n'), call.write('}') ], all_calls)
def test_show_command() -> None: command = ( '/a/path/to/test', '-a', '--abc=true', '--def', 'false', ) with patch('builtins.print') as prnt: prnt.return_value.write.side_effect = [ None, None, None, None, None, None, None, ] _show_command(command) calls = [ call.write(_DIVIDER), call.write('/a/path/to/test\n'), call.write(' -a\n'), call.write(' --abc=true\n'), call.write(' --def\n'), call.write(' false\n'), call.write('\n\n'), ] prnt.assert_has_calls(calls)
def test_remove_token_filename(self): filename = 'path/to/open/dummyqi.rc' os.makedirs = MagicMock() json.load = MagicMock() with patch.dict('os.environ', values={'QI_TOKEN': ''}): existing_token = 'secret' wrong_token = 'not_secret' no_token = '' json.load.return_value = {'token': existing_token} with patch("builtins.open", mock_open()) as mock_file: with patch('os.makedirs', os.makedirs): delete_account( wrong_token, filename) # remove token, while another exists mock_file.assert_called_once() mock_file.assert_called_with( filename, 'r') # file not written, only read once delete_account(existing_token, filename) # remove token, the right one mock_file.assert_called_with(filename, 'w') # file is written handle = mock_file() all_calls = handle.mock_calls # the empty token is written self.assertIn([ call.write('{'), call.write('\n '), call.write('"token"'), call.write(': '), call.write('"' + no_token + '"'), call.write('\n'), call.write('}') ], all_calls)
def test_save_and_load_token_filename(self): filename = 'path/to/open/dummyqi.rc' os.makedirs = MagicMock() json.load = MagicMock() os.environ.get = MagicMock() os.environ.get.return_value = None expected_token = 'secret' json.load.return_value = {'token': expected_token} with patch("builtins.open", mock_open()) as mock_file: with patch('os.makedirs', os.makedirs): save_account(expected_token, filename) mock_file.assert_called_with(filename, 'w') handle = mock_file() all_calls = handle.mock_calls self.assertIn([ call.write('{'), call.write('\n '), call.write('"token"'), call.write(': '), call.write('"' + expected_token + '"'), call.write('\n'), call.write('}') ], all_calls) token = load_account(filename) self.assertEqual(expected_token, token)
def test_serialize_to_stream(self): s = ICAPResponse(http=HTTPResponse()) s.http.body = b'abc' stream = MagicMock() Serializer(s, 'asdf', is_options=False).serialize_to_stream(stream) calls = stream.mock_calls print(calls) assert calls[1:] == [ call.write(b'\r\n'), call.write(b'HTTP/1.1 200 OK\r\n\r\n'), call.write(b'3\r\n'), call.write(b'abc\r\n'), call.write(b'0\r\n\r\n') ]
def test_serialize_options_to_stream(self): s = ICAPResponse.from_error(200) s.is_options = True stream = MagicMock() Serializer(s, 'asdf', is_options=True).serialize_to_stream(stream) calls = stream.mock_calls print(calls) assert calls[-1] == call.write(b'\r\n')
def _make_expected_write_calls(self, lines, lines_per_file, header=False): expected_write_calls = [] start_line = 1 if header else 0 for i in range(start_line, len(lines), lines_per_file): if header: expected_write_calls.append(call.write(self.lines[0])) expected_write_calls.append( call.writelines(lines[i:i+lines_per_file])) expected_write_calls.append(call.close()) return expected_write_calls
def testTestTempAndLog_withMultipleProviders(self): tempProvider1 = TempProvider("sensorid1") tempProvider1.getTemp = mock.Mock(return_value=18.0) tempProvider2 = TempProvider("sensorid2") tempProvider2.getTemp = mock.Mock(return_value=22.0) tempProvider3 = TempProvider("sensorid3") tempProvider3.getTemp = mock.Mock(return_value=21.0) heatingElement = HeatingElement() tempLogger = TempLogger() tempLogger._write = mock.Mock() time.time = mock.Mock(return_value=1234) beerTemp = BeerTemp([tempProvider1, tempProvider2, tempProvider3], heatingElement, tempLogger) beerTemp.testTempAndLog() self.assertFalse(heatingElement.isActive()) calls = [call.write(json.dumps({"active": False, "sensorId": "sensorid1", "temp": 18.0, "time": 1234})), call.write(json.dumps({"active": False, "sensorId": "sensorid2", "temp": 22.0, "time": 1234})), call.write(json.dumps({"active": False, "sensorId": "sensorid3", "temp": 21.0, "time": 1234}))] tempLogger._write.assert_has_calls(calls) tempProvider3.getTemp = mock.Mock(return_value=19.8) time.time = mock.Mock(return_value=5678) beerTemp.testTempAndLog() self.assertTrue(heatingElement.isActive()) calls = [call.write(json.dumps({"active": True, "sensorId": "sensorid1", "temp": 18.0, "time": 5678})), call.write(json.dumps({"active": True, "sensorId": "sensorid2", "temp": 22.0, "time": 5678})), call.write(json.dumps({"active": True, "sensorId": "sensorid3", "temp": 19.8, "time": 5678}))] tempLogger._write.assert_has_calls(calls)