Skip to content

Commit

Permalink
port extra tests from ovos-core
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 15, 2024
1 parent 66e5d8a commit edcc0ea
Show file tree
Hide file tree
Showing 5 changed files with 590 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/unittests/test_common_query_skill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from unittest import TestCase, mock

from ovos_bus_client.message import Message

from ovos_workshop.skills.common_query_skill import CommonQuerySkill


class AnyCallable:
"""Class matching any callable.
Useful for assert_called_with arguments.
"""
def __eq__(self, other):
return callable(other)



class TestCommonQuerySkill(TestCase):
def setUp(self):
self.skill = CQSTest()
self.bus = mock.Mock(name='bus')
self.skill.bind(self.bus)
self.skill.config_core = {'enclosure': {'platform': 'mycroft_mark_1'}}

def test_lifecycle(self):
"""Test startup and shutdown."""
skill = CQSTest()
bus = mock.Mock(name='bus')
skill.bind(bus)
bus.on.assert_any_call('question:query', AnyCallable())
bus.on.assert_any_call('question:action', AnyCallable())
skill.shutdown()

def test_common_test_skill_action(self):
"""Test that the optional action is triggered."""
query_action = self.bus.on.call_args_list[-2][0][1]
query_action(Message('query:action', data={
'phrase': 'What\'s the meaning of life',
'skill_id': 'asdf'}))
self.skill.CQS_action.assert_not_called()
query_action(Message('query:action', data={
'phrase': 'What\'s the meaning of life',
'skill_id': 'CQSTest'}))
self.skill.CQS_action.assert_called_once_with(
'What\'s the meaning of life', {})


