async def test_match_regex(self): with OpsDroid() as opsdroid: regex = r"(.*)" decorator = matchers.match_regex(regex) opsdroid.skills.append(decorator(await self.getMockSkill())) self.assertEqual(len(opsdroid.skills), 1) self.assertEqual(opsdroid.skills[0].matchers[0]["regex"]["expression"], regex) self.assertTrue(asyncio.iscoroutinefunction(opsdroid.skills[0]))
async def test_parse_regex(self): with OpsDroid() as opsdroid: mock_skill = await self.getMockSkill() opsdroid.skills.append(match_regex(r"(.*)")(mock_skill)) mock_connector = amock.CoroutineMock() message = Message("Hello world", "user", "default", mock_connector) skills = await parse_regex(opsdroid, opsdroid.skills, message) self.assertEqual(mock_skill, skills[0]["skill"])
async def test_constrain_users_skips(self): with OpsDroid() as opsdroid: opsdroid.eventloop = mock.CoroutineMock() skill = await self.getMockSkill() skill = match_regex(r'.*')(skill) skill = constraints.constrain_users(['user'])(skill) opsdroid.skills.append(skill) tasks = await opsdroid.parse( Message('Hello', 'user', '#general', None) ) self.assertEqual(len(tasks), 2) # match_always and the skill
async def test_parse_regex_insensitive(self): with OpsDroid() as opsdroid: regex = r"Hello .*" mock_connector = Connector({}, opsdroid=opsdroid) mock_connector.send = amock.CoroutineMock() skill = await self.getMockSkill() opsdroid.skills.append(match_regex(regex, case_sensitive=False)(skill)) message = Message("HELLO world", "user", "default", mock_connector) tasks = await opsdroid.parse(message) for task in tasks: await task self.assertTrue(mock_connector.send.called)
async def test_parse_regex_matching_condition_match(self): with OpsDroid() as opsdroid: regex = r"(.*)" mock_skill_matching_match = await self.getMockSkill() opsdroid.skills.append(match_regex(regex, matching_condition="match")(mock_skill_matching_match)) mock_connector = amock.CoroutineMock() message = Message("Hello world", "user", "default", mock_connector) skills = await opsdroid.get_ranked_skills(opsdroid.skills, message) self.assertEqual(mock_skill_matching_match, skills[0]["skill"])
async def test_parse_regex_priority_high(self): with OpsDroid() as opsdroid: regex = r"(.*)" mock_skill_high = await self.getMockSkill() opsdroid.skills.append(match_regex(regex, score_factor=1)(mock_skill_high)) mock_connector = amock.CoroutineMock() message = Message("Hello world", "user", "default", mock_connector) skills = await opsdroid.get_ranked_skills(opsdroid.skills, message) self.assertEqual(mock_skill_high, skills[0]["skill"])
async def test_constrain_connectors_constrains(self): with OpsDroid() as opsdroid: opsdroid.eventloop = mock.CoroutineMock() skill = await self.getMockSkill() skill = match_regex(r'.*')(skill) skill = constraints.constrain_connectors(['slack'])(skill) opsdroid.skills.append(skill) connector = mock.Mock() connector.configure_mock(name='twitter') tasks = await opsdroid.parse( Message('Hello', 'user', '#random', connector) ) self.assertEqual(len(tasks), 1) # Just match_always
async def test_constrain_connectors_skips(self): with OpsDroid() as opsdroid: skill = await self.getMockSkill() skill = match_regex(r".*")(skill) skill = constraints.constrain_connectors(["slack"])(skill) opsdroid.skills.append(skill) connector = mock.Mock() connector.configure_mock(name="slack") tasks = await opsdroid.parse( Message(text="Hello", user="******", target="#general", connector=connector)) self.assertEqual(len(tasks), 3) # match_always, match_event and the skill
async def test_parse_regex_insensitive(self): with OpsDroid() as opsdroid: regex = r"Hello .*" mock_connector = Connector({}, opsdroid=opsdroid) mock_connector.send = amock.CoroutineMock() skill = await self.getMockSkill() opsdroid.skills.append( match_regex(regex, case_sensitive=False)(skill)) message = Message( text="HELLO world", user="******", target="default", connector=mock_connector, ) await opsdroid.parse(message) self.assertTrue(mock_connector.send.called)
async def test_parse_regex_method_skill(self): with OpsDroid() as opsdroid: regex = r"Hello .*" mock_connector = Connector({}, opsdroid=opsdroid) mock_connector.send = amock.CoroutineMock() skill = await self.getMockMethodSkill() opsdroid.skills.append(match_regex(regex)(skill)) message = Message( text="Hello world", user="******", target="default", connector=mock_connector, ) tasks = await opsdroid.parse(message) for task in tasks: await task self.assertTrue(mock_connector.send.called)
async def test_constrain_connectors_inverted(self): with OpsDroid() as opsdroid: opsdroid.eventloop = mock.CoroutineMock() skill = await self.getMockSkill() skill = match_regex(r".*")(skill) skill = constraints.constrain_connectors(["slack"], invert=True)(skill) opsdroid.skills.append(skill) connector = mock.Mock() connector.configure_mock(name="slack") tasks = await opsdroid.parse( Message(text="Hello", user="******", target="#general", connector=connector)) self.assertEqual(len(tasks), 1) # match_always only
async def test_parse_regex_named_groups_entities(self): with OpsDroid() as opsdroid: regex = r"Hello (?P<name>.*)" mock_skill_named_groups = await self.getMockSkill() opsdroid.skills.append(match_regex(regex)(mock_skill_named_groups)) mock_connector = amock.CoroutineMock() message = Message("Hello opsdroid", "user", "default", mock_connector) [skill] = await opsdroid.get_ranked_skills(opsdroid.skills, message) parsed_message = skill["message"] self.assertEqual(len(parsed_message.entities.keys()), 1) self.assertTrue("name" in parsed_message.entities.keys()) self.assertEqual(parsed_message.entities["name"]["value"], "opsdroid")
async def test_constraint_can_be_called_after_skip(self): with OpsDroid() as opsdroid: opsdroid.eventloop = mock.CoroutineMock() skill = await self.getMockSkill() skill = match_regex(r".*")(skill) skill = constraints.constrain_users(["user"])(skill) opsdroid.skills.append(skill) tasks = await opsdroid.parse(Message("Hello", "user", "#general", None)) self.assertEqual(len(tasks), 2) # match_always and the skill tasks = await opsdroid.parse( Message("Hello", "otheruser", "#general", None) ) self.assertEqual(len(tasks), 1) # Just match_always tasks = await opsdroid.parse(Message("Hello", "user", "#general", None)) self.assertEqual(len(tasks), 2) # match_always and the skill
async def test_constraint_can_be_called_after_skip(self): with OpsDroid() as opsdroid: skill = await self.getMockSkill() skill = match_regex(r".*")(skill) skill = constraints.constrain_users(["user"])(skill) opsdroid.skills.append(skill) tasks = await opsdroid.parse( Message(text="Hello", user="******", target="#general", connector=None) ) self.assertEqual(len(tasks), 3) # match_always, match_event and the skill tasks = await opsdroid.parse( Message( text="Hello", user="******", target="#general", connector=None ) ) self.assertEqual(len(tasks), 3) # Just match_always and match_event tasks = await opsdroid.parse( Message(text="Hello", user="******", target="#general", connector=None) ) self.assertEqual(len(tasks), 3) # match_always, match_event and the skill
async def erro34(opsdroid, config, message): if match_regex(r'exit 34|exit status 34|erro 34') == "exit 34": message.respond("Tudo bem, meu bacano? \n") await message.respond('')