Fix test cases under python 3
Lots of minor fixes including, sorting dicts, making ints of strings,
     MagicMock file spec and some other things
A couple of issues in the mycroft-core code base were identified and
fixed. Most notably the incorrect version check for python three when
adding basestring.
Update .travis.yml
			
			
				pull/1568/head
			
			
		
							parent
							
								
									8840a43886
								
							
						
					
					
						commit
						b200d51d39
					
				| 
						 | 
				
			
			@ -1,9 +1,9 @@
 | 
			
		|||
language: python
 | 
			
		||||
before_install:
 | 
			
		||||
 - sudo apt-get update -qq
 | 
			
		||||
 - sudo apt-get install -qq mpg123 portaudio19-dev libglib2.0-dev swig bison libtool autoconf libglib2.0-dev libicu-dev libfann-dev
 | 
			
		||||
 - sudo apt-get install -qq mpg123 portaudio19-dev libglib2.0-dev swig bison libtool autoconf libglib2.0-dev libicu-dev libfann-dev realpath
 | 
			
		||||
python:
 | 
			
		||||
  - "2.7"
 | 
			
		||||
  - "3.4"
 | 
			
		||||
# don't rebuild pocketsphinx for every build
 | 
			
		||||
cache: pocketsphinx-python
 | 
			
		||||
# command to install dependencies
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,8 +24,10 @@ from mycroft.util.json_helper import load_commented_json
 | 
			
		|||
from mycroft.util.log import LOG
 | 
			
		||||
 | 
			
		||||
# Python 2+3 compatibility
 | 
			
		||||
import sys
 | 
			
		||||
from future.utils import iteritems
 | 
			
		||||
from past.builtins import basestring
 | 
			
		||||