class CQSTest(CommonQuerySkill):
"""Simple skill for testing the CommonQuerySkill"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.CQS_match_query_phrase = mock.Mock(name='match_phrase')
self.CQS_action = mock.Mock(name='selected_action')
self.skill_id = 'CQSTest'

def CQS_match_query_phrase(self, phrase):
pass

def CQS_action(self, phrase, data):
pass
40 changes: 40 additions & 0 deletions test/unittests/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import TestCase, mock

# TODO - move to ovos-workshop
from ovos_workshop.decorators import adds_context, removes_context


class ContextSkillMock(mock.Mock):
"""Mock class to apply decorators on."""
@adds_context('DestroyContext')
def handler_adding_context(self):
pass

@adds_context('DestroyContext', 'exterminate')
def handler_adding_context_with_words(self):
pass

@removes_context('DestroyContext')
def handler_removing_context(self):
pass


class TestContextDecorators(TestCase):
def test_adding_context(self):
"""Check that calling handler adds the correct Keyword."""
skill = ContextSkillMock()
skill.handler_adding_context()
skill.set_context.assert_called_once_with('DestroyContext', '')

def test_adding_context_with_words(self):
"""Ensure that decorated handler adds Keyword and content."""
skill = ContextSkillMock()
skill.handler_adding_context_with_words()
skill.set_context.assert_called_once_with('DestroyContext',
'exterminate')

def test_removing_context(self):
"""Make sure the decorated handler removes the specified context."""
skill = ContextSkillMock()
skill.handler_removing_context()
skill.remove_context.assert_called_once_with('DestroyContext')
145 changes: 145 additions & 0 deletions test/unittests/test_intent_service_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import unittest
# TODO - move test to ovos-workshop
from ovos_workshop.intents import IntentBuilder, IntentServiceInterface


class MockEmitter:
def __init__(self):
self.reset()

def emit(self, message):
self.types.append(message.msg_type)
self.results.append(message.data)

def get_types(self):
return self.types

def get_results(self):
return self.results

def on(self, event, f):
pass

def reset(self):
self.types = []
self.results = []


class KeywordRegistrationTest(unittest.TestCase):
def check_emitter(self, expected_message_data):
"""Verify that the registration messages matches the expected."""
for msg_type in self.emitter.get_types():
self.assertEqual(msg_type, 'register_vocab')
self.assertEqual(
sorted(self.emitter.get_results(),
key=lambda d: sorted(d.items())),
sorted(expected_message_data, key=lambda d: sorted(d.items())))
self.emitter.reset()

def setUp(self):
self.emitter = MockEmitter()

def test_register_keyword(self):
intent_service = IntentServiceInterface(self.emitter)
intent_service.register_adapt_keyword('test_intent', 'test', lang='en-US')
entity_data = {'entity_value': 'test', 'entity_type': 'test_intent', 'lang': 'en-US'}
compatibility_data = {'start': 'test', 'end': 'test_intent'}
expected_data = {**entity_data, **compatibility_data}
self.check_emitter([expected_data])

def test_register_keyword_with_aliases(self):
# TODO 22.02: Remove compatibility data
intent_service = IntentServiceInterface(self.emitter)
intent_service.register_adapt_keyword('test_intent', 'test',
['test2', 'test3'],
lang='en-US')

entity_data = {'entity_value': 'test', 'entity_type': 'test_intent', 'lang': 'en-US'}
compatibility_data = {'start': 'test', 'end': 'test_intent'}
expected_initial_vocab = {**entity_data, **compatibility_data}

alias_data = {
'entity_value': 'test2',
'entity_type': 'test_intent',
'alias_of': 'test',
'lang': 'en-US'
}
alias_compatibility = {'start': 'test2', 'end': 'test_intent'}
expected_alias1 = {**alias_data, **alias_compatibility}

alias_data2 = {
'entity_value': 'test3',
'entity_type': 'test_intent',
'alias_of': 'test',
'lang': 'en-US'
}
alias_compatibility2 = {'start': 'test3', 'end': 'test_intent'}
expected_alias2 = {**alias_data2, **alias_compatibility2}

self.check_emitter([expected_initial_vocab,
expected_alias1,
expected_alias2])

def test_register_regex(self):
intent_service = IntentServiceInterface(self.emitter)
intent_service.register_adapt_regex('.*', lang="en-us")
self.check_emitter([{'regex': '.*', 'lang': 'en-us'}])


class KeywordIntentRegistrationTest(unittest.TestCase):
def check_emitter(self, expected_message_data):
"""Verify that the registration messages matches the expected."""
for msg_type in self.emitter.get_types():
self.assertEqual(msg_type, 'register_intent')
self.assertEqual(
sorted(self.emitter.get_results(),
key=lambda d: sorted(d.items())),
sorted(expected_message_data, key=lambda d: sorted(d.items())))
self.emitter.reset()

def setUp(self):
self.emitter = MockEmitter()

def test_register_intent(self):
intent_service = IntentServiceInterface(self.emitter)
intent_service.register_adapt_keyword('testA', 'testA', lang='en-US')
intent_service.register_adapt_keyword('testB', 'testB', lang='en-US')
self.emitter.reset()

intent = IntentBuilder("test").require("testA").optionally("testB")
intent_service.register_adapt_intent("test", intent)
expected_data = {'at_least_one': [],
'name': 'test',
'excludes': [],
'optional': [('testB', 'testB')],
'requires': [('testA', 'testA')]}
self.check_emitter([expected_data])



class UtteranceIntentRegistrationTest(unittest.TestCase):
def check_emitter(self, expected_message_data):
"""Verify that the registration messages matches the expected."""
for msg_type in self.emitter.get_types():
self.assertEqual(msg_type, 'padatious:register_intent')

self.assertEqual(
sorted(self.emitter.get_results(),
key=lambda d: sorted(d.items())),
sorted(expected_message_data, key=lambda d: sorted(d.items())))
self.emitter.reset()

def setUp(self):
self.emitter = MockEmitter()

def test_register_intent(self):
intent_service = IntentServiceInterface(self.emitter)
filename = "/tmp/test.intent"
with open(filename, "w") as f:
f.write("this is a test\ntest the intent")

intent_service.register_padatious_intent('test', filename, lang='en-US')
expected_data = {'file_name': '/tmp/test.intent', 'lang': 'en-US', 'name': 'test',
'samples': ['this is a test', 'test the intent']}
self.check_emitter([expected_data])

Loading

0 comments on commit edcc0ea

Please sign in to comment.