def test_add_endpoints_add_tracker_endpoint(self): processor = MongoProcessor() config = { "tracker_endpoint": { "url": "mongodb://localhost:27017/", "db": "conversations", } } processor.add_endpoints(config, bot="tests3", user="******") endpoint = processor.get_endpoints("tests3") assert endpoint.get("bot_endpoint") is None assert endpoint.get("action_endpoint") is None assert (endpoint.get("tracker_endpoint").get("url") == "mongodb://localhost:27017/") assert endpoint.get("tracker_endpoint").get("db") == "conversations" assert endpoint.get("tracker_endpoint").get("type") == "mongo"
def test_delete_training_example(self): processor = MongoProcessor() training_examples = TrainingExamples.objects(bot="tests", intent="get_priority", status=True) expected_length = training_examples.__len__() - 1 training_example = training_examples[0] expected_text = training_example.text processor.remove_document(TrainingExamples, training_example.id, "tests", "testUser") new_training_examples = list( processor.get_training_examples(intent="get_priority", bot="tests")) assert new_training_examples.__len__() == expected_length assert any(expected_text != example["text"] for example in new_training_examples)
def test_update_session_config(self): processor = MongoProcessor() session_config = processor.get_session_config("tests") assert session_config assert all(session_config[key] for key in ["sesssionExpirationTime", "carryOverSlots"]) id = processor.add_session_config( id=session_config["_id"], sesssionExpirationTime=30, carryOverSlots=False, bot="tests", user="******", ) assert id == session_config["_id"] session_config = processor.get_session_config("tests") assert session_config["sesssionExpirationTime"] == 30 assert session_config["carryOverSlots"] is False
def test_load_from_path_all_sccenario(self): processor = MongoProcessor() processor.save_from_path("tests/testing_data/all", "all", "testUser") training_data = processor.load_nlu("all") assert isinstance(training_data, TrainingData) assert training_data.training_examples.__len__() == 283 assert training_data.entity_synonyms.__len__() == 3 assert training_data.regex_features.__len__() == 5 assert training_data.lookup_tables.__len__() == 1 story_graph = processor.load_stories("all") assert isinstance(story_graph, StoryGraph) == True assert story_graph.story_steps.__len__() == 13 domain = processor.load_domain("all") assert isinstance(domain, Domain) assert domain.slots.__len__() == 8 assert domain.templates.keys().__len__() == 21 assert domain.entities.__len__() == 7 assert domain.form_names.__len__() == 2 assert domain.user_actions.__len__() == 32 assert domain.intents.__len__() == 22 assert not Utility.check_empty_string( domain.templates["utter_cheer_up"][0]["image"] ) assert domain.templates["utter_did_that_help"][0]["buttons"].__len__() == 2 assert domain.templates["utter_offer_help"][0]["custom"] assert domain.slots[0].type_name == "unfeaturized"
def test_add_story_end_with_user(self): processor = MongoProcessor() events = [ { "name": "greeting", "type": "user" }, { "name": "utter_greet", "type": "action" }, { "name": "mood_great", "type": "user" }, ] with pytest.raises(ValidationError): processor.add_story("greeting", events, "tests", "testUser")
def add_user(): Utility.load_evironment() connect(Utility.environment["mongo_db"], host=Utility.environment["mongo_url"]) user = AccountProcessor.account_setup({"email": "*****@*****.**", "first_name": "Demo", "last_name": "User", "password": "******", "account": "integration", "bot": "integration"}, user="******") processor = MongoProcessor() processor.save_from_path("tests/testing_data/all", user['bot'], "testAdmin") AccountProcessor.account_setup({"email": "*****@*****.**", "first_name": "Demo", "last_name": "User", "password": "******", "account": "integration2", "bot": "integration2"}, user="******")
def test_update_endpoints_any(self): processor = MongoProcessor() config = { "action_endpoint": { "url": "http://127.0.0.1:8000/" }, "bot_endpoint": { "url": "http://127.0.0.1:5000/" }, } processor.add_endpoints(config, bot="tests", user="******") endpoint = processor.get_endpoints("tests") assert endpoint.get("bot_endpoint").get( "url") == "http://127.0.0.1:5000/" assert endpoint.get("action_endpoint").get( "url") == "http://127.0.0.1:8000/" assert (endpoint.get("tracker_endpoint").get("url") == "mongodb://localhost:27017/") assert endpoint.get("tracker_endpoint").get("db") == "conversations" assert endpoint.get("tracker_endpoint").get("type") == "mongo"
def test_add_story(self): processor = MongoProcessor() events = [ { "name": "greet", "type": "user" }, { "name": "utter_greet", "type": "action" }, { "name": "mood_great", "type": "user" }, { "name": "utter_greet", "type": "action" }, ] processor.add_story("happy path", events, "tests", "testUser")
def test_add_training_example_with_entity(self): processor = MongoProcessor() results = list(processor.add_training_example( ["Log a [critical issue](priority)"], "get_priority", "tests", "testUser" )) assert results[0]['_id'] assert results[0]['text'] == "Log a [critical issue](priority)" assert results[0]['message'] == "Training Example added successfully!" intents = processor.get_intents("tests") assert any("get_priority" == intent['name'] for intent in intents) entities = processor.get_entities("tests") assert any("priority" == entity['name'] for entity in entities) new_training_example = TrainingExamples.objects(bot="tests").get( text="Log a critical issue" ) slots = Slots.objects(bot="tests") new_slot = slots.get(name="priority") assert slots.__len__() == 1 assert new_slot.name == "priority" assert new_slot.type == "text" assert new_training_example.text == "Log a critical issue"
def train_model_for_bot(bot: str): """ Trains the rasa model, using the data that is loaded onto Mongo, through the bot files """ processor = MongoProcessor() nlu = processor.load_nlu(bot) if not nlu.training_examples: raise AppException("Training data does not exists!") domain = processor.load_domain(bot) stories = processor.load_stories(bot) config = processor.load_config(bot) directory = Utility.save_files( nlu.nlu_as_markdown().encode(), domain.as_yaml().encode(), stories.as_story_string().encode(), yaml.dump(config).encode(), ) output = os.path.join(DEFAULT_MODELS_PATH, bot) model = train(domain=os.path.join(directory,DEFAULT_DOMAIN_PATH), config=os.path.join(directory,DEFAULT_CONFIG_PATH), training_files=os.path.join(directory,DEFAULT_DATA_PATH), output=output) Utility.delete_directory(directory) return model
def test_get_training_examples_with_entities(self): processor = MongoProcessor() processor.add_training_example( "Make [TKT456](ticketID) a [critical issue](priority)", "get_priority", "tests", "testUser", ) actual = list(processor.get_training_examples("get_priority", "tests")) slots = Slots.objects(bot="tests") new_slot = slots.get(name="ticketID") assert any([ value["text"] == "Log a [critical issue](priority)" for value in actual ]) assert any([ value["text"] == "Make [TKT456](ticketID) a [critical issue](priority)" for value in actual ]) assert slots.__len__() == 2 assert new_slot.name == "ticketID" assert new_slot.type == "text" expected = [ "hey", "hello", "hi", "good morning", "good evening", "hey there" ] actual = list(processor.get_training_examples("greet", "tests")) assert actual.__len__() == expected.__len__() assert all(a_val["text"] in expected for a_val in actual)
def test_add_duplicate_story(self): processor = MongoProcessor() events = [ { "name": "greet", "type": "user" }, { "name": "utter_greet", "type": "action" }, { "name": "mood_great", "type": "user" }, { "name": "utter_greet", "type": "action" }, ] with pytest.raises(Exception): processor.add_story("happy path", events, "tests", "testUser")
def test_add_blank_story_name(self): processor = MongoProcessor() events = [ { "name": "greeting", "type": "user" }, { "name": "utter_greet", "type": "action" }, { "name": "mood_great", "type": "user" }, { "name": "utter_greet", "type": "action" }, ] with pytest.raises(AssertionError): processor.add_story(" ", events, "tests", "testUser")
def default_account_setup(): account = { "account": "DemoAccount", "bot": "Demo", "email": "*****@*****.**", "first_name": "Test_First", "last_name": "Test_Last", "password": "******" } try: user = AccountProcessor.account_setup(account, user="******") if user: MongoProcessor().save_from_path('template/', user['bot'], user="******") return user except Exception as e: logging.info(str(e))
def test_delete_text_response(self): processor = MongoProcessor() responses = list( processor.get_response(name="utter_happy", bot="tests")) expected_length = responses.__len__() - 1 response = responses[0] expected_text = response['value']['text'] processor.remove_document(Responses, response['_id'], "tests", "testUser") actual = list(processor.get_response("utter_happy", "tests")) assert actual.__len__() == expected_length assert all(expected_text != item["value"]["text"] for item in actual if "text" in item["value"])
def test_add_none_response_name(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_text_response("Greet", None, "tests", "testUser")
def test_add_blank_response_name(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_text_response("Welcome", " ", "tests", "testUser")
def test_add_text_response_duplicate(self): processor = MongoProcessor() with pytest.raises(Exception): processor.add_text_response("Great", "utter_happy", "tests", "testUser")
def test_add_blank_text_response(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_text_response("", "utter_happy", "tests", "testUser")
def test_add_empty_story_event(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_story("happy path", [], "tests", "testUser")
def test_add_blank_action(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_action(" ", "tests", "testUser")
def test_add_action_duplicate(self): processor = MongoProcessor() with pytest.raises(Exception): assert processor.add_action("utter_priority", "tests", "testUser") == None
def test_add_action(self): processor = MongoProcessor() assert processor.add_action("utter_priority", "tests", "testUser") == None action = Actions.objects(bot="tests").get(name="utter_priority") assert action.name == "utter_priority"
def test_add_empty_entity(self): processor = MongoProcessor() with pytest.raises(ValidationError): processor.add_entity("", "tests", "testUser")
def test_add_entity_duplicate(self): processor = MongoProcessor() with pytest.raises(Exception): assert processor.add_entity("file_text", "tests", "testUser")
def test_load_stories(self): processor = MongoProcessor() story_graph = processor.load_stories("tests") assert isinstance(story_graph, StoryGraph) assert story_graph.story_steps.__len__() == 5
def test_add_session_config(self): processor = MongoProcessor() id = processor.add_session_config( sesssionExpirationTime=30, carryOverSlots=False, bot="test", user="******" ) assert id
def test_get_entities(self): processor = MongoProcessor() expected = ["priority", "file_text", "ticketID"] actual = processor.get_entities("tests") assert actual.__len__() == expected.__len__() assert all(item["name"] in expected for item in actual)
def test_add_intent(self): processor = MongoProcessor() assert processor.add_intent("greeting", "tests", "testUser") intent = Intents.objects(bot="tests").get(name="greeting") assert intent.name == "greeting"
def test_load_from_path_error(self): processor = MongoProcessor() with pytest.raises(Exception): processor.save_from_path("tests/testing_data/error", "tests", "testUser")