if sys.version_info[0] >= 3:
 | 
			
		||||
    basestring = str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def merge_dict(base, delta):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -42,7 +42,9 @@ from mycroft.skills.skill_data import (load_vocabulary, load_regex, to_letters,
 | 
			
		|||
from mycroft.util import resolve_resource_file
 | 
			
		||||
from mycroft.util.log import LOG
 | 
			
		||||
# python 2+3 compatibility
 | 
			
		||||
from past.builtins import basestring
 | 
			
		||||
import sys
 | 
			
		||||
if sys.version_info[0] >= 3:
 | 
			
		||||
    basestring = str
 | 
			
		||||
 | 
			
		||||
MainModule = '__init__'
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1026,7 +1026,7 @@ def extract_datetime_pt(input_str, currentDate=None):
 | 
			
		|||
                                remainder = "am"
 | 
			
		||||
                                used += 1
 | 
			
		||||
                            elif wordNextNextNext == "noite":
 | 
			
		||||
                                if 0 > strHH > 6:
 | 
			
		||||
                                if 0 > int(strHH) > 6:
 | 
			
		||||
                                    remainder = "am"
 | 
			
		||||
                                else:
 | 
			
		||||
                                    remainder = "pm"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,7 +49,7 @@ def match_one(query, choices):
 | 
			
		|||
        Returns: tuple with best match, score
 | 
			
		||||
    """
 | 
			
		||||
    if isinstance(choices, dict):
 | 
			
		||||
        _choices = choices.keys()
 | 
			
		||||
        _choices = list(choices.keys())
 | 
			
		||||
    elif isinstance(choices, list):
 | 
			
		||||
        _choices = choices
 | 
			
		||||
    else:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,6 @@
 | 
			
		|||
# limitations under the License.
 | 
			
		||||
#
 | 
			
		||||
import unittest
 | 
			
		||||
from Queue import Queue
 | 
			
		||||
 | 
			
		||||
import speech_recognition
 | 
			
		||||
from os.path import dirname, join
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +20,11 @@ from speech_recognition import WavFile, AudioData
 | 
			
		|||
 | 
			
		||||
from mycroft.client.speech.listener import AudioConsumer, RecognizerLoop
 | 
			
		||||
from mycroft.stt import MycroftSTT
 | 
			
		||||
import sys
 | 
			
		||||
if sys.version_info[0] < 3:
 | 
			
		||||
    from Queue import Queue
 | 
			
		||||
else:
 | 
			
		||||
    from queue import Queue
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MockRecognizer(object):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,7 +44,6 @@ class PocketSphinxTest(unittest.TestCase):
 | 
			
		|||
        self.assertEquals(p.key_phrase, 'hey mycroft')
 | 
			
		||||
 | 
			
		||||
    def testVictoria(self):
 | 
			
		||||
        print "VICTORIA!"
 | 
			
		||||
        config = {
 | 
			
		||||
            'hey victoria': {
 | 
			
		||||
                'module': 'pocketsphinx',
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -33,6 +33,9 @@ from mycroft.configuration.config import LocalConf, DEFAULT_CONFIG
 | 
			
		|||
 | 
			
		||||
BASE_CONF = LocalConf(DEFAULT_CONFIG)
 | 
			
		||||
 | 
			
		||||
if sys.version_info[0] >= 3:
 | 
			
		||||
    basestring = str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MockEmitter(object):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -89,8 +92,9 @@ class MycroftSkillTest(unittest.TestCase):
 | 
			
		|||
    def check_emitter(self, result_list):
 | 
			
		||||
        for type in self.emitter.get_types():
 | 
			
		||||
            self.assertEquals(type, 'register_vocab')
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results()),
 | 
			
		||||
                          sorted(result_list))
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results(),
 | 
			
		||||
                                 key=lambda d: sorted(d.items())),
 | 
			
		||||
                          sorted(result_list, key=lambda d: sorted(d.items())))
 | 
			
		||||
        self.emitter.reset()
 | 
			
		||||
 | 
			
		||||
    def test_load_regex_from_file_single(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -106,17 +110,12 @@ class MycroftSkillTest(unittest.TestCase):
 | 
			
		|||
        self.check_regex_from_file('invalid/none.rx')
 | 
			
		||||
 | 
			
		||||
    def test_load_regex_from_file_invalid(self):
 | 
			
		||||
        try:
 | 
			
		||||
        with self.assertRaises(error):
 | 
			
		||||
            self.check_regex_from_file('invalid/invalid.rx')
 | 
			
		||||
        except error as e:
 | 
			
		||||
            self.assertEquals(e.__str__(),
 | 
			
		||||
                              'unexpected end of regular expression')
 | 
			
		||||
 | 
			
		||||
    def test_load_regex_from_file_does_not_exist(self):
 | 
			
		||||
        try:
 | 
			
		||||
        with self.assertRaises(IOError):
 | 
			
		||||
            self.check_regex_from_file('does_not_exist.rx')
 | 
			
		||||
        except IOError as e:
 | 
			
		||||
            self.assertEquals(e.strerror, 'No such file or directory')
 | 
			
		||||
 | 
			
		||||
    def test_load_regex_full(self):
 | 
			
		||||
        self.check_regex(join(self.regex_path, 'valid'),
 | 
			
		||||
| 
						 | 
				
			
			@ -295,8 +294,9 @@ class MycroftSkillTest(unittest.TestCase):
 | 
			
		|||
    def check_register_object_file(self, types_list, result_list):
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_types()),
 | 
			
		||||
                          sorted(types_list))
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results()),
 | 
			
		||||
                          sorted(result_list))
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results(),
 | 
			
		||||
                                 key=lambda d: sorted(d.items())),
 | 
			
		||||
                          sorted(result_list, key=lambda d: sorted(d.items())))
 | 
			
		||||
        self.emitter.reset()
 | 
			
		||||
 | 
			
		||||
    def test_register_intent_file(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -326,8 +326,9 @@ class MycroftSkillTest(unittest.TestCase):
 | 
			
		|||
        self.check_register_object_file(expected_types, expected_results)
 | 
			
		||||
 | 
			
		||||
    def check_register_decorators(self, result_list):
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results()),
 | 
			
		||||
                          sorted(result_list))
 | 
			
		||||
        self.assertEquals(sorted(self.emitter.get_results(),
 | 
			
		||||
                                 key=lambda d: sorted(d.items())),
 | 
			
		||||
                          sorted(result_list, key=lambda d: sorted(d.items())))
 | 
			
		||||
        self.emitter.reset()
 | 
			
		||||
 | 
			
		||||
    def test_register_decorators(self):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,7 +46,6 @@ class ContextManagerTest(unittest.TestCase):
 | 
			
		|||
        entity = {'confidence': 1.0}
 | 
			
		||||
        context = 'TestContext'
 | 
			
		||||
        word = 'TestWord'
 | 
			
		||||
        print "Adding " + context
 | 
			
		||||
        entity['data'] = [(word, context)]
 | 
			
		||||
        entity['match'] = word
 | 
			
		||||
        entity['key'] = word
 | 
			
		||||
| 
						 | 
				
			
			@ -59,7 +58,6 @@ class ContextManagerTest(unittest.TestCase):
 | 
			
		|||
        entity = {'confidence': 1.0}
 | 
			
		||||
        context = 'TestContext'
 | 
			
		||||
        word = 'TestWord'
 | 
			
		||||
        print "Adding " + context
 | 
			
		||||
        entity['data'] = [(word, context)]
 | 
			
		||||
        entity['match'] = word
 | 
			
		||||
        entity['key'] = word
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ class TestEventScheduler(unittest.TestCase):
 | 
			
		|||
            Test creating and shutting down event_scheduler.
 | 
			
		||||
        """
 | 
			
		||||
        mock_load.return_value = ''
 | 
			
		||||
        mock_open.return_value = mock.MagicMock(spec=file)
 | 
			
		||||
        mock_open.return_value = mock.MagicMock()
 | 
			
		||||
        emitter = mock.MagicMock()
 | 
			
		||||
        es = EventScheduler(emitter)
 | 
			
		||||
        es.shutdown()
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +36,7 @@ class TestEventScheduler(unittest.TestCase):
 | 
			
		|||
        """
 | 
			
		||||
        # Thread start is mocked so will not actually run the thread loop
 | 
			
		||||
        mock_load.return_value = ''
 | 
			
		||||
        mock_open.return_value = mock.MagicMock(spec=file)
 | 
			
		||||
        mock_open.return_value = mock.MagicMock()
 | 
			
		||||
        emitter = mock.MagicMock()
 | 
			
		||||
        es = EventScheduler(emitter)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -63,7 +63,7 @@ class TestEventScheduler(unittest.TestCase):
 | 
			
		|||
            Test save functionality.
 | 
			
		||||
        """
 | 
			
		||||
        mock_load.return_value = ''
 | 
			
		||||
        mock_open.return_value = mock.MagicMock(spec=file)
 | 
			
		||||
        mock_open.return_value = mock.MagicMock()
 | 
			
		||||
        emitter = mock.MagicMock()
 | 
			
		||||
        es = EventScheduler(emitter)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -87,7 +87,7 @@ class TestEventScheduler(unittest.TestCase):
 | 
			
		|||
            Test save functionality.
 | 
			
		||||
        """
 | 
			
		||||
        mock_load.return_value = ''
 | 
			
		||||
        mock_open.return_value = mock.MagicMock(spec=file)
 | 
			
		||||
        mock_open.return_value = mock.MagicMock()
 | 
			
		||||
        emitter = mock.MagicMock()
 | 
			
		||||
        es = EventScheduler(emitter)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,7 @@ class TestFileLoad(unittest.TestCase):
 | 
			
		|||
        root_dir = dirname(__file__)
 | 
			
		||||
        # Load normal JSON file
 | 
			
		||||
        plainfile = join(root_dir, 'plain.json')
 | 
			
		||||
        with open(plainfile, 'rw') as f:
 | 
			
		||||
        with open(plainfile, 'r') as f:
 | 
			
		||||
            data_from_plain = json.load(f)
 | 
			
		||||
 | 
			
		||||
        # Load commented JSON file
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,7 @@
 | 
			
		|||
#
 | 
			
		||||
import unittest
 | 
			
		||||
import sys
 | 
			
		||||
from cStringIO import StringIO
 | 
			
		||||
from io import StringIO
 | 
			
		||||
from threading import Thread
 | 
			
		||||
from mycroft.util.log import LOG
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -66,5 +66,6 @@ class TestLog(unittest.TestCase):
 | 
			
		|||
                    found_msg = True
 | 
			
		||||
            assert found_msg
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    unittest.main()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -83,7 +83,7 @@ class TestNormalize(unittest.TestCase):
 | 
			
		|||
 | 
			
		||||
    def test_extractdatetime_en(self):
 | 
			
		||||
        def extractWithFormat(text):
 | 
			
		||||
            date = datetime(2017, 06, 27, 00, 00)
 | 
			
		||||
            date = datetime(2017, 6, 27, 0, 0)
 | 
			
		||||
            [extractedDate, leftover] = extract_datetime(text, date)
 | 
			
		||||
            extractedDate = extractedDate.strftime("%Y-%m-%d %H:%M:%S")
 | 
			
		||||
            return [extractedDate, leftover]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -175,7 +175,7 @@ class TestNormalize(unittest.TestCase):
 | 
			
		|||
 | 
			
		||||
    def test_extractdatetime_it(self):
 | 
			
		||||
        def extractWithFormat(text):
 | 
			
		||||
            date = datetime(2018, 01, 13, 00, 00)
 | 
			
		||||
            date = datetime(2018, 1, 13, 0, 0)
 | 
			
		||||
            [extractedDate, leftover] = extract_datetime(text, date,
 | 
			
		||||
                                                         lang="it")
 | 
			
		||||
            extractedDate = extractedDate.strftime("%Y-%m-%d %H:%M:%S")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -140,7 +140,7 @@ class TestNormalize(unittest.TestCase):
 | 
			
		|||
 | 
			
		||||
    def test_extractdatetime_pt(self):
 | 
			
		||||
        def extractWithFormat(text):
 | 
			
		||||
            date = datetime(2017, 06, 27, 00, 00)
 | 
			
		||||
            date = datetime(2017, 6, 27, 0, 0)
 | 
			
		||||
            [extractedDate, leftover] = extract_datetime(text, date,
 | 
			
		||||
                                                         lang="pt")
 | 
			
		||||
            extractedDate = extractedDate.strftime("%Y-%m-%d %H:%M:%S")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue