Skip to content

Commit

Permalink
unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 11, 2024
1 parent 311cc3a commit 2c50258
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 120 deletions.
2 changes: 2 additions & 0 deletions ovos_plugin_manager/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def create(cls, config=None) -> LanguageDetector:
cfg = config.get(lang_module, {})
fallback = cfg.get("fallback_module")
try:
config["module"] = lang_module
clazz = OVOSLangDetectionFactory.get_class(config)
if clazz is None:
raise ValueError(f"Failed to load module: {lang_module}")
Expand Down Expand Up @@ -180,6 +181,7 @@ def create(cls, config=None) -> LanguageTranslator:
cfg = config.get(lang_module, {})
fallback = cfg.get("fallback_module")
try:
config["module"] = lang_module
clazz = OVOSLangTranslationFactory.get_class(config)
if clazz is None:
raise ValueError(f"Failed to load module: {lang_module}")
Expand Down
8 changes: 2 additions & 6 deletions test/unittests/test_g2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,8 @@ def _copy_args(*args):

OVOSG2PFactory.create(config=_FALLBACK_CONFIG)
mock_get_class.assert_called()
self.assertEqual(call_args, ({**_FALLBACK_CONFIG['g2p']['good'],
**{"module": "good",
"lang": "en-us"}},))
self.assertEqual(bad_call_args, ({**_FALLBACK_CONFIG['g2p']['bad'],
**{"module": "bad",
"lang": "en-us"}},))
self.assertEqual(call_args[0]["module"], 'good')
self.assertEqual(bad_call_args[0]["module"], 'bad')
mock_class.assert_called_once_with({**_FALLBACK_CONFIG['g2p']['good'],
**{"module": "good",
"lang": "en-us"}})
Expand Down
204 changes: 96 additions & 108 deletions test/unittests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
from unittest.mock import patch, Mock
from ovos_plugin_manager.utils import PluginTypes, PluginConfigTypes

_TEST_CONFIG = {
"language": {
"detection_module": "good",
"translation_module": "good",
"good": {"a": "b"}
}
}
_FALLBACK_CONFIG = {
"language": {
"detection_module": "bad",
"translation_module": "bad",
"bad": {"fallback_module": "good"},
"good": {"a": "b"}
}
}


class TestLanguageTemplate(unittest.TestCase):
def test_language_detector(self):
Expand Down Expand Up @@ -83,39 +99,15 @@ def test_get_module_configs(self, load_plugin_configs):


class TestLangDetectionFactory(unittest.TestCase):
def test_mappings(self):
from ovos_plugin_manager.language import OVOSLangDetectionFactory
self.assertIsInstance(OVOSLangDetectionFactory.MAPPINGS, dict)
for conf in OVOSLangDetectionFactory.MAPPINGS:
self.assertIsInstance(conf, str)
self.assertIsInstance(OVOSLangDetectionFactory.MAPPINGS[conf],
str)
self.assertNotEqual(conf, OVOSLangDetectionFactory.MAPPINGS[conf])

