async def test_trigger_data_importer_validate_exception(self, monkeypatch): bot = 'test_events' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) os.mkdir(test_data_path) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) DataImporterLogProcessor.add_log(bot, user, files_received=REQUIREMENTS - {"http_actions"}) await EventsTrigger.trigger_data_importer(bot, user, False, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 2 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert logs[0].get('exception') == 'Some training files are absent!' assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Failure' assert logs[0]['event_status'] == EVENT_STATUS.FAIL.value
async def test_trigger_data_importer_validate_file_with_errors( self, monkeypatch): bot = 'test_events' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) shutil.copytree('tests/testing_data/validator/intent_name_mismatch', test_data_path) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) DataImporterLogProcessor.add_log(bot, user, files_received=REQUIREMENTS - {"http_actions"}) await EventsTrigger.trigger_data_importer(bot, user, True, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 4 assert logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Failure' assert logs[0]['event_status'] == EVENT_STATUS.COMPLETED.value
async def test_trigger_data_importer_event_connection_error( self, monkeypatch): bot = 'test_events_bot_1' user = '******' event_url = "http://url.event4" monkeypatch.setitem(Utility.environment['model']['data_importer'], "event_url", event_url) await EventsTrigger.trigger_data_importer(bot, user, False, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 1 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert logs[0].get('exception') == 'Failed to trigger the event.' assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Failure' assert logs[0]['event_status'] == EVENT_STATUS.FAIL.value
def test_add_log_success(self): bot = 'test' user = '******' DataImporterLogProcessor.add_log(bot, user, files_received=list( REQUIREMENTS.copy()), is_data_uploaded=False) DataImporterLogProcessor.add_log( bot, user, status='Success', event_status=EVENT_STATUS.COMPLETED.value) log = list(DataImporterLogProcessor.get_logs(bot)) assert not log[0].get('intents').get('data') assert not log[0].get('stories').get('data') assert not log[0].get('utterances').get('data') assert not log[0].get('http_actions').get('data') assert not log[0].get('training_examples').get('data') assert not log[0].get('domain').get('data') assert not log[0].get('config').get('data') assert not log[0].get('exception') assert not log[0]['is_data_uploaded'] assert log[0]['start_timestamp'] assert log[0].get('end_timestamp') assert all(file in log[0]['files_received'] for file in REQUIREMENTS) assert log[0].get('status') == 'Success' assert log[0]['event_status'] == EVENT_STATUS.COMPLETED.value
def test_update_log_create_new(self): bot = 'test' user = '******' count = COMPONENT_COUNT.copy() count['http_action'] = 6 count['domain']['intents'] = 12 summary = { 'intents': ['Intent not added to domain'], 'config': ['Invalid component'] } DataImporterLogProcessor.update_summary(bot, user, count, summary) log = next(DataImporterLogProcessor.get_logs(bot)) assert log.get('intents').get('data') == ['Intent not added to domain'] assert not log.get('stories').get('data') assert not log.get('utterances').get('data') assert not log.get('http_actions').get('data') assert not log.get('training_examples').get('data') assert not log.get('domain').get('data') assert log.get('config').get('data') == ['Invalid component'] assert not log.get('exception') assert not log['is_data_uploaded'] assert log['start_timestamp'] assert log.get('end_timestamp') assert not log.get('validation_status') assert log['event_status'] == EVENT_STATUS.COMPLETED.value
async def get_data_importer_logs(current_user: User = Depends( auth.get_current_user_and_bot)): """ Get data importer event logs. """ logs = list(DataImporterLogProcessor.get_logs(current_user.get_bot())) return Response(data=logs)
async def test_trigger_data_importer_rules_only(self, monkeypatch, get_training_data): bot = 'test_trigger_data_importer_rules_only' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) data_path = os.path.join(test_data_path, 'data') Utility.make_dirs(data_path) shutil.copy2('tests/testing_data/validator/valid/data/rules.yml', data_path) nlu, story_graph, domain, config, http_actions = await get_training_data( 'tests/testing_data/validator/valid') mongo_processor = MongoProcessor() mongo_processor.save_domain(domain, bot, user) mongo_processor.save_nlu(nlu, bot, user) config["bot"] = bot config["user"] = user config_obj = Configs._from_son(config) config_obj.save() mongo_processor.save_stories(story_graph.story_steps, bot, user) mongo_processor.save_http_action(http_actions, bot, user) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) DataImporterLogProcessor.add_log(bot, user, files_received=["rules"]) await EventsTrigger.trigger_data_importer(bot, user, True, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 1 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Success' assert logs[0]['event_status'] == EVENT_STATUS.COMPLETED.value assert len(mongo_processor.fetch_stories(bot)) == 2 assert len(list(mongo_processor.fetch_training_examples(bot))) == 7 assert len(list(mongo_processor.fetch_responses(bot))) == 2 assert len(mongo_processor.fetch_actions(bot)) == 2 assert len(mongo_processor.fetch_rule_block_names(bot)) == 3
async def test_trigger_data_importer_validate_and_save_event_overwrite( self, monkeypatch): bot = 'test_events_bot_1' user = '******' event_url = "http://url.event2" monkeypatch.setitem(Utility.environment['model']['data_importer'], "event_url", event_url) responses.add( "POST", event_url, json={"message": "Event triggered successfully!"}, status=200, match=[ responses.json_params_matcher([{ 'name': 'BOT', 'value': bot }, { 'name': 'USER', 'value': user }, { 'name': 'IMPORT_DATA', 'value': '--import-data' }, { 'name': 'OVERWRITE', 'value': '--overwrite' }]) ], ) responses.start() await EventsTrigger.trigger_data_importer(bot, user, True, True) responses.stop() request = json.loads(responses.calls[1].request.body) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 1 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert not logs[0].get('end_timestamp') assert not logs[0].get('status') assert logs[0]['event_status'] == EVENT_STATUS.TASKSPAWNED.value
async def test_trigger_data_importer_validate_and_save_append( self, monkeypatch): bot = 'test_events' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) shutil.copytree('tests/testing_data/validator/append', test_data_path) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) DataImporterLogProcessor.add_log(bot, user, files_received=REQUIREMENTS - {"http_actions", "rules"}) await EventsTrigger.trigger_data_importer(bot, user, True, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 6 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Success' assert logs[0]['event_status'] == EVENT_STATUS.COMPLETED.value processor = MongoProcessor() assert 'greet' in processor.fetch_intents(bot) assert 'deny' in processor.fetch_intents(bot) assert 'location' in processor.fetch_intents(bot) assert 'affirm' in processor.fetch_intents(bot) assert len(processor.fetch_stories(bot)) == 4 assert len(list(processor.fetch_training_examples(bot))) == 13 assert len(list(processor.fetch_responses(bot))) == 6 assert len(processor.fetch_actions(bot)) == 4 assert len(processor.fetch_rule_block_names(bot)) == 4
async def test_trigger_data_importer_import_with_intent_issues( self, monkeypatch): bot = 'test_trigger_data_importer_import_with_intent_issues' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) shutil.copytree('tests/testing_data/validator/intent_name_mismatch', test_data_path) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) BotSettings(ignore_utterances=True, bot=bot, user=user).save() DataImporterLogProcessor.add_log( bot, user, files_received=['nlu', 'stories', 'domain', 'config']) await EventsTrigger.trigger_data_importer(bot, user, True, True) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 1 assert logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Failure' assert logs[0]['event_status'] == EVENT_STATUS.COMPLETED.value mongo_processor = MongoProcessor() assert len(mongo_processor.fetch_stories(bot)) == 0 assert len(list(mongo_processor.fetch_training_examples(bot))) == 0 assert len(list(mongo_processor.fetch_responses(bot))) == 0 assert len(mongo_processor.fetch_actions(bot)) == 0 assert len(mongo_processor.fetch_rule_block_names(bot)) == 0
async def test_trigger_data_importer_validate_existing_data( self, monkeypatch, get_training_data): bot = 'test_trigger_data_importer_domain_only' user = '******' test_data_path = os.path.join(pytest.tmp_dir, str(datetime.utcnow())) Utility.make_dirs(test_data_path) def _path(*args, **kwargs): return test_data_path monkeypatch.setattr(Utility, "get_latest_file", _path) DataImporterLogProcessor.add_log(bot, user) await EventsTrigger.trigger_data_importer(bot, user, True, False) logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 2 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not logs[0].get('http_actions').get('data') assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert logs[0]['end_timestamp'] assert logs[0]['status'] == 'Success' assert logs[0]['event_status'] == EVENT_STATUS.COMPLETED.value mongo_processor = MongoProcessor() assert len(mongo_processor.fetch_stories(bot)) == 2 assert len(list(mongo_processor.fetch_training_examples(bot))) == 7 assert len(list(mongo_processor.fetch_responses(bot))) == 2 assert len(mongo_processor.fetch_actions(bot)) == 2 assert len(mongo_processor.fetch_rule_block_names(bot)) == 3
def test_get_logs(self): bot = 'test' logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 3