Coverage for test/unittests/test_intent_service.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-17 13:44 +0000

1# Copyright 2017 Mycroft AI Inc. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# 

15import time 

16import unittest 

17from copy import deepcopy 

18from unittest import TestCase, mock 

19 

20from ovos_bus_client.message import Message 

21from ovos_bus_client.session import IntentContextManager as ContextManager 

22from ovos_bus_client.util import get_message_lang 

23from ovos_config import Configuration 

24from ovos_config import LocalConf, DEFAULT_CONFIG 

25from ovos_config.locale import setup_locale 

26from ovos_core.intent_services import IntentService 

27from ovos_utils.fakebus import FakeBus 

28from ovos_workshop.intents import IntentBuilder 

29 

30# Setup configurations to use with default language tests 

31BASE_CONF = deepcopy(LocalConf(DEFAULT_CONFIG)) 

32BASE_CONF['lang'] = 'it-it' 

33 

34 

35class ContextManagerTest(TestCase): 

36 

37 def setUp(self): 

38 self.context_manager = ContextManager(3) 

39 

40 def test_add_context(self): 

41 entity = {'confidence': 1.0} 

42 context = 'TestContext' 

43 word = 'TestWord' 

44 entity['data'] = [(word, context)] 

45 entity['match'] = word 

46 entity['key'] = word 

47 

48 self.assertEqual(len(self.context_manager.frame_stack), 0) 

49 self.context_manager.inject_context(entity) 

50 self.assertEqual(len(self.context_manager.frame_stack), 1) 

51 

52 def test_remove_context(self): 

53 entity = {'confidence': 1.0} 

54 context = 'TestContext' 

55 word = 'TestWord' 

56 entity['data'] = [(word, context)] 

57 entity['match'] = word 

58 entity['key'] = word 

59 

60 self.context_manager.inject_context(entity) 

61 self.assertEqual(len(self.context_manager.frame_stack), 1) 

62 self.context_manager.remove_context('TestContext') 

63 self.assertEqual(len(self.context_manager.frame_stack), 0) 

64 

65 

66class TestLanguageExtraction(TestCase): 

67 @mock.patch.dict(Configuration._Configuration__patch, BASE_CONF) 

68 def test_no_lang_in_message(self): 

69 """No lang in message should result in lang from active locale.""" 

70 setup_locale("it-it") 

71 msg = Message('test msg', data={}) 

72 self.assertEqual(get_message_lang(msg), 'it-IT') 

73 setup_locale("en-US") 

74 self.assertEqual(get_message_lang(msg), 'en-US') 

75 

76 @mock.patch.dict(Configuration._Configuration__patch, BASE_CONF) 

77 def test_lang_exists(self): 

78 """Message has a lang code in data, it should be used.""" 

79 msg = Message('test msg', data={'lang': 'de-de'}) 

80 self.assertEqual(get_message_lang(msg), 'de-DE') 

81 msg = Message('test msg', data={'lang': 'sv-se'}) 

82 self.assertEqual(get_message_lang(msg), 'sv-SE') 

83