Skip to content

Commit

Permalink
port/tests_from_core (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Oct 15, 2024
1 parent 9c7f9fa commit 4aa338d
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 0 deletions.
Empty file.
1 change: 1 addition & 0 deletions test/unittests/dialog/multiple_dialogs/one.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ONE
1 change: 1 addition & 0 deletions test/unittests/dialog/multiple_dialogs/two.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TWO
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Chris",
"value": 10000,
"taxed_value": 6000
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{name}}, You have just won {{value}} dollars! Well, {{taxed_value}} dollars, after taxes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Chris, You have just won 10000 dollars! Well, 6000 dollars, after taxes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there!
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a commented line
This is a line without comment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a line without comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Sherlock"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello there {{name}}!
Another possible outcome, {{name}}
Oh, {{name}} look at the capabilities
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello there Sherlock!
Another possible outcome, Sherlock
Oh, Sherlock look at the capabilities
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello there!
Another possible outcome
Oh look at the capabilities
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello there!
Another possible outcome
Oh look at the capabilities
107 changes: 107 additions & 0 deletions test/unittests/dialog/test_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest
import pathlib
import json


from ovos_utils.dialog import MustacheDialogRenderer, load_dialogs, get_dialog


# TODO - move to ovos-workshop
class DialogTest(unittest.TestCase):
def setUp(self):
self.stache = MustacheDialogRenderer()
self.topdir = pathlib.Path(__file__).parent

def test_general_dialog(self):
""" Test the loading and filling of valid simple mustache dialogs """
template_path = self.topdir.joinpath('./mustache_templates')
for file in template_path.iterdir():
if file.suffix == '.dialog':
self.stache.load_template_file(file.name, str(file.absolute()))
context = json.load(
file.with_suffix('.context.json').open(
'r', encoding='utf-8'))
self.assertEqual(
self.stache.render(file.name, context),
file.with_suffix('.result').open('r',
encoding='utf-8').read())

def test_unknown_dialog(self):
""" Test for returned file name literals in case of unkown dialog """
self.assertEqual(
self.stache.render("unknown.template"), "unknown template")

def test_multiple_dialog(self):
"""
Test the loading and filling of valid mustache dialogs
where a dialog file contains multiple text versions
"""
template_path = self.topdir.joinpath('./mustache_templates_multiple')
for file in template_path.iterdir():
if file.suffix == '.dialog':
self.stache.load_template_file(file.name, str(file.absolute()))
context = json.load(
file.with_suffix('.context.json').open(
'r', encoding='utf-8'))
results = [
line.strip() for line in file.with_suffix('.result').open(
'r', encoding='utf-8')
]
# Try all lines
for index, line in enumerate(results):
self.assertEqual(
self.stache.render(
file.name, index=index, context=context),
line.strip())
# Test random index function
# (bad test because non-deterministic?)
self.assertIn(
self.stache.render(file.name, context=context), results)

def test_comment_dialog(self):
"""
Test the loading and filling of valid mustache dialogs
where a dialog file contains multiple text versions
"""
template_path = self.topdir.joinpath('./mustache_templates_comments')
for f in template_path.iterdir():
if f.suffix == '.dialog':
self.stache.load_template_file(f.name, str(f.absolute()))
results = [line.strip()
for line in f.with_suffix('.result').open('r')]
# Try all lines
for index, line in enumerate(results):
self.assertEqual(self.stache.render(f.name, index=index),
line.strip())

def test_dialog_loader(self):
template_path = self.topdir.joinpath('./multiple_dialogs')
renderer = load_dialogs(template_path)
self.assertEqual(renderer.render('one'), 'ONE')
self.assertEqual(renderer.render('two'), 'TWO')

def test_dialog_loader_missing(self):
template_path = self.topdir.joinpath('./missing_dialogs')
renderer = load_dialogs(template_path)
self.assertEqual(renderer.render('test'), 'test')




if __name__ == "__main__":
unittest.main()
68 changes: 68 additions & 0 deletions test/unittests/test_event_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import unittest
from unittest import mock
from ovos_utils.events import EventContainer


def example_handler(message):
pass


class TestEventContainer(unittest.TestCase):
def test_init(self):
bus = mock.MagicMock()

# Set bus via init
container = EventContainer(bus)
self.assertEqual(container.bus, bus)

# Set bus using .set_bus
container = EventContainer(None)
self.assertIsNotNone(container.bus)
container.set_bus(bus)
self.assertEqual(container.bus, bus)

def test_add(self):
bus = mock.MagicMock()
container = EventContainer(bus)
self.assertEqual(len(container.events), 0)

# Test add normal event handler
container.add('test1', example_handler)
self.assertTrue(bus.on.called)

