2021-03-06 17:03:50 +01:00
|
|
|
import pytest
|
|
|
|
|
2018-09-27 01:52:35 +02:00
|
|
|
from platypush.event.hook import EventCondition
|
2023-04-26 01:45:58 +02:00
|
|
|
from platypush.message.event.assistant import SpeechRecognizedEvent
|
2018-01-03 00:16:01 +01:00
|
|
|
from platypush.message.event.ping import PingEvent
|
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_event_parse():
|
|
|
|
"""
|
|
|
|
Test for the events/conditions matching logic.
|
|
|
|
"""
|
2023-04-26 01:45:58 +02:00
|
|
|
condition = EventCondition.build(
|
|
|
|
{
|
|
|
|
'type': 'platypush.message.event.ping.PingEvent',
|
|
|
|
'message': 'This is a test message',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
event = PingEvent(message=condition.args['message'])
|
|
|
|
result = event.matches_condition(condition)
|
|
|
|
assert result.is_match
|
|
|
|
|
|
|
|
event = PingEvent(message="This is not a test message")
|
|
|
|
result = event.matches_condition(condition)
|
|
|
|
assert not result.is_match
|
|
|
|
|
|
|
|
|
|
|
|
def test_nested_event_condition():
|
|
|
|
"""
|
|
|
|
Verify that nested event conditions work as expected.
|
|
|
|
"""
|
|
|
|
condition = EventCondition.build(
|
|
|
|
{
|
|
|
|
'type': 'platypush.message.event.ping.PingEvent',
|
|
|
|
'message': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
event = PingEvent(
|
|
|
|
message={
|
|
|
|
'foo': 'bar',
|
|
|
|
'baz': 'clang',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert event.matches_condition(condition).is_match
|
|
|
|
|
|
|
|
event = PingEvent(
|
|
|
|
message={
|
|
|
|
'something': 'else',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert not event.matches_condition(condition).is_match
|
|
|
|
|
|
|
|
event = PingEvent(
|
|
|
|
message={
|
|
|
|
'foo': 'baz',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert not event.matches_condition(condition).is_match
|
|
|
|
|
|
|
|
|
|
|
|
def test_speech_recognized_event_parse():
|
|
|
|
"""
|
|
|
|
Test the event parsing and text extraction logic for the
|
|
|
|
SpeechRecognizedEvent.
|
|
|
|
"""
|
|
|
|
condition = EventCondition.build(
|
|
|
|
{
|
|
|
|
'type': 'platypush.message.event.assistant.SpeechRecognizedEvent',
|
|
|
|
'phrase': 'This is (the)? answer: ${answer}',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
event = SpeechRecognizedEvent(phrase="GARBAGE GARBAGE this is the answer: 42")
|
2021-03-06 16:21:28 +01:00
|
|
|
result = event.matches_condition(condition)
|
|
|
|
assert result.is_match
|
|
|
|
assert 'answer' in result.parsed_args
|
|
|
|
assert result.parsed_args['answer'] == '42'
|
2018-01-03 00:16:01 +01:00
|
|
|
|
2023-04-26 01:45:58 +02:00
|
|
|
event = PingEvent(phrase="what is not the answer? 43")
|
2021-03-06 16:21:28 +01:00
|
|
|
result = event.matches_condition(condition)
|
|
|
|
assert not result.is_match
|
2018-01-03 00:16:01 +01:00
|
|
|
|
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
pytest.main()
|
|
|
|
|
|
|
|
|
2018-01-03 00:16:01 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|