def meguca_standard_plg(mock_plg, general_config): """A Meguca instance with a mock plugin which behaves like a real one.""" def get_plugins(category=None): if category == 'Collector': return [mock_plg('Collector1', 'c1'), mock_plg('Collector2', 'c2')] elif category == 'Stat': return [mock_plg('Stat1', 's1'), mock_plg('Stat2', 's2')] elif category == 'View': return [mock_plg('View1', 'v1'), mock_plg('View2', 'v2')] elif category == 'Service': return [mock_plg('Service1', 'sv1', service=True), mock_plg('Service2', 'sv2', service=True)] else: return [mock_plg('Test1', 't1'), mock_plg('Test2', 't2')] plg_manager = mock.Mock(get_plugins=mock.Mock(side_effect=get_plugins), get_all_plugins=mock.Mock(side_effect=get_plugins)) general_config.update({'plugin_schedule': {'c1': {'schedule_mode': 'interval', 'seconds': 6}, 'c2': {'schedule_mode': 'interval', 'seconds': 8}, 'v1': {'schedule_mode': 'interval', 'seconds': 10}, 'v2': {'schedule_mode': 'interval', 'seconds': 12}}, 'dry_run': {'enable': False, 'plugins': ['t1', 't2']}}) meguca_ins = meguca.Meguca(plg_manager, general_config, None) return meguca_ins
def test_run_stat_plugins_with_plugin_indexes_not_yet_exist_key_of_data_dict(self, mock_plg): def stub_run_1(): return {'TestData1': 'Test Data'} def stub_run_2(data): return {'TestData2': data['TestData1']} mock_plg_config_1 = configparser.ConfigParser() mock_plg_config_1['Core'] = {'Id': '1'} mock_plg_1 = mock.Mock(plugin_object=mock.Mock(run=stub_run_1), details=mock_plg_config_1) mock_plg_config_2 = configparser.ConfigParser() mock_plg_config_2['Core'] = {'Id': '2'} mock_plg_2 = mock.Mock(plugin_object=mock.Mock(run=stub_run_2), details=mock_plg_config_2) plg_manager = mock.Mock(get_plugins=mock.Mock(return_value=[mock_plg_2, mock_plg_1])) general_config = {'general': {'blacklist': []}} meguca_ins = meguca.Meguca(plg_manager, general_config, None) meguca_ins.data = mock.Mock(__getitem__=mock.Mock(), update=mock.Mock()) type(meguca_ins.data).raise_notyetexist = mock.PropertyMock(return_value=False) meguca_ins.run_stat_plugins('run') meguca_ins.data.__getitem__.assert_called_with('TestData1') meguca_ins.data.update.assert_called_with({'TestData1': 'Test Data'})
def meguca_dummy_plg(general_config): """A Meguca instance with a dummy plugin.""" plg_manager = mock.Mock(get_plugins=mock.Mock(return_value=[mock.Mock()])) meguca_ins = meguca.Meguca(plg_manager, general_config, None) return meguca_ins
def test_schedule_with_a_method_with_kwargs(self): meguca_ins = meguca.Meguca(mock.Mock(), {}, {}) mock_method = mock.Mock() config = {'schedule_mode': 'interval', 'seconds': 1} meguca_ins.schedule(mock_method, kwargs={'Test': 'Test'}, name='Test Method', schedule_config=config) assert meguca_ins.scheduler.get_jobs()[0].kwargs == {'Test': 'Test'} assert str(meguca_ins.scheduler.get_jobs()[0].trigger) == 'interval[0:00:01]'
def test_dry_run_meguca_real_plugins_and_config(self): general_config = utils.load_config('tests/resources/general_config_dryrun.toml') plg_manager = plugin.PlgManager('tests/resources/plugins', 'plugin') plugin_config = plg_manager.load_plugins() meguca_ins = meguca.Meguca(plg_manager, general_config, plugin_config) meguca_ins.prepare() meguca_ins.run() assert meguca_ins.data == {'MadokaDry': 'TestVal', 'SayakaPrep': 'TestVal', 'SayakaDry': 'TestVal'}
def test_load_services(self): def get(): return 'Test' plg_info = configparser.ConfigParser() plg_info['Core'] = {'Id': 'Test'} mock_plg = mock.Mock(plugin_object=mock.Mock(get=get), details=plg_info) plg_manager = mock.Mock(get_plugins=mock.Mock(return_value=[mock_plg])) meguca_ins = meguca.Meguca(plg_manager, {}, {}) meguca_ins.load_services() assert meguca_ins.services == {'Test': 'Test'}
def test_run_plugin_with_entry_method_has_params(self, mock_plg): meguca_ins = meguca.Meguca(mock.Mock(), {}, {}) meguca_ins.data = {'TestData': 'Test Data'} def stub_run(data): return {'Test': data['TestData']} mock_plg = mock_plg() mock_plg.plugin_object.run = stub_run meguca_ins.run_plugin(mock_plg, 'run') assert meguca_ins.data['Test'] == 'Test Data'
def test_run_plugin_with_entry_method_has_service_params(self, mock_plg): meguca_ins = meguca.Meguca(mock.Mock(), {}, {}) meguca_ins.services = {'service': mock.Mock(return_value='Test')} def stub_run(service): return {'Test': service()} mock_plg = mock_plg() mock_plg.plugin_object.run = stub_run meguca_ins.run_plugin(mock_plg, 'run') assert meguca_ins.data['Test'] == 'Test'
def test_run_meguca_with_real_plugins_and_config(self): general_config = utils.load_config('tests/resources/general_config.toml') plg_manager = plugin.PlgManager('tests/resources/plugins', 'plugin') plugin_config = plg_manager.load_plugins() meguca_ins = meguca.Meguca(plg_manager, general_config, plugin_config) meguca_ins.prepare() meguca_ins.run() time.sleep(8) assert meguca_ins.data == {'Homura': 'TestVal', 'Madoka': 'TestVal', 'Sayaka': 'TestVal', 'SayakaPrep': 'TestVal'}
def test_run_plugin_with_plugin_indexes_non_existent_key_of_data_dict(self, run_plugin, mock_plg): meguca_ins = meguca.Meguca(mock.Mock(), {}, {}) with pytest.raises(exceptions.NotFound): meguca_ins.run_plugin(mock_plg())
def test_run_plugin_with_entry_method_has_no_param(self, mock_plg): meguca_ins = meguca.Meguca(mock.Mock(), {}, {}) meguca_ins.run_plugin(mock_plg(), 'run') assert meguca_ins.data == {'Test': 'Test'}