# Test add single shot event handler
len_before = len(container.events)
container.add('test2', example_handler, once=True)
self.assertEqual(len_before + 1, len(container.events))
self.assertTrue(bus.once.called)

# Verify correct content in event container
self.assertTrue(('test1', example_handler) in container.events)
self.assertEqual(len(container.events), 2)

def test_remove(self):
bus = mock.MagicMock()
container = EventContainer(bus)
self.assertEqual(len(container.events), 0)

container.add('test1', example_handler)
container.add('test2', example_handler)
container.add('test3', example_handler)
self.assertEqual(len(container.events), 3)

self.assertTrue(('test2', example_handler) in container.events)
container.remove('test2')
self.assertTrue(('test2', example_handler) not in container.events)
self.assertTrue(bus.remove_all_listeners.called)

def test_clear(self):
bus = mock.MagicMock()
container = EventContainer(bus)

container.add('test1', example_handler)
container.add('test2', example_handler)
container.add('test3', example_handler)
self.assertEqual(len(container.events), 3)

container.clear()
self.assertEqual(len(container.events), 0)
123 changes: 123 additions & 0 deletions test/unittests/test_event_scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Test cases regarding the event scheduler.
"""

import unittest
import time
from pyee import ExecutorEventEmitter

from unittest.mock import MagicMock, patch
from ovos_utils.messagebus import FakeBus
from ovos_bus_client.util.scheduler import EventScheduler, EventSchedulerInterface


class TestEventScheduler(unittest.TestCase):
@patch('threading.Thread')
@patch('json.load')
@patch('json.dump')
@patch('builtins.open')
def test_create(self, mock_open, mock_json_dump, mock_load, mock_thread):
"""
Test creating and shutting down event_scheduler.
"""
mock_load.return_value = ''
mock_open.return_value = MagicMock()
emitter = MagicMock()
es = EventScheduler(emitter)
es.shutdown()
self.assertEqual(mock_json_dump.call_args[0][0], {})

@patch('threading.Thread')
@patch('json.load')
@patch('json.dump')
@patch('builtins.open')
def test_add_remove(self, mock_open, mock_json_dump,
mock_load, mock_thread):
"""
Test add an event and then remove it.
"""
# Thread start is mocked so will not actually run the thread loop
mock_load.return_value = ''
mock_open.return_value = MagicMock()
emitter = MagicMock()
es = EventScheduler(emitter)

# 900000000000 should be in the future for a long time
es.schedule_event('test', 90000000000, None)
es.schedule_event('test-2', 90000000000, None)

es.check_state() # run one cycle
self.assertTrue('test' in es.events)
self.assertTrue('test-2' in es.events)

es.remove_event('test')
es.check_state() # run one cycle
self.assertTrue('test' not in es.events)
self.assertTrue('test-2' in es.events)
es.shutdown()

@patch('threading.Thread')
@patch('json.load')
@patch('json.dump')
@patch('builtins.open')
def test_save(self, mock_open, mock_dump, mock_load, mock_thread):
"""
Test save functionality.
"""
mock_load.return_value = ''
mock_open.return_value = MagicMock()
emitter = MagicMock()
es = EventScheduler(emitter)

# 900000000000 should be in the future for a long time
es.schedule_event('test', 900000000000, None)
es.schedule_event('test-repeat', 910000000000, 60)
es.check_state()

es.shutdown()

# Make sure the dump method wasn't called with test-repeat
self.assertEqual(mock_dump.call_args[0][0],
{'test': [(900000000000, None, {}, None)]})

@patch('threading.Thread')
@patch('json.load')
@patch('json.dump')
@patch('builtins.open')
def test_send_event(self, mock_open, mock_dump, mock_load, mock_thread):
"""
Test save functionality.
"""
mock_load.return_value = ''
mock_open.return_value = MagicMock()
emitter = MagicMock()
es = EventScheduler(emitter)

# 0 should be in the future for a long time
es.schedule_event('test', time.time(), None)

es.check_state()
self.assertEqual(emitter.emit.call_args[0][0].msg_type, 'test')
self.assertEqual(emitter.emit.call_args[0][0].data, {})
es.shutdown()


class TestEventSchedulerInterface(unittest.TestCase):
def test_shutdown(self):
def f(message):
print('TEST FUNC')

bus = ExecutorEventEmitter()

es = EventSchedulerInterface('tester')
es.set_bus(FakeBus())
es.set_id('id')

# Schedule a repeating event
es.schedule_repeating_event(f, None, 10, name='f')
self.assertTrue(len(es.bus.ee._events['id:f']) == 1)

es.shutdown()
# Check that the reference to the function has been removed from the
# bus emitter
self.assertTrue(len(bus._events['id:f']) == 0)
Loading

0 comments on commit 4aa338d

Please sign in to comment.