@patch("ovos_plugin_manager.language.load_lang_detect_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_get_class(self, config, load_plugin):
def test_get_class(self, load_plugin):
from ovos_plugin_manager.language import OVOSLangDetectionFactory
test_config = {"language": {
"detection_module": "libretranslate"
}}

mock_class = Mock()
config.return_value = test_config
load_plugin.return_value = mock_class

# Test mapped plugin from config
self.assertEquals(OVOSLangDetectionFactory.get_class(), mock_class)
load_plugin.assert_called_with("libretranslate_detection_plug")

# Test explicitly specified mapped plugin
conf = {"module": "google"}
self.assertEquals(OVOSLangDetectionFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("googletranslate_detection_plug")

# Test unmapped plugin
conf = {"language": {"detection_module": "real-detect-plug"}}
self.assertEquals(OVOSLangDetectionFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("real-detect-plug")
self.assertEqual(OVOSLangDetectionFactory.get_class(_TEST_CONFIG), mock_class)
load_plugin.assert_called_with("good")

# Test invalid module config
conf = {"language": {}}
Expand All @@ -128,78 +120,64 @@ def test_create(self, config, load_plugin):
from ovos_plugin_manager.language import OVOSLangDetectionFactory
plug_instance = Mock()
mock_plugin = Mock(return_value=plug_instance)
default_config = {
"lang": "core_lang",
"language": {
"detection_module": "google",
"lang": "detect"
}
}
config.return_value = default_config

config.return_value = _TEST_CONFIG
load_plugin.return_value = mock_plugin

# Create from core config
plug = OVOSLangDetectionFactory.create()
load_plugin.assert_called_once_with('googletranslate_detection_plug')
mock_plugin.assert_called_once_with(
config={'lang': "detect",
"module": "googletranslate_detection_plug"})
load_plugin.assert_called_once_with('good')
mock_plugin.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
self.assertEquals(plug_instance, plug)

# Create plugin fully specified in passed config
config_with_module = {"detection_module": "detect-plugin",
"lang": "lang"}
plug = OVOSLangDetectionFactory.create(config_with_module)
load_plugin.assert_called_with("detect-plugin")
mock_plugin.assert_called_with(config={"module": "detect-plugin",
"lang": "lang"})
mock_plugin.reset_mock()
plug = OVOSLangDetectionFactory.create(_TEST_CONFIG)
load_plugin.assert_called_with("good")
mock_plugin.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
self.assertEquals(plug_instance, plug)

# Create plugin fallback module config parsing
config_with_fallback_module = {"module": "test-detect-plugin",
"lang": "lang"}
plug = OVOSLangDetectionFactory.create(config_with_fallback_module)
load_plugin.assert_called_with("test-detect-plugin")
mock_plugin.assert_called_with(config=config_with_fallback_module)
self.assertEquals(plug_instance, plug)
# TODO: Test exception handling fallback to libretranslate
def test_create_fallback(self):
from ovos_plugin_manager.language import OVOSLangDetectionFactory
real_get_class = OVOSLangDetectionFactory.get_class
mock_class = Mock()
call_args = None
bad_call_args = None
from copy import deepcopy

def _copy_args(*args):
nonlocal call_args, bad_call_args
if args[0]["module"] == "bad":
bad_call_args = deepcopy(args)
return None
call_args = deepcopy(args)
return mock_class

mock_get_class = Mock(side_effect=_copy_args)
OVOSLangDetectionFactory.get_class = mock_get_class

OVOSLangDetectionFactory.create(config=_FALLBACK_CONFIG)
mock_get_class.assert_called()
self.assertEqual(call_args[0]["module"], 'good')
self.assertEqual(bad_call_args[0]["module"], 'bad')
mock_class.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
OVOSLangDetectionFactory.get_class = real_get_class


class TestLangTranslationFactory(unittest.TestCase):
def test_mappings(self):
from ovos_plugin_manager.language import OVOSLangTranslationFactory
self.assertIsInstance(OVOSLangTranslationFactory.MAPPINGS, dict)
for conf in OVOSLangTranslationFactory.MAPPINGS:
self.assertIsInstance(conf, str)
self.assertIsInstance(OVOSLangTranslationFactory.MAPPINGS[conf],
str)
self.assertNotEqual(conf, OVOSLangTranslationFactory.MAPPINGS[conf])

@patch("ovos_plugin_manager.language.load_tx_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_get_class(self, config, load_plugin):
def test_get_class(self, load_plugin):
from ovos_plugin_manager.language import OVOSLangTranslationFactory
test_config = {"language": {
"translation_module": "libretranslate"
}}

mock_class = Mock()
config.return_value = test_config
load_plugin.return_value = mock_class

# Test mapped plugin from config
self.assertEquals(OVOSLangTranslationFactory.get_class(), mock_class)
load_plugin.assert_called_with("libretranslate_plug")

# Test explicitly specified mapped plugin
conf = {"module": "google"}
self.assertEquals(OVOSLangTranslationFactory.get_class(conf),
mock_class)
load_plugin.assert_called_with("googletranslate_plug")

# Test unmapped plugin
conf = {"language": {"translation_module": "real-detect-plug"}}
self.assertEquals(OVOSLangTranslationFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("real-detect-plug")
self.assertEqual(OVOSLangTranslationFactory.get_class(_TEST_CONFIG), mock_class)
load_plugin.assert_called_with("good")

# Test invalid module config
conf = {"language": {}}
Expand All @@ -212,38 +190,48 @@ def test_create(self, config, load_plugin):
from ovos_plugin_manager.language import OVOSLangTranslationFactory
plug_instance = Mock()
mock_plugin = Mock(return_value=plug_instance)
default_config = {
"lang": "core_lang",
"language": {
"translation_module": "google",
"lang": "tx"
}
}
config.return_value = default_config

config.return_value = _TEST_CONFIG
load_plugin.return_value = mock_plugin

# Create from core config
plug = OVOSLangTranslationFactory.create()
load_plugin.assert_called_once_with('googletranslate_plug')
mock_plugin.assert_called_once_with(
config={'lang': "tx", "module": "googletranslate_plug"})
load_plugin.assert_called_once_with('good')
mock_plugin.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
self.assertEquals(plug_instance, plug)

# Create plugin fully specified in passed config
config_with_module = {"translation_module": "translate-plugin",
"lang": "lang"}
plug = OVOSLangTranslationFactory.create(config_with_module)
load_plugin.assert_called_with("translate-plugin")
mock_plugin.assert_called_with(config={"module": "translate-plugin",
"lang": "lang"})
self.assertEquals(plug_instance, plug)

# Create plugin fallback module config parsing
config_with_fallback_module = {"module": "test-translate-plugin",
"lang": "lang"}
plug = OVOSLangTranslationFactory.create(config_with_fallback_module)
load_plugin.assert_called_with("test-translate-plugin")
mock_plugin.assert_called_with(config=config_with_fallback_module)
mock_plugin.reset_mock()
plug = OVOSLangTranslationFactory.create(_TEST_CONFIG)
load_plugin.assert_called_with("good")
mock_plugin.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
self.assertEquals(plug_instance, plug)

# TODO: Test exception handling fallback to libretranslate
def test_create_fallback(self):
from ovos_plugin_manager.language import OVOSLangTranslationFactory
real_get_class = OVOSLangTranslationFactory.get_class
mock_class = Mock()
call_args = None
bad_call_args = None
from copy import deepcopy

def _copy_args(*args):
nonlocal call_args, bad_call_args
if args[0]["module"] == "bad":
bad_call_args = deepcopy(args)
return None
call_args = deepcopy(args)
return mock_class

mock_get_class = Mock(side_effect=_copy_args)
OVOSLangTranslationFactory.get_class = mock_get_class

OVOSLangTranslationFactory.create(config=_FALLBACK_CONFIG)
mock_get_class.assert_called()
self.assertEqual(call_args[0]["module"], 'good')
self.assertEqual(bad_call_args[0]["module"], 'bad')
mock_class.assert_called_once_with(config={**_TEST_CONFIG["language"]["good"],
**{'module': 'good', 'lang': 'en-us'}})
OVOSLangTranslationFactory.get_class = real_get_class
8 changes: 2 additions & 6 deletions test/unittests/test_microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@ def _copy_args(*args):

OVOSMicrophoneFactory.create(config=_FALLBACK_CONFIG)
mock_get_class.assert_called()
self.assertEqual(call_args, ({**_FALLBACK_CONFIG['microphone']['dummy'],
**{"module": "dummy",
"lang": "en-us"}},))
self.assertEqual(bad_call_args, ({**_FALLBACK_CONFIG['microphone']['bad'],
**{"module": "bad",
"lang": "en-us"}},))
self.assertEqual(call_args[0]["module"], 'dummy')
self.assertEqual(bad_call_args[0]["module"], 'bad')
mock_class.assert_called_once_with(**_FALLBACK_CONFIG['microphone']['dummy'])
OVOSMicrophoneFactory.get_class = real_get_class

Expand Down

0 comments on commit 2c50258

Please sign in to comment.