Make test_api slightly more robust

pull/1076/head
Åke Forslund 2017-09-13 22:13:18 +02:00
parent d750935fe3
commit 0d4a841d4e
2 changed files with 42 additions and 22 deletions

View File

@ -25,7 +25,7 @@ from mycroft.identity import IdentityManager
from mycroft.version import VersionManager
__author__ = 'jdorleans'
__paired_cache = False
_paired_cache = False
class Api(object):
@ -124,7 +124,11 @@ class Api(object):
def build_url(self, params):
path = params.get("path", "")
print "PATH"
print path
print "VERSION"
version = params.get("version", self.version)
print version
return self.url + "/" + version + "/" + path
@ -259,8 +263,8 @@ def is_paired():
Returns:
bool: True if paired with backend
"""
global __paired_cache
if __paired_cache:
global _paired_cache
if _paired_cache:
# NOTE: This assumes once paired, the unit remains paired. So
# un-pairing must restart the system (or clear this value).
# The Mark 1 does perform a restart on RESET.
@ -269,8 +273,8 @@ def is_paired():
try:
api = DeviceApi()
device = api.get()
__paired_cache = api.identity.uuid is not None and \
_paired_cache = api.identity.uuid is not None and \
api.identity.uuid != ""
return __paired_cache
return _paired_cache
except:
return False

View File

@ -1,21 +1,17 @@
import mycroft.configuration
import mock
import unittest
import mycroft.api
from copy import copy
import unittest
import mock
import mycroft.configuration
import mycroft.api
mock_config = mock.MagicMock()
mock_config.get.return_value = {
'server': {
'url': 'https://api-test.mycroft.ai',
'version': 'v1',
'update': True,
'metrics': False
}
CONFIG = {
'server': {
'url': 'https://api-test.mycroft.ai',
'version': 'v1',
'update': True,
'metrics': False
}
mock_config_manager = mock.MagicMock()
mock_config_manager.return_value = mock_config
mycroft.api.ConfigurationManager = mock_config_manager
}
mycroft.api.requests.post = mock.MagicMock()
@ -31,9 +27,17 @@ def create_response(status, json=None, url='', data=''):
class TestApi(unittest.TestCase):
def setUp(self):
patcher = mock.patch('mycroft.configuration.ConfigurationManager.get',
return_value=CONFIG)
self.mock_config_get = patcher.start()
self.addCleanup(patcher.stop)
super(TestApi, self).setUp()
@mock.patch('mycroft.api.IdentityManager.get')
def test_init(self, mock_identity_get):
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
a = mycroft.api.Api('test-path')
@ -81,6 +85,7 @@ class TestApi(unittest.TestCase):
def test_device(self, mock_request, mock_identity_get):
mock_request.return_value = create_response(200)
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
# Test init
@ -93,6 +98,7 @@ class TestApi(unittest.TestCase):
def test_device_activate(self, mock_request, mock_identity_get):
mock_request.return_value = create_response(200)
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
# Test activate
@ -107,6 +113,7 @@ class TestApi(unittest.TestCase):
def test_device_get(self, mock_request, mock_identity_get):
mock_request.return_value = create_response(200)
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
# Test get
@ -115,11 +122,14 @@ class TestApi(unittest.TestCase):
url = mock_request.call_args[0][1]
self.assertEquals(url, 'https://api-test.mycroft.ai/v1/device/1234')
@mock.patch('mycroft.api.IdentityManager.update')
@mock.patch('mycroft.api.IdentityManager.get')
@mock.patch('mycroft.api.requests.request')
def test_device_get_code(self, mock_request, mock_identity_get):
def test_device_get_code(self, mock_request, mock_identity_get,
mock_identit_update):
mock_request.return_value = create_response(200, '123ABC')
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
device = mycroft.api.DeviceApi()
@ -134,6 +144,7 @@ class TestApi(unittest.TestCase):
def test_device_get_settings(self, mock_request, mock_identity_get):
mock_request.return_value = create_response(200, {})
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
device = mycroft.api.DeviceApi()
@ -147,6 +158,7 @@ class TestApi(unittest.TestCase):
def test_device_get_location(self, mock_request, mock_identity_get):
mock_request.return_value = create_response(200, {})
mock_identity = mock.MagicMock()
mock_identity.is_expired.return_value = False
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
device = mycroft.api.DeviceApi()
@ -205,6 +217,7 @@ class TestApi(unittest.TestCase):
@mock.patch('mycroft.api.IdentityManager.load')
def test_has_been_paired(self, mock_identity_load):
# reset pairing cache
mock_identity = mock.MagicMock()
mock_identity_load.return_value = mock_identity
# Test None
@ -217,6 +230,7 @@ class TestApi(unittest.TestCase):
mock_identity.uuid = "1234"
self.assertTrue(mycroft.api.has_been_paired())
@mock.patch('mycroft.api._paired_cache', False)
@mock.patch('mycroft.api.IdentityManager.get')
@mock.patch('mycroft.api.requests.request')
def test_is_paired_true(self, mock_request, mock_identity_get):
@ -226,13 +240,15 @@ class TestApi(unittest.TestCase):
mock_identity.uuid = '1234'
mock_identity_get.return_value = mock_identity
num_calls = mock_identity_get.num_calls
# reset paired cache
self.assertTrue(mycroft.api.is_paired())
self.assertEquals(num_calls, mock_identity_get.num_calls)
url = mock_request.call_args[0][1]
self.assertEquals(url, 'https://api-test.mycroft.ai/v1/device/1234')
@mock.patch('mycroft.api._paired_cache', False)
@mock.patch('mycroft.api.IdentityManager.get')
@mock.patch('mycroft.api.requests.request')
def test_is_paired_false(self, mock_request, mock_identity